OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/extensions/extension_app_icon_service.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "chrome/browser/extensions/extension_app_icon.h" |
| 9 #include "chrome/browser/extensions/extension_app_icon_service_factory.h" |
| 10 #include "extensions/browser/extension_registry.h" |
| 11 |
| 12 #if defined(OS_CHROMEOS) |
| 13 #include "chrome/browser/ui/ash/launcher/launcher_extension_app_updater.h" |
| 14 #endif |
| 15 |
| 16 namespace extensions { |
| 17 |
| 18 #if defined(OS_CHROMEOS) |
| 19 |
| 20 class ExtensionAppIconService::Updater : public LauncherAppUpdater::Delegate { |
| 21 public: |
| 22 Updater(ExtensionAppIconService* host, content::BrowserContext* context) |
| 23 : host_(host), extension_app_updater_(this, context) {} |
| 24 |
| 25 ~Updater() override = default; |
| 26 |
| 27 private: |
| 28 // LauncherAppUpdater::Delegate: |
| 29 void OnAppUpdated(content::BrowserContext* browser_context, |
| 30 const std::string& app_id) override { |
| 31 host_->OnAppUpdated(app_id); |
| 32 } |
| 33 |
| 34 // Unowned pointer. |
| 35 ExtensionAppIconService* const host_; |
| 36 |
| 37 LauncherExtensionAppUpdater extension_app_updater_; |
| 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(Updater); |
| 40 }; |
| 41 |
| 42 #endif |
| 43 |
| 44 // static |
| 45 ExtensionAppIconService* ExtensionAppIconService::Create( |
| 46 content::BrowserContext* context) { |
| 47 return new ExtensionAppIconService(context); |
| 48 } |
| 49 |
| 50 // static |
| 51 ExtensionAppIconService* ExtensionAppIconService::Get( |
| 52 content::BrowserContext* context) { |
| 53 return ExtensionAppIconServiceFactory::GetInstance()->GetForBrowserContext( |
| 54 context); |
| 55 } |
| 56 |
| 57 ExtensionAppIconService::ExtensionAppIconService( |
| 58 content::BrowserContext* context) |
| 59 : context_(context), weak_ptr_factory_(this) { |
| 60 #if defined(OS_CHROMEOS) |
| 61 app_updater_ = base::MakeUnique<Updater>(this, context); |
| 62 #endif |
| 63 ExtensionRegistry* registry = ExtensionRegistry::Get(context_); |
| 64 registry->AddObserver(this); |
| 65 } |
| 66 |
| 67 ExtensionAppIconService::~ExtensionAppIconService() = default; |
| 68 |
| 69 void ExtensionAppIconService::Shutdown() { |
| 70 #if defined(OS_CHROMEOS) |
| 71 app_updater_.reset(); |
| 72 #endif |
| 73 ExtensionRegistry* registry = extensions::ExtensionRegistry::Get(context_); |
| 74 registry->RemoveObserver(this); |
| 75 } |
| 76 |
| 77 std::unique_ptr<ExtensionAppIcon> ExtensionAppIconService::CreateIcon( |
| 78 ExtensionAppIconDelegate* delegate, |
| 79 const std::string& app_id, |
| 80 int resource_size_in_dip) { |
| 81 std::unique_ptr<ExtensionAppIcon> icon = base::MakeUnique<ExtensionAppIcon>( |
| 82 weak_ptr_factory_.GetWeakPtr(), delegate, context_, app_id, |
| 83 resource_size_in_dip); |
| 84 |
| 85 icon_map_.insert(std::make_pair(icon->app_id(), icon.get())); |
| 86 return icon; |
| 87 } |
| 88 void ExtensionAppIconService::OnExtensionLoaded( |
| 89 content::BrowserContext* browser_context, |
| 90 const Extension* extension) { |
| 91 OnAppUpdated(extension->id()); |
| 92 } |
| 93 |
| 94 void ExtensionAppIconService::OnExtensionUnloaded( |
| 95 content::BrowserContext* browser_context, |
| 96 const Extension* extension, |
| 97 UnloadedExtensionInfo::Reason reason) { |
| 98 OnAppUpdated(extension->id()); |
| 99 } |
| 100 |
| 101 void ExtensionAppIconService::OnAppUpdated(const std::string& app_id) { |
| 102 auto icon_range = icon_map_.equal_range(app_id); |
| 103 for (auto icon = icon_range.first; icon != icon_range.second; ++icon) |
| 104 icon->second->UpdateIcon(); |
| 105 } |
| 106 |
| 107 void ExtensionAppIconService::OnIconDestroyed(ExtensionAppIcon* icon) { |
| 108 DCHECK(icon); |
| 109 auto icon_range = icon_map_.equal_range(icon->app_id()); |
| 110 for (auto scan_icon = icon_range.first; scan_icon != icon_range.second; |
| 111 ++scan_icon) { |
| 112 if (scan_icon->second == icon) { |
| 113 icon_map_.erase(scan_icon); |
| 114 return; |
| 115 } |
| 116 } |
| 117 NOTREACHED(); |
| 118 } |
| 119 |
| 120 } // namespace extensions |
OLD | NEW |