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