1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/child/blink_platform_impl.h"
12 #include "base/command_line.h"
13 #include "base/files/file_path.h"
14 #include "base/functional/bind.h"
15 #include "base/location.h"
16 #include "base/memory/raw_ptr.h"
17 #include "base/memory/singleton.h"
18 #include "base/metrics/user_metrics_action.h"
19 #include "base/rand_util.h"
20 #include "base/run_loop.h"
21 #include "base/sequence_checker.h"
22 #include "base/strings/string_number_conversions.h"
23 #include "base/strings/string_piece.h"
24 #include "base/strings/string_util.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "base/synchronization/lock.h"
27 #include "base/system/sys_info.h"
28 #include "base/task/sequenced_task_runner.h"
29 #include "base/task/single_thread_task_runner.h"
30 #include "base/task/thread_pool.h"
31 #include "base/time/time.h"
32 #include "base/trace_event/memory_allocator_dump_guid.h"
33 #include "base/trace_event/memory_dump_manager.h"
34 #include "base/trace_event/trace_event.h"
35 #include "build/build_config.h"
36 #include "content/child/child_thread_impl.h"
37 #include "content/common/child_process.mojom.h"
38 #include "content/public/common/content_client.h"
39 #include "content/public/common/content_features.h"
40 #include "content/public/common/content_switches.h"
41 #include "content/public/common/url_utils.h"
42 #include "mojo/public/cpp/bindings/shared_remote.h"
43 #include "net/base/net_errors.h"
44 #include "services/network/public/cpp/features.h"
45 #include "third_party/blink/public/common/features.h"
46 #include "third_party/blink/public/common/thread_safe_browser_interface_broker_proxy.h"
47 #include "third_party/blink/public/platform/user_metrics_action.h"
48 #include "third_party/blink/public/platform/web_data.h"
49 #include "third_party/blink/public/platform/web_security_origin.h"
50 #include "third_party/blink/public/platform/web_string.h"
51 #include "third_party/blink/public/platform/web_url.h"
52 #include "third_party/blink/public/resources/grit/blink_image_resources.h"
53 #include "third_party/blink/public/resources/grit/blink_resources.h"
54 #include "third_party/blink/public/strings/grit/blink_strings.h"
55 #include "ui/base/resource/resource_scale_factor.h"
56 #include "ui/base/ui_base_features.h"
57 #include "ui/events/gestures/blink/web_gesture_curve_impl.h"
60 using blink::WebString;
62 using blink::WebURLError;
67 // This must match third_party/WebKit/public/blink_resources.grd.
71 ui::ResourceScaleFactor scale_factor;
74 class NestedMessageLoopRunnerImpl
75 : public blink::Platform::NestedMessageLoopRunner {
77 NestedMessageLoopRunnerImpl() = default;
79 ~NestedMessageLoopRunnerImpl() override {
80 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
84 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
85 base::RunLoop* const previous_run_loop = run_loop_;
86 base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
87 run_loop_ = &run_loop;
89 run_loop_ = previous_run_loop;
92 void QuitNow() override {
93 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
99 raw_ptr<base::RunLoop> run_loop_ = nullptr;
101 SEQUENCE_CHECKER(sequence_checker_);
104 mojo::SharedRemote<mojom::ChildProcessHost> GetChildProcessHost() {
105 auto* thread = ChildThreadImpl::current();
107 return thread->child_process_host();
111 // An implementation of BrowserInterfaceBroker which forwards to the
112 // ChildProcessHost interface. This lives on the IO thread.
113 class ThreadSafeBrowserInterfaceBrokerProxyImpl
114 : public blink::ThreadSafeBrowserInterfaceBrokerProxy {
116 ThreadSafeBrowserInterfaceBrokerProxyImpl()
117 : process_host_(GetChildProcessHost()) {}
119 ThreadSafeBrowserInterfaceBrokerProxyImpl(
120 const ThreadSafeBrowserInterfaceBrokerProxyImpl&) = delete;
121 ThreadSafeBrowserInterfaceBrokerProxyImpl& operator=(
122 const ThreadSafeBrowserInterfaceBrokerProxyImpl&) = delete;
124 // blink::ThreadSafeBrowserInterfaceBrokerProxy implementation:
125 void GetInterfaceImpl(mojo::GenericPendingReceiver receiver) override {
127 process_host_->BindHostReceiver(std::move(receiver));
131 ~ThreadSafeBrowserInterfaceBrokerProxyImpl() override = default;
133 const mojo::SharedRemote<mojom::ChildProcessHost> process_host_;
138 // TODO(skyostil): Ensure that we always have an active task runner when
139 // constructing the platform.
140 BlinkPlatformImpl::BlinkPlatformImpl() : BlinkPlatformImpl(nullptr) {}
142 BlinkPlatformImpl::BlinkPlatformImpl(
143 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner)
144 : io_thread_task_runner_(std::move(io_thread_task_runner)),
145 media_stream_video_source_video_task_runner_(
146 base::FeatureList::IsEnabled(
147 blink::features::kUseThreadPoolForMediaStreamVideoTaskRunner)
148 ? base::ThreadPool::CreateSequencedTaskRunner(base::TaskTraits{})
149 : io_thread_task_runner_),
150 browser_interface_broker_proxy_(
151 base::MakeRefCounted<ThreadSafeBrowserInterfaceBrokerProxyImpl>()) {}
153 BlinkPlatformImpl::~BlinkPlatformImpl() = default;
155 void BlinkPlatformImpl::RecordAction(const blink::UserMetricsAction& name) {
156 if (ChildThread* child_thread = ChildThread::Get())
157 child_thread->RecordComputedAction(name.Action());
160 WebData BlinkPlatformImpl::GetDataResource(
162 ui::ResourceScaleFactor scale_factor) {
163 base::StringPiece resource =
164 GetContentClient()->GetDataResource(resource_id, scale_factor);
165 return WebData(resource.data(), resource.size());
168 std::string BlinkPlatformImpl::GetDataResourceString(int resource_id) {
169 return GetContentClient()->GetDataResourceString(resource_id);
172 WebString BlinkPlatformImpl::QueryLocalizedString(int resource_id) {
175 return WebString::FromUTF16(
176 GetContentClient()->GetLocalizedString(resource_id));
179 WebString BlinkPlatformImpl::QueryLocalizedString(int resource_id,
180 const WebString& value) {
184 std::u16string format_string =
185 GetContentClient()->GetLocalizedString(resource_id);
187 // If the ContentClient returned an empty string, e.g. because it's using the
188 // default implementation of ContentClient::GetLocalizedString, return an
189 // empty string instead of crashing with a failed DCHECK in
190 // base::ReplaceStringPlaceholders below. This is useful for tests that don't
191 // specialize a full ContentClient, since this way they can behave as though
192 // there isn't a defined |resource_id| for the |name| instead of crashing
194 if (format_string.empty())
197 return WebString::FromUTF16(
198 base::ReplaceStringPlaceholders(format_string, value.Utf16(), nullptr));
201 WebString BlinkPlatformImpl::QueryLocalizedString(int resource_id,
202 const WebString& value1,
203 const WebString& value2) {
206 std::vector<std::u16string> values;
208 values.push_back(value1.Utf16());
209 values.push_back(value2.Utf16());
210 return WebString::FromUTF16(base::ReplaceStringPlaceholders(
211 GetContentClient()->GetLocalizedString(resource_id), values, nullptr));
214 blink::WebCrypto* BlinkPlatformImpl::Crypto() {
218 blink::ThreadSafeBrowserInterfaceBrokerProxy*
219 BlinkPlatformImpl::GetBrowserInterfaceBroker() {
220 return browser_interface_broker_proxy_.get();
223 bool BlinkPlatformImpl::IsURLSavableForSavableResource(
224 const blink::WebURL& url) {
225 return IsSavableURL(url);
228 size_t BlinkPlatformImpl::MaxDecodedImageBytes() {
229 const int kMB = 1024 * 1024;
230 const int kMaxNumberOfBytesPerPixel = 4;
231 #if BUILDFLAG(IS_ANDROID)
232 if (base::SysInfo::IsLowEndDevice()) {
233 // Limit image decoded size to 3M pixels on low end devices.
234 // 4 is maximum number of bytes per pixel.
235 return 3 * kMB * kMaxNumberOfBytesPerPixel;
237 // For other devices, limit decoded image size based on the amount of physical
239 // In some cases all physical memory is not accessible by Chromium, as it can
240 // be reserved for direct use by certain hardware. Thus, we set the limit so
241 // that 1.6GB of reported physical memory on a 2GB device is enough to set the
242 // limit at 16M pixels, which is a desirable value since 4K*4K is a relatively
243 // common texture size.
244 return base::SysInfo::AmountOfPhysicalMemory() / 25;
246 size_t max_decoded_image_byte_limit = kNoDecodedImageByteLimit;
247 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
248 if (command_line.HasSwitch(switches::kMaxDecodedImageSizeMb)) {
249 if (base::StringToSizeT(
250 command_line.GetSwitchValueASCII(switches::kMaxDecodedImageSizeMb),
251 &max_decoded_image_byte_limit)) {
252 max_decoded_image_byte_limit *= kMB * kMaxNumberOfBytesPerPixel;
255 return max_decoded_image_byte_limit;
259 bool BlinkPlatformImpl::IsLowEndDevice() {
260 // This value is static for performance because calculating it is non-trivial.
261 static bool is_low_end_device = base::SysInfo::IsLowEndDevice();
262 return is_low_end_device;
265 scoped_refptr<base::SingleThreadTaskRunner> BlinkPlatformImpl::GetIOTaskRunner()
267 return io_thread_task_runner_;
270 scoped_refptr<base::SequencedTaskRunner>
271 BlinkPlatformImpl::GetMediaStreamVideoSourceVideoTaskRunner() const {
272 return media_stream_video_source_video_task_runner_;
275 std::unique_ptr<blink::Platform::NestedMessageLoopRunner>
276 BlinkPlatformImpl::CreateNestedMessageLoopRunner() const {
277 return std::make_unique<NestedMessageLoopRunnerImpl>();
280 } // namespace content