Index: chrome/browser/extensions/chrome_app_icon_service.cc |
diff --git a/chrome/browser/extensions/chrome_app_icon_service.cc b/chrome/browser/extensions/chrome_app_icon_service.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..df59e96e8546b162853acc9ef6921342ec12f775 |
--- /dev/null |
+++ b/chrome/browser/extensions/chrome_app_icon_service.cc |
@@ -0,0 +1,115 @@ |
+// 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/chrome_app_icon_service.h" |
+ |
+#include "base/bind.h" |
+#include "base/memory/ptr_util.h" |
+#include "chrome/browser/extensions/chrome_app_icon.h" |
+#include "chrome/browser/extensions/chrome_app_icon_service_factory.h" |
+#include "extensions/browser/extension_registry.h" |
+ |
+#if defined(OS_CHROMEOS) |
+#include "chrome/browser/ui/ash/launcher/launcher_extension_app_updater.h" |
+#endif |
+ |
+namespace extensions { |
+ |
+#if defined(OS_CHROMEOS) |
+ |
+// Handles Chrome app life-cycle events that may change how extension based app |
+// icon looks like. |
+class ChromeAppIconService::Updater : public LauncherAppUpdater::Delegate { |
+ public: |
+ Updater(ChromeAppIconService* 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 { |
+ host_->OnAppUpdated(app_id); |
+ } |
+ |
+ // Unowned pointer. |
+ ChromeAppIconService* const host_; |
+ |
+ LauncherExtensionAppUpdater extension_app_updater_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(Updater); |
+}; |
+ |
+#endif |
+ |
+// static |
+ChromeAppIconService* ChromeAppIconService::Get( |
+ content::BrowserContext* context) { |
+ return ChromeAppIconServiceFactory::GetInstance()->GetForBrowserContext( |
+ context); |
+} |
+ |
+ChromeAppIconService::ChromeAppIconService(content::BrowserContext* context) |
+ : context_(context), observer_(this), weak_ptr_factory_(this) { |
+#if defined(OS_CHROMEOS) |
+ app_updater_ = base::MakeUnique<Updater>(this, context); |
+#endif |
+ |
+ observer_.Add(ExtensionRegistry::Get(context_)); |
+} |
+ |
+ChromeAppIconService::~ChromeAppIconService() = default; |
+ |
+void ChromeAppIconService::Shutdown() { |
+#if defined(OS_CHROMEOS) |
+ app_updater_.reset(); |
+#endif |
+} |
+ |
+std::unique_ptr<ChromeAppIcon> ChromeAppIconService::CreateIcon( |
+ ChromeAppIconDelegate* delegate, |
+ const std::string& app_id, |
+ int resource_size_in_dip) { |
+ std::unique_ptr<ChromeAppIcon> icon = base::MakeUnique<ChromeAppIcon>( |
+ delegate, context_, |
+ base::Bind(&ChromeAppIconService::OnIconDestroyed, |
+ weak_ptr_factory_.GetWeakPtr()), |
+ app_id, resource_size_in_dip); |
+ |
+ icon_map_[icon->app_id()].insert(icon.get()); |
+ return icon; |
+} |
+ |
+void ChromeAppIconService::OnExtensionLoaded( |
+ content::BrowserContext* browser_context, |
+ const Extension* extension) { |
+ OnAppUpdated(extension->id()); |
+} |
+ |
+void ChromeAppIconService::OnExtensionUnloaded( |
+ content::BrowserContext* browser_context, |
+ const Extension* extension, |
+ UnloadedExtensionReason reason) { |
+ OnAppUpdated(extension->id()); |
+} |
+ |
+void ChromeAppIconService::OnAppUpdated(const std::string& app_id) { |
+ IconMap::const_iterator it = icon_map_.find(app_id); |
+ if (it == icon_map_.end()) |
+ return; |
+ for (auto* icon : it->second) |
+ icon->UpdateIcon(); |
+} |
+ |
+void ChromeAppIconService::OnIconDestroyed(ChromeAppIcon* icon) { |
+ DCHECK(icon); |
+ IconMap::iterator it = icon_map_.find(icon->app_id()); |
+ DCHECK(it != icon_map_.end()); |
+ it->second.erase(icon); |
+ if (it->second.empty()) |
+ icon_map_.erase(it); |
+} |
+ |
+} // namespace extensions |