| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" | 5 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/media/router/media_router_feature.h" | 10 #include "chrome/browser/media/router/media_router_feature.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" | 13 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" |
| 14 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" |
| 14 #include "extensions/browser/extension_registry.h" | 15 #include "extensions/browser/extension_registry.h" |
| 15 #include "extensions/common/feature_switch.h" | 16 #include "extensions/common/feature_switch.h" |
| 16 | 17 |
| 17 #if defined(ENABLE_MEDIA_ROUTER) | 18 #if defined(ENABLE_MEDIA_ROUTER) |
| 18 #include "chrome/browser/ui/toolbar/media_router_action.h" | 19 #include "chrome/browser/ui/toolbar/media_router_action.h" |
| 19 #include "chrome/browser/ui/toolbar/media_router_action_controller.h" | 20 #include "chrome/browser/ui/toolbar/media_router_action_controller.h" |
| 20 #endif | 21 #endif |
| 21 | 22 |
| 22 namespace { | |
| 23 | |
| 24 ComponentToolbarActionsFactory* testing_factory_ = nullptr; | |
| 25 | |
| 26 base::LazyInstance<ComponentToolbarActionsFactory> lazy_factory = | |
| 27 LAZY_INSTANCE_INITIALIZER; | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 // static | 23 // static |
| 32 const char ComponentToolbarActionsFactory::kCastBetaExtensionId[] = | 24 const char ComponentToolbarActionsFactory::kCastBetaExtensionId[] = |
| 33 "dliochdbjfkdbacpmhlcpmleaejidimm"; | 25 "dliochdbjfkdbacpmhlcpmleaejidimm"; |
| 34 const char ComponentToolbarActionsFactory::kCastExtensionId[] = | 26 const char ComponentToolbarActionsFactory::kCastExtensionId[] = |
| 35 "boadgeojelhgndaghljhdicfkmllpafd"; | 27 "boadgeojelhgndaghljhdicfkmllpafd"; |
| 36 const char ComponentToolbarActionsFactory::kMediaRouterActionId[] = | 28 const char ComponentToolbarActionsFactory::kMediaRouterActionId[] = |
| 37 "media_router_action"; | 29 "media_router_action"; |
| 38 | 30 |
| 39 ComponentToolbarActionsFactory::ComponentToolbarActionsFactory() {} | 31 ComponentToolbarActionsFactory::ComponentToolbarActionsFactory(Profile* profile) |
| 32 : profile_(profile) { |
| 33 #if defined(ENABLE_MEDIA_ROUTER) |
| 34 if (media_router::MediaRouterEnabled(profile_) && |
| 35 MediaRouterActionController::IsActionShownByPolicy(profile_)) { |
| 36 initial_ids_.insert(kMediaRouterActionId); |
| 37 } |
| 38 #endif |
| 39 } |
| 40 |
| 40 ComponentToolbarActionsFactory::~ComponentToolbarActionsFactory() {} | 41 ComponentToolbarActionsFactory::~ComponentToolbarActionsFactory() {} |
| 41 | 42 |
| 42 // static | 43 std::set<std::string> ComponentToolbarActionsFactory::GetInitialComponentIds() { |
| 43 ComponentToolbarActionsFactory* ComponentToolbarActionsFactory::GetInstance() { | 44 return initial_ids_; |
| 44 return testing_factory_ ? testing_factory_ : &lazy_factory.Get(); | |
| 45 } | 45 } |
| 46 | 46 |
| 47 std::set<std::string> ComponentToolbarActionsFactory::GetInitialComponentIds( | 47 void ComponentToolbarActionsFactory::OnAddComponentActionBeforeInit( |
| 48 Profile* profile) { | 48 const std::string& action_id) { |
| 49 std::set<std::string> component_ids; | 49 initial_ids_.insert(action_id); |
| 50 #if defined(ENABLE_MEDIA_ROUTER) | 50 } |
| 51 if (media_router::MediaRouterEnabled(profile) && | |
| 52 MediaRouterActionController::IsActionShownByPolicy(profile)) { | |
| 53 component_ids.insert(kMediaRouterActionId); | |
| 54 } | |
| 55 #endif | |
| 56 | 51 |
| 57 return component_ids; | 52 void ComponentToolbarActionsFactory::OnRemoveComponentActionBeforeInit( |
| 53 const std::string& action_id) { |
| 54 initial_ids_.erase(action_id); |
| 58 } | 55 } |
| 59 | 56 |
| 60 std::unique_ptr<ToolbarActionViewController> | 57 std::unique_ptr<ToolbarActionViewController> |
| 61 ComponentToolbarActionsFactory::GetComponentToolbarActionForId( | 58 ComponentToolbarActionsFactory::GetComponentToolbarActionForId( |
| 62 const std::string& id, | 59 const std::string& action_id, |
| 63 Browser* browser, | 60 Browser* browser, |
| 64 ToolbarActionsBar* bar) { | 61 ToolbarActionsBar* bar) { |
| 65 // This is currently behind the extension-action-redesign flag, as it is | 62 // This is currently behind the extension-action-redesign flag, as it is |
| 66 // designed for the new toolbar. | 63 // designed for the new toolbar. |
| 67 DCHECK(extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()); | 64 DCHECK(extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()); |
| 68 | 65 |
| 69 // Add component toolbar actions here. | 66 // Add component toolbar actions here. |
| 70 // This current design means that the ComponentToolbarActionsFactory is aware | 67 // This current design means that the ComponentToolbarActionsFactory is aware |
| 71 // of all actions. Since we should *not* have an excessive amount of these | 68 // of all actions. Since we should *not* have an excessive amount of these |
| 72 // (since each will have an action in the toolbar or overflow menu), this | 69 // (since each will have an action in the toolbar or overflow menu), this |
| 73 // should be okay. If this changes, we should rethink this design to have, | 70 // should be okay. If this changes, we should rethink this design to have, |
| 74 // e.g., RegisterChromeAction(). | 71 // e.g., RegisterChromeAction(). |
| 75 #if defined(ENABLE_MEDIA_ROUTER) | 72 #if defined(ENABLE_MEDIA_ROUTER) |
| 76 if (id == kMediaRouterActionId) | 73 if (action_id == kMediaRouterActionId) |
| 77 return std::unique_ptr<ToolbarActionViewController>( | 74 return std::unique_ptr<ToolbarActionViewController>( |
| 78 new MediaRouterAction(browser, bar)); | 75 new MediaRouterAction(browser, bar)); |
| 79 #endif // defined(ENABLE_MEDIA_ROUTER) | 76 #endif // defined(ENABLE_MEDIA_ROUTER) |
| 80 | 77 |
| 81 NOTREACHED(); | 78 NOTREACHED(); |
| 82 return std::unique_ptr<ToolbarActionViewController>(); | 79 return std::unique_ptr<ToolbarActionViewController>(); |
| 83 } | 80 } |
| 84 | 81 |
| 85 void ComponentToolbarActionsFactory::UnloadMigratedExtensions( | 82 void ComponentToolbarActionsFactory::UnloadMigratedExtensions( |
| 86 ExtensionService* service, | 83 ExtensionService* service, |
| 87 extensions::ExtensionRegistry* registry) { | 84 extensions::ExtensionRegistry* registry) { |
| 88 // TODO(takumif): Replace the unloading of Cast and Cast Beta extensions with | 85 // TODO(takumif): Replace the unloading of Cast and Cast Beta extensions with |
| 89 // uninstallation. | 86 // uninstallation. |
| 90 UnloadExtension(service, registry, kCastExtensionId); | 87 UnloadExtension(service, registry, kCastExtensionId); |
| 91 UnloadExtension(service, registry, kCastBetaExtensionId); | 88 UnloadExtension(service, registry, kCastBetaExtensionId); |
| 92 } | 89 } |
| 93 | 90 |
| 94 // static | |
| 95 void ComponentToolbarActionsFactory::SetTestingFactory( | |
| 96 ComponentToolbarActionsFactory* factory) { | |
| 97 testing_factory_ = factory; | |
| 98 } | |
| 99 | |
| 100 void ComponentToolbarActionsFactory::UnloadExtension( | 91 void ComponentToolbarActionsFactory::UnloadExtension( |
| 101 ExtensionService* service, | 92 ExtensionService* service, |
| 102 extensions::ExtensionRegistry* registry, | 93 extensions::ExtensionRegistry* registry, |
| 103 const std::string& extension_id) { | 94 const std::string& extension_id) { |
| 104 if (registry->enabled_extensions().Contains(extension_id)) { | 95 if (registry->enabled_extensions().Contains(extension_id)) { |
| 105 service->UnloadExtension( | 96 service->UnloadExtension( |
| 106 extension_id, | 97 extension_id, |
| 107 extensions::UnloadedExtensionInfo::REASON_MIGRATED_TO_COMPONENT); | 98 extensions::UnloadedExtensionInfo::REASON_MIGRATED_TO_COMPONENT); |
| 108 } | 99 } |
| 109 } | 100 } |
| OLD | NEW |