blob: 6077d4e257675106808de1111147b8a783eaa32b [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"
kundajie548e7442015-09-18 23:19:0915#include "extensions/common/manifest_handlers/incognito_info.h"
Clark DuValle2bdd332019-08-07 18:32:1316#include "extensions/common/manifest_handlers/shared_module_info.h"
[email protected]411f8ae2014-05-22 11:12:2317
18namespace extensions {
19namespace util {
20
kundajie548e7442015-09-18 23:19:0921bool CanBeIncognitoEnabled(const Extension* extension) {
22 return IncognitoInfo::IsIncognitoAllowed(extension) &&
23 (!extension->is_platform_app() ||
24 extension->location() == Manifest::COMPONENT);
25}
26
karandeepb810e33402017-04-05 23:41:2227bool IsIncognitoEnabled(const std::string& extension_id,
28 content::BrowserContext* context) {
29 const Extension* extension =
30 ExtensionRegistry::Get(context)->GetExtensionById(
31 extension_id, ExtensionRegistry::ENABLED);
32 if (extension) {
33 if (!CanBeIncognitoEnabled(extension))
34 return false;
35 // If this is an existing component extension we always allow it to
36 // work in incognito mode.
37 if (Manifest::IsComponentLocation(extension->location()))
38 return true;
Alexander Hendrichd03bafa2019-05-29 22:18:0339 if (extension->is_login_screen_extension())
40 return true;
karandeepb810e33402017-04-05 23:41:2241 }
42 return ExtensionPrefs::Get(context)->IsIncognitoEnabled(extension_id);
43}
44
Clark DuVall1d816192019-07-19 19:54:4245bool CanCrossIncognito(const Extension* extension,
46 content::BrowserContext* context) {
47 // We allow the extension to see events and data from another profile iff it
48 // uses "spanning" behavior and it has incognito access. "split" mode
49 // extensions only see events for a matching profile.
50 CHECK(extension);
51 return IsIncognitoEnabled(extension->id(), context) &&
52 !IncognitoInfo::IsSplitMode(extension);
53}
54
Michael Giuffrida7efeed142017-06-07 06:29:2155GURL GetSiteForExtensionId(const std::string& extension_id,
56 content::BrowserContext* context) {
57 return content::SiteInstance::GetSiteForURL(
58 context, Extension::GetBaseURLFromExtensionId(extension_id));
59}
60
lazyboy4c82177a2016-10-18 00:04:0961content::StoragePartition* GetStoragePartitionForExtensionId(
62 const std::string& extension_id,
63 content::BrowserContext* browser_context) {
64 GURL site_url = content::SiteInstance::GetSiteForURL(
65 browser_context, Extension::GetBaseURLFromExtensionId(extension_id));
66 content::StoragePartition* storage_partition =
67 content::BrowserContext::GetStoragePartitionForSite(browser_context,
68 site_url);
69 return storage_partition;
70}
71
Clark DuValle2bdd332019-08-07 18:32:1372// This function is security sensitive. Bugs could cause problems that break
73// restrictions on local file access or NaCl's validation caching. If you modify
74// this function, please get a security review from a NaCl person.
75bool MapUrlToLocalFilePath(const ExtensionSet* extensions,
76 const GURL& file_url,
77 bool use_blocking_api,
78 base::FilePath* file_path) {
79 // Check that the URL is recognized by the extension system.
80 const Extension* extension = extensions->GetExtensionOrAppByURL(file_url);
81 if (!extension)
82 return false;
83
84 // This is a short-cut which avoids calling a blocking file operation
85 // (GetFilePath()), so that this can be called on the non blocking threads. It
86 // only handles a subset of the urls.
87 if (!use_blocking_api) {
88 if (file_url.SchemeIs(extensions::kExtensionScheme)) {
89 std::string path = file_url.path();
90 base::TrimString(path, "/", &path); // Remove first slash
91 *file_path = extension->path().AppendASCII(path);
92 return true;
93 }
94 return false;
95 }
96
97 std::string path = file_url.path();
98 ExtensionResource resource;
99
100 if (SharedModuleInfo::IsImportedPath(path)) {
101 // Check if this is a valid path that is imported for this extension.
102 std::string new_extension_id;
103 std::string new_relative_path;
104 SharedModuleInfo::ParseImportedPath(path, &new_extension_id,
105 &new_relative_path);
106 const Extension* new_extension = extensions->GetByID(new_extension_id);
107 if (!new_extension)
108 return false;
109
110 if (!SharedModuleInfo::ImportsExtensionById(extension, new_extension_id))
111 return false;
112
113 resource = new_extension->GetResource(new_relative_path);
114 } else {
115 // Check that the URL references a resource in the extension.
116 resource = extension->GetResource(path);
117 }
118
119 if (resource.empty())
120 return false;
121
122 // GetFilePath is a blocking function call.
123 const base::FilePath resource_file_path = resource.GetFilePath();
124 if (resource_file_path.empty())
125 return false;
126
127 *file_path = resource_file_path;
128 return true;
129}
130
[email protected]411f8ae2014-05-22 11:12:23131} // namespace util
132} // namespace extensions