Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / task_manager / extension_information.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/task_manager/extension_information.h"
6
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/browser/task_manager/renderer_resource.h"
13 #include "chrome/browser/task_manager/task_manager_util.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "extensions/browser/guest_view/guest_view_base.h"
19 #include "extensions/browser/process_manager.h"
20 #include "extensions/browser/view_type_utils.h"
21 #include "extensions/common/extension.h"
22 #include "grit/theme_resources.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/gfx/image/image_skia.h"
26
27 using content::WebContents;
28 using extensions::Extension;
29
30 namespace {
31
32 const Extension* GetExtensionForWebContents(WebContents* web_contents) {
33   return extensions::ProcessManager::Get(web_contents->GetBrowserContext())
34       ->GetExtensionForRenderViewHost(web_contents->GetRenderViewHost());
35 }
36
37 }  // namespace
38
39 namespace task_manager {
40
41 class ExtensionProcessResource : public RendererResource {
42  public:
43   explicit ExtensionProcessResource(const Extension* extension,
44                                     content::RenderViewHost* render_view_host);
45   ~ExtensionProcessResource() override;
46
47   // Resource methods:
48   base::string16 GetTitle() const override;
49   gfx::ImageSkia GetIcon() const override;
50   Type GetType() const override;
51
52  private:
53   // Returns true if the associated extension has a background page.
54   bool IsBackground() const;
55
56   // The icon painted for the extension process.
57   static gfx::ImageSkia* default_icon_;
58
59   // Cached data about the extension.
60   base::string16 title_;
61
62   DISALLOW_COPY_AND_ASSIGN(ExtensionProcessResource);
63 };
64
65 gfx::ImageSkia* ExtensionProcessResource::default_icon_ = NULL;
66
67 ExtensionProcessResource::ExtensionProcessResource(
68     const Extension* extension,
69     content::RenderViewHost* render_view_host)
70     : RendererResource(render_view_host->GetProcess()->GetHandle(),
71                        render_view_host) {
72   if (!default_icon_) {
73     ResourceBundle& rb = ResourceBundle::GetSharedInstance();
74     default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON);
75   }
76   base::string16 extension_name = base::UTF8ToUTF16(extension->name());
77   DCHECK(!extension_name.empty());
78
79   Profile* profile = Profile::FromBrowserContext(
80       render_view_host->GetProcess()->GetBrowserContext());
81   int message_id = util::GetMessagePrefixID(extension->is_app(),
82                                             true,  // is_extension
83                                             profile->IsOffTheRecord(),
84                                             false,  // is_prerender
85                                             IsBackground());
86   title_ = l10n_util::GetStringFUTF16(message_id, extension_name);
87 }
88
89 ExtensionProcessResource::~ExtensionProcessResource() {}
90
91 base::string16 ExtensionProcessResource::GetTitle() const { return title_; }
92
93 gfx::ImageSkia ExtensionProcessResource::GetIcon() const {
94   return *default_icon_;
95 }
96
97 Resource::Type ExtensionProcessResource::GetType() const { return EXTENSION; }
98
99 bool ExtensionProcessResource::IsBackground() const {
100   WebContents* web_contents =
101       WebContents::FromRenderViewHost(render_view_host());
102   extensions::ViewType view_type = extensions::GetViewType(web_contents);
103   return view_type == extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
104 }
105
106 ////////////////////////////////////////////////////////////////////////////////
107 // ExtensionInformation class
108 ////////////////////////////////////////////////////////////////////////////////
109
110 ExtensionInformation::ExtensionInformation() {}
111
112 ExtensionInformation::~ExtensionInformation() {}
113
114 void ExtensionInformation::GetAll(const NewWebContentsCallback& callback) {
115   // Add all the existing extension views from all Profiles, including those
116   // from incognito split mode.
117   ProfileManager* profile_manager = g_browser_process->profile_manager();
118   std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
119   size_t num_default_profiles = profiles.size();
120   for (size_t i = 0; i < num_default_profiles; ++i) {
121     if (profiles[i]->HasOffTheRecordProfile()) {
122       profiles.push_back(profiles[i]->GetOffTheRecordProfile());
123     }
124   }
125
126   for (size_t i = 0; i < profiles.size(); ++i) {
127     const extensions::ProcessManager::ViewSet all_views =
128         extensions::ProcessManager::Get(profiles[i])->GetAllViews();
129     extensions::ProcessManager::ViewSet::const_iterator jt = all_views.begin();
130     for (; jt != all_views.end(); ++jt) {
131       WebContents* web_contents = WebContents::FromRenderViewHost(*jt);
132       if (CheckOwnership(web_contents))
133         callback.Run(web_contents);
134     }
135   }
136 }
137
138 bool ExtensionInformation::CheckOwnership(WebContents* web_contents) {
139   // Don't add WebContents that belong to a guest (those are handled by
140   // GuestInformation). Otherwise they will be added twice.
141   if (extensions::GuestViewBase::IsGuest(web_contents))
142     return false;
143
144   // Extension WebContents tracked by this class will always host extension
145   // content.
146   if (!GetExtensionForWebContents(web_contents))
147     return false;
148
149   extensions::ViewType view_type = extensions::GetViewType(web_contents);
150
151   // Don't add tab contents (those are handled by TabContentsResourceProvider)
152   // or background contents (handled by BackgroundInformation) or panels
153   // (handled by PanelInformation)
154   return (view_type != extensions::VIEW_TYPE_INVALID &&
155           view_type != extensions::VIEW_TYPE_TAB_CONTENTS &&
156           view_type != extensions::VIEW_TYPE_BACKGROUND_CONTENTS &&
157           view_type != extensions::VIEW_TYPE_PANEL);
158 }
159
160 scoped_ptr<RendererResource> ExtensionInformation::MakeResource(
161     WebContents* web_contents) {
162   const Extension* extension = GetExtensionForWebContents(web_contents);
163   if (!extension) {
164     NOTREACHED();
165     return scoped_ptr<RendererResource>();
166   }
167   return scoped_ptr<RendererResource>(new ExtensionProcessResource(
168       extension, web_contents->GetRenderViewHost()));
169 }
170
171 }  // namespace task_manager