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/background_information.h" 6 7#include "base/i18n/rtl.h" 8#include "base/strings/string16.h" 9#include "base/strings/utf_string_conversions.h" 10#include "chrome/browser/background/background_contents_service.h" 11#include "chrome/browser/background/background_contents_service_factory.h" 12#include "chrome/browser/browser_process.h" 13#include "chrome/browser/chrome_notification_types.h" 14#include "chrome/browser/profiles/profile.h" 15#include "chrome/browser/profiles/profile_manager.h" 16#include "chrome/browser/tab_contents/background_contents.h" 17#include "chrome/browser/task_manager/renderer_resource.h" 18#include "chrome/browser/task_manager/resource_provider.h" 19#include "chrome/browser/task_manager/task_manager.h" 20#include "chrome/grit/generated_resources.h" 21#include "content/public/browser/notification_service.h" 22#include "content/public/browser/render_frame_host.h" 23#include "content/public/browser/render_process_host.h" 24#include "content/public/browser/render_view_host.h" 25#include "content/public/browser/web_contents.h" 26#include "extensions/browser/extension_registry.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 34using content::RenderProcessHost; 35using content::RenderViewHost; 36using content::WebContents; 37using extensions::Extension; 38 39namespace task_manager { 40 41class BackgroundContentsResource : public RendererResource { 42 public: 43 BackgroundContentsResource(BackgroundContents* background_contents, 44 const base::string16& application_name); 45 virtual ~BackgroundContentsResource(); 46 47 // Resource methods: 48 virtual base::string16 GetTitle() const OVERRIDE; 49 virtual gfx::ImageSkia GetIcon() const OVERRIDE; 50 51 const base::string16& application_name() const { return application_name_; } 52 53 private: 54 BackgroundContents* background_contents_; 55 56 base::string16 application_name_; 57 58 // The icon painted for BackgroundContents. 59 // TODO(atwilson): Use the favicon when there's a way to get the favicon for 60 // BackgroundContents. 61 static gfx::ImageSkia* default_icon_; 62 63 DISALLOW_COPY_AND_ASSIGN(BackgroundContentsResource); 64}; 65 66gfx::ImageSkia* BackgroundContentsResource::default_icon_ = NULL; 67 68BackgroundContentsResource::BackgroundContentsResource( 69 BackgroundContents* background_contents, 70 const base::string16& application_name) 71 : RendererResource( 72 background_contents->web_contents()->GetRenderProcessHost()-> 73 GetHandle(), 74 background_contents->web_contents()->GetRenderViewHost()), 75 background_contents_(background_contents), 76 application_name_(application_name) { 77 // Just use the same icon that other extension resources do. 78 // TODO(atwilson): Use the favicon when that's available. 79 if (!default_icon_) { 80 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 81 default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON); 82 } 83 // Ensure that the string has the appropriate direction markers (see comment 84 // in TabContentsResource::GetTitle()). 85 base::i18n::AdjustStringForLocaleDirection(&application_name_); 86} 87 88BackgroundContentsResource::~BackgroundContentsResource() {} 89 90base::string16 BackgroundContentsResource::GetTitle() const { 91 base::string16 title = application_name_; 92 93 if (title.empty()) { 94 // No title (can't locate the parent app for some reason) so just display 95 // the URL (properly forced to be LTR). 96 title = base::i18n::GetDisplayStringInLTRDirectionality( 97 base::UTF8ToUTF16(background_contents_->GetURL().spec())); 98 } 99 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_BACKGROUND_PREFIX, title); 100} 101 102gfx::ImageSkia BackgroundContentsResource::GetIcon() const { 103 return *default_icon_; 104} 105 106//////////////////////////////////////////////////////////////////////////////// 107// BackgroundInformation class 108//////////////////////////////////////////////////////////////////////////////// 109 110BackgroundInformation::BackgroundInformation() {} 111 112BackgroundInformation::~BackgroundInformation() {} 113 114bool BackgroundInformation::CheckOwnership(WebContents* web_contents) { 115 extensions::ViewType view_type = extensions::GetViewType(web_contents); 116 return view_type == extensions::VIEW_TYPE_BACKGROUND_CONTENTS; 117} 118 119void BackgroundInformation::GetAll(const NewWebContentsCallback& callback) { 120 // Add all the existing BackgroundContents from every profile, including 121 // incognito profiles. 122 ProfileManager* profile_manager = g_browser_process->profile_manager(); 123 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles()); 124 size_t num_default_profiles = profiles.size(); 125 for (size_t i = 0; i < num_default_profiles; ++i) { 126 if (profiles[i]->HasOffTheRecordProfile()) { 127 profiles.push_back(profiles[i]->GetOffTheRecordProfile()); 128 } 129 } 130 for (size_t i = 0; i < profiles.size(); ++i) { 131 BackgroundContentsService* background_contents_service = 132 BackgroundContentsServiceFactory::GetForProfile(profiles[i]); 133 std::vector<BackgroundContents*> contents = 134 background_contents_service->GetBackgroundContents(); 135 for (std::vector<BackgroundContents*>::iterator iterator = contents.begin(); 136 iterator != contents.end(); 137 ++iterator) { 138 callback.Run((*iterator)->web_contents()); 139 } 140 } 141} 142 143scoped_ptr<RendererResource> BackgroundInformation::MakeResource( 144 WebContents* web_contents) { 145 Profile* profile = 146 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 147 const extensions::ExtensionSet& extensions_set = 148 extensions::ExtensionRegistry::Get(profile)->enabled_extensions(); 149 BackgroundContentsService* background_contents_service = 150 BackgroundContentsServiceFactory::GetForProfile(profile); 151 std::vector<BackgroundContents*> contents = 152 background_contents_service->GetBackgroundContents(); 153 for (std::vector<BackgroundContents*>::iterator iterator = contents.begin(); 154 iterator != contents.end(); 155 ++iterator) { 156 if ((*iterator)->web_contents() == web_contents) { 157 base::string16 application_name; 158 // Lookup the name from the parent extension. 159 const base::string16& application_id = 160 background_contents_service->GetParentApplicationId(*iterator); 161 const Extension* extension = 162 extensions_set.GetByID(base::UTF16ToUTF8(application_id)); 163 if (extension) 164 application_name = base::UTF8ToUTF16(extension->name()); 165 return scoped_ptr<RendererResource>( 166 new BackgroundContentsResource(*iterator, application_name)); 167 } 168 } 169 NOTREACHED(); 170 return scoped_ptr<RendererResource>(); 171} 172 173} // namespace task_manager 174