[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" |
kundaji | e548e744 | 2015-09-18 23:19:09 | [diff] [blame] | 15 | #include "extensions/common/manifest_handlers/incognito_info.h" |
Clark DuVall | e2bdd33 | 2019-08-07 18:32:13 | [diff] [blame] | 16 | #include "extensions/common/manifest_handlers/shared_module_info.h" |
[email protected] | 411f8ae | 2014-05-22 11:12:23 | [diff] [blame] | 17 | |
| 18 | namespace extensions { |
| 19 | namespace util { |
| 20 | |
kundaji | e548e744 | 2015-09-18 23:19:09 | [diff] [blame] | 21 | bool CanBeIncognitoEnabled(const Extension* extension) { |
| 22 | return IncognitoInfo::IsIncognitoAllowed(extension) && |
| 23 | (!extension->is_platform_app() || |
| 24 | extension->location() == Manifest::COMPONENT); |
| 25 | } |
| 26 | |
karandeepb | 810e3340 | 2017-04-05 23:41:22 | [diff] [blame] | 27 | bool 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 Hendrich | d03bafa | 2019-05-29 22:18:03 | [diff] [blame] | 39 | if (extension->is_login_screen_extension()) |
| 40 | return true; |
karandeepb | 810e3340 | 2017-04-05 23:41:22 | [diff] [blame] | 41 | } |
| 42 | return ExtensionPrefs::Get(context)->IsIncognitoEnabled(extension_id); |
| 43 | } |
| 44 | |
Clark DuVall | 1d81619 | 2019-07-19 19:54:42 | [diff] [blame] | 45 | bool 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 Giuffrida | 7efeed14 | 2017-06-07 06:29:21 | [diff] [blame] | 55 | GURL GetSiteForExtensionId(const std::string& extension_id, |
| 56 | content::BrowserContext* context) { |
| 57 | return content::SiteInstance::GetSiteForURL( |
| 58 | context, Extension::GetBaseURLFromExtensionId(extension_id)); |
| 59 | } |
| 60 | |
lazyboy | 4c82177a | 2016-10-18 00:04:09 | [diff] [blame] | 61 | content::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 DuVall | e2bdd33 | 2019-08-07 18:32:13 | [diff] [blame] | 72 | // 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. |
| 75 | bool 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] | 411f8ae | 2014-05-22 11:12:23 | [diff] [blame] | 131 | } // namespace util |
| 132 | } // namespace extensions |