[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
Devlin Cronin | 94832fc | 2020-08-04 18:39:41 | [diff] [blame] | 5 | #include "extensions/browser/extension_action_manager.h" |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 6 | |
Devlin Cronin | 91975c3 | 2019-05-01 21:52:48 | [diff] [blame] | 7 | #include "base/memory/singleton.h" |
[email protected] | b33f0b11 | 2014-03-13 17:05:30 | [diff] [blame] | 8 | #include "components/keyed_service/content/browser_context_dependency_manager.h" |
Devlin Cronin | 91975c3 | 2019-05-01 21:52:48 | [diff] [blame] | 9 | #include "components/keyed_service/content/browser_context_keyed_service_factory.h" |
Devlin Cronin | 94832fc | 2020-08-04 18:39:41 | [diff] [blame] | 10 | #include "content/public/browser/browser_context.h" |
Devlin Cronin | 5911e74 | 2020-04-28 15:56:34 | [diff] [blame] | 11 | #include "extensions/browser/extension_action.h" |
rdevlin.cronin | d09e1c4d5c | 2016-11-19 01:03:20 | [diff] [blame] | 12 | #include "extensions/browser/extension_icon_image.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 13 | #include "extensions/browser/extension_system.h" |
[email protected] | 175a74d | 2014-02-08 02:06:20 | [diff] [blame] | 14 | #include "extensions/browser/extensions_browser_client.h" |
[email protected] | 6a24a039 | 2014-08-12 21:31:33 | [diff] [blame] | 15 | #include "extensions/common/constants.h" |
rdevlin.cronin | d09e1c4d5c | 2016-11-19 01:03:20 | [diff] [blame] | 16 | #include "ui/gfx/image/image_skia.h" |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 17 | |
| 18 | namespace extensions { |
| 19 | |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 20 | namespace { |
| 21 | |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame] | 22 | // BrowserContextKeyedServiceFactory for ExtensionActionManager. |
| 23 | class ExtensionActionManagerFactory : public BrowserContextKeyedServiceFactory { |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 24 | public: |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame] | 25 | // BrowserContextKeyedServiceFactory implementation: |
[email protected] | 4193473 | 2014-08-22 21:38:18 | [diff] [blame] | 26 | static ExtensionActionManager* GetForBrowserContext( |
| 27 | content::BrowserContext* context) { |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 28 | return static_cast<ExtensionActionManager*>( |
[email protected] | 4193473 | 2014-08-22 21:38:18 | [diff] [blame] | 29 | GetInstance()->GetServiceForBrowserContext(context, true)); |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static ExtensionActionManagerFactory* GetInstance(); |
| 33 | |
| 34 | private: |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 35 | friend struct base::DefaultSingletonTraits<ExtensionActionManagerFactory>; |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 36 | |
| 37 | ExtensionActionManagerFactory() |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame] | 38 | : BrowserContextKeyedServiceFactory( |
Devlin Cronin | 94832fc | 2020-08-04 18:39:41 | [diff] [blame] | 39 | "ExtensionActionManager", |
| 40 | BrowserContextDependencyManager::GetInstance()) {} |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 41 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 42 | KeyedService* BuildServiceInstanceFor( |
Devlin Cronin | 94832fc | 2020-08-04 18:39:41 | [diff] [blame] | 43 | content::BrowserContext* browser_context) const override { |
| 44 | return new ExtensionActionManager(browser_context); |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 45 | } |
| 46 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 47 | content::BrowserContext* GetBrowserContextToUse( |
mostynb | a15bee1 | 2014-10-04 00:40:32 | [diff] [blame] | 48 | content::BrowserContext* context) const override { |
[email protected] | 175a74d | 2014-02-08 02:06:20 | [diff] [blame] | 49 | return ExtensionsBrowserClient::Get()->GetOriginalContext(context); |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 50 | } |
| 51 | }; |
| 52 | |
Devlin Cronin | 94832fc | 2020-08-04 18:39:41 | [diff] [blame] | 53 | ExtensionActionManagerFactory* ExtensionActionManagerFactory::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 54 | return base::Singleton<ExtensionActionManagerFactory>::get(); |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | } // namespace |
| 58 | |
Devlin Cronin | 94832fc | 2020-08-04 18:39:41 | [diff] [blame] | 59 | ExtensionActionManager::ExtensionActionManager( |
| 60 | content::BrowserContext* browser_context) |
| 61 | : browser_context_(browser_context) { |
| 62 | CHECK(!browser_context_->IsOffTheRecord()) |
| 63 | << "Don't instantiate this with an off-the-record context."; |
Sigurdur Asgeirsson | 834f057 | 2021-03-24 13:24:58 | [diff] [blame] | 64 | extension_registry_observation_.Observe( |
| 65 | ExtensionRegistry::Get(browser_context_)); |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | ExtensionActionManager::~ExtensionActionManager() { |
Devlin Cronin | 94832fc | 2020-08-04 18:39:41 | [diff] [blame] | 69 | // Don't assert that the ExtensionAction maps are empty because Extensions |
| 70 | // are sometimes (only in tests?) not unloaded before the associated context |
| 71 | // is destroyed. |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | 4193473 | 2014-08-22 21:38:18 | [diff] [blame] | 74 | ExtensionActionManager* ExtensionActionManager::Get( |
| 75 | content::BrowserContext* context) { |
| 76 | return ExtensionActionManagerFactory::GetForBrowserContext(context); |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | 01d3a96 | 2014-04-11 17:54:07 | [diff] [blame] | 79 | void ExtensionActionManager::OnExtensionUnloaded( |
[email protected] | e51232f3 | 2014-04-18 20:05:36 | [diff] [blame] | 80 | content::BrowserContext* browser_context, |
| 81 | const Extension* extension, |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 82 | UnloadedExtensionReason reason) { |
Devlin Cronin | c7002c2 | 2019-05-15 00:46:16 | [diff] [blame] | 83 | actions_.erase(extension->id()); |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 84 | } |
| 85 | |
Devlin Cronin | c7002c2 | 2019-05-15 00:46:16 | [diff] [blame] | 86 | ExtensionAction* ExtensionActionManager::GetExtensionAction( |
| 87 | const Extension& extension) const { |
| 88 | auto iter = actions_.find(extension.id()); |
| 89 | if (iter != actions_.end()) |
| 90 | return iter->second.get(); |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame] | 91 | |
Devlin Cronin | 4831bfdb | 2020-04-15 22:31:35 | [diff] [blame] | 92 | const ActionInfo* action_info = |
| 93 | ActionInfo::GetExtensionActionInfo(&extension); |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 94 | if (!action_info) |
limasdf | 8fc2d0b9 | 2016-11-06 04:54:25 | [diff] [blame] | 95 | return nullptr; |
[email protected] | 2a88b40b | 2013-07-31 17:33:51 | [diff] [blame] | 96 | |
| 97 | // Only create action info for enabled extensions. |
| 98 | // This avoids bugs where actions are recreated just after being removed |
[email protected] | 01d3a96 | 2014-04-11 17:54:07 | [diff] [blame] | 99 | // in response to OnExtensionUnloaded(). |
Devlin Cronin | 94832fc | 2020-08-04 18:39:41 | [diff] [blame] | 100 | if (!ExtensionRegistry::Get(browser_context_) |
| 101 | ->enabled_extensions() |
| 102 | .Contains(extension.id())) { |
limasdf | 8fc2d0b9 | 2016-11-06 04:54:25 | [diff] [blame] | 103 | return nullptr; |
[email protected] | 6a24a039 | 2014-08-12 21:31:33 | [diff] [blame] | 104 | } |
[email protected] | 2a88b40b | 2013-07-31 17:33:51 | [diff] [blame] | 105 | |
Devlin Cronin | 580d636 | 2019-05-07 20:38:45 | [diff] [blame] | 106 | auto action = std::make_unique<ExtensionAction>(extension, *action_info); |
rdevlin.cronin | d09e1c4d5c | 2016-11-19 01:03:20 | [diff] [blame] | 107 | |
| 108 | if (action->default_icon()) { |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 109 | action->SetDefaultIconImage(std::make_unique<IconImage>( |
Devlin Cronin | 94832fc | 2020-08-04 18:39:41 | [diff] [blame] | 110 | browser_context_, &extension, *action->default_icon(), |
rdevlin.cronin | d09e1c4d5c | 2016-11-19 01:03:20 | [diff] [blame] | 111 | ExtensionAction::ActionIconSize(), |
| 112 | ExtensionAction::FallbackIcon().AsImageSkia(), nullptr)); |
| 113 | } |
| 114 | |
limasdf | 8fc2d0b9 | 2016-11-06 04:54:25 | [diff] [blame] | 115 | ExtensionAction* raw_action = action.get(); |
Devlin Cronin | c7002c2 | 2019-05-15 00:46:16 | [diff] [blame] | 116 | actions_[extension.id()] = std::move(action); |
limasdf | 8fc2d0b9 | 2016-11-06 04:54:25 | [diff] [blame] | 117 | return raw_action; |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | ad12b6b | 2012-11-28 23:21:15 | [diff] [blame] | 120 | } // namespace extensions |