Chromium Code Reviews| 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/memory/ptr_util.h" | |
| 8 #include "chrome/browser/extensions/extension_app_icon.h" | |
| 9 #include "chrome/browser/extensions/extension_app_icon_service_factory.h" | |
| 10 | |
| 11 #if defined(OS_CHROMEOS) | |
| 12 #include "chrome/browser/ui/ash/launcher/launcher_extension_app_updater.h" | |
| 13 #endif | |
| 14 | |
| 15 namespace extensions { | |
| 16 | |
| 17 #if defined(OS_CHROMEOS) | |
| 18 | |
| 19 class ExtensionAppIconService::Updater : public LauncherAppUpdater::Delegate { | |
| 20 public: | |
| 21 Updater(const base::WeakPtr<ExtensionAppIconService>& host, | |
| 22 content::BrowserContext* context) | |
| 23 : host_(host), extension_app_updater_(this, context) {} | |
| 24 | |
| 25 ~Updater() override = default; | |
| 26 | |
| 27 private: | |
| 28 // LauncherAppUpdater::Delegate: | |
| 29 void OnAppUpdated(content::BrowserContext* browser_context, | |
| 30 const std::string& app_id) override { | |
| 31 if (host_) | |
| 32 host_->OnAppUpdated(app_id); | |
| 33 } | |
| 34 | |
| 35 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.
| |
| 36 LauncherExtensionAppUpdater extension_app_updater_; | |
| 37 | |
| 38 DISALLOW_COPY_AND_ASSIGN(Updater); | |
| 39 }; | |
| 40 | |
| 41 #endif | |
| 42 | |
| 43 // static | |
| 44 ExtensionAppIconService* ExtensionAppIconService::Create( | |
| 45 content::BrowserContext* context) { | |
| 46 return new ExtensionAppIconService(context); | |
| 47 } | |
| 48 | |
| 49 // static | |
| 50 ExtensionAppIconService* ExtensionAppIconService::Get( | |
| 51 content::BrowserContext* context) { | |
| 52 return ExtensionAppIconServiceFactory::GetInstance()->GetForBrowserContext( | |
| 53 context); | |
| 54 } | |
| 55 | |
| 56 ExtensionAppIconService::ExtensionAppIconService( | |
| 57 content::BrowserContext* context) | |
| 58 : context_(context), weak_ptr_factory_(this) { | |
| 59 #if defined(OS_CHROMEOS) | |
| 60 app_updater_ = | |
| 61 base::MakeUnique<Updater>(weak_ptr_factory_.GetWeakPtr(), context); | |
| 62 #endif | |
| 63 } | |
| 64 | |
| 65 ExtensionAppIconService::~ExtensionAppIconService() = default; | |
| 66 | |
| 67 void ExtensionAppIconService::Shutdown() { | |
| 68 #if defined(OS_CHROMEOS) | |
| 69 app_updater_.reset(); | |
| 70 #endif | |
| 71 } | |
| 72 | |
| 73 std::unique_ptr<ExtensionAppIcon> ExtensionAppIconService::CreateIcon( | |
| 74 ExtensionAppIconDelegate* delegate, | |
| 75 const std::string& app_id, | |
| 76 int resource_size_in_dip) { | |
| 77 std::unique_ptr<ExtensionAppIcon> icon = base::MakeUnique<ExtensionAppIcon>( | |
| 78 weak_ptr_factory_.GetWeakPtr(), delegate, context_, app_id, | |
| 79 resource_size_in_dip); | |
| 80 | |
| 81 icon_map_.insert(std::make_pair(icon->app_id(), icon.get())); | |
| 82 return icon; | |
| 83 } | |
| 84 | |
| 85 void ExtensionAppIconService::OnAppUpdated(const std::string& app_id) { | |
| 86 auto icon_range = icon_map_.equal_range(app_id); | |
| 87 for (auto icon = icon_range.first; icon != icon_range.second; ++icon) | |
| 88 icon->second->UpdateIcon(); | |
| 89 } | |
| 90 | |
| 91 void ExtensionAppIconService::OnIconDestroyed(ExtensionAppIcon* icon) { | |
| 92 DCHECK(icon); | |
| 93 auto icon_range = icon_map_.equal_range(icon->app_id()); | |
| 94 for (auto scan_icon = icon_range.first; scan_icon != icon_range.second; | |
| 95 ++scan_icon) { | |
| 96 if (scan_icon->second == icon) { | |
| 97 icon_map_.erase(scan_icon); | |
| 98 return; | |
| 99 } | |
| 100 } | |
| 101 NOTREACHED(); | |
| 102 } | |
| 103 | |
| 104 } // namespace extensions | |
| OLD | NEW |