blob: 0961f42713d89262da4d95e577c38509cd741f95 [file] [log] [blame]
[email protected]411f8ae2014-05-22 11:12:231// 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
lazyboy4c82177a2016-10-18 00:04:097#include "content/public/browser/browser_context.h"
8#include "content/public/browser/site_instance.h"
[email protected]411f8ae2014-05-22 11:12:239#include "extensions/browser/extension_prefs.h"
10#include "extensions/browser/extension_registry.h"
karandeepb810e33402017-04-05 23:41:2211#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"
tfarina01710e32015-07-13 21:53:2215#include "extensions/common/manifest_handlers/app_isolation_info.h"
kundajie548e7442015-09-18 23:19:0916#include "extensions/common/manifest_handlers/incognito_info.h"
[email protected]411f8ae2014-05-22 11:12:2317
18namespace extensions {
19namespace util {
20
karandeepb810e33402017-04-05 23:41:2221namespace {
22
23// Returns true if |extension| should always be enabled in incognito mode.
24bool 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
tfarina01710e32015-07-13 21:53:2232bool 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
49bool 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
kundajie548e7442015-09-18 23:19:0957bool CanBeIncognitoEnabled(const Extension* extension) {
58 return IncognitoInfo::IsIncognitoAllowed(extension) &&
59 (!extension->is_platform_app() ||
60 extension->location() == Manifest::COMPONENT);
61}
62
karandeepb810e33402017-04-05 23:41:2263bool 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
lazyboy4c82177a2016-10-18 00:04:0981content::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]411f8ae2014-05-22 11:12:2392} // namespace util
93} // namespace extensions