This repository has been archived by the owner on Jan 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
atom_extensions_browser_client.cc
549 lines (471 loc) · 19.2 KB
/
atom_extensions_browser_client.cc
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include <string>
#include <utility>
#include <vector>
#include "atom/browser/browser.h"
#include "atom/browser/extensions/api/atom_extensions_api_client.h"
#include "atom/browser/extensions/atom_extension_host_delegate.h"
#include "atom/browser/extensions/atom_extension_system_factory.h"
#include "atom/browser/extensions/atom_extension_web_contents_observer.h"
#include "atom/browser/extensions/atom_extensions_browser_client.h"
#include "atom/browser/extensions/atom_process_manager_delegate.h"
#include "atom/browser/web_contents_preferences.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/path_service.h"
#include "base/task_scheduler/post_task.h"
#include "base/version.h"
#include "brave/browser/brave_browser_context.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/cryptotoken_private/cryptotoken_private_api.h"
#include "chrome/browser/extensions/api/i18n/i18n_api.h"
#include "chrome/browser/extensions/chrome_component_extension_resource_manager.h"
#include "chrome/browser/extensions/chrome_extension_api_frame_id_map_helper.h"
#include "chrome/browser/extensions/chrome_url_request_util.h"
#include "chrome/browser/extensions/event_router_forwarder.h"
#include "chrome/browser/renderer_host/chrome_navigation_ui_data.h"
#include "chrome/common/chrome_paths.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_process_host.h"
#include "extensions/browser/api/alarms/alarms_api.h"
#include "extensions/browser/api/audio/audio_api.h"
#include "extensions/browser/api/declarative/declarative_api.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/idle/idle_api.h"
#include "extensions/browser/api/management/management_api.h"
#include "extensions/browser/api/runtime/runtime_api.h"
#include "extensions/browser/api/runtime/runtime_api_delegate.h"
#include "extensions/browser/api/socket/socket_api.h"
#include "extensions/browser/api/sockets_tcp/sockets_tcp_api.h"
#include "extensions/browser/api/sockets_tcp_server/sockets_tcp_server_api.h"
#include "extensions/browser/api/sockets_udp/sockets_udp_api.h"
#include "extensions/browser/api/storage/storage_api.h"
#include "extensions/browser/api/web_request/web_request_api.h"
#include "extensions/browser/component_extension_resource_manager.h"
#include "extensions/browser/extension_function_registry.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/mojo/interface_registration.h"
#include "extensions/browser/pref_names.h"
#include "extensions/browser/updater/null_extension_cache.h"
#include "extensions/browser/url_request_util.h"
#include "extensions/common/features/behavior_feature.h"
#include "extensions/common/features/feature.h"
#include "extensions/common/features/feature_provider.h"
#include "extensions/common/manifest_handlers/incognito_info.h"
#include "extensions/shell/browser/delegates/shell_kiosk_delegate.h"
#include "ui/base/resource/resource_bundle.h"
// URLRequestResourceBundleJob
// TODO(bridiver) move to a separate file
#include "base/base64.h"
#include "base/format_macros.h"
#include "base/sha1.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "extensions/common/file_util.h"
#include "net/url_request/url_request_simple_job.h"
#include "electron/brave/common/extensions/api/generated_api_registration.h"
#include "extensions/browser/api/generated_api_registration.h"
namespace {
bool IsWhitelistedForIncognito(const extensions::Extension* extension) {
const extensions::Feature* feature =
extensions::FeatureProvider::GetBehaviorFeature(
extensions::behavior_feature::kWhitelistedForIncognito);
return feature && feature->IsAvailableToExtension(extension).is_available();
}
net::HttpResponseHeaders* BuildHttpHeaders(
const std::string& content_security_policy,
bool send_cors_header,
const base::Time& last_modified_time) {
std::string raw_headers;
raw_headers.append("HTTP/1.1 200 OK");
if (!content_security_policy.empty()) {
raw_headers.append(1, '\0');
raw_headers.append("Content-Security-Policy: ");
raw_headers.append(content_security_policy);
}
if (send_cors_header) {
raw_headers.append(1, '\0');
raw_headers.append("Access-Control-Allow-Origin: *");
}
if (!last_modified_time.is_null()) {
// Hash the time and make an etag to avoid exposing the exact
// user installation time of the extension.
std::string hash =
base::StringPrintf("%" PRId64, last_modified_time.ToInternalValue());
hash = base::SHA1HashString(hash);
std::string etag;
base::Base64Encode(hash, &etag);
raw_headers.append(1, '\0');
raw_headers.append("ETag: \"");
raw_headers.append(etag);
raw_headers.append("\"");
// Also force revalidation.
raw_headers.append(1, '\0');
raw_headers.append("cache-control: no-cache");
}
raw_headers.append(2, '\0');
return new net::HttpResponseHeaders(raw_headers);
}
// A request for an extension resource in a Chrome .pak file. These are used
// by component extensions.
class URLRequestResourceBundleJob : public net::URLRequestSimpleJob {
public:
URLRequestResourceBundleJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const base::FilePath& filename,
int resource_id,
const std::string& content_security_policy,
bool send_cors_header)
: net::URLRequestSimpleJob(request, network_delegate),
filename_(filename),
resource_id_(resource_id),
weak_factory_(this) {
// Leave cache headers out of resource bundle requests.
response_info_.headers = BuildHttpHeaders(
content_security_policy, send_cors_header, base::Time());
}
// Overridden from URLRequestSimpleJob:
int GetRefCountedData(
std::string* mime_type,
std::string* charset,
scoped_refptr<base::RefCountedMemory>* data,
const net::CompletionCallback& callback) const override {
const ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
*data = rb.LoadDataResourceBytes(resource_id_);
// Add the Content-Length header now that we know the resource length.
response_info_.headers->AddHeader(
base::StringPrintf("%s: %s", net::HttpRequestHeaders::kContentLength,
base::NumberToString((*data)->size()).c_str()));
std::string* read_mime_type = new std::string;
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock{}},
base::Bind(&net::GetMimeTypeFromFile, filename_,
base::Unretained(read_mime_type)),
base::Bind(&URLRequestResourceBundleJob::OnMimeTypeRead,
weak_factory_.GetWeakPtr(), mime_type, charset, *data,
base::Owned(read_mime_type), callback));
return net::ERR_IO_PENDING;
}
void GetResponseInfo(net::HttpResponseInfo* info) override {
*info = response_info_;
}
private:
~URLRequestResourceBundleJob() override {}
void OnMimeTypeRead(std::string* out_mime_type,
std::string* charset,
scoped_refptr<base::RefCountedMemory> data,
std::string* read_mime_type,
const net::CompletionCallback& callback,
bool read_result) {
*out_mime_type = *read_mime_type;
if (base::StartsWith(*read_mime_type, "text/",
base::CompareCase::INSENSITIVE_ASCII)) {
// All of our HTML files should be UTF-8 and for other resource types
// (like images), charset doesn't matter.
DCHECK(base::IsStringUTF8(base::StringPiece(
reinterpret_cast<const char*>(data->front()), data->size())));
*charset = "utf-8";
}
int result = read_result ? net::OK : net::ERR_INVALID_URL;
callback.Run(result);
}
// We need the filename of the resource to determine the mime type.
base::FilePath filename_;
// The resource bundle id to load.
int resource_id_;
net::HttpResponseInfo response_info_;
mutable base::WeakPtrFactory<URLRequestResourceBundleJob> weak_factory_;
};
} // namespace
namespace extensions {
class AtomRuntimeAPIDelegate : public RuntimeAPIDelegate {
public:
AtomRuntimeAPIDelegate() {}
~AtomRuntimeAPIDelegate() override {};
// RuntimeAPIDelegate implementation.
void AddUpdateObserver(UpdateObserver* observer) override {};
void RemoveUpdateObserver(UpdateObserver* observer) override {};
void ReloadExtension(const std::string& extension_id) override {};
bool CheckForUpdates(const std::string& extension_id,
const UpdateCheckCallback& callback) override {
return false;
};
void OpenURL(const GURL& uninstall_url) override {};
bool GetPlatformInfo(api::runtime::PlatformInfo* info) override {
return true;
};
bool RestartDevice(std::string* error_message) override { return false; };
private:
DISALLOW_COPY_AND_ASSIGN(AtomRuntimeAPIDelegate);
};
AtomExtensionsBrowserClient::AtomExtensionsBrowserClient()
: extension_cache_(new NullExtensionCache()) {
api_client_.reset(new AtomExtensionsAPIClient);
process_manager_delegate_.reset(new AtomProcessManagerDelegate);
resource_manager_.reset(new ChromeComponentExtensionResourceManager);
}
AtomExtensionsBrowserClient::~AtomExtensionsBrowserClient() {}
std::unique_ptr<ExtensionApiFrameIdMapHelper>
AtomExtensionsBrowserClient::CreateExtensionApiFrameIdMapHelper(
ExtensionApiFrameIdMap* map) {
return base::WrapUnique(new ChromeExtensionApiFrameIdMapHelper(map));
}
bool AtomExtensionsBrowserClient::IsShuttingDown() {
return atom::Browser::Get()->is_shutting_down();
}
bool AtomExtensionsBrowserClient::AreExtensionsDisabled(
const base::CommandLine& command_line,
content::BrowserContext* context) {
return false;
}
bool AtomExtensionsBrowserClient::IsValidContext(
content::BrowserContext* context) {
return true;
}
bool AtomExtensionsBrowserClient::IsSameContext(
content::BrowserContext* first,
content::BrowserContext* second) {
if (GetOriginalContext(first) == GetOriginalContext(second))
return true;
return false;
}
bool AtomExtensionsBrowserClient::HasOffTheRecordContext(
content::BrowserContext* context) {
return static_cast<brave::BraveBrowserContext*>(context)->otr_context()
!= nullptr;
}
content::BrowserContext* AtomExtensionsBrowserClient::GetOffTheRecordContext(
content::BrowserContext* context) {
return static_cast<brave::BraveBrowserContext*>(context)->otr_context();
}
content::BrowserContext* AtomExtensionsBrowserClient::GetOriginalContext(
content::BrowserContext* context) {
return static_cast<brave::BraveBrowserContext*>(context)->original_context();
}
bool AtomExtensionsBrowserClient::IsGuestSession(
content::BrowserContext* context) const {
// return true for sub-contexts so the process manager original context
// will match the extension registry context
auto original_context =
static_cast<brave::BraveBrowserContext*>(context)->original_context();
return !context->IsOffTheRecord() && (context != original_context);
}
// static
bool AtomExtensionsBrowserClient::IsIncognitoEnabled(
const std::string& extension_id,
content::BrowserContext* context) {
auto registry = ExtensionRegistry::Get(context);
if (!registry)
return false;
const Extension* extension = registry->
GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
if (extension) {
if (!util::CanBeIncognitoEnabled(extension))
return false;
if (extension->location() == Manifest::COMPONENT ||
extension->location() == Manifest::EXTERNAL_COMPONENT) {
return true;
}
if (IsWhitelistedForIncognito(extension))
return true;
}
// TODO(bridiver)
// ExtensionPrefs::Get(context)->IsIncognitoEnabled(extension_id);
return true;
}
bool AtomExtensionsBrowserClient::CanExtensionCrossIncognito(
const Extension* extension,
content::BrowserContext* context) const {
DCHECK(extension);
return IsExtensionIncognitoEnabled(extension->id(), context) &&
!IncognitoInfo::IsSplitMode(extension);
}
net::URLRequestJob*
AtomExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const base::FilePath& directory_path,
const std::string& content_security_policy,
bool send_cors_header) {
return chrome_url_request_util::MaybeCreateURLRequestResourceBundleJob(
request, network_delegate, directory_path, content_security_policy,
send_cors_header);
}
base::FilePath AtomExtensionsBrowserClient::GetBundleResourcePath(
const network::ResourceRequest& request,
const base::FilePath& extension_resources_path,
int* resource_id) const {
return chrome_url_request_util::GetBundleResourcePath(
request, extension_resources_path, resource_id);
}
void AtomExtensionsBrowserClient::LoadResourceFromResourceBundle(
const network::ResourceRequest& request,
network::mojom::URLLoaderRequest loader,
const base::FilePath& resource_relative_path,
int resource_id,
const std::string& content_security_policy,
network::mojom::URLLoaderClientPtr client,
bool send_cors_header) {
chrome_url_request_util::LoadResourceFromResourceBundle(
request, std::move(loader), resource_relative_path, resource_id,
content_security_policy, std::move(client), send_cors_header);
}
bool AtomExtensionsBrowserClient::AllowCrossRendererResourceLoad(
const GURL& url,
content::ResourceType resource_type,
ui::PageTransition page_transition,
int child_id,
bool is_incognito,
const Extension* extension,
const ExtensionSet& extensions,
const ProcessMap& process_map) {
bool allowed = false;
if (url_request_util::AllowCrossRendererResourceLoad(
url, resource_type, page_transition, child_id, is_incognito,
extension, extensions, process_map, &allowed)) {
return allowed;
}
// Couldn't determine if resource is allowed. Block the load.
return false;
}
void AtomExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
content::BrowserContext* context,
std::vector<ExtensionPrefsObserver*>* observers) const {
}
ProcessManagerDelegate*
AtomExtensionsBrowserClient::GetProcessManagerDelegate() const {
return process_manager_delegate_.get();
}
std::unique_ptr<ExtensionHostDelegate>
AtomExtensionsBrowserClient::CreateExtensionHostDelegate() {
return std::unique_ptr<ExtensionHostDelegate>(new AtomExtensionHostDelegate);
}
PrefService* AtomExtensionsBrowserClient::GetPrefServiceForContext(
content::BrowserContext* context) {
return user_prefs::UserPrefs::Get(context);
}
bool AtomExtensionsBrowserClient::DidVersionUpdate(
content::BrowserContext* context) {
return false;
}
void AtomExtensionsBrowserClient::PermitExternalProtocolHandler() {
}
bool AtomExtensionsBrowserClient::IsRunningInForcedAppMode() {
return false;
}
bool AtomExtensionsBrowserClient::IsAppModeForcedForApp(
const ExtensionId& extension_id) {
return false;
}
bool AtomExtensionsBrowserClient::IsLoggedInAsPublicAccount() {
return false;
}
ExtensionSystemProvider*
AtomExtensionsBrowserClient::GetExtensionSystemFactory() {
return AtomExtensionSystemFactory::GetInstance();
}
void AtomExtensionsBrowserClient::RegisterExtensionFunctions(
ExtensionFunctionRegistry* registry) const {
api::GeneratedFunctionRegistry::RegisterAll(registry);
api::BraveGeneratedFunctionRegistry::RegisterAll(registry);
// Instead of registering all Chrome extension functions,
// via api::ChromeGeneratedFunctionRegistry::RegisterAll(registry)
// explicitly register just the ones we want
constexpr ExtensionFunctionRegistry::FactoryEntry chromeEntries[] = {
{
&NewExtensionFunction<
api::CryptotokenPrivateCanOriginAssertAppIdFunction>,
api::CryptotokenPrivateCanOriginAssertAppIdFunction::function_name(),
api::CryptotokenPrivateCanOriginAssertAppIdFunction::
histogram_value(),
},
{
NewExtensionFunction<I18nGetAcceptLanguagesFunction>,
I18nGetAcceptLanguagesFunction::function_name(),
I18nGetAcceptLanguagesFunction::histogram_value(),
},
};
for (const auto& entry : chromeEntries) {
registry->Register(entry);
}
}
std::unique_ptr<RuntimeAPIDelegate>
AtomExtensionsBrowserClient::CreateRuntimeAPIDelegate(
content::BrowserContext* context) const {
return std::unique_ptr<RuntimeAPIDelegate>(new AtomRuntimeAPIDelegate());
}
const ComponentExtensionResourceManager*
AtomExtensionsBrowserClient::GetComponentExtensionResourceManager() {
return resource_manager_.get();
}
void AtomExtensionsBrowserClient::RegisterExtensionInterfaces(
service_manager::BinderRegistryWithArgs<
content::RenderFrameHost*>* registry,
content::RenderFrameHost* render_frame_host,
const Extension* extension) const {
RegisterInterfacesForExtension(registry, render_frame_host, extension);
}
void AtomExtensionsBrowserClient::BroadcastEventToRenderers(
events::HistogramValue histogram_value,
const std::string& event_name,
std::unique_ptr<base::ListValue> args) {
g_browser_process->extension_event_router_forwarder()
->BroadcastEventToRenderers(histogram_value, event_name, std::move(args),
GURL());
}
net::NetLog* AtomExtensionsBrowserClient::GetNetLog() {
return nullptr;
}
ExtensionCache* AtomExtensionsBrowserClient::GetExtensionCache() {
return extension_cache_.get();
}
bool AtomExtensionsBrowserClient::IsBackgroundUpdateAllowed() {
return true;
}
bool AtomExtensionsBrowserClient::IsMinBrowserVersionSupported(
const std::string& min_version) {
return true;
}
ExtensionWebContentsObserver*
AtomExtensionsBrowserClient::GetExtensionWebContentsObserver(
content::WebContents* web_contents) {
return AtomExtensionWebContentsObserver::FromWebContents(web_contents);;
}
ExtensionNavigationUIData*
AtomExtensionsBrowserClient::GetExtensionNavigationUIData(
net::URLRequest* request) {
const content::ResourceRequestInfo* info =
content::ResourceRequestInfo::ForRequest(request);
if (!info)
return nullptr;
ChromeNavigationUIData* navigation_data =
static_cast<ChromeNavigationUIData*>(info->GetNavigationUIData());
if (!navigation_data)
return nullptr;
return navigation_data->GetExtensionNavigationUIData();
}
KioskDelegate* AtomExtensionsBrowserClient::GetKioskDelegate() {
return nullptr;
}
void AtomExtensionsBrowserClient::CleanUpWebView(
content::BrowserContext* browser_context,
int embedder_process_id,
int view_instance_id) {
}
void AtomExtensionsBrowserClient::AttachExtensionTaskManagerTag(
content::WebContents* web_contents,
ViewType view_type) {
}
bool AtomExtensionsBrowserClient::IsLockScreenContext(
content::BrowserContext* context) {
return false;
}
std::string AtomExtensionsBrowserClient::GetApplicationLocale() {
return g_browser_process->GetApplicationLocale();
}
} // namespace extensions