[M120 Migration] Enable input picker for chrome
[platform/framework/web/chromium-efl.git] / content / child / blink_platform_impl.cc
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.
4
5 #include "content/child/blink_platform_impl.h"
6
7 #include <math.h>
8
9 #include <memory>
10 #include <vector>
11
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"
58
59 using blink::WebData;
60 using blink::WebString;
61 using blink::WebURL;
62 using blink::WebURLError;
63
64 namespace content {
65 namespace {
66
67 // This must match third_party/WebKit/public/blink_resources.grd.
68 struct DataResource {
69   const char* name;
70   int id;
71   ui::ResourceScaleFactor scale_factor;
72 };
73
74 class NestedMessageLoopRunnerImpl
75     : public blink::Platform::NestedMessageLoopRunner {
76  public:
77   NestedMessageLoopRunnerImpl() = default;
78
79   ~NestedMessageLoopRunnerImpl() override {
80     DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
81   }
82
83   void Run() override {
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;
88     run_loop.Run();
89     run_loop_ = previous_run_loop;
90   }
91
92   void QuitNow() override {
93     DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
94     DCHECK(run_loop_);
95     run_loop_->Quit();
96   }
97
98  private:
99   raw_ptr<base::RunLoop> run_loop_ = nullptr;
100
101   SEQUENCE_CHECKER(sequence_checker_);
102 };
103
104 mojo::SharedRemote<mojom::ChildProcessHost> GetChildProcessHost() {
105   auto* thread = ChildThreadImpl::current();
106   if (thread)
107     return thread->child_process_host();
108   return {};
109 }
110
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 {
115  public:
116   ThreadSafeBrowserInterfaceBrokerProxyImpl()
117       : process_host_(GetChildProcessHost()) {}
118
119   ThreadSafeBrowserInterfaceBrokerProxyImpl(
120       const ThreadSafeBrowserInterfaceBrokerProxyImpl&) = delete;
121   ThreadSafeBrowserInterfaceBrokerProxyImpl& operator=(
122       const ThreadSafeBrowserInterfaceBrokerProxyImpl&) = delete;
123
124   // blink::ThreadSafeBrowserInterfaceBrokerProxy implementation:
125   void GetInterfaceImpl(mojo::GenericPendingReceiver receiver) override {
126     if (process_host_)
127       process_host_->BindHostReceiver(std::move(receiver));
128   }
129
130  private:
131   ~ThreadSafeBrowserInterfaceBrokerProxyImpl() override = default;
132
133   const mojo::SharedRemote<mojom::ChildProcessHost> process_host_;
134 };
135
136 }  // namespace
137
138 // TODO(skyostil): Ensure that we always have an active task runner when
139 // constructing the platform.
140 BlinkPlatformImpl::BlinkPlatformImpl() : BlinkPlatformImpl(nullptr) {}
141
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>()) {}
152
153 BlinkPlatformImpl::~BlinkPlatformImpl() = default;
154
155 void BlinkPlatformImpl::RecordAction(const blink::UserMetricsAction& name) {
156   if (ChildThread* child_thread = ChildThread::Get())
157     child_thread->RecordComputedAction(name.Action());
158 }
159
160 WebData BlinkPlatformImpl::GetDataResource(
161     int resource_id,
162     ui::ResourceScaleFactor scale_factor) {
163   base::StringPiece resource =
164       GetContentClient()->GetDataResource(resource_id, scale_factor);
165   return WebData(resource.data(), resource.size());
166 }
167
168 std::string BlinkPlatformImpl::GetDataResourceString(int resource_id) {
169   return GetContentClient()->GetDataResourceString(resource_id);
170 }
171
172 WebString BlinkPlatformImpl::QueryLocalizedString(int resource_id) {
173   if (resource_id < 0)
174     return WebString();
175   return WebString::FromUTF16(
176       GetContentClient()->GetLocalizedString(resource_id));
177 }
178
179 WebString BlinkPlatformImpl::QueryLocalizedString(int resource_id,
180                                                   const WebString& value) {
181   if (resource_id < 0)
182     return WebString();
183
184   std::u16string format_string =
185       GetContentClient()->GetLocalizedString(resource_id);
186
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
193   // outright.
194   if (format_string.empty())
195     return WebString();
196
197   return WebString::FromUTF16(
198       base::ReplaceStringPlaceholders(format_string, value.Utf16(), nullptr));
199 }
200
201 WebString BlinkPlatformImpl::QueryLocalizedString(int resource_id,
202                                                   const WebString& value1,
203                                                   const WebString& value2) {
204   if (resource_id < 0)
205     return WebString();
206   std::vector<std::u16string> values;
207   values.reserve(2);
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));
212 }
213
214 blink::WebCrypto* BlinkPlatformImpl::Crypto() {
215   return &web_crypto_;
216 }
217
218 blink::ThreadSafeBrowserInterfaceBrokerProxy*
219 BlinkPlatformImpl::GetBrowserInterfaceBroker() {
220   return browser_interface_broker_proxy_.get();
221 }
222
223 bool BlinkPlatformImpl::IsURLSavableForSavableResource(
224     const blink::WebURL& url) {
225   return IsSavableURL(url);
226 }
227
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;
236   }
237   // For other devices, limit decoded image size based on the amount of physical
238   // memory.
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;
245 #else
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;
253     }
254   }
255   return max_decoded_image_byte_limit;
256 #endif
257 }
258
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;
263 }
264
265 scoped_refptr<base::SingleThreadTaskRunner> BlinkPlatformImpl::GetIOTaskRunner()
266     const {
267   return io_thread_task_runner_;
268 }
269
270 scoped_refptr<base::SequencedTaskRunner>
271 BlinkPlatformImpl::GetMediaStreamVideoSourceVideoTaskRunner() const {
272   return media_stream_video_source_video_task_runner_;
273 }
274
275 std::unique_ptr<blink::Platform::NestedMessageLoopRunner>
276 BlinkPlatformImpl::CreateNestedMessageLoopRunner() const {
277   return std::make_unique<NestedMessageLoopRunnerImpl>();
278 }
279
280 }  // namespace content