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::Get( | |
47 content::BrowserContext* context) { | |
48 return ExtensionAppIconServiceFactory::GetInstance()->GetForBrowserContext( | |
49 context); | |
50 } | |
51 | |
52 ExtensionAppIconService::ExtensionAppIconService( | |
53 content::BrowserContext* context) | |
54 : context_(context), observer_(this), weak_ptr_factory_(this) { | |
55 #if defined(OS_CHROMEOS) | |
56 app_updater_ = base::MakeUnique<Updater>(this, context); | |
57 #endif | |
58 | |
59 observer_.Add(ExtensionRegistry::Get(context_)); | |
60 } | |
61 | |
62 ExtensionAppIconService::~ExtensionAppIconService() = default; | |
63 | |
64 void ExtensionAppIconService::Shutdown() { | |
65 #if defined(OS_CHROMEOS) | |
66 app_updater_.reset(); | |
67 #endif | |
68 observer_.Remove(ExtensionRegistry::Get(context_)); | |
Devlin
2017/05/01 14:51:11
Given this depends on ExtensionRegistry(), we shou
khmel
2017/05/01 20:18:18
Done.
| |
69 } | |
70 | |
71 std::unique_ptr<ExtensionAppIcon> ExtensionAppIconService::CreateIcon( | |
72 ExtensionAppIconDelegate* delegate, | |
73 const std::string& app_id, | |
74 int resource_size_in_dip) { | |
75 std::unique_ptr<ExtensionAppIcon> icon = base::MakeUnique<ExtensionAppIcon>( | |
76 delegate, context_, | |
77 base::Bind(&ExtensionAppIconService::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 ExtensionAppIconService::OnExtensionLoaded( | |
86 content::BrowserContext* browser_context, | |
87 const Extension* extension) { | |
88 OnAppUpdated(extension->id()); | |
89 } | |
90 | |
91 void ExtensionAppIconService::OnExtensionUnloaded( | |
92 content::BrowserContext* browser_context, | |
93 const Extension* extension, | |
94 UnloadedExtensionInfo::Reason reason) { | |
95 OnAppUpdated(extension->id()); | |
96 } | |
97 | |
98 void ExtensionAppIconService::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 ExtensionAppIconService::OnIconDestroyed(ExtensionAppIcon* icon) { | |
107 DCHECK(icon); | |
108 IconMap::iterator it = icon_map_.find(icon->app_id()); | |
109 if (it == icon_map_.end()) { | |
110 NOTREACHED(); | |
Devlin
2017/05/01 14:51:11
Why not a DCHECK? The NOTREACHED(); return; patte
khmel
2017/05/01 20:18:18
Changed to DCHECK
| |
111 return; | |
112 } | |
113 | |
114 it->second.erase(icon); | |
115 if (it->second.empty()) | |
116 icon_map_.erase(it); | |
117 } | |
118 | |
119 } // namespace extensions | |
OLD | NEW |