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

Side by Side Diff: chrome/browser/extensions/chrome_app_icon_service.cc

Issue 2819413003: Refactor extension app icon. (Closed)
Patch Set: nits Created 3 years, 7 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 unified diff | Download patch
OLDNEW
(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/chrome_app_icon_service.h"
6
7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h"
9 #include "chrome/browser/extensions/chrome_app_icon.h"
10 #include "chrome/browser/extensions/chrome_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 // Handles Chrome app life-cycle events that may change how extension based app
22 // icon looks like.
23 class ChromeAppIconService::Updater : public LauncherAppUpdater::Delegate {
24 public:
25 Updater(ChromeAppIconService* host, content::BrowserContext* context)
26 : host_(host), extension_app_updater_(this, context) {}
27
28 ~Updater() override = default;
29
30 private:
31 // LauncherAppUpdater::Delegate:
32 void OnAppUpdated(content::BrowserContext* browser_context,
33 const std::string& app_id) override {
34 host_->OnAppUpdated(app_id);
35 }
36
37 // Unowned pointer.
38 ChromeAppIconService* const host_;
39
40 LauncherExtensionAppUpdater extension_app_updater_;
41
42 DISALLOW_COPY_AND_ASSIGN(Updater);
43 };
44
45 #endif
46
47 // static
48 ChromeAppIconService* ChromeAppIconService::Get(
49 content::BrowserContext* context) {
50 return ChromeAppIconServiceFactory::GetInstance()->GetForBrowserContext(
51 context);
52 }
53
54 ChromeAppIconService::ChromeAppIconService(content::BrowserContext* context)
55 : context_(context), observer_(this), weak_ptr_factory_(this) {
56 #if defined(OS_CHROMEOS)
57 app_updater_ = base::MakeUnique<Updater>(this, context);
58 #endif
59
60 observer_.Add(ExtensionRegistry::Get(context_));
61 }
62
63 ChromeAppIconService::~ChromeAppIconService() = default;
64
65 void ChromeAppIconService::Shutdown() {
66 #if defined(OS_CHROMEOS)
67 app_updater_.reset();
68 #endif
69 }
70
71 std::unique_ptr<ChromeAppIcon> ChromeAppIconService::CreateIcon(
72 ChromeAppIconDelegate* delegate,
73 const std::string& app_id,
74 int resource_size_in_dip) {
75 std::unique_ptr<ChromeAppIcon> icon = base::MakeUnique<ChromeAppIcon>(
76 delegate, context_,
77 base::Bind(&ChromeAppIconService::OnIconDestroyed,
78 weak_ptr_factory_.GetWeakPtr()),
79 app_id, resource_size_in_dip);
80
81 icon_map_[icon->app_id()].insert(icon.get());
82 return icon;
83 }
84
85 void ChromeAppIconService::OnExtensionLoaded(
86 content::BrowserContext* browser_context,
87 const Extension* extension) {
88 OnAppUpdated(extension->id());
89 }
90
91 void ChromeAppIconService::OnExtensionUnloaded(
92 content::BrowserContext* browser_context,
93 const Extension* extension,
94 UnloadedExtensionReason reason) {
95 OnAppUpdated(extension->id());
96 }
97
98 void ChromeAppIconService::OnAppUpdated(const std::string& app_id) {
99 IconMap::const_iterator it = icon_map_.find(app_id);
100 if (it == icon_map_.end())
101 return;
102 for (auto* icon : it->second)
103 icon->UpdateIcon();
104 }
105
106 void ChromeAppIconService::OnIconDestroyed(ChromeAppIcon* icon) {
107 DCHECK(icon);
108 IconMap::iterator it = icon_map_.find(icon->app_id());
109 DCHECK(it != icon_map_.end());
110 it->second.erase(icon);
111 if (it->second.empty())
112 icon_map_.erase(it);
113 }
114
115 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698