| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 #ifndef CHROME_BROWSER_EXTENSIONS_COMPONENT_MIGRATION_HELPER_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_COMPONENT_MIGRATION_HELPER_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 #include <utility> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/macros.h" | |
| 14 #include "base/scoped_observer.h" | |
| 15 #include "extensions/browser/extension_registry_observer.h" | |
| 16 | |
| 17 class Profile; | |
| 18 class PrefRegistrySimple; | |
| 19 class PrefService; | |
| 20 | |
| 21 namespace content { | |
| 22 class BrowserContext; | |
| 23 } | |
| 24 | |
| 25 namespace extensions { | |
| 26 | |
| 27 class ExtensionRegistry; | |
| 28 class ExtensionSystem; | |
| 29 | |
| 30 // For migrating existing extensions to component actions, and vice versa. A | |
| 31 // previous enabled extension is used as a signal to add the corresponding | |
| 32 // component action to the visible area of the toolbar. This allows users who | |
| 33 // have already installed the extension to have their preference for a component | |
| 34 // action in the visible area of the toolbar respected, without enabling this | |
| 35 // for the entire user base. | |
| 36 // | |
| 37 // MIGRATION LOGIC | |
| 38 // | |
| 39 // When the feature is enabled (i.e. by experiment or flag), the client should | |
| 40 // call OnFeatureEnabled(action_id). The extension action redesign MUST also be | |
| 41 // enabled. | |
| 42 // | |
| 43 // - If the extension is enabled, it is unloaded with a reason of | |
| 44 // MIGRATED_TO_COMPONENT, the component action shown, and a pref set | |
| 45 // recording the migration. | |
| 46 // - If pref is set the component action is shown. | |
| 47 // - Otherwise, the component action is not shown. | |
| 48 // | |
| 49 // When the feature is disabled (for example, by starting with a flag off), the | |
| 50 // client should call OnFeatureDisabled(action_id). | |
| 51 // | |
| 52 // - The pref is removed. | |
| 53 // - If the extension action redesign is enabled, the associated component | |
| 54 // action is removed. | |
| 55 // | |
| 56 // USAGE | |
| 57 // helper->Register("some-action-id", "some-extension-id"); | |
| 58 // helper->Register("some-action-id", "other-extension-id"); | |
| 59 // ... | |
| 60 // // When feature is enabled | |
| 61 // helper->OnFeatureEnabled("some-action-id"); | |
| 62 // ... | |
| 63 // // When feature is disabled | |
| 64 // helper->OnFeatureDisabled("some-action-id"); | |
| 65 // | |
| 66 // It is legal to register more than one extension per action but not vice | |
| 67 // versa. | |
| 68 class ComponentMigrationHelper : public ExtensionRegistryObserver { | |
| 69 public: | |
| 70 // Object that knows how to manage component actions in the toolbar model. | |
| 71 class ComponentActionDelegate { | |
| 72 public: | |
| 73 // Adds or removes the component action labeled by |action_id| from the | |
| 74 // toolbar model. The caller will not add the same action twice. | |
| 75 virtual void AddComponentAction(const std::string& action_id) = 0; | |
| 76 virtual void RemoveComponentAction(const std::string& action_id) = 0; | |
| 77 | |
| 78 // Returns |true| if the toolbar model has an action for |action_id|. | |
| 79 virtual bool HasComponentAction(const std::string& action_id) const = 0; | |
| 80 }; | |
| 81 | |
| 82 ComponentMigrationHelper(Profile* profile, ComponentActionDelegate* delegate); | |
| 83 ~ComponentMigrationHelper() override; | |
| 84 | |
| 85 static void RegisterPrefs(PrefRegistrySimple* registry); | |
| 86 | |
| 87 // Registers and unregisters a component action/extension pair. A component | |
| 88 // action may have more than one associated extension id, but not vice versa. | |
| 89 void Register(const std::string& component_action_id, | |
| 90 const ExtensionId& extension_id); | |
| 91 void Unregister(const std::string& component_action_id, | |
| 92 const ExtensionId& extension_id); | |
| 93 | |
| 94 // Call when we should potentially add the component action and unload | |
| 95 // the extension. PREREQUISITE: The extension action redesign MUST be | |
| 96 // enabled. | |
| 97 void OnFeatureEnabled(const std::string& component_action_id); | |
| 98 | |
| 99 // Call when we should potentially remove the component action and re-enable | |
| 100 // extension loading. | |
| 101 void OnFeatureDisabled(const std::string& component_action_id); | |
| 102 | |
| 103 // Call when the user manually removes the component action from the toolbar. | |
| 104 void OnActionRemoved(const std::string& component_action_id); | |
| 105 | |
| 106 // extensions::ExtensionRegistryObserver: | |
| 107 void OnExtensionReady(content::BrowserContext* browser_context, | |
| 108 const Extension* extension) override; | |
| 109 | |
| 110 // Gets and sets the preference for whether to put the component action with | |
| 111 // the given ID on the toolbar (or in the overflow menu). | |
| 112 // GetComponentActionPref() returns false if the pref has not been set. | |
| 113 bool GetComponentActionPref(const std::string& component_action_id) const; | |
| 114 void SetComponentActionPref(const std::string& component_action_id, | |
| 115 bool enabled); | |
| 116 | |
| 117 protected: | |
| 118 // A set of component action ids whose features are currently enabled. | |
| 119 // Protected for unit testing. | |
| 120 std::set<std::string> enabled_actions_; | |
| 121 | |
| 122 private: | |
| 123 bool IsExtensionInstalledAndEnabled(const ExtensionId& extension_id) const; | |
| 124 void UnloadExtension(const ExtensionId& extension_id); | |
| 125 void RemoveComponentActionPref(const std::string& component_action_id); | |
| 126 std::vector<std::string> GetExtensionIdsForActionId( | |
| 127 const std::string& component_action_id) const; | |
| 128 std::string GetActionIdForExtensionId(const ExtensionId& extension_id) const; | |
| 129 | |
| 130 ComponentActionDelegate* const delegate_; | |
| 131 // The ExtensionRegistry, PrefService, and ExtensionSystem, cached for | |
| 132 // convenience. | |
| 133 ExtensionRegistry* const extension_registry_; | |
| 134 PrefService* const pref_service_; | |
| 135 ExtensionSystem* const extension_system_; | |
| 136 | |
| 137 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | |
| 138 extension_registry_observer_; | |
| 139 | |
| 140 // A list of pairs of component action ids and extension ids. | |
| 141 std::vector<std::pair<std::string, ExtensionId>> migrated_actions_; | |
| 142 | |
| 143 DISALLOW_COPY_AND_ASSIGN(ComponentMigrationHelper); | |
| 144 }; | |
| 145 | |
| 146 } // namespace extensions | |
| 147 | |
| 148 #endif // CHROME_BROWSER_EXTENSIONS_COMPONENT_MIGRATION_HELPER_H_ | |
| OLD | NEW |