Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / task_manager / extension_process_resource_provider.cc
1 // Copyright 2013 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_process_resource_provider.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/chrome_notification_types.h"
11 #include "chrome/browser/devtools/devtools_window.h"
12 #include "chrome/browser/extensions/extension_host.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/task_manager/resource_provider.h"
16 #include "chrome/browser/task_manager/task_manager.h"
17 #include "chrome/browser/task_manager/task_manager_util.h"
18 #include "content/public/browser/notification_details.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/site_instance.h"
24 #include "content/public/browser/web_contents.h"
25 #include "extensions/browser/extension_system.h"
26 #include "extensions/browser/process_manager.h"
27 #include "extensions/browser/view_type_utils.h"
28 #include "extensions/common/extension.h"
29 #include "grit/theme_resources.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/gfx/image/image_skia.h"
33
34 using content::WebContents;
35 using extensions::Extension;
36
37 namespace task_manager {
38
39 class ExtensionProcessResource : public Resource {
40  public:
41   explicit ExtensionProcessResource(
42       content::RenderViewHost* render_view_host);
43   virtual ~ExtensionProcessResource();
44
45   // Resource methods:
46   virtual base::string16 GetTitle() const OVERRIDE;
47   virtual base::string16 GetProfileName() const OVERRIDE;
48   virtual gfx::ImageSkia GetIcon() const OVERRIDE;
49   virtual base::ProcessHandle GetProcess() const OVERRIDE;
50   virtual int GetUniqueChildProcessId() const OVERRIDE;
51   virtual Type GetType() const OVERRIDE;
52   virtual bool CanInspect() const OVERRIDE;
53   virtual void Inspect() const OVERRIDE;
54   virtual bool SupportNetworkUsage() const OVERRIDE;
55   virtual void SetSupportNetworkUsage() OVERRIDE;
56   virtual const extensions::Extension* GetExtension() const OVERRIDE;
57
58   // Returns the pid of the extension process.
59   int process_id() const { return pid_; }
60
61   // Returns true if the associated extension has a background page.
62   virtual bool IsBackground() const OVERRIDE;
63
64  private:
65   // The icon painted for the extension process.
66   static gfx::ImageSkia* default_icon_;
67
68   content::RenderViewHost* render_view_host_;
69
70   // Cached data about the extension.
71   base::ProcessHandle process_handle_;
72   int pid_;
73   int unique_process_id_;
74   base::string16 title_;
75
76   DISALLOW_COPY_AND_ASSIGN(ExtensionProcessResource);
77 };
78
79 gfx::ImageSkia* ExtensionProcessResource::default_icon_ = NULL;
80
81 ExtensionProcessResource::ExtensionProcessResource(
82     content::RenderViewHost* render_view_host)
83     : render_view_host_(render_view_host) {
84   if (!default_icon_) {
85     ResourceBundle& rb = ResourceBundle::GetSharedInstance();
86     default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON);
87   }
88   process_handle_ = render_view_host_->GetProcess()->GetHandle();
89   unique_process_id_ = render_view_host->GetProcess()->GetID();
90   pid_ = base::GetProcId(process_handle_);
91   base::string16 extension_name = base::UTF8ToUTF16(GetExtension()->name());
92   DCHECK(!extension_name.empty());
93
94   Profile* profile = Profile::FromBrowserContext(
95       render_view_host->GetProcess()->GetBrowserContext());
96   int message_id = util::GetMessagePrefixID(
97       GetExtension()->is_app(),
98       true,  // is_extension
99       profile->IsOffTheRecord(),
100       false,  // is_prerender
101       IsBackground());
102   title_ = l10n_util::GetStringFUTF16(message_id, extension_name);
103 }
104
105 ExtensionProcessResource::~ExtensionProcessResource() {
106 }
107
108 base::string16 ExtensionProcessResource::GetTitle() const {
109   return title_;
110 }
111
112 base::string16 ExtensionProcessResource::GetProfileName() const {
113   return util::GetProfileNameFromInfoCache(
114       Profile::FromBrowserContext(
115           render_view_host_->GetProcess()->GetBrowserContext()));
116 }
117
118 gfx::ImageSkia ExtensionProcessResource::GetIcon() const {
119   return *default_icon_;
120 }
121
122 base::ProcessHandle ExtensionProcessResource::GetProcess() const {
123   return process_handle_;
124 }
125
126 int ExtensionProcessResource::GetUniqueChildProcessId() const {
127   return unique_process_id_;
128 }
129
130 Resource::Type ExtensionProcessResource::GetType() const {
131   return EXTENSION;
132 }
133
134 bool ExtensionProcessResource::CanInspect() const {
135   return true;
136 }
137
138 void ExtensionProcessResource::Inspect() const {
139   DevToolsWindow::OpenDevToolsWindow(render_view_host_);
140 }
141
142 bool ExtensionProcessResource::SupportNetworkUsage() const {
143   return true;
144 }
145
146 void ExtensionProcessResource::SetSupportNetworkUsage() {
147   NOTREACHED();
148 }
149
150 const Extension* ExtensionProcessResource::GetExtension() const {
151   Profile* profile = Profile::FromBrowserContext(
152       render_view_host_->GetProcess()->GetBrowserContext());
153   extensions::ProcessManager* process_manager =
154       extensions::ExtensionSystem::Get(profile)->process_manager();
155   return process_manager->GetExtensionForRenderViewHost(render_view_host_);
156 }
157
158 bool ExtensionProcessResource::IsBackground() const {
159   WebContents* web_contents =
160       WebContents::FromRenderViewHost(render_view_host_);
161   extensions::ViewType view_type = extensions::GetViewType(web_contents);
162   return view_type == extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
163 }
164
165 ////////////////////////////////////////////////////////////////////////////////
166 // ExtensionProcessResourceProvider class
167 ////////////////////////////////////////////////////////////////////////////////
168
169 ExtensionProcessResourceProvider::
170     ExtensionProcessResourceProvider(TaskManager* task_manager)
171     : task_manager_(task_manager),
172       updating_(false) {
173 }
174
175 ExtensionProcessResourceProvider::~ExtensionProcessResourceProvider() {
176 }
177
178 Resource* ExtensionProcessResourceProvider::GetResource(
179     int origin_pid,
180     int child_id,
181     int route_id) {
182   // If an origin PID was specified, the request is from a plugin, not the
183   // render view host process
184   if (origin_pid)
185     return NULL;
186
187   content::RenderFrameHost* rfh =
188       content::RenderFrameHost::FromID(child_id, route_id);
189   content::WebContents* web_contents =
190       content::WebContents::FromRenderFrameHost(rfh);
191
192   for (ExtensionRenderViewHostMap::iterator i = resources_.begin();
193        i != resources_.end(); i++) {
194     content::WebContents* view_contents =
195         content::WebContents::FromRenderViewHost(i->first);
196     if (web_contents == view_contents)
197       return i->second;
198   }
199
200   // Can happen if the page went away while a network request was being
201   // performed.
202   return NULL;
203 }
204
205 void ExtensionProcessResourceProvider::StartUpdating() {
206   DCHECK(!updating_);
207   updating_ = true;
208
209   // Add all the existing extension views from all Profiles, including those
210   // from incognito split mode.
211   ProfileManager* profile_manager = g_browser_process->profile_manager();
212   std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
213   size_t num_default_profiles = profiles.size();
214   for (size_t i = 0; i < num_default_profiles; ++i) {
215     if (profiles[i]->HasOffTheRecordProfile()) {
216       profiles.push_back(profiles[i]->GetOffTheRecordProfile());
217     }
218   }
219
220   for (size_t i = 0; i < profiles.size(); ++i) {
221     extensions::ProcessManager* process_manager =
222         extensions::ExtensionSystem::Get(profiles[i])->process_manager();
223     if (process_manager) {
224       const extensions::ProcessManager::ViewSet all_views =
225           process_manager->GetAllViews();
226       extensions::ProcessManager::ViewSet::const_iterator jt =
227           all_views.begin();
228       for (; jt != all_views.end(); ++jt) {
229         content::RenderViewHost* rvh = *jt;
230         // Don't add dead extension processes.
231         if (!rvh->IsRenderViewLive())
232           continue;
233
234         AddToTaskManager(rvh);
235       }
236     }
237   }
238
239   // Register for notifications about extension process changes.
240   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED,
241                  content::NotificationService::AllBrowserContextsAndSources());
242   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
243                  content::NotificationService::AllBrowserContextsAndSources());
244   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED,
245                  content::NotificationService::AllBrowserContextsAndSources());
246 }
247
248 void ExtensionProcessResourceProvider::StopUpdating() {
249   DCHECK(updating_);
250   updating_ = false;
251
252   // Unregister for notifications about extension process changes.
253   registrar_.Remove(
254       this, chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED,
255       content::NotificationService::AllBrowserContextsAndSources());
256   registrar_.Remove(
257       this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
258       content::NotificationService::AllBrowserContextsAndSources());
259   registrar_.Remove(
260       this, chrome::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED,
261       content::NotificationService::AllBrowserContextsAndSources());
262
263   // Delete all the resources.
264   STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end());
265
266   resources_.clear();
267 }
268
269 void ExtensionProcessResourceProvider::Observe(
270     int type,
271     const content::NotificationSource& source,
272     const content::NotificationDetails& details) {
273   switch (type) {
274     case chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED:
275       AddToTaskManager(
276           content::Details<content::RenderViewHost>(details).ptr());
277       break;
278     case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED:
279       RemoveFromTaskManager(
280           content::Details<extensions::ExtensionHost>(details).ptr()->
281           render_view_host());
282       break;
283     case chrome::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED:
284       RemoveFromTaskManager(
285           content::Details<content::RenderViewHost>(details).ptr());
286       break;
287     default:
288       NOTREACHED() << "Unexpected notification.";
289       return;
290   }
291 }
292
293 bool ExtensionProcessResourceProvider::
294     IsHandledByThisProvider(content::RenderViewHost* render_view_host) {
295   WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host);
296   // Don't add WebContents that belong to a guest (those are handled by
297   // GuestResourceProvider). Otherwise they will be added twice, and
298   // in this case they will have the app's name as a title (due to the
299   // ExtensionProcessResource constructor).
300   if (web_contents->GetRenderProcessHost()->IsGuest())
301     return false;
302   extensions::ViewType view_type = extensions::GetViewType(web_contents);
303   // Don't add WebContents (those are handled by
304   // TabContentsResourceProvider) or background contents (handled
305   // by BackgroundResourceProvider).
306 #if defined(USE_ASH)
307   return (view_type != extensions::VIEW_TYPE_TAB_CONTENTS &&
308           view_type != extensions::VIEW_TYPE_BACKGROUND_CONTENTS);
309 #else
310   return (view_type != extensions::VIEW_TYPE_TAB_CONTENTS &&
311           view_type != extensions::VIEW_TYPE_BACKGROUND_CONTENTS &&
312           view_type != extensions::VIEW_TYPE_PANEL);
313 #endif  // USE_ASH
314 }
315
316 void ExtensionProcessResourceProvider::AddToTaskManager(
317     content::RenderViewHost* render_view_host) {
318   if (!IsHandledByThisProvider(render_view_host))
319     return;
320
321   ExtensionProcessResource* resource =
322       new ExtensionProcessResource(render_view_host);
323   if (resources_.find(render_view_host) != resources_.end())
324     return;
325   resources_[render_view_host] = resource;
326   task_manager_->AddResource(resource);
327 }
328
329 void ExtensionProcessResourceProvider::RemoveFromTaskManager(
330     content::RenderViewHost* render_view_host) {
331   if (!updating_)
332     return;
333   std::map<content::RenderViewHost*, ExtensionProcessResource*>
334       ::iterator iter = resources_.find(render_view_host);
335   if (iter == resources_.end())
336     return;
337
338   // Remove the resource from the Task Manager.
339   ExtensionProcessResource* resource = iter->second;
340   task_manager_->RemoveResource(resource);
341
342   // Remove it from the provider.
343   resources_.erase(iter);
344
345   // Finally, delete the resource.
346   delete resource;
347 }
348
349 }  // namespace task_manager