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

Side by Side Diff: chrome/browser/extensions/api/settings_overrides/settings_overrides_api.cc

Issue 244473002: Remove NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED from c/b/e/api (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: feedback Created 6 years, 8 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/extensions/api/settings_overrides/settings_overrides_ap i.h" 5 #include "chrome/browser/extensions/api/settings_overrides/settings_overrides_ap i.h"
6 6
7 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/api/preference/preference_api.h" 9 #include "chrome/browser/extensions/api/preference/preference_api.h"
11 #include "chrome/browser/prefs/session_startup_pref.h" 10 #include "chrome/browser/prefs/session_startup_pref.h"
12 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/search_engines/template_url.h" 12 #include "chrome/browser/search_engines/template_url.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h" 13 #include "chrome/browser/search_engines/template_url_service_factory.h"
15 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler. h" 14 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler. h"
16 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
17 #include "content/public/browser/notification_details.h"
18 #include "content/public/browser/notification_source.h"
19 #include "extensions/browser/extension_prefs.h" 16 #include "extensions/browser/extension_prefs.h"
20 #include "extensions/browser/extension_prefs_factory.h" 17 #include "extensions/browser/extension_prefs_factory.h"
18 #include "extensions/browser/extension_registry.h"
21 #include "extensions/common/error_utils.h" 19 #include "extensions/common/error_utils.h"
22 #include "extensions/common/manifest_constants.h" 20 #include "extensions/common/manifest_constants.h"
23 21
24 namespace extensions { 22 namespace extensions {
25 23
26 namespace { 24 namespace {
27 25
28 base::LazyInstance<BrowserContextKeyedAPIFactory<SettingsOverridesAPI> > 26 base::LazyInstance<BrowserContextKeyedAPIFactory<SettingsOverridesAPI> >
29 g_factory = LAZY_INSTANCE_INITIALIZER; 27 g_factory = LAZY_INSTANCE_INITIALIZER;
30 28
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 search_provider.alternate_urls->at(i), install_parameter)); 80 search_provider.alternate_urls->at(i), install_parameter));
83 } 81 }
84 } 82 }
85 return data; 83 return data;
86 } 84 }
87 85
88 } // namespace 86 } // namespace
89 87
90 SettingsOverridesAPI::SettingsOverridesAPI(content::BrowserContext* context) 88 SettingsOverridesAPI::SettingsOverridesAPI(content::BrowserContext* context)
91 : profile_(Profile::FromBrowserContext(context)), 89 : profile_(Profile::FromBrowserContext(context)),
92 url_service_(TemplateURLServiceFactory::GetForProfile(profile_)) { 90 url_service_(TemplateURLServiceFactory::GetForProfile(profile_)),
93 DCHECK(profile_); 91 extension_registry_observer_(this) {
94 registrar_.Add(this, 92 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
95 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
96 content::Source<Profile>(profile_));
97 registrar_.Add(this,
98 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
99 content::Source<Profile>(profile_));
100 } 93 }
101 94
102 SettingsOverridesAPI::~SettingsOverridesAPI() { 95 SettingsOverridesAPI::~SettingsOverridesAPI() {
103 } 96 }
104 97
105 BrowserContextKeyedAPIFactory<SettingsOverridesAPI>* 98 BrowserContextKeyedAPIFactory<SettingsOverridesAPI>*
106 SettingsOverridesAPI::GetFactoryInstance() { 99 SettingsOverridesAPI::GetFactoryInstance() {
107 return g_factory.Pointer(); 100 return g_factory.Pointer();
108 } 101 }
109 102
(...skipping 13 matching lines...) Expand all
123 const std::string& pref_key) { 116 const std::string& pref_key) {
124 PreferenceAPI* prefs = PreferenceAPI::Get(profile_); 117 PreferenceAPI* prefs = PreferenceAPI::Get(profile_);
125 if (!prefs) 118 if (!prefs)
126 return; // Expected in unit tests. 119 return; // Expected in unit tests.
127 prefs->RemoveExtensionControlledPref( 120 prefs->RemoveExtensionControlledPref(
128 extension_id, 121 extension_id,
129 pref_key, 122 pref_key,
130 kExtensionPrefsScopeRegular); 123 kExtensionPrefsScopeRegular);
131 } 124 }
132 125
133 void SettingsOverridesAPI::Observe( 126 void SettingsOverridesAPI::OnExtensionLoaded(
134 int type, 127 content::BrowserContext* browser_context,
135 const content::NotificationSource& source, 128 const Extension* extension) {
136 const content::NotificationDetails& details) { 129 const SettingsOverrides* settings = SettingsOverrides::Get(extension);
137 switch (type) { 130 if (settings) {
138 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { 131 std::string install_parameter =
139 const Extension* extension = 132 ExtensionPrefs::Get(profile_)->GetInstallParam(extension->id());
140 content::Details<const Extension>(details).ptr(); 133 if (settings->homepage) {
141 const SettingsOverrides* settings = 134 SetPref(extension->id(),
142 SettingsOverrides::Get(extension); 135 prefs::kHomePage,
143 if (settings) { 136 new base::StringValue(SubstituteInstallParam(
144 std::string install_parameter = 137 settings->homepage->spec(), install_parameter)));
145 ExtensionPrefs::Get(profile_)->GetInstallParam(extension->id()); 138 SetPref(extension->id(),
146 if (settings->homepage) { 139 prefs::kHomePageIsNewTabPage,
147 SetPref(extension->id(), 140 new base::FundamentalValue(false));
148 prefs::kHomePage, 141 }
149 new base::StringValue(SubstituteInstallParam( 142 if (!settings->startup_pages.empty()) {
150 settings->homepage->spec(), install_parameter))); 143 SetPref(extension->id(),
151 SetPref(extension->id(), prefs::kHomePageIsNewTabPage, 144 prefs::kRestoreOnStartup,
152 new base::FundamentalValue(false)); 145 new base::FundamentalValue(SessionStartupPref::kPrefValueURLs));
146 if (settings->startup_pages.size() > 1) {
147 VLOG(1) << extensions::ErrorUtils::FormatErrorMessage(
148 kManyStartupPagesWarning,
149 manifest_keys::kSettingsOverride);
150 }
151 scoped_ptr<base::ListValue> url_list(new base::ListValue);
152 url_list->Append(new base::StringValue(SubstituteInstallParam(
153 settings->startup_pages[0].spec(), install_parameter)));
154 SetPref(
155 extension->id(), prefs::kURLsToRestoreOnStartup, url_list.release());
156 }
157 if (settings->search_engine) {
158 // Bring the preference to the correct state. Before this code set it
159 // to "true" for all search engines. Thus, we should overwrite it for
160 // all search engines.
161 if (settings->search_engine->is_default) {
162 SetPref(extension->id(),
163 prefs::kDefaultSearchProviderEnabled,
164 new base::FundamentalValue(true));
165 } else {
166 UnsetPref(extension->id(), prefs::kDefaultSearchProviderEnabled);
167 }
168 DCHECK(url_service_);
169 if (url_service_->loaded()) {
170 RegisterSearchProvider(extension);
171 } else {
172 if (!template_url_sub_) {
173 template_url_sub_ = url_service_->RegisterOnLoadedCallback(
174 base::Bind(&SettingsOverridesAPI::OnTemplateURLsLoaded,
175 base::Unretained(this)));
153 } 176 }
154 if (!settings->startup_pages.empty()) { 177 url_service_->Load();
155 SetPref(extension->id(), prefs::kRestoreOnStartup, 178 pending_extensions_.insert(extension);
156 new base::FundamentalValue(
157 SessionStartupPref::kPrefValueURLs));
158 if (settings->startup_pages.size() > 1) {
159 VLOG(1) << extensions::ErrorUtils::FormatErrorMessage(
160 kManyStartupPagesWarning, manifest_keys::kSettingsOverride);
161 }
162 scoped_ptr<base::ListValue> url_list(new base::ListValue);
163 url_list->Append(new base::StringValue(SubstituteInstallParam(
164 settings->startup_pages[0].spec(), install_parameter)));
165 SetPref(extension->id(), prefs::kURLsToRestoreOnStartup,
166 url_list.release());
167 }
168 if (settings->search_engine) {
169 // Bring the preference to the correct state. Before this code set it
170 // to "true" for all search engines. Thus, we should overwrite it for
171 // all search engines.
172 if (settings->search_engine->is_default) {
173 SetPref(extension->id(), prefs::kDefaultSearchProviderEnabled,
174 new base::FundamentalValue(true));
175 } else {
176 UnsetPref(extension->id(), prefs::kDefaultSearchProviderEnabled);
177 }
178 DCHECK(url_service_);
179 if (url_service_->loaded()) {
180 RegisterSearchProvider(extension);
181 } else {
182 if (!template_url_sub_) {
183 template_url_sub_ = url_service_->RegisterOnLoadedCallback(
184 base::Bind(&SettingsOverridesAPI::OnTemplateURLsLoaded,
185 base::Unretained(this)));
186 }
187 url_service_->Load();
188 pending_extensions_.insert(extension);
189 }
190 }
191 } 179 }
192 break;
193 }
194 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
195 const Extension* extension =
196 content::Details<UnloadedExtensionInfo>(details)->extension;
197 const SettingsOverrides* settings = SettingsOverrides::Get(extension);
198 if (settings) {
199 if (settings->homepage) {
200 UnsetPref(extension->id(), prefs::kHomePage);
201 UnsetPref(extension->id(), prefs::kHomePageIsNewTabPage);
202 }
203 if (!settings->startup_pages.empty()) {
204 UnsetPref(extension->id(), prefs::kRestoreOnStartup);
205 UnsetPref(extension->id(), prefs::kURLsToRestoreOnStartup);
206 }
207 if (settings->search_engine) {
208 DCHECK(url_service_);
209 if (url_service_->loaded())
210 url_service_->RemoveExtensionControlledTURL(extension->id());
211 else
212 pending_extensions_.erase(extension);
213 }
214 }
215 break;
216 }
217 default: {
218 NOTREACHED();
219 break;
220 } 180 }
221 } 181 }
222 } 182 }
183 void SettingsOverridesAPI::OnExtensionUnloaded(
184 content::BrowserContext* browser_context,
185 const Extension* extension,
186 UnloadedExtensionInfo::Reason reason) {
187 const SettingsOverrides* settings = SettingsOverrides::Get(extension);
188 if (settings) {
189 if (settings->homepage) {
190 UnsetPref(extension->id(), prefs::kHomePage);
191 UnsetPref(extension->id(), prefs::kHomePageIsNewTabPage);
192 }
193 if (!settings->startup_pages.empty()) {
194 UnsetPref(extension->id(), prefs::kRestoreOnStartup);
195 UnsetPref(extension->id(), prefs::kURLsToRestoreOnStartup);
196 }
197 if (settings->search_engine) {
198 DCHECK(url_service_);
199 if (url_service_->loaded())
200 url_service_->RemoveExtensionControlledTURL(extension->id());
201 else
202 pending_extensions_.erase(extension);
203 }
204 }
205 }
223 206
224 void SettingsOverridesAPI::Shutdown() { 207 void SettingsOverridesAPI::Shutdown() {
225 template_url_sub_.reset(); 208 template_url_sub_.reset();
226 } 209 }
227 210
228 void SettingsOverridesAPI::OnTemplateURLsLoaded() { 211 void SettingsOverridesAPI::OnTemplateURLsLoaded() {
229 // Register search providers for pending extensions. 212 // Register search providers for pending extensions.
230 template_url_sub_.reset(); 213 template_url_sub_.reset();
231 for (PendingExtensions::const_iterator i(pending_extensions_.begin()); 214 for (PendingExtensions::const_iterator i(pending_extensions_.begin());
232 i != pending_extensions_.end(); ++i) { 215 i != pending_extensions_.end(); ++i) {
(...skipping 24 matching lines...) Expand all
257 240
258 template <> 241 template <>
259 void BrowserContextKeyedAPIFactory< 242 void BrowserContextKeyedAPIFactory<
260 SettingsOverridesAPI>::DeclareFactoryDependencies() { 243 SettingsOverridesAPI>::DeclareFactoryDependencies() {
261 DependsOn(ExtensionPrefsFactory::GetInstance()); 244 DependsOn(ExtensionPrefsFactory::GetInstance());
262 DependsOn(PreferenceAPI::GetFactoryInstance()); 245 DependsOn(PreferenceAPI::GetFactoryInstance());
263 DependsOn(TemplateURLServiceFactory::GetInstance()); 246 DependsOn(TemplateURLServiceFactory::GetInstance());
264 } 247 }
265 248
266 } // namespace extensions 249 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698