1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_util.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_sync_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
#include "chrome/common/extensions/sync_helper.h"
#include "content/public/browser/site_instance.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extension_util.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_icon_set.h"
#include "extensions/common/manifest.h"
#include "extensions/common/manifest_handlers/incognito_info.h"
#include "grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"
namespace extensions {
namespace util {
namespace {
// The entry into the ExtensionPrefs for allowing an extension to script on
// all urls without explicit permission.
const char kExtensionAllowedOnAllUrlsPrefName[] =
"extension_can_script_all_urls";
}
bool IsIncognitoEnabled(const std::string& extension_id,
content::BrowserContext* context) {
const Extension* extension = ExtensionRegistry::Get(context)->
GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
if (extension) {
if (!extension->can_be_incognito_enabled())
return false;
// If this is an existing component extension we always allow it to
// work in incognito mode.
if (extension->location() == Manifest::COMPONENT)
return true;
}
return ExtensionPrefs::Get(context)->IsIncognitoEnabled(extension_id);
}
void SetIsIncognitoEnabled(const std::string& extension_id,
content::BrowserContext* context,
bool enabled) {
ExtensionService* service =
ExtensionSystem::Get(context)->extension_service();
CHECK(service);
const Extension* extension = service->GetInstalledExtension(extension_id);
if (extension) {
if (!extension->can_be_incognito_enabled())
return;
if (extension->location() == Manifest::COMPONENT) {
// This shouldn't be called for component extensions unless it is called
// by sync, for syncable component extensions.
// See http://crbug.com/112290 and associated CLs for the sordid history.
DCHECK(sync_helper::IsSyncable(extension));
// If we are here, make sure the we aren't trying to change the value.
DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id, service->profile()));
return;
}
}
ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(service->profile());
// Broadcast unloaded and loaded events to update browser state. Only bother
// if the value changed and the extension is actually enabled, since there is
// no UI otherwise.
bool old_enabled = extension_prefs->IsIncognitoEnabled(extension_id);
if (enabled == old_enabled)
return;
extension_prefs->SetIsIncognitoEnabled(extension_id, enabled);
bool extension_is_enabled = service->extensions()->Contains(extension_id);
// When we reload the extension the ID may be invalidated if we've passed it
// by const ref everywhere. Make a copy to be safe.
std::string id = extension_id;
if (extension_is_enabled)
service->ReloadExtension(id);
// Reloading the extension invalidates the |extension| pointer.
extension = service->GetInstalledExtension(id);
if (extension) {
ExtensionSyncService::Get(service->profile())->
SyncExtensionChangeIfNeeded(*extension);
}
}
bool CanCrossIncognito(const Extension* extension,
content::BrowserContext* context) {
// We allow the extension to see events and data from another profile iff it
// uses "spanning" behavior and it has incognito access. "split" mode
// extensions only see events for a matching profile.
CHECK(extension);
return IsIncognitoEnabled(extension->id(), context) &&
!IncognitoInfo::IsSplitMode(extension);
}
bool CanLoadInIncognito(const Extension* extension,
content::BrowserContext* context) {
CHECK(extension);
if (extension->is_hosted_app())
return true;
// Packaged apps and regular extensions need to be enabled specifically for
// incognito (and split mode should be set).
return IncognitoInfo::IsSplitMode(extension) &&
IsIncognitoEnabled(extension->id(), context);
}
bool AllowFileAccess(const std::string& extension_id,
content::BrowserContext* context) {
return CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableExtensionsFileAccessCheck) ||
ExtensionPrefs::Get(context)->AllowFileAccess(extension_id);
}
void SetAllowFileAccess(const std::string& extension_id,
content::BrowserContext* context,
bool allow) {
ExtensionService* service =
ExtensionSystem::Get(context)->extension_service();
CHECK(service);
// Reload to update browser state. Only bother if the value changed and the
// extension is actually enabled, since there is no UI otherwise.
if (allow == AllowFileAccess(extension_id, context))
return;
ExtensionPrefs::Get(context)->SetAllowFileAccess(extension_id, allow);
bool extension_is_enabled = service->extensions()->Contains(extension_id);
if (extension_is_enabled)
service->ReloadExtension(extension_id);
}
bool AllowedScriptingOnAllUrls(const std::string& extension_id,
content::BrowserContext* context) {
bool allowed = false;
return ExtensionPrefs::Get(context)->ReadPrefAsBoolean(
extension_id,
kExtensionAllowedOnAllUrlsPrefName,
&allowed) &&
allowed;
}
void SetAllowedScriptingOnAllUrls(const std::string& extension_id,
content::BrowserContext* context,
bool allowed) {
ExtensionPrefs::Get(context)->UpdateExtensionPref(
extension_id,
kExtensionAllowedOnAllUrlsPrefName,
allowed ? new base::FundamentalValue(true) : NULL);
}
bool IsAppLaunchable(const std::string& extension_id,
content::BrowserContext* context) {
return !(ExtensionPrefs::Get(context)->GetDisableReasons(extension_id) &
Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
}
bool IsAppLaunchableWithoutEnabling(const std::string& extension_id,
content::BrowserContext* context) {
return ExtensionRegistry::Get(context)->GetExtensionById(
extension_id, ExtensionRegistry::ENABLED) != NULL;
}
bool ShouldSyncExtension(const Extension* extension,
content::BrowserContext* context) {
return sync_helper::IsSyncableExtension(extension) &&
!ExtensionPrefs::Get(context)->DoNotSync(extension->id());
}
bool ShouldSyncApp(const Extension* app, content::BrowserContext* context) {
return sync_helper::IsSyncableApp(app) &&
!util::IsEphemeralApp(app->id(), context) &&
!ExtensionPrefs::Get(context)->DoNotSync(app->id());
}
bool IsExtensionIdle(const std::string& extension_id,
content::BrowserContext* context) {
ProcessManager* process_manager =
ExtensionSystem::Get(context)->process_manager();
DCHECK(process_manager);
ExtensionHost* host =
process_manager->GetBackgroundHostForExtension(extension_id);
if (host)
return false;
content::SiteInstance* site_instance = process_manager->GetSiteInstanceForURL(
Extension::GetBaseURLFromExtensionId(extension_id));
if (site_instance && site_instance->HasProcess())
return false;
return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
}
GURL GetSiteForExtensionId(const std::string& extension_id,
content::BrowserContext* context) {
return content::SiteInstance::GetSiteForURL(
context, Extension::GetBaseURLFromExtensionId(extension_id));
}
scoped_ptr<base::DictionaryValue> GetExtensionInfo(const Extension* extension) {
DCHECK(extension);
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
dict->SetString("id", extension->id());
dict->SetString("name", extension->name());
GURL icon = extensions::ExtensionIconSource::GetIconURL(
extension,
extension_misc::EXTENSION_ICON_SMALLISH,
ExtensionIconSet::MATCH_BIGGER,
false, // Not grayscale.
NULL); // Don't set bool if exists.
dict->SetString("icon", icon.spec());
return dict.Pass();
}
bool HasIsolatedStorage(const ExtensionInfo& info) {
if (!info.extension_manifest.get())
return false;
std::string error;
scoped_refptr<const Extension> extension(Extension::Create(
info.extension_path,
info.extension_location,
*info.extension_manifest,
Extension::NO_FLAGS,
info.extension_id,
&error));
if (!extension.get())
return false;
return AppIsolationInfo::HasIsolatedStorage(extension.get());
}
bool SiteHasIsolatedStorage(const GURL& extension_site_url,
content::BrowserContext* context) {
const Extension* extension = ExtensionRegistry::Get(context)->
enabled_extensions().GetExtensionOrAppByURL(extension_site_url);
if (!extension)
return false;
return AppIsolationInfo::HasIsolatedStorage(extension);
}
const gfx::ImageSkia& GetDefaultAppIcon() {
return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
IDR_APP_DEFAULT_ICON);
}
const gfx::ImageSkia& GetDefaultExtensionIcon() {
return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
IDR_EXTENSION_DEFAULT_ICON);
}
} // namespace util
} // namespace extensions
|