Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_util.cc
blob34914197c475ec6e3ae2df8e4eb652d0a0dbb332
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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/extension_util.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/values.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_sync_service.h"
12 #include "chrome/browser/extensions/permissions_updater.h"
13 #include "chrome/browser/extensions/shared_module_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
18 #include "chrome/common/extensions/sync_helper.h"
19 #include "content/public/browser/site_instance.h"
20 #include "extensions/browser/extension_prefs.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/browser/extension_system.h"
23 #include "extensions/browser/extension_util.h"
24 #include "extensions/common/extension.h"
25 #include "extensions/common/extension_icon_set.h"
26 #include "extensions/common/feature_switch.h"
27 #include "extensions/common/features/behavior_feature.h"
28 #include "extensions/common/features/feature_provider.h"
29 #include "extensions/common/manifest.h"
30 #include "extensions/common/manifest_handlers/incognito_info.h"
31 #include "extensions/common/permissions/permissions_data.h"
32 #include "extensions/grit/extensions_browser_resources.h"
33 #include "ui/base/resource/resource_bundle.h"
35 namespace extensions {
36 namespace util {
38 namespace {
40 // The entry into the ExtensionPrefs for allowing an extension to script on
41 // all urls without explicit permission.
42 const char kExtensionAllowedOnAllUrlsPrefName[] =
43 "extension_can_script_all_urls";
45 // The entry into the prefs for when a user has explicitly set the "extension
46 // allowed on all urls" pref.
47 const char kHasSetScriptOnAllUrlsPrefName[] = "has_set_script_all_urls";
49 // Returns true if |extension| should always be enabled in incognito mode.
50 bool IsWhitelistedForIncognito(const Extension* extension) {
51 return FeatureProvider::GetBehaviorFeature(
52 BehaviorFeature::kWhitelistedForIncognito)
53 ->IsAvailableToExtension(extension)
54 .is_available();
57 // Returns |extension_id|. See note below.
58 std::string ReloadExtensionIfEnabled(const std::string& extension_id,
59 content::BrowserContext* context) {
60 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
61 bool extension_is_enabled =
62 registry->enabled_extensions().Contains(extension_id);
64 if (!extension_is_enabled)
65 return extension_id;
67 // When we reload the extension the ID may be invalidated if we've passed it
68 // by const ref everywhere. Make a copy to be safe. http://crbug.com/103762
69 std::string id = extension_id;
70 ExtensionService* service =
71 ExtensionSystem::Get(context)->extension_service();
72 CHECK(service);
73 service->ReloadExtension(id);
74 return id;
77 // Sets the preference for scripting on all urls to |allowed|, optionally
78 // updating the extension's active permissions (based on |update_permissions|).
79 void SetAllowedScriptingOnAllUrlsHelper(
80 content::BrowserContext* context,
81 const std::string& extension_id,
82 bool allowed,
83 bool update_permissions) {
84 // TODO(devlin): Right now, we always need to have a value for this pref.
85 // Once the scripts-require-action feature launches, we can change the set
86 // to be null if false.
87 ExtensionPrefs::Get(context)->UpdateExtensionPref(
88 extension_id,
89 kExtensionAllowedOnAllUrlsPrefName,
90 new base::FundamentalValue(allowed));
92 if (update_permissions) {
93 const Extension* extension =
94 ExtensionRegistry::Get(context)->enabled_extensions().GetByID(
95 extension_id);
96 if (extension) {
97 PermissionsUpdater updater(context);
98 if (allowed)
99 updater.GrantWithheldImpliedAllHosts(extension);
100 else
101 updater.WithholdImpliedAllHosts(extension);
103 // If this was an update to permissions, we also need to sync the change.
104 ExtensionSyncService* sync_service = ExtensionSyncService::Get(context);
105 if (sync_service) // sync_service can be null in unittests.
106 sync_service->SyncExtensionChangeIfNeeded(*extension);
111 } // namespace
113 bool IsIncognitoEnabled(const std::string& extension_id,
114 content::BrowserContext* context) {
115 const Extension* extension = ExtensionRegistry::Get(context)->
116 GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
117 if (extension) {
118 if (!extension->can_be_incognito_enabled())
119 return false;
120 // If this is an existing component extension we always allow it to
121 // work in incognito mode.
122 if (extension->location() == Manifest::COMPONENT)
123 return true;
124 if (IsWhitelistedForIncognito(extension))
125 return true;
127 return ExtensionPrefs::Get(context)->IsIncognitoEnabled(extension_id);
130 void SetIsIncognitoEnabled(const std::string& extension_id,
131 content::BrowserContext* context,
132 bool enabled) {
133 ExtensionRegistry* registry = ExtensionRegistry::Get(context);
134 const Extension* extension =
135 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING);
137 if (extension) {
138 if (!extension->can_be_incognito_enabled())
139 return;
141 if (extension->location() == Manifest::COMPONENT) {
142 // This shouldn't be called for component extensions unless it is called
143 // by sync, for syncable component extensions.
144 // See http://crbug.com/112290 and associated CLs for the sordid history.
145 DCHECK(sync_helper::IsSyncable(extension));
147 // If we are here, make sure the we aren't trying to change the value.
148 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id, context));
149 return;
153 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context);
154 // Broadcast unloaded and loaded events to update browser state. Only bother
155 // if the value changed and the extension is actually enabled, since there is
156 // no UI otherwise.
157 bool old_enabled = extension_prefs->IsIncognitoEnabled(extension_id);
158 if (enabled == old_enabled)
159 return;
161 extension_prefs->SetIsIncognitoEnabled(extension_id, enabled);
163 std::string id = ReloadExtensionIfEnabled(extension_id, context);
165 // Reloading the extension invalidates the |extension| pointer.
166 extension = registry->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
167 if (extension) {
168 Profile* profile = Profile::FromBrowserContext(context);
169 ExtensionSyncService::Get(profile)->SyncExtensionChangeIfNeeded(*extension);
173 bool CanCrossIncognito(const Extension* extension,
174 content::BrowserContext* context) {
175 // We allow the extension to see events and data from another profile iff it
176 // uses "spanning" behavior and it has incognito access. "split" mode
177 // extensions only see events for a matching profile.
178 CHECK(extension);
179 return IsIncognitoEnabled(extension->id(), context) &&
180 !IncognitoInfo::IsSplitMode(extension);
183 bool CanLoadInIncognito(const Extension* extension,
184 content::BrowserContext* context) {
185 CHECK(extension);
186 if (extension->is_hosted_app())
187 return true;
188 // Packaged apps and regular extensions need to be enabled specifically for
189 // incognito (and split mode should be set).
190 return IncognitoInfo::IsSplitMode(extension) &&
191 IsIncognitoEnabled(extension->id(), context);
194 bool AllowFileAccess(const std::string& extension_id,
195 content::BrowserContext* context) {
196 return base::CommandLine::ForCurrentProcess()->HasSwitch(
197 switches::kDisableExtensionsFileAccessCheck) ||
198 ExtensionPrefs::Get(context)->AllowFileAccess(extension_id);
201 void SetAllowFileAccess(const std::string& extension_id,
202 content::BrowserContext* context,
203 bool allow) {
204 // Reload to update browser state. Only bother if the value changed and the
205 // extension is actually enabled, since there is no UI otherwise.
206 if (allow == AllowFileAccess(extension_id, context))
207 return;
209 ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow);
211 ReloadExtensionIfEnabled(extension_id, context);
214 bool AllowedScriptingOnAllUrls(const std::string& extension_id,
215 content::BrowserContext* context) {
216 bool allowed = false;
217 ExtensionPrefs* prefs = ExtensionPrefs::Get(context);
218 if (!prefs->ReadPrefAsBoolean(extension_id,
219 kExtensionAllowedOnAllUrlsPrefName,
220 &allowed)) {
221 // If there is no value present, we make one, defaulting it to the value of
222 // the 'scripts require action' flag. If the flag is on, then the extension
223 // does not have permission to script on all urls by default.
224 allowed = DefaultAllowedScriptingOnAllUrls();
225 SetAllowedScriptingOnAllUrlsHelper(context, extension_id, allowed, false);
227 return allowed;
230 void SetAllowedScriptingOnAllUrls(const std::string& extension_id,
231 content::BrowserContext* context,
232 bool allowed) {
233 if (allowed != AllowedScriptingOnAllUrls(extension_id, context)) {
234 SetAllowedScriptingOnAllUrlsHelper(context, extension_id, allowed, true);
235 ExtensionPrefs::Get(context)->UpdateExtensionPref(
236 extension_id,
237 kHasSetScriptOnAllUrlsPrefName,
238 new base::FundamentalValue(true));
242 bool HasSetAllowedScriptingOnAllUrls(const std::string& extension_id,
243 content::BrowserContext* context) {
244 bool did_set = false;
245 return ExtensionPrefs::Get(context)->ReadPrefAsBoolean(
246 extension_id,
247 kHasSetScriptOnAllUrlsPrefName,
248 &did_set) && did_set;
251 bool DefaultAllowedScriptingOnAllUrls() {
252 return !FeatureSwitch::scripts_require_action()->IsEnabled();
255 bool IsAppLaunchable(const std::string& extension_id,
256 content::BrowserContext* context) {
257 int reason = ExtensionPrefs::Get(context)->GetDisableReasons(extension_id);
258 return !((reason & Extension::DISABLE_UNSUPPORTED_REQUIREMENT) ||
259 (reason & Extension::DISABLE_CORRUPTED));
262 bool IsAppLaunchableWithoutEnabling(const std::string& extension_id,
263 content::BrowserContext* context) {
264 return ExtensionRegistry::Get(context)->GetExtensionById(
265 extension_id, ExtensionRegistry::ENABLED) != NULL;
268 bool ShouldSyncExtension(const Extension* extension,
269 content::BrowserContext* context) {
270 return sync_helper::IsSyncableExtension(extension) &&
271 !ExtensionPrefs::Get(context)->DoNotSync(extension->id());
274 bool ShouldSyncApp(const Extension* app, content::BrowserContext* context) {
275 return sync_helper::IsSyncableApp(app) &&
276 !util::IsEphemeralApp(app->id(), context) &&
277 !ExtensionPrefs::Get(context)->DoNotSync(app->id());
280 bool IsExtensionIdle(const std::string& extension_id,
281 content::BrowserContext* context) {
282 std::vector<std::string> ids_to_check;
283 ids_to_check.push_back(extension_id);
285 const Extension* extension =
286 ExtensionRegistry::Get(context)
287 ->GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
288 if (extension && extension->is_shared_module()) {
289 // We have to check all the extensions that use this shared module for idle
290 // to tell whether it is really 'idle'.
291 SharedModuleService* service = ExtensionSystem::Get(context)
292 ->extension_service()
293 ->shared_module_service();
294 scoped_ptr<ExtensionSet> dependents =
295 service->GetDependentExtensions(extension);
296 for (ExtensionSet::const_iterator i = dependents->begin();
297 i != dependents->end();
298 i++) {
299 ids_to_check.push_back((*i)->id());
303 ProcessManager* process_manager = ProcessManager::Get(context);
304 for (std::vector<std::string>::const_iterator i = ids_to_check.begin();
305 i != ids_to_check.end();
306 i++) {
307 const std::string id = (*i);
308 ExtensionHost* host = process_manager->GetBackgroundHostForExtension(id);
309 if (host)
310 return false;
312 scoped_refptr<content::SiteInstance> site_instance =
313 process_manager->GetSiteInstanceForURL(
314 Extension::GetBaseURLFromExtensionId(id));
315 if (site_instance && site_instance->HasProcess())
316 return false;
318 if (!process_manager->GetRenderFrameHostsForExtension(id).empty())
319 return false;
321 return true;
324 GURL GetSiteForExtensionId(const std::string& extension_id,
325 content::BrowserContext* context) {
326 return content::SiteInstance::GetSiteForURL(
327 context, Extension::GetBaseURLFromExtensionId(extension_id));
330 scoped_ptr<base::DictionaryValue> GetExtensionInfo(const Extension* extension) {
331 DCHECK(extension);
332 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
334 dict->SetString("id", extension->id());
335 dict->SetString("name", extension->name());
337 GURL icon = extensions::ExtensionIconSource::GetIconURL(
338 extension,
339 extension_misc::EXTENSION_ICON_SMALLISH,
340 ExtensionIconSet::MATCH_BIGGER,
341 false, // Not grayscale.
342 NULL); // Don't set bool if exists.
343 dict->SetString("icon", icon.spec());
345 return dict.Pass();
348 bool HasIsolatedStorage(const ExtensionInfo& info) {
349 if (!info.extension_manifest.get())
350 return false;
352 std::string error;
353 scoped_refptr<const Extension> extension(Extension::Create(
354 info.extension_path,
355 info.extension_location,
356 *info.extension_manifest,
357 Extension::NO_FLAGS,
358 info.extension_id,
359 &error));
360 if (!extension.get())
361 return false;
363 return AppIsolationInfo::HasIsolatedStorage(extension.get());
366 bool SiteHasIsolatedStorage(const GURL& extension_site_url,
367 content::BrowserContext* context) {
368 const Extension* extension = ExtensionRegistry::Get(context)->
369 enabled_extensions().GetExtensionOrAppByURL(extension_site_url);
370 if (!extension)
371 return false;
373 return AppIsolationInfo::HasIsolatedStorage(extension);
376 const gfx::ImageSkia& GetDefaultAppIcon() {
377 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
378 IDR_APP_DEFAULT_ICON);
381 const gfx::ImageSkia& GetDefaultExtensionIcon() {
382 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
383 IDR_EXTENSION_DEFAULT_ICON);
386 bool IsNewBookmarkAppsEnabled() {
387 return base::CommandLine::ForCurrentProcess()->HasSwitch(
388 switches::kEnableNewBookmarkApps);
391 bool IsExtensionSupervised(const Extension* extension, Profile* profile) {
392 return extension->was_installed_by_custodian() && profile->IsSupervised();
395 } // namespace util
396 } // namespace extensions