1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
// Copyright 2017 The Chromium Authors
// 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/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/chrome_app_icon.h"
#include "chrome/browser/extensions/chrome_app_icon_service_factory.h"
namespace extensions {
// static
ChromeAppIconService* ChromeAppIconService::Get(
content::BrowserContext* context) {
return ChromeAppIconServiceFactory::GetInstance()->GetForBrowserContext(
context);
}
ChromeAppIconService::ChromeAppIconService(content::BrowserContext* context)
: context_(context) {
#if BUILDFLAG(IS_CHROMEOS)
app_updater_ = std::make_unique<ShelfExtensionAppUpdater>(
this, context, false /* extensions_only */);
#endif
observation_.Observe(ExtensionRegistry::Get(context_));
}
ChromeAppIconService::~ChromeAppIconService() = default;
void ChromeAppIconService::Shutdown() {
#if BUILDFLAG(IS_CHROMEOS)
app_updater_.reset();
#endif
}
std::unique_ptr<ChromeAppIcon> ChromeAppIconService::CreateIcon(
ChromeAppIconDelegate* delegate,
const std::string& app_id,
int resource_size_in_dip,
const ResizeFunction& resize_function) {
std::unique_ptr<ChromeAppIcon> icon = std::make_unique<ChromeAppIcon>(
delegate, context_,
base::BindOnce(&ChromeAppIconService::OnIconDestroyed,
weak_ptr_factory_.GetWeakPtr()),
app_id, resource_size_in_dip, resize_function);
icon_map_[icon->app_id()].insert(icon.get());
return icon;
}
std::unique_ptr<ChromeAppIcon> ChromeAppIconService::CreateIcon(
ChromeAppIconDelegate* delegate,
const std::string& app_id,
int resource_size_in_dip) {
return CreateIcon(delegate, app_id, resource_size_in_dip, ResizeFunction());
}
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());
}
#if BUILDFLAG(IS_CHROMEOS)
void ChromeAppIconService::OnAppUpdated(
content::BrowserContext* browser_context,
const std::string& app_id,
bool reload_icon) {
if (!reload_icon)
return;
OnAppUpdated(app_id);
}
#endif
void ChromeAppIconService::OnAppUpdated(const std::string& app_id) {
IconMap::const_iterator it = icon_map_.find(app_id);
if (it == icon_map_.end())
return;
// Set can be updated during the UpdateIcon call.
const std::set<raw_ptr<ChromeAppIcon, SetExperimental>> icons_to_update =
it->second;
for (ChromeAppIcon* icon : icons_to_update) {
if (it->second.count(icon))
icon->UpdateIcon();
}
}
void ChromeAppIconService::OnIconDestroyed(ChromeAppIcon* icon) {
DCHECK(icon);
auto it = icon_map_.find(icon->app_id());
CHECK(it != icon_map_.end());
it->second.erase(icon);
if (it->second.empty()) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&ChromeAppIconService::MaybeCleanupIconSet,
weak_ptr_factory_.GetWeakPtr(), icon->app_id()));
}
}
void ChromeAppIconService::MaybeCleanupIconSet(const std::string& app_id) {
auto it = icon_map_.find(app_id);
if (it != icon_map_.end() && it->second.empty())
icon_map_.erase(it);
}
} // namespace extensions
|