Commit 7b4bd934 authored by thestig's avatar thestig Committed by Commit bot

Cleanup: Remove some deprecated ExtensionService usage.

Review URL: https://codereview.chromium.org/543693003

Cr-Commit-Position: refs/heads/master@{#294023}
parent 8ef0627a
...@@ -262,6 +262,9 @@ class ExtensionService ...@@ -262,6 +262,9 @@ class ExtensionService
// Reloads the specified extension, sending the onLaunched() event to it if it // Reloads the specified extension, sending the onLaunched() event to it if it
// currently has any window showing. // currently has any window showing.
// Allows noisy failures. // Allows noisy failures.
// NOTE: Reloading an extension can invalidate |extension_id| and Extension
// pointers for the given extension. Consider making a copy of |extension_id|
// first and retrieving a new Extension pointer afterwards.
void ReloadExtension(const std::string& extension_id); void ReloadExtension(const std::string& extension_id);
// Suppresses noisy failures. // Suppresses noisy failures.
......
...@@ -34,6 +34,7 @@ namespace extensions { ...@@ -34,6 +34,7 @@ namespace extensions {
namespace util { namespace util {
namespace { namespace {
// The entry into the ExtensionPrefs for allowing an extension to script on // The entry into the ExtensionPrefs for allowing an extension to script on
// all urls without explicit permission. // all urls without explicit permission.
const char kExtensionAllowedOnAllUrlsPrefName[] = const char kExtensionAllowedOnAllUrlsPrefName[] =
...@@ -54,6 +55,27 @@ bool IsWhitelistedForIncognito(const std::string& extension_id) { ...@@ -54,6 +55,27 @@ bool IsWhitelistedForIncognito(const std::string& extension_id) {
kExtensionWhitelist, kExtensionWhitelist,
kExtensionWhitelist + arraysize(kExtensionWhitelist))); kExtensionWhitelist + arraysize(kExtensionWhitelist)));
} }
// Returns |extension_id|. See note below.
std::string ReloadExtensionIfEnabled(const std::string& extension_id,
content::BrowserContext* context) {
ExtensionRegistry* registry = ExtensionRegistry::Get(context);
bool extension_is_enabled =
registry->enabled_extensions().Contains(extension_id);
if (!extension_is_enabled)
return extension_id;
// When we reload the extension the ID may be invalidated if we've passed it
// by const ref everywhere. Make a copy to be safe. http://crbug.com/103762
std::string id = extension_id;
ExtensionService* service =
ExtensionSystem::Get(context)->extension_service();
CHECK(service);
service->ReloadExtension(id);
return id;
}
} // namespace } // namespace
bool IsIncognitoEnabled(const std::string& extension_id, bool IsIncognitoEnabled(const std::string& extension_id,
...@@ -79,10 +101,9 @@ bool IsIncognitoEnabled(const std::string& extension_id, ...@@ -79,10 +101,9 @@ bool IsIncognitoEnabled(const std::string& extension_id,
void SetIsIncognitoEnabled(const std::string& extension_id, void SetIsIncognitoEnabled(const std::string& extension_id,
content::BrowserContext* context, content::BrowserContext* context,
bool enabled) { bool enabled) {
ExtensionService* service = ExtensionRegistry* registry = ExtensionRegistry::Get(context);
ExtensionSystem::Get(context)->extension_service(); const Extension* extension =
CHECK(service); registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING);
const Extension* extension = service->GetInstalledExtension(extension_id);
if (extension) { if (extension) {
if (!extension->can_be_incognito_enabled()) if (!extension->can_be_incognito_enabled())
...@@ -95,12 +116,12 @@ void SetIsIncognitoEnabled(const std::string& extension_id, ...@@ -95,12 +116,12 @@ void SetIsIncognitoEnabled(const std::string& extension_id,
DCHECK(sync_helper::IsSyncable(extension)); DCHECK(sync_helper::IsSyncable(extension));
// If we are here, make sure the we aren't trying to change the value. // If we are here, make sure the we aren't trying to change the value.
DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id, service->profile())); DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id, context));
return; return;
} }
} }
ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(service->profile()); ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context);
// Broadcast unloaded and loaded events to update browser state. Only bother // Broadcast unloaded and loaded events to update browser state. Only bother
// if the value changed and the extension is actually enabled, since there is // if the value changed and the extension is actually enabled, since there is
// no UI otherwise. // no UI otherwise.
...@@ -110,19 +131,13 @@ void SetIsIncognitoEnabled(const std::string& extension_id, ...@@ -110,19 +131,13 @@ void SetIsIncognitoEnabled(const std::string& extension_id,
extension_prefs->SetIsIncognitoEnabled(extension_id, enabled); extension_prefs->SetIsIncognitoEnabled(extension_id, enabled);
bool extension_is_enabled = service->extensions()->Contains(extension_id); std::string id = ReloadExtensionIfEnabled(extension_id, context);
// When we reload the extension the ID may be invalidated if we've passed it
// by const ref everywhere. Make a copy to be safe.
std::string id = extension_id;
if (extension_is_enabled)
service->ReloadExtension(id);
// Reloading the extension invalidates the |extension| pointer. // Reloading the extension invalidates the |extension| pointer.
extension = service->GetInstalledExtension(id); extension = registry->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
if (extension) { if (extension) {
ExtensionSyncService::Get(service->profile())-> Profile* profile = Profile::FromBrowserContext(context);
SyncExtensionChangeIfNeeded(*extension); ExtensionSyncService::Get(profile)->SyncExtensionChangeIfNeeded(*extension);
} }
} }
...@@ -157,10 +172,6 @@ bool AllowFileAccess(const std::string& extension_id, ...@@ -157,10 +172,6 @@ bool AllowFileAccess(const std::string& extension_id,
void SetAllowFileAccess(const std::string& extension_id, void SetAllowFileAccess(const std::string& extension_id,
content::BrowserContext* context, content::BrowserContext* context,
bool allow) { bool allow) {
ExtensionService* service =
ExtensionSystem::Get(context)->extension_service();
CHECK(service);
// Reload to update browser state. Only bother if the value changed and the // Reload to update browser state. Only bother if the value changed and the
// extension is actually enabled, since there is no UI otherwise. // extension is actually enabled, since there is no UI otherwise.
if (allow == AllowFileAccess(extension_id, context)) if (allow == AllowFileAccess(extension_id, context))
...@@ -168,9 +179,7 @@ void SetAllowFileAccess(const std::string& extension_id, ...@@ -168,9 +179,7 @@ void SetAllowFileAccess(const std::string& extension_id,
ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow); ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow);
bool extension_is_enabled = service->extensions()->Contains(extension_id); ReloadExtensionIfEnabled(extension_id, context);
if (extension_is_enabled)
service->ReloadExtension(extension_id);
} }
bool AllowedScriptingOnAllUrls(const std::string& extension_id, bool AllowedScriptingOnAllUrls(const std::string& extension_id,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment