Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / components / html_viewer / blink_platform_impl.cc
blob560a4f51427b4070b6a716a82ad7a832dca11c9e
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/html_viewer/blink_platform_impl.h"
7 #include <cmath>
9 #include "base/command_line.h"
10 #include "base/rand_util.h"
11 #include "base/stl_util.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/threading/platform_thread.h"
15 #include "base/time/time.h"
16 #include "base/trace_event/trace_event.h"
17 #include "components/html_viewer/blink_resource_constants.h"
18 #include "components/html_viewer/web_clipboard_impl.h"
19 #include "components/html_viewer/web_cookie_jar_impl.h"
20 #include "components/html_viewer/web_graphics_context_3d_command_buffer_impl.h"
21 #include "components/html_viewer/web_socket_handle_impl.h"
22 #include "components/html_viewer/web_url_loader_impl.h"
23 #include "components/message_port/web_message_port_channel_impl.h"
24 #include "components/mime_util/mime_util.h"
25 #include "components/scheduler/child/webthread_impl_for_worker_scheduler.h"
26 #include "components/scheduler/renderer/renderer_scheduler.h"
27 #include "components/scheduler/renderer/webthread_impl_for_renderer_scheduler.h"
28 #include "mojo/application/public/cpp/application_impl.h"
29 #include "mojo/application/public/cpp/connect.h"
30 #include "mojo/common/user_agent.h"
31 #include "net/base/data_url.h"
32 #include "net/base/ip_address_number.h"
33 #include "net/base/net_errors.h"
34 #include "net/base/net_util.h"
35 #include "third_party/WebKit/public/platform/WebWaitableEvent.h"
36 #include "ui/base/resource/resource_bundle.h"
37 #include "ui/events/gestures/blink/web_gesture_curve_impl.h"
38 #include "url/gurl.h"
40 namespace html_viewer {
41 namespace {
43 // Allows overriding user agent scring.
44 const char kUserAgentSwitch[] = "user-agent";
46 class WebWaitableEventImpl : public blink::WebWaitableEvent {
47 public:
48 WebWaitableEventImpl(ResetPolicy policy, InitialState state) {
49 bool manual_reset = policy == ResetPolicy::Manual;
50 bool initially_signaled = state == InitialState::Signaled;
51 impl_.reset(new base::WaitableEvent(manual_reset, initially_signaled));
53 virtual ~WebWaitableEventImpl() {}
55 virtual void reset() { impl_->Reset(); }
56 virtual void wait() { impl_->Wait(); }
57 virtual void signal() { impl_->Signal(); }
59 base::WaitableEvent* impl() {
60 return impl_.get();
63 private:
64 scoped_ptr<base::WaitableEvent> impl_;
65 DISALLOW_COPY_AND_ASSIGN(WebWaitableEventImpl);
68 } // namespace
70 BlinkPlatformImpl::BlinkPlatformImpl(
71 mojo::ApplicationImpl* app,
72 scheduler::RendererScheduler* renderer_scheduler)
73 : app_(app),
74 main_thread_task_runner_(renderer_scheduler->DefaultTaskRunner()),
75 main_thread_(new scheduler::WebThreadImplForRendererScheduler(
76 renderer_scheduler)) {
77 if (app) {
78 mojo::URLRequestPtr request(mojo::URLRequest::New());
79 request->url = mojo::String::From("mojo:network_service");
80 scoped_ptr<mojo::ApplicationConnection> connection =
81 app->ConnectToApplication(request.Pass());
82 connection->ConnectToService(&web_socket_factory_);
83 connection->ConnectToService(&url_loader_factory_);
85 mojo::CookieStorePtr cookie_store;
86 connection->ConnectToService(&cookie_store);
87 cookie_jar_.reset(new WebCookieJarImpl(cookie_store.Pass()));
89 mojo::ClipboardPtr clipboard;
90 mojo::URLRequestPtr request2(mojo::URLRequest::New());
91 request2->url = mojo::String::From("mojo:clipboard");
92 app->ConnectToService(request2.Pass(), &clipboard);
93 clipboard_.reset(new WebClipboardImpl(clipboard.Pass()));
97 BlinkPlatformImpl::~BlinkPlatformImpl() {
100 blink::WebCookieJar* BlinkPlatformImpl::cookieJar() {
101 return cookie_jar_.get();
104 blink::WebClipboard* BlinkPlatformImpl::clipboard() {
105 return clipboard_.get();
108 blink::WebMimeRegistry* BlinkPlatformImpl::mimeRegistry() {
109 return &mime_registry_;
112 blink::WebThemeEngine* BlinkPlatformImpl::themeEngine() {
113 return &theme_engine_;
116 blink::WebString BlinkPlatformImpl::defaultLocale() {
117 return blink::WebString::fromUTF8("en-US");
120 blink::WebBlobRegistry* BlinkPlatformImpl::blobRegistry() {
121 return &blob_registry_;
124 double BlinkPlatformImpl::currentTime() {
125 return base::Time::Now().ToDoubleT();
128 double BlinkPlatformImpl::monotonicallyIncreasingTime() {
129 return base::TimeTicks::Now().ToInternalValue() /
130 static_cast<double>(base::Time::kMicrosecondsPerSecond);
133 void BlinkPlatformImpl::cryptographicallyRandomValues(unsigned char* buffer,
134 size_t length) {
135 base::RandBytes(buffer, length);
138 bool BlinkPlatformImpl::isThreadedCompositingEnabled() {
139 return true;
142 blink::WebCompositorSupport* BlinkPlatformImpl::compositorSupport() {
143 return &compositor_support_;
146 uint32_t BlinkPlatformImpl::getUniqueIdForProcess() {
147 // TODO(rickyz): Replace this with base::GetUniqueIdForProcess when that's
148 // ready.
149 return base::trace_event::TraceLog::GetInstance()->process_id();
152 void BlinkPlatformImpl::createMessageChannel(
153 blink::WebMessagePortChannel** channel1,
154 blink::WebMessagePortChannel** channel2) {
155 message_port::WebMessagePortChannelImpl::CreatePair(channel1, channel2);
158 blink::WebScrollbarBehavior* BlinkPlatformImpl::scrollbarBehavior() {
159 return &scrollbar_behavior_;
162 const unsigned char* BlinkPlatformImpl::getTraceCategoryEnabledFlag(
163 const char* category_name) {
164 static const unsigned char buf[] = "*";
165 return buf;
168 blink::WebGraphicsContext3D*
169 BlinkPlatformImpl::createOffscreenGraphicsContext3D(
170 const blink::WebGraphicsContext3D::Attributes& attributes,
171 blink::WebGraphicsContext3D* share_context) {
172 return createOffscreenGraphicsContext3D(attributes, share_context, nullptr);
175 blink::WebGraphicsContext3D*
176 BlinkPlatformImpl::createOffscreenGraphicsContext3D(
177 const blink::WebGraphicsContext3D::Attributes& attributes,
178 blink::WebGraphicsContext3D* share_context,
179 blink::WebGLInfo* gl_info) {
180 return WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
181 app_, GURL(attributes.topDocumentURL), attributes, share_context,
182 gl_info);
185 blink::WebGraphicsContext3D*
186 BlinkPlatformImpl::createOffscreenGraphicsContext3D(
187 const blink::WebGraphicsContext3D::Attributes& attributes) {
188 return createOffscreenGraphicsContext3D(attributes, nullptr, nullptr);
191 blink::WebGraphicsContext3DProvider*
192 BlinkPlatformImpl::createSharedOffscreenGraphicsContext3DProvider() {
193 return nullptr;
196 blink::WebData BlinkPlatformImpl::loadResource(const char* resource) {
197 for (size_t i = 0; i < arraysize(kDataResources); ++i) {
198 if (!strcmp(resource, kDataResources[i].name)) {
199 base::StringPiece data =
200 ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
201 kDataResources[i].id, ui::SCALE_FACTOR_100P);
202 return blink::WebData(data.data(), data.size());
205 NOTREACHED() << "Requested resource is unavailable: " << resource;
206 return blink::WebData();
209 blink::WebURLLoader* BlinkPlatformImpl::createURLLoader() {
210 return new WebURLLoaderImpl(url_loader_factory_.get(), &blob_registry_);
213 blink::WebSocketHandle* BlinkPlatformImpl::createWebSocketHandle() {
214 return new WebSocketHandleImpl(web_socket_factory_.get());
217 blink::WebString BlinkPlatformImpl::userAgent() {
218 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
219 if (command_line->HasSwitch(kUserAgentSwitch)) {
220 return blink::WebString::fromUTF8(
221 command_line->GetSwitchValueASCII(kUserAgentSwitch));
223 return blink::WebString::fromUTF8(mojo::common::GetUserAgent());
226 blink::WebData BlinkPlatformImpl::parseDataURL(
227 const blink::WebURL& url,
228 blink::WebString& mimetype_out,
229 blink::WebString& charset_out) {
230 std::string mimetype, charset, data;
231 if (net::DataURL::Parse(url, &mimetype, &charset, &data) &&
232 mime_util::IsSupportedMimeType(mimetype)) {
233 mimetype_out = blink::WebString::fromUTF8(mimetype);
234 charset_out = blink::WebString::fromUTF8(charset);
235 return data;
237 return blink::WebData();
240 blink::WebURLError BlinkPlatformImpl::cancelledError(const blink::WebURL& url)
241 const {
242 blink::WebURLError error;
243 error.domain = blink::WebString::fromUTF8(net::kErrorDomain);
244 error.reason = net::ERR_ABORTED;
245 error.unreachableURL = url;
246 error.staleCopyInCache = false;
247 error.isCancellation = true;
248 return error;
251 bool BlinkPlatformImpl::isReservedIPAddress(
252 const blink::WebString& host) const {
253 net::IPAddressNumber address;
254 if (!net::ParseURLHostnameToNumber(host.utf8(), &address))
255 return false;
256 return net::IsIPAddressReserved(address);
259 blink::WebThread* BlinkPlatformImpl::createThread(const char* name) {
260 scheduler::WebThreadImplForWorkerScheduler* thread =
261 new scheduler::WebThreadImplForWorkerScheduler(name);
262 thread->TaskRunner()->PostTask(
263 FROM_HERE, base::Bind(&BlinkPlatformImpl::UpdateWebThreadTLS,
264 base::Unretained(this), thread));
265 return thread;
268 blink::WebThread* BlinkPlatformImpl::currentThread() {
269 if (main_thread_->isCurrentThread())
270 return main_thread_.get();
271 return static_cast<blink::WebThread*>(current_thread_slot_.Get());
274 void BlinkPlatformImpl::yieldCurrentThread() {
275 base::PlatformThread::YieldCurrentThread();
278 blink::WebWaitableEvent* BlinkPlatformImpl::createWaitableEvent(
279 blink::WebWaitableEvent::ResetPolicy policy,
280 blink::WebWaitableEvent::InitialState state) {
281 return new WebWaitableEventImpl(policy, state);
284 blink::WebWaitableEvent* BlinkPlatformImpl::waitMultipleEvents(
285 const blink::WebVector<blink::WebWaitableEvent*>& web_events) {
286 std::vector<base::WaitableEvent*> events;
287 for (size_t i = 0; i < web_events.size(); ++i)
288 events.push_back(static_cast<WebWaitableEventImpl*>(web_events[i])->impl());
289 size_t idx = base::WaitableEvent::WaitMany(
290 vector_as_array(&events), events.size());
291 DCHECK_LT(idx, web_events.size());
292 return web_events[idx];
295 blink::WebGestureCurve* BlinkPlatformImpl::createFlingAnimationCurve(
296 blink::WebGestureDevice device_source,
297 const blink::WebFloatPoint& velocity,
298 const blink::WebSize& cumulative_scroll) {
299 const bool is_main_thread = true;
300 return ui::WebGestureCurveImpl::CreateFromDefaultPlatformCurve(
301 gfx::Vector2dF(velocity.x, velocity.y),
302 gfx::Vector2dF(cumulative_scroll.width, cumulative_scroll.height),
303 is_main_thread).release();
306 blink::WebCrypto* BlinkPlatformImpl::crypto() {
307 return &web_crypto_;
310 blink::WebNotificationManager*
311 BlinkPlatformImpl::notificationManager() {
312 return &web_notification_manager_;
315 void BlinkPlatformImpl::UpdateWebThreadTLS(blink::WebThread* thread) {
316 DCHECK(!current_thread_slot_.Get());
317 current_thread_slot_.Set(thread);
320 } // namespace html_viewer