[email protected] | 411f8ae | 2014-05-22 11:12:23 | [diff] [blame] | 1 | // Copyright 2014 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 "extensions/browser/extension_util.h" |
| 6 | |
lazyboy | 4c82177a | 2016-10-18 00:04:09 | [diff] [blame] | 7 | #include "content/public/browser/browser_context.h" |
| 8 | #include "content/public/browser/site_instance.h" |
[email protected] | 411f8ae | 2014-05-22 11:12:23 | [diff] [blame] | 9 | #include "extensions/browser/extension_prefs.h" |
| 10 | #include "extensions/browser/extension_registry.h" |
karandeepb | 810e3340 | 2017-04-05 23:41:22 | [diff] [blame^] | 11 | #include "extensions/common/features/behavior_feature.h" |
| 12 | #include "extensions/common/features/feature.h" |
| 13 | #include "extensions/common/features/feature_provider.h" |
| 14 | #include "extensions/common/manifest.h" |
tfarina | 01710e3 | 2015-07-13 21:53:22 | [diff] [blame] | 15 | #include "extensions/common/manifest_handlers/app_isolation_info.h" |
kundaji | e548e744 | 2015-09-18 23:19:09 | [diff] [blame] | 16 | #include "extensions/common/manifest_handlers/incognito_info.h" |
[email protected] | 411f8ae | 2014-05-22 11:12:23 | [diff] [blame] | 17 | |
| 18 | namespace extensions { |
| 19 | namespace util { |
| 20 | |
karandeepb | 810e3340 | 2017-04-05 23:41:22 | [diff] [blame^] | 21 | namespace { |
| 22 | |
| 23 | // Returns true if |extension| should always be enabled in incognito mode. |
| 24 | bool IsWhitelistedForIncognito(const Extension* extension) { |
| 25 | const Feature* feature = FeatureProvider::GetBehaviorFeature( |
| 26 | behavior_feature::kWhitelistedForIncognito); |
| 27 | return feature && feature->IsAvailableToExtension(extension).is_available(); |
| 28 | } |
| 29 | |
| 30 | } // namespace |
| 31 | |
tfarina | 01710e3 | 2015-07-13 21:53:22 | [diff] [blame] | 32 | bool HasIsolatedStorage(const ExtensionInfo& info) { |
| 33 | if (!info.extension_manifest.get()) |
| 34 | return false; |
| 35 | |
| 36 | std::string error; |
| 37 | scoped_refptr<const Extension> extension(Extension::Create( |
| 38 | info.extension_path, |
| 39 | info.extension_location, |
| 40 | *info.extension_manifest, |
| 41 | Extension::NO_FLAGS, |
| 42 | info.extension_id, |
| 43 | &error)); |
| 44 | |
| 45 | return extension.get() && |
| 46 | AppIsolationInfo::HasIsolatedStorage(extension.get()); |
| 47 | } |
| 48 | |
| 49 | bool SiteHasIsolatedStorage(const GURL& extension_site_url, |
| 50 | content::BrowserContext* context) { |
| 51 | const Extension* extension = ExtensionRegistry::Get(context)-> |
| 52 | enabled_extensions().GetExtensionOrAppByURL(extension_site_url); |
| 53 | |
| 54 | return extension && AppIsolationInfo::HasIsolatedStorage(extension); |
| 55 | } |
| 56 | |
kundaji | e548e744 | 2015-09-18 23:19:09 | [diff] [blame] | 57 | bool CanBeIncognitoEnabled(const Extension* extension) { |
| 58 | return IncognitoInfo::IsIncognitoAllowed(extension) && |
| 59 | (!extension->is_platform_app() || |
| 60 | extension->location() == Manifest::COMPONENT); |
| 61 | } |
| 62 | |
karandeepb | 810e3340 | 2017-04-05 23:41:22 | [diff] [blame^] | 63 | bool IsIncognitoEnabled(const std::string& extension_id, |
| 64 | content::BrowserContext* context) { |
| 65 | const Extension* extension = |
| 66 | ExtensionRegistry::Get(context)->GetExtensionById( |
| 67 | extension_id, ExtensionRegistry::ENABLED); |
| 68 | if (extension) { |
| 69 | if (!CanBeIncognitoEnabled(extension)) |
| 70 | return false; |
| 71 | // If this is an existing component extension we always allow it to |
| 72 | // work in incognito mode. |
| 73 | if (Manifest::IsComponentLocation(extension->location())) |
| 74 | return true; |
| 75 | if (IsWhitelistedForIncognito(extension)) |
| 76 | return true; |
| 77 | } |
| 78 | return ExtensionPrefs::Get(context)->IsIncognitoEnabled(extension_id); |
| 79 | } |
| 80 | |
lazyboy | 4c82177a | 2016-10-18 00:04:09 | [diff] [blame] | 81 | content::StoragePartition* GetStoragePartitionForExtensionId( |
| 82 | const std::string& extension_id, |
| 83 | content::BrowserContext* browser_context) { |
| 84 | GURL site_url = content::SiteInstance::GetSiteForURL( |
| 85 | browser_context, Extension::GetBaseURLFromExtensionId(extension_id)); |
| 86 | content::StoragePartition* storage_partition = |
| 87 | content::BrowserContext::GetStoragePartitionForSite(browser_context, |
| 88 | site_url); |
| 89 | return storage_partition; |
| 90 | } |
| 91 | |
[email protected] | 411f8ae | 2014-05-22 11:12:23 | [diff] [blame] | 92 | } // namespace util |
| 93 | } // namespace extensions |