Index: chrome/browser/extensions/extension_app_icon_service.cc |
diff --git a/chrome/browser/extensions/extension_app_icon_service.cc b/chrome/browser/extensions/extension_app_icon_service.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5ffbebc77f4647ae9e8b03190bcddacc62d2b098 |
--- /dev/null |
+++ b/chrome/browser/extensions/extension_app_icon_service.cc |
@@ -0,0 +1,104 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/extension_app_icon_service.h" |
+ |
+#include "base/memory/ptr_util.h" |
+#include "chrome/browser/extensions/extension_app_icon.h" |
+#include "chrome/browser/extensions/extension_app_icon_service_factory.h" |
+ |
+#if defined(OS_CHROMEOS) |
+#include "chrome/browser/ui/ash/launcher/launcher_extension_app_updater.h" |
+#endif |
+ |
+namespace extensions { |
+ |
+#if defined(OS_CHROMEOS) |
+ |
+class ExtensionAppIconService::Updater : public LauncherAppUpdater::Delegate { |
+ public: |
+ Updater(const base::WeakPtr<ExtensionAppIconService>& host, |
+ content::BrowserContext* context) |
+ : host_(host), extension_app_updater_(this, context) {} |
+ |
+ ~Updater() override = default; |
+ |
+ private: |
+ // LauncherAppUpdater::Delegate: |
+ void OnAppUpdated(content::BrowserContext* browser_context, |
+ const std::string& app_id) override { |
+ if (host_) |
+ host_->OnAppUpdated(app_id); |
+ } |
+ |
+ base::WeakPtr<ExtensionAppIconService> host_; |
xiyuan
2017/04/18 20:43:00
WeakPtr is not necessary since |host_| owns |this|
khmel
2017/04/20 00:13:38
Done.
|
+ LauncherExtensionAppUpdater extension_app_updater_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(Updater); |
+}; |
+ |
+#endif |
+ |
+// static |
+ExtensionAppIconService* ExtensionAppIconService::Create( |
+ content::BrowserContext* context) { |
+ return new ExtensionAppIconService(context); |
+} |
+ |
+// static |
+ExtensionAppIconService* ExtensionAppIconService::Get( |
+ content::BrowserContext* context) { |
+ return ExtensionAppIconServiceFactory::GetInstance()->GetForBrowserContext( |
+ context); |
+} |
+ |
+ExtensionAppIconService::ExtensionAppIconService( |
+ content::BrowserContext* context) |
+ : context_(context), weak_ptr_factory_(this) { |
+#if defined(OS_CHROMEOS) |
+ app_updater_ = |
+ base::MakeUnique<Updater>(weak_ptr_factory_.GetWeakPtr(), context); |
+#endif |
+} |
+ |
+ExtensionAppIconService::~ExtensionAppIconService() = default; |
+ |
+void ExtensionAppIconService::Shutdown() { |
+#if defined(OS_CHROMEOS) |
+ app_updater_.reset(); |
+#endif |
+} |
+ |
+std::unique_ptr<ExtensionAppIcon> ExtensionAppIconService::CreateIcon( |
+ ExtensionAppIconDelegate* delegate, |
+ const std::string& app_id, |
+ int resource_size_in_dip) { |
+ std::unique_ptr<ExtensionAppIcon> icon = base::MakeUnique<ExtensionAppIcon>( |
+ weak_ptr_factory_.GetWeakPtr(), delegate, context_, app_id, |
+ resource_size_in_dip); |
+ |
+ icon_map_.insert(std::make_pair(icon->app_id(), icon.get())); |
+ return icon; |
+} |
+ |
+void ExtensionAppIconService::OnAppUpdated(const std::string& app_id) { |
+ auto icon_range = icon_map_.equal_range(app_id); |
+ for (auto icon = icon_range.first; icon != icon_range.second; ++icon) |
+ icon->second->UpdateIcon(); |
+} |
+ |
+void ExtensionAppIconService::OnIconDestroyed(ExtensionAppIcon* icon) { |
+ DCHECK(icon); |
+ auto icon_range = icon_map_.equal_range(icon->app_id()); |
+ for (auto scan_icon = icon_range.first; scan_icon != icon_range.second; |
+ ++scan_icon) { |
+ if (scan_icon->second == icon) { |
+ icon_map_.erase(scan_icon); |
+ return; |
+ } |
+ } |
+ NOTREACHED(); |
+} |
+ |
+} // namespace extensions |