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
content / browser / device / device_service.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/public/browser/device_service.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/threading/sequence_local_storage_slot.h"
#include "build/build_config.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/common/content_client.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/device_service.h"
#include "services/device/public/cpp/geolocation/geolocation_system_permission_manager.h"
#include "services/network/public/cpp/cross_thread_pending_shared_url_loader_factory.h"
#include "services/network/public/mojom/network_service_test.mojom.h"
#include "services/network/public/mojom/url_loader.mojom.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "content/browser/wake_lock/wake_lock_context_host.h"
#include "content/public/android/content_jni_headers/ContentNfcDelegate_jni.h"
#endif
namespace content {
namespace {
// SharedURLLoaderFactory for device service, backed by
// GetContentClient()->browser()->GetSystemSharedURLLoaderFactory().
class DeviceServiceURLLoaderFactory : public network::SharedURLLoaderFactory {
public:
DeviceServiceURLLoaderFactory() = default;
DeviceServiceURLLoaderFactory(const DeviceServiceURLLoaderFactory&) = delete;
DeviceServiceURLLoaderFactory& operator=(
const DeviceServiceURLLoaderFactory&) = delete;
// mojom::URLLoaderFactory implementation:
void CreateLoaderAndStart(
mojo::PendingReceiver<network::mojom::URLLoader> receiver,
int32_t request_id,
uint32_t options,
const network::ResourceRequest& url_request,
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
override {
auto factory =
GetContentClient()->browser()->GetSystemSharedURLLoaderFactory();
if (!factory)
return;
factory->CreateLoaderAndStart(std::move(receiver), request_id, options,
url_request, std::move(client),
traffic_annotation);
}
// SharedURLLoaderFactory implementation:
void Clone(mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver)
override {
GetContentClient()->browser()->GetSystemSharedURLLoaderFactory()->Clone(
std::move(receiver));
}
std::unique_ptr<network::PendingSharedURLLoaderFactory> Clone() override {
return std::make_unique<network::CrossThreadPendingSharedURLLoaderFactory>(
this);
}
private:
friend class base::RefCounted<DeviceServiceURLLoaderFactory>;
~DeviceServiceURLLoaderFactory() override = default;
};
void BindDeviceServiceReceiver(
mojo::PendingReceiver<device::mojom::DeviceService> receiver) {
// Bind the lifetime of the service instance to that of the sequence it's
// running on.
static base::SequenceLocalStorageSlot<std::unique_ptr<device::DeviceService>>
service_slot;
auto& service = service_slot.GetOrCreateValue();
if (service) {
service->AddReceiver(std::move(receiver));
return;
}
auto params = std::make_unique<device::DeviceServiceParams>();
// This task runner may be used by some device service implementation bits
// to interface with dbus client code, which in turn imposes some subtle
// thread affinity on the clients. We therefore require a single-thread
// runner.
params->file_task_runner = base::ThreadPool::CreateSingleThreadTaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT});
params->io_task_runner = GetIOThreadTaskRunner({});
params->url_loader_factory =
base::MakeRefCounted<DeviceServiceURLLoaderFactory>();
params->network_connection_tracker = content::GetNetworkConnectionTracker();
params->geolocation_api_key =
GetContentClient()->browser()->GetGeolocationApiKey();
params->custom_location_provider_callback =
base::BindRepeating(&ContentBrowserClient::OverrideSystemLocationProvider,
base::Unretained(GetContentClient()->browser()));
params->geolocation_system_permission_manager =
GetContentClient()->browser()->GetGeolocationSystemPermissionManager();
#if BUILDFLAG(IS_ANDROID)
JNIEnv* env = base::android::AttachCurrentThread();
params->java_nfc_delegate = Java_ContentNfcDelegate_create(env);
DCHECK(!params->java_nfc_delegate.is_null());
params->wake_lock_context_callback =
base::BindRepeating(&WakeLockContextHost::GetNativeViewForContext);
params->use_gms_core_location_provider =
GetContentClient()->browser()->ShouldUseGmsCoreGeolocationProvider();
#endif
service = device::CreateDeviceService(std::move(params), std::move(receiver));
}
} // namespace
device::mojom::DeviceService& GetDeviceService() {
static base::SequenceLocalStorageSlot<
mojo::Remote<device::mojom::DeviceService>>
remote_slot;
mojo::Remote<device::mojom::DeviceService>& remote =
remote_slot.GetOrCreateValue();
if (!remote) {
// This may be called very early in startup, too early for some Device
// Service initialization steps (for example, in browser test environments,
// the Device Service's connection to the Network Service could deadlock).
// We post a task to defer until the main message loop has started, when
// initialization is reliably safe.
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&BindDeviceServiceReceiver,
remote.BindNewPipeAndPassReceiver()));
}
return *remote.get();
}
} // namespace content