Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: chrome/browser/extensions/component_migration_helper.cc

Issue 2678083005: Remove extension-to-component migration mechanism (Closed)
Patch Set: Address Derek's comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "chrome/browser/extensions/component_migration_helper.h"
6
7 #include "base/stl_util.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_system_impl.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/pref_names.h"
13 #include "components/prefs/pref_registry_simple.h"
14 #include "components/prefs/pref_service.h"
15 #include "components/prefs/scoped_user_pref_update.h"
16 #include "content/public/browser/browser_context.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/common/feature_switch.h"
19
20 namespace extensions {
21
22 ComponentMigrationHelper::ComponentMigrationHelper(
23 Profile* profile,
24 ComponentActionDelegate* delegate)
25 : delegate_(delegate),
26 extension_registry_(ExtensionRegistry::Get(profile)),
27 pref_service_(profile->GetPrefs()),
28 extension_system_(ExtensionSystem::Get(profile)),
29 extension_registry_observer_(this) {
30 DCHECK(delegate_);
31 extension_registry_observer_.Add(extension_registry_);
32 }
33
34 ComponentMigrationHelper::~ComponentMigrationHelper() {}
35
36 // static
37 void ComponentMigrationHelper::RegisterPrefs(PrefRegistrySimple* registry) {
38 registry->RegisterDictionaryPref(
39 ::prefs::kToolbarMigratedComponentActionStatus);
40 }
41
42 void ComponentMigrationHelper::Register(const std::string& component_action_id,
43 const ExtensionId& extension_id) {
44 DCHECK(GetActionIdForExtensionId(extension_id).empty());
45 migrated_actions_.push_back(
46 std::make_pair(component_action_id, extension_id));
47 }
48
49 void ComponentMigrationHelper::Unregister(
50 const std::string& component_action_id,
51 const ExtensionId& extension_id) {
52 for (auto it = migrated_actions_.begin(); it != migrated_actions_.end();
53 it++) {
54 if (it->first == component_action_id && it->second == extension_id) {
55 migrated_actions_.erase(it);
56 return;
57 }
58 }
59 }
60
61 void ComponentMigrationHelper::OnFeatureEnabled(
62 const std::string& component_action_id) {
63 DCHECK(FeatureSwitch::extension_action_redesign()->IsEnabled());
64 std::vector<ExtensionId> extension_ids =
65 GetExtensionIdsForActionId(component_action_id);
66 DCHECK(!extension_ids.empty());
67
68 enabled_actions_.insert(component_action_id);
69 // Unload any extensions that we are migrating from.
70 bool extension_was_installed = false;
71 for (const auto& id : extension_ids) {
72 if (IsExtensionInstalledAndEnabled(id)) {
73 extension_was_installed = true;
74 UnloadExtension(id);
75 }
76 }
77
78 // Read the pref to determine component action status. If not set, set to
79 // true if we unloaded an extension.
80 const base::DictionaryValue* migration_pref = pref_service_->GetDictionary(
81 ::prefs::kToolbarMigratedComponentActionStatus);
82 bool component_action_pref = false;
83 if (migration_pref->HasKey(component_action_id)) {
84 component_action_pref = GetComponentActionPref(component_action_id);
85 } else if (extension_was_installed) {
86 SetComponentActionPref(component_action_id, true);
87 component_action_pref = true;
88 }
89
90 if (component_action_pref &&
91 !delegate_->HasComponentAction(component_action_id)) {
92 delegate_->AddComponentAction(component_action_id);
93 }
94 }
95
96 void ComponentMigrationHelper::OnFeatureDisabled(
97 const std::string& component_action_id) {
98 std::vector<ExtensionId> extension_ids =
99 GetExtensionIdsForActionId(component_action_id);
100 DCHECK(!extension_ids.empty());
101
102 enabled_actions_.erase(component_action_id);
103 RemoveComponentActionPref(component_action_id);
104
105 if (FeatureSwitch::extension_action_redesign()->IsEnabled() &&
106 delegate_->HasComponentAction(component_action_id))
107 delegate_->RemoveComponentAction(component_action_id);
108 }
109
110 void ComponentMigrationHelper::OnActionRemoved(
111 const std::string& component_action_id) {
112 // Record preference for the future.
113 SetComponentActionPref(component_action_id, false);
114
115 // Remove the action.
116 if (delegate_->HasComponentAction(component_action_id))
117 delegate_->RemoveComponentAction(component_action_id);
118 }
119
120 void ComponentMigrationHelper::OnExtensionReady(
121 content::BrowserContext* browser_context,
122 const Extension* extension) {
123 const ExtensionId& extension_id = extension->id();
124 const std::string& component_action_id =
125 GetActionIdForExtensionId(extension_id);
126 if (component_action_id.empty())
127 return;
128 if (base::ContainsKey(enabled_actions_, component_action_id)) {
129 UnloadExtension(extension_id);
130 SetComponentActionPref(component_action_id, true);
131
132 if (!delegate_->HasComponentAction(component_action_id))
133 delegate_->AddComponentAction(component_action_id);
134 }
135 }
136
137 bool ComponentMigrationHelper::GetComponentActionPref(
138 const std::string& component_action_id) const {
139 const base::DictionaryValue* migration_pref = pref_service_->GetDictionary(
140 ::prefs::kToolbarMigratedComponentActionStatus);
141 bool component_action_pref = false;
142
143 // If the entry for |component_action_id| doesn't exist, GetBoolean() does not
144 // modify |component_action_pref|.
145 migration_pref->GetBoolean(component_action_id, &component_action_pref);
146 return component_action_pref;
147 }
148
149 void ComponentMigrationHelper::SetComponentActionPref(
150 const std::string& component_action_id,
151 bool enabled) {
152 DictionaryPrefUpdate update(pref_service_,
153 ::prefs::kToolbarMigratedComponentActionStatus);
154 update->SetBoolean(component_action_id, enabled);
155 }
156
157 bool ComponentMigrationHelper::IsExtensionInstalledAndEnabled(
158 const ExtensionId& extension_id) const {
159 return extension_registry_->enabled_extensions().Contains(extension_id) ||
160 extension_registry_->terminated_extensions().Contains(extension_id);
161 }
162
163 void ComponentMigrationHelper::UnloadExtension(
164 const ExtensionId& extension_id) {
165 extension_system_->extension_service()->UnloadExtension(
166 extension_id, UnloadedExtensionInfo::REASON_MIGRATED_TO_COMPONENT);
167 }
168
169 void ComponentMigrationHelper::RemoveComponentActionPref(
170 const std::string& component_action_id) {
171 DictionaryPrefUpdate update(pref_service_,
172 ::prefs::kToolbarMigratedComponentActionStatus);
173 update->Remove(component_action_id, nullptr);
174 }
175
176 std::vector<std::string> ComponentMigrationHelper::GetExtensionIdsForActionId(
177 const std::string& component_action_id) const {
178 std::vector<ExtensionId> extension_ids;
179 for (const auto& i : migrated_actions_) {
180 if (i.first == component_action_id)
181 extension_ids.push_back(i.second);
182 }
183 return extension_ids;
184 }
185
186 std::string ComponentMigrationHelper::GetActionIdForExtensionId(
187 const ExtensionId& extension_id) const {
188 for (const auto& i : migrated_actions_) {
189 if (i.second == extension_id)
190 return i.first;
191 }
192 return "";
193 }
194
195 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698