Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Unified Diff: chrome/browser/extensions/extension_app_icon_service.cc

Issue 2819413003: Refactor extension app icon. (Closed)
Patch Set: fixed tests on non-CrOS (use app_list deps only in CrOS) Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..07fd4d2ac70439d50de288439ab34ecf7c843ff0
--- /dev/null
+++ b/chrome/browser/extensions/extension_app_icon_service.cc
@@ -0,0 +1,120 @@
+// 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"
+#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)
+
+class ExtensionAppIconService::Updater : public LauncherAppUpdater::Delegate {
+ public:
+ Updater(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 {
+ host_->OnAppUpdated(app_id);
+ }
+
+ // Unowned pointer.
+ ExtensionAppIconService* const host_;
+
+ 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>(this, context);
+#endif
+ ExtensionRegistry* registry = ExtensionRegistry::Get(context_);
+ registry->AddObserver(this);
xiyuan 2017/04/20 16:36:31 nit: get rid of |registry| and inline ExtensionReg
khmel 2017/04/20 17:46:18 Done.
+}
+
+ExtensionAppIconService::~ExtensionAppIconService() = default;
+
+void ExtensionAppIconService::Shutdown() {
+#if defined(OS_CHROMEOS)
+ app_updater_.reset();
+#endif
+ ExtensionRegistry* registry = extensions::ExtensionRegistry::Get(context_);
+ registry->RemoveObserver(this);
xiyuan 2017/04/20 16:36:31 nit: ditto
khmel 2017/04/20 17:46:18 Done.
+}
+
+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::OnExtensionLoaded(
xiyuan 2017/04/20 16:36:31 nit: insert an empty line before
khmel 2017/04/20 17:46:18 Done.
+ content::BrowserContext* browser_context,
+ const Extension* extension) {
+ OnAppUpdated(extension->id());
+}
+
+void ExtensionAppIconService::OnExtensionUnloaded(
+ content::BrowserContext* browser_context,
+ const Extension* extension,
+ UnloadedExtensionInfo::Reason reason) {
+ OnAppUpdated(extension->id());
+}
+
+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

Powered by Google App Engine
This is Rietveld 408576698