File: extension_task.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (144 lines) | stat: -rw-r--r-- 4,861 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/task_manager/providers/web_contents/extension_task.h"

#include <memory>

#include "base/notimplemented.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_handlers/icons_handler.h"
#include "extensions/grit/extensions_browser_resources.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/chrome_pages.h"
#endif

namespace task_manager {

gfx::ImageSkia* ExtensionTask::s_icon_ = nullptr;

ExtensionTask::ExtensionTask(content::WebContents* web_contents,
                             const extensions::Extension* extension,
                             extensions::mojom::ViewType view_type)
    : RendererTask(GetExtensionTitle(web_contents, extension, view_type),
                   FetchIcon(IDR_EXTENSIONS_FAVICON, &s_icon_),
                   web_contents),
      view_type_(view_type) {
  LoadExtensionIcon(extension);
}

ExtensionTask::~ExtensionTask() = default;

void ExtensionTask::UpdateTitle() {
  // The title of the extension should not change as a result of title change
  // in its WebContents, so we ignore this.
}

void ExtensionTask::UpdateFavicon() {
  // We don't care about the favicon of the WebContents but rather of the
  // extension.
}

void ExtensionTask::Activate() {
#if BUILDFLAG(ENABLE_EXTENSIONS)
  // This task represents the extension view of (for example) a background page
  // or browser action button, so there is no top-level window to bring to the
  // front. Instead, when this task is double-clicked, we bring up the
  // chrome://extensions page in a tab, and highlight the details for this
  // extension.
  //
  // TODO(nick): For extensions::mojom::ViewType::kAppWindow, and maybe others,
  // there may actually be a window we could focus. Special case those here as
  // needed.
  const extensions::Extension* extension =
      extensions::ProcessManager::Get(web_contents()->GetBrowserContext())
          ->GetExtensionForWebContents(web_contents());

  if (!extension)
    return;

  Browser* browser = chrome::FindTabbedBrowser(
      Profile::FromBrowserContext(web_contents()->GetBrowserContext()), true);

  // If an existing browser isn't found, don't create a new one.
  if (!browser)
    return;

  chrome::ShowExtensions(browser, extension->id());
#else
  // TODO(crbug.com/417512763): Support activation on desktop Android.
  NOTIMPLEMENTED();
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)
}

Task::Type ExtensionTask::GetType() const {
  return Task::EXTENSION;
}

int ExtensionTask::GetKeepaliveCount() const {
  if (view_type_ != extensions::mojom::ViewType::kExtensionBackgroundPage)
    return -1;

  const extensions::Extension* extension =
      extensions::ProcessManager::Get(web_contents()->GetBrowserContext())
          ->GetExtensionForWebContents(web_contents());
  if (!extension)
    return -1;

  return extensions::ProcessManager::Get(web_contents()->GetBrowserContext())
      ->GetLazyKeepaliveCount(extension);
}

void ExtensionTask::OnExtensionIconImageChanged(extensions::IconImage* image) {
  DCHECK_EQ(extension_icon_.get(), image);

  if (!image->image_skia().isNull())
    set_icon(image->image_skia());
}

std::u16string ExtensionTask::GetExtensionTitle(
    content::WebContents* web_contents,
    const extensions::Extension* extension,
    extensions::mojom::ViewType view_type) const {
  DCHECK(web_contents);

  std::u16string title =
      extension ? base::UTF8ToUTF16(extension->name())
                : RendererTask::GetTitleFromWebContents(web_contents);

  bool is_background =
      view_type == extensions::mojom::ViewType::kExtensionBackgroundPage;

  return RendererTask::PrefixRendererTitle(
      title,
      extension && extension->is_app(),
      true,  // is_extension
      web_contents->GetBrowserContext()->IsOffTheRecord(),
      is_background);
}

void ExtensionTask::LoadExtensionIcon(const extensions::Extension* extension) {
  if (!extension)
    return;

  extension_icon_ = std::make_unique<extensions::IconImage>(
      web_contents()->GetBrowserContext(), extension,
      extensions::IconsInfo::GetIcons(extension),
      extension_misc::EXTENSION_ICON_SMALL, icon(), this);

  // Triggers actual image loading with 1x resources.
  extension_icon_->image_skia().GetRepresentation(1.0f);
  set_icon(extension_icon_->image_skia());
}

}  // namespace task_manager