Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1509)

Side by Side Diff: chrome/browser/task_manager/task_manager_resource_providers.cc

Issue 10790062: [Panels refactor] Track Panels in TaskManager now that they are not under the tab contents umbrella. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: feedback changes Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/task_manager/task_manager_resource_providers.h" 5 #include "chrome/browser/task_manager/task_manager_resource_providers.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 20 matching lines...) Expand all
31 #include "chrome/browser/prerender/prerender_manager.h" 31 #include "chrome/browser/prerender/prerender_manager.h"
32 #include "chrome/browser/prerender/prerender_manager_factory.h" 32 #include "chrome/browser/prerender/prerender_manager_factory.h"
33 #include "chrome/browser/profiles/profile.h" 33 #include "chrome/browser/profiles/profile.h"
34 #include "chrome/browser/profiles/profile_info_cache.h" 34 #include "chrome/browser/profiles/profile_info_cache.h"
35 #include "chrome/browser/profiles/profile_manager.h" 35 #include "chrome/browser/profiles/profile_manager.h"
36 #include "chrome/browser/tab_contents/background_contents.h" 36 #include "chrome/browser/tab_contents/background_contents.h"
37 #include "chrome/browser/tab_contents/tab_util.h" 37 #include "chrome/browser/tab_contents/tab_util.h"
38 #include "chrome/browser/ui/browser.h" 38 #include "chrome/browser/ui/browser.h"
39 #include "chrome/browser/ui/browser_instant_controller.h" 39 #include "chrome/browser/ui/browser_instant_controller.h"
40 #include "chrome/browser/ui/browser_list.h" 40 #include "chrome/browser/ui/browser_list.h"
41 #include "chrome/browser/ui/panels/panel.h"
42 #include "chrome/browser/ui/panels/panel_manager.h"
41 #include "chrome/browser/ui/tab_contents/tab_contents.h" 43 #include "chrome/browser/ui/tab_contents/tab_contents.h"
42 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" 44 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
43 #include "chrome/browser/view_type_utils.h" 45 #include "chrome/browser/view_type_utils.h"
44 #include "chrome/common/chrome_notification_types.h" 46 #include "chrome/common/chrome_notification_types.h"
45 #include "chrome/common/chrome_switches.h" 47 #include "chrome/common/chrome_switches.h"
46 #include "chrome/common/extensions/extension.h" 48 #include "chrome/common/extensions/extension.h"
47 #include "chrome/common/render_messages.h" 49 #include "chrome/common/render_messages.h"
48 #include "chrome/common/url_constants.h" 50 #include "chrome/common/url_constants.h"
49 #include "content/public/browser/browser_child_process_host_iterator.h" 51 #include "content/public/browser/browser_child_process_host_iterator.h"
50 #include "content/public/browser/browser_thread.h" 52 #include "content/public/browser/browser_thread.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 return IDS_TASK_MANAGER_EXTENSION_PREFIX; 98 return IDS_TASK_MANAGER_EXTENSION_PREFIX;
97 } else if (is_prerender) { 99 } else if (is_prerender) {
98 return IDS_TASK_MANAGER_PRERENDER_PREFIX; 100 return IDS_TASK_MANAGER_PRERENDER_PREFIX;
99 } else if (is_instant_preview) { 101 } else if (is_instant_preview) {
100 return IDS_TASK_MANAGER_INSTANT_PREVIEW_PREFIX; 102 return IDS_TASK_MANAGER_INSTANT_PREVIEW_PREFIX;
101 } else { 103 } else {
102 return IDS_TASK_MANAGER_TAB_PREFIX; 104 return IDS_TASK_MANAGER_TAB_PREFIX;
103 } 105 }
104 } 106 }
105 107
108 string16 GetProfileNameFromInfoCache(Profile* profile) {
109 ProfileInfoCache& cache =
110 g_browser_process->profile_manager()->GetProfileInfoCache();
111 size_t index = cache.GetIndexOfProfileWithPath(
112 profile->GetOriginalProfile()->GetPath());
113 if (index == std::string::npos)
114 return string16();
115 else
116 return cache.GetNameOfProfileAtIndex(index);
117 }
118
106 } // namespace 119 } // namespace
107 120
108 //////////////////////////////////////////////////////////////////////////////// 121 ////////////////////////////////////////////////////////////////////////////////
109 // TaskManagerRendererResource class 122 // TaskManagerRendererResource class
110 //////////////////////////////////////////////////////////////////////////////// 123 ////////////////////////////////////////////////////////////////////////////////
111 TaskManagerRendererResource::TaskManagerRendererResource( 124 TaskManagerRendererResource::TaskManagerRendererResource(
112 base::ProcessHandle process, content::RenderViewHost* render_view_host) 125 base::ProcessHandle process, content::RenderViewHost* render_view_host)
113 : process_(process), 126 : process_(process),
114 render_view_host_(render_view_host), 127 render_view_host_(render_view_host),
115 pending_stats_update_(false), 128 pending_stats_update_(false),
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 int message_id = GetMessagePrefixID( 321 int message_id = GetMessagePrefixID(
309 is_app, 322 is_app,
310 HostsExtension(), 323 HostsExtension(),
311 tab_contents_->profile()->IsOffTheRecord(), 324 tab_contents_->profile()->IsOffTheRecord(),
312 IsPrerendering(), 325 IsPrerendering(),
313 is_instant_preview_); 326 is_instant_preview_);
314 return l10n_util::GetStringFUTF16(message_id, tab_title); 327 return l10n_util::GetStringFUTF16(message_id, tab_title);
315 } 328 }
316 329
317 string16 TaskManagerTabContentsResource::GetProfileName() const { 330 string16 TaskManagerTabContentsResource::GetProfileName() const {
318 ProfileInfoCache& cache = 331 return GetProfileNameFromInfoCache(tab_contents_->profile());
319 g_browser_process->profile_manager()->GetProfileInfoCache();
320 Profile* profile = tab_contents_->profile()->GetOriginalProfile();
321 size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
322 if (index == std::string::npos)
323 return string16();
324 else
325 return cache.GetNameOfProfileAtIndex(index);
326 } 332 }
327 333
328 gfx::ImageSkia TaskManagerTabContentsResource::GetIcon() const { 334 gfx::ImageSkia TaskManagerTabContentsResource::GetIcon() const {
329 if (IsPrerendering()) 335 if (IsPrerendering())
330 return *prerender_icon_; 336 return *prerender_icon_;
331 return tab_contents_->favicon_tab_helper()->GetFavicon(); 337 return tab_contents_->favicon_tab_helper()->GetFavicon();
332 } 338 }
333 339
334 TabContents* TaskManagerTabContentsResource::GetTabContents() const { 340 WebContents* TaskManagerTabContentsResource::GetWebContents() const {
335 return tab_contents_; 341 return tab_contents_->web_contents();
336 } 342 }
337 343
338 const Extension* TaskManagerTabContentsResource::GetExtension() const { 344 const Extension* TaskManagerTabContentsResource::GetExtension() const {
339 if (HostsExtension()) { 345 if (HostsExtension()) {
340 ExtensionService* extension_service = 346 ExtensionService* extension_service =
341 tab_contents_->profile()->GetExtensionService(); 347 tab_contents_->profile()->GetExtensionService();
342 return extension_service->extensions()->GetByID( 348 return extension_service->extensions()->GetByID(
343 tab_contents_->web_contents()->GetURL().host()); 349 tab_contents_->web_contents()->GetURL().host());
344 } 350 }
345 351
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 case chrome::NOTIFICATION_INSTANT_COMMITTED: 523 case chrome::NOTIFICATION_INSTANT_COMMITTED:
518 Update(tab_contents); 524 Update(tab_contents);
519 break; 525 break;
520 default: 526 default:
521 NOTREACHED() << "Unexpected notification."; 527 NOTREACHED() << "Unexpected notification.";
522 return; 528 return;
523 } 529 }
524 } 530 }
525 531
526 //////////////////////////////////////////////////////////////////////////////// 532 ////////////////////////////////////////////////////////////////////////////////
533 // TaskManagerPanelResource class
534 ////////////////////////////////////////////////////////////////////////////////
535
536 TaskManagerPanelResource::TaskManagerPanelResource(Panel* panel)
537 : TaskManagerRendererResource(
538 panel->GetWebContents()->GetRenderProcessHost()->GetHandle(),
539 panel->GetWebContents()->GetRenderViewHost()),
540 panel_(panel) {
541 message_prefix_id_ = GetMessagePrefixID(
542 GetExtension()->is_app(), true, panel->profile()->IsOffTheRecord(),
543 false, false);
544 }
545
546 TaskManagerPanelResource::~TaskManagerPanelResource() {
547 }
548
549 TaskManager::Resource::Type TaskManagerPanelResource::GetType() const {
550 return EXTENSION;
551 }
552
553 string16 TaskManagerPanelResource::GetTitle() const {
554 string16 title = panel_->GetWindowTitle();
555 // Since the title will be concatenated with an IDS_TASK_MANAGER_* prefix
556 // we need to explicitly set the title to be LTR format if there is no
557 // strong RTL charater in it. Otherwise, if the task manager prefix is an
558 // RTL word, the concatenated result might be wrong. For example,
559 // a page whose title is "Yahoo! Mail: The best web-based Email!", without
560 // setting it explicitly as LTR format, the concatenated result will be
561 // "!Yahoo! Mail: The best web-based Email :PPA", in which the capital
562 // letters "PPA" stands for the Hebrew word for "app".
563 base::i18n::AdjustStringForLocaleDirection(&title);
564
565 return l10n_util::GetStringFUTF16(message_prefix_id_, title);
566 }
567
568 string16 TaskManagerPanelResource::GetProfileName() const {
569 return GetProfileNameFromInfoCache(panel_->profile());
570 }
571
572 gfx::ImageSkia TaskManagerPanelResource::GetIcon() const {
573 return panel_->GetCurrentPageIcon();
574 }
575
576 WebContents* TaskManagerPanelResource::GetWebContents() const {
577 return panel_->GetWebContents();
578 }
579
580 const Extension* TaskManagerPanelResource::GetExtension() const {
581 ExtensionService* extension_service =
582 panel_->profile()->GetExtensionService();
583 return extension_service->extensions()->GetByID(panel_->extension_id());
584 }
585
586 ////////////////////////////////////////////////////////////////////////////////
587 // TaskManagerPanelResourceProvider class
588 ////////////////////////////////////////////////////////////////////////////////
589
590 TaskManagerPanelResourceProvider::TaskManagerPanelResourceProvider(
591 TaskManager* task_manager)
592 : updating_(false),
593 task_manager_(task_manager) {
594 }
595
596 TaskManagerPanelResourceProvider::~TaskManagerPanelResourceProvider() {
597 }
598
599 TaskManager::Resource* TaskManagerPanelResourceProvider::GetResource(
600 int origin_pid,
601 int render_process_host_id,
602 int routing_id) {
603 // If an origin PID was specified, the request is from a plugin, not the
604 // render view host process
605 if (origin_pid)
606 return NULL;
607
608 for (PanelResourceMap::iterator i = resources_.begin();
609 i != resources_.end(); ++i) {
610 WebContents* contents = i->first->GetWebContents();
611 if (contents &&
612 contents->GetRenderProcessHost()->GetID() == render_process_host_id &&
613 contents->GetRenderViewHost()->GetRoutingID() == routing_id) {
614 return i->second;
615 }
616 }
617
618 // Can happen if the panel went away while a network request was being
619 // performed.
620 return NULL;
621 }
622
623 void TaskManagerPanelResourceProvider::StartUpdating() {
624 if (!CommandLine::ForCurrentProcess()->HasSwitch(
625 switches::kBrowserlessPanels))
626 return;
627
628 DCHECK(!updating_);
629 updating_ = true;
630
631 // Add all the Panels.
632 std::vector<Panel*> panels = PanelManager::GetInstance()->panels();
633 for (size_t i = 0; i < panels.size(); ++i)
634 Add(panels[i]);
635
636 // Then we register for notifications to get new and remove closed panels.
637 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
638 content::NotificationService::AllSources());
639 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
640 content::NotificationService::AllSources());
641 }
642
643 void TaskManagerPanelResourceProvider::StopUpdating() {
644 if (!CommandLine::ForCurrentProcess()->HasSwitch(
645 switches::kBrowserlessPanels))
646 return;
647
648 DCHECK(updating_);
649 updating_ = false;
650
651 // Unregister for notifications about new/removed panels.
652 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
653 content::NotificationService::AllSources());
654 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
655 content::NotificationService::AllSources());
656
657 // Delete all the resources.
658 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end());
659 resources_.clear();
660 }
661
662 void TaskManagerPanelResourceProvider::Add(Panel* panel) {
663 if (!updating_)
664 return;
665
666 PanelResourceMap::const_iterator iter = resources_.find(panel);
667 if (iter != resources_.end())
668 return;
669
670 TaskManagerPanelResource* resource = new TaskManagerPanelResource(panel);
671 resources_[panel] = resource;
672 task_manager_->AddResource(resource);
673 }
674
675 void TaskManagerPanelResourceProvider::Remove(Panel* panel) {
676 if (!updating_)
677 return;
678
679 PanelResourceMap::iterator iter = resources_.find(panel);
680 if (iter == resources_.end())
681 return;
682
683 TaskManagerPanelResource* resource = iter->second;
684 task_manager_->RemoveResource(resource);
685 resources_.erase(iter);
686 delete resource;
687 }
688
689 void TaskManagerPanelResourceProvider::Observe(int type,
690 const content::NotificationSource& source,
691 const content::NotificationDetails& details) {
692 WebContents* web_contents = content::Source<WebContents>(source).ptr();
693 if (chrome::GetViewType(web_contents) != chrome::VIEW_TYPE_PANEL)
694 return;
695
696 switch (type) {
697 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED:
698 {
699 std::vector<Panel*>panels = PanelManager::GetInstance()->panels();
700 for (size_t i = 0; i < panels.size(); ++i) {
701 if (panels[i]->GetWebContents() == web_contents) {
702 Add(panels[i]);
703 break;
704 }
705 }
706 break;
707 }
708 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED:
709 {
710 for (PanelResourceMap::iterator iter = resources_.begin();
711 iter != resources_.end(); ++iter) {
712 Panel* panel = iter->first;
713 if (panel->GetWebContents() == web_contents) {
714 Remove(panel);
715 break;
716 }
717 }
718 break;
719 }
720 default:
721 NOTREACHED() << "Unexpected notificiation.";
722 break;
723 }
724 }
725
726 ////////////////////////////////////////////////////////////////////////////////
527 // TaskManagerBackgroundContentsResource class 727 // TaskManagerBackgroundContentsResource class
528 //////////////////////////////////////////////////////////////////////////////// 728 ////////////////////////////////////////////////////////////////////////////////
529 729
530 gfx::ImageSkia* TaskManagerBackgroundContentsResource::default_icon_ = NULL; 730 gfx::ImageSkia* TaskManagerBackgroundContentsResource::default_icon_ = NULL;
531 731
532 // TODO(atwilson): http://crbug.com/116893 732 // TODO(atwilson): http://crbug.com/116893
533 // HACK: if the process handle is invalid, we use the current process's handle. 733 // HACK: if the process handle is invalid, we use the current process's handle.
534 // This preserves old behavior but is incorrect, and should be fixed. 734 // This preserves old behavior but is incorrect, and should be fixed.
535 TaskManagerBackgroundContentsResource::TaskManagerBackgroundContentsResource( 735 TaskManagerBackgroundContentsResource::TaskManagerBackgroundContentsResource(
536 BackgroundContents* background_contents, 736 BackgroundContents* background_contents,
(...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after
1130 } 1330 }
1131 1331
1132 TaskManagerExtensionProcessResource::~TaskManagerExtensionProcessResource() { 1332 TaskManagerExtensionProcessResource::~TaskManagerExtensionProcessResource() {
1133 } 1333 }
1134 1334
1135 string16 TaskManagerExtensionProcessResource::GetTitle() const { 1335 string16 TaskManagerExtensionProcessResource::GetTitle() const {
1136 return title_; 1336 return title_;
1137 } 1337 }
1138 1338
1139 string16 TaskManagerExtensionProcessResource::GetProfileName() const { 1339 string16 TaskManagerExtensionProcessResource::GetProfileName() const {
1140 ProfileInfoCache& cache = 1340 return GetProfileNameFromInfoCache(Profile::FromBrowserContext(
1141 g_browser_process->profile_manager()->GetProfileInfoCache(); 1341 render_view_host_->GetProcess()->GetBrowserContext()));
1142 Profile* profile = Profile::FromBrowserContext(
1143 render_view_host_->GetProcess()->GetBrowserContext());
1144 profile = profile->GetOriginalProfile();
1145 size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
1146 if (index == std::string::npos)
1147 return string16();
1148 else
1149 return cache.GetNameOfProfileAtIndex(index);
1150 } 1342 }
1151 1343
1152 gfx::ImageSkia TaskManagerExtensionProcessResource::GetIcon() const { 1344 gfx::ImageSkia TaskManagerExtensionProcessResource::GetIcon() const {
1153 return *default_icon_; 1345 return *default_icon_;
1154 } 1346 }
1155 1347
1156 base::ProcessHandle TaskManagerExtensionProcessResource::GetProcess() const { 1348 base::ProcessHandle TaskManagerExtensionProcessResource::GetProcess() const {
1157 return process_handle_; 1349 return process_handle_;
1158 } 1350 }
1159 1351
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
1310 } 1502 }
1311 } 1503 }
1312 1504
1313 bool TaskManagerExtensionProcessResourceProvider:: 1505 bool TaskManagerExtensionProcessResourceProvider::
1314 IsHandledByThisProvider(content::RenderViewHost* render_view_host) { 1506 IsHandledByThisProvider(content::RenderViewHost* render_view_host) {
1315 // Don't add WebContents (those are handled by 1507 // Don't add WebContents (those are handled by
1316 // TaskManagerTabContentsResourceProvider) or background contents (handled 1508 // TaskManagerTabContentsResourceProvider) or background contents (handled
1317 // by TaskManagerBackgroundResourceProvider). 1509 // by TaskManagerBackgroundResourceProvider).
1318 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host); 1510 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host);
1319 chrome::ViewType view_type = chrome::GetViewType(web_contents); 1511 chrome::ViewType view_type = chrome::GetViewType(web_contents);
1512 #if defined(USE_ASH)
1320 return (view_type != chrome::VIEW_TYPE_TAB_CONTENTS && 1513 return (view_type != chrome::VIEW_TYPE_TAB_CONTENTS &&
1321 view_type != chrome::VIEW_TYPE_BACKGROUND_CONTENTS); 1514 view_type != chrome::VIEW_TYPE_BACKGROUND_CONTENTS);
1515 #else
1516 return (view_type != chrome::VIEW_TYPE_TAB_CONTENTS &&
1517 view_type != chrome::VIEW_TYPE_BACKGROUND_CONTENTS &&
1518 view_type != chrome::VIEW_TYPE_PANEL);
1519 #endif // USE_ASH
1322 } 1520 }
1323 1521
1324 void TaskManagerExtensionProcessResourceProvider::AddToTaskManager( 1522 void TaskManagerExtensionProcessResourceProvider::AddToTaskManager(
1325 content::RenderViewHost* render_view_host) { 1523 content::RenderViewHost* render_view_host) {
1326 if (!IsHandledByThisProvider(render_view_host)) 1524 if (!IsHandledByThisProvider(render_view_host))
1327 return; 1525 return;
1328 1526
1329 TaskManagerExtensionProcessResource* resource = 1527 TaskManagerExtensionProcessResource* resource =
1330 new TaskManagerExtensionProcessResource(render_view_host); 1528 new TaskManagerExtensionProcessResource(render_view_host);
1331 DCHECK(resources_.find(render_view_host) == resources_.end()); 1529 DCHECK(resources_.find(render_view_host) == resources_.end());
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
1492 1690
1493 return &resource_; 1691 return &resource_;
1494 } 1692 }
1495 1693
1496 void TaskManagerBrowserProcessResourceProvider::StartUpdating() { 1694 void TaskManagerBrowserProcessResourceProvider::StartUpdating() {
1497 task_manager_->AddResource(&resource_); 1695 task_manager_->AddResource(&resource_);
1498 } 1696 }
1499 1697
1500 void TaskManagerBrowserProcessResourceProvider::StopUpdating() { 1698 void TaskManagerBrowserProcessResourceProvider::StopUpdating() {
1501 } 1699 }
OLDNEW
« no previous file with comments | « chrome/browser/task_manager/task_manager_resource_providers.h ('k') | chrome/browser/ui/panels/panel_host.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698