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