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"
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_socket_handle_impl.h"
21 #include "components/html_viewer/web_url_loader_impl.h"
22 #include "components/message_port/web_message_port_channel_impl.h"
23 #include "components/mime_util/mime_util.h"
24 #include "components/scheduler/child/webthread_impl_for_worker_scheduler.h"
25 #include "components/scheduler/renderer/renderer_scheduler.h"
26 #include "components/scheduler/renderer/webthread_impl_for_renderer_scheduler.h"
27 #include "mojo/application/public/cpp/application_impl.h"
28 #include "mojo/application/public/cpp/connect.h"
29 #include "mojo/common/user_agent.h"
30 #include "net/base/data_url.h"
31 #include "net/base/ip_address_number.h"
32 #include "net/base/net_errors.h"
33 #include "net/base/net_util.h"
34 #include "third_party/WebKit/public/platform/WebWaitableEvent.h"
35 #include "ui/events/gestures/blink/web_gesture_curve_impl.h"
37 namespace html_viewer
{
40 // Allows overriding user agent scring.
41 const char kUserAgentSwitch
[] = "user-agent";
43 class WebWaitableEventImpl
: public blink::WebWaitableEvent
{
45 WebWaitableEventImpl() : impl_(new base::WaitableEvent(false, false)) {}
46 ~WebWaitableEventImpl() override
{}
48 void wait() override
{ impl_
->Wait(); }
49 void signal() override
{ impl_
->Signal(); }
51 base::WaitableEvent
* impl() {
56 scoped_ptr
<base::WaitableEvent
> impl_
;
57 DISALLOW_COPY_AND_ASSIGN(WebWaitableEventImpl
);
62 BlinkPlatformImpl::BlinkPlatformImpl(
63 mojo::ApplicationImpl
* app
,
64 scheduler::RendererScheduler
* renderer_scheduler
)
65 : main_thread_task_runner_(renderer_scheduler
->DefaultTaskRunner()),
66 main_thread_(new scheduler::WebThreadImplForRendererScheduler(
67 renderer_scheduler
)) {
69 mojo::URLRequestPtr
request(mojo::URLRequest::New());
70 request
->url
= mojo::String::From("mojo:network_service");
71 mojo::ApplicationConnection
* connection
=
72 app
->ConnectToApplication(request
.Pass());
73 connection
->ConnectToService(&network_service_
);
74 connection
->ConnectToService(&url_loader_factory_
);
76 mojo::CookieStorePtr cookie_store
;
77 network_service_
->GetCookieStore(GetProxy(&cookie_store
));
78 cookie_jar_
.reset(new WebCookieJarImpl(cookie_store
.Pass()));
80 mojo::ClipboardPtr clipboard
;
81 mojo::URLRequestPtr
request2(mojo::URLRequest::New());
82 request2
->url
= mojo::String::From("mojo:clipboard");
83 app
->ConnectToService(request2
.Pass(), &clipboard
);
84 clipboard_
.reset(new WebClipboardImpl(clipboard
.Pass()));
88 BlinkPlatformImpl::~BlinkPlatformImpl() {
91 blink::WebCookieJar
* BlinkPlatformImpl::cookieJar() {
92 return cookie_jar_
.get();
95 blink::WebClipboard
* BlinkPlatformImpl::clipboard() {
96 return clipboard_
.get();
99 blink::WebMimeRegistry
* BlinkPlatformImpl::mimeRegistry() {
100 return &mime_registry_
;
103 blink::WebThemeEngine
* BlinkPlatformImpl::themeEngine() {
104 return &theme_engine_
;
107 blink::WebString
BlinkPlatformImpl::defaultLocale() {
108 return blink::WebString::fromUTF8("en-US");
111 blink::WebBlobRegistry
* BlinkPlatformImpl::blobRegistry() {
112 return &blob_registry_
;
115 double BlinkPlatformImpl::currentTime() {
116 return base::Time::Now().ToDoubleT();
119 double BlinkPlatformImpl::monotonicallyIncreasingTime() {
120 return base::TimeTicks::Now().ToInternalValue() /
121 static_cast<double>(base::Time::kMicrosecondsPerSecond
);
124 void BlinkPlatformImpl::cryptographicallyRandomValues(unsigned char* buffer
,
126 base::RandBytes(buffer
, length
);
129 bool BlinkPlatformImpl::isThreadedCompositingEnabled() {
133 blink::WebCompositorSupport
* BlinkPlatformImpl::compositorSupport() {
134 return &compositor_support_
;
137 uint32_t BlinkPlatformImpl::getUniqueIdForProcess() {
138 // TODO(rickyz): Replace this with base::GetUniqueIdForProcess when that's
140 return base::trace_event::TraceLog::GetInstance()->process_id();
143 void BlinkPlatformImpl::createMessageChannel(
144 blink::WebMessagePortChannel
** channel1
,
145 blink::WebMessagePortChannel
** channel2
) {
146 message_port::WebMessagePortChannelImpl::CreatePair(channel1
, channel2
);
149 blink::WebScrollbarBehavior
* BlinkPlatformImpl::scrollbarBehavior() {
150 return &scrollbar_behavior_
;
153 const unsigned char* BlinkPlatformImpl::getTraceCategoryEnabledFlag(
154 const char* category_name
) {
155 static const unsigned char buf
[] = "*";
159 blink::WebData
BlinkPlatformImpl::loadResource(const char* resource
) {
160 for (size_t i
= 0; i
< arraysize(kDataResources
); ++i
) {
161 if (!strcmp(resource
, kDataResources
[i
].name
)) {
163 const unsigned char* data
=
164 blink_resource_map_
.GetResource(kDataResources
[i
].id
, &length
);
165 CHECK(data
!= nullptr && length
> 0);
166 return blink::WebData(reinterpret_cast<const char*>(data
), length
);
169 NOTREACHED() << "Requested resource is unavailable: " << resource
;
170 return blink::WebData();
173 blink::WebURLLoader
* BlinkPlatformImpl::createURLLoader() {
174 return new WebURLLoaderImpl(url_loader_factory_
.get(), &blob_registry_
);
177 blink::WebSocketHandle
* BlinkPlatformImpl::createWebSocketHandle() {
178 return new WebSocketHandleImpl(network_service_
.get());
181 blink::WebString
BlinkPlatformImpl::userAgent() {
182 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
183 if (command_line
->HasSwitch(kUserAgentSwitch
)) {
184 return blink::WebString::fromUTF8(
185 command_line
->GetSwitchValueASCII(kUserAgentSwitch
));
187 return blink::WebString::fromUTF8(mojo::common::GetUserAgent());
190 blink::WebData
BlinkPlatformImpl::parseDataURL(
191 const blink::WebURL
& url
,
192 blink::WebString
& mimetype_out
,
193 blink::WebString
& charset_out
) {
194 std::string mimetype
, charset
, data
;
195 if (net::DataURL::Parse(url
, &mimetype
, &charset
, &data
) &&
196 mime_util::IsSupportedMimeType(mimetype
)) {
197 mimetype_out
= blink::WebString::fromUTF8(mimetype
);
198 charset_out
= blink::WebString::fromUTF8(charset
);
201 return blink::WebData();
204 blink::WebURLError
BlinkPlatformImpl::cancelledError(const blink::WebURL
& url
)
206 blink::WebURLError error
;
207 error
.domain
= blink::WebString::fromUTF8(net::kErrorDomain
);
208 error
.reason
= net::ERR_ABORTED
;
209 error
.unreachableURL
= url
;
210 error
.staleCopyInCache
= false;
211 error
.isCancellation
= true;
215 bool BlinkPlatformImpl::isReservedIPAddress(
216 const blink::WebString
& host
) const {
217 net::IPAddressNumber address
;
218 if (!net::ParseURLHostnameToNumber(host
.utf8(), &address
))
220 return net::IsIPAddressReserved(address
);
223 blink::WebThread
* BlinkPlatformImpl::createThread(const char* name
) {
224 scheduler::WebThreadImplForWorkerScheduler
* thread
=
225 new scheduler::WebThreadImplForWorkerScheduler(name
);
226 thread
->TaskRunner()->PostTask(
227 FROM_HERE
, base::Bind(&BlinkPlatformImpl::UpdateWebThreadTLS
,
228 base::Unretained(this), thread
));
232 blink::WebThread
* BlinkPlatformImpl::currentThread() {
233 if (main_thread_
->isCurrentThread())
234 return main_thread_
.get();
235 return static_cast<blink::WebThread
*>(current_thread_slot_
.Get());
238 void BlinkPlatformImpl::yieldCurrentThread() {
239 base::PlatformThread::YieldCurrentThread();
242 blink::WebWaitableEvent
* BlinkPlatformImpl::createWaitableEvent() {
243 return new WebWaitableEventImpl();
246 blink::WebWaitableEvent
* BlinkPlatformImpl::waitMultipleEvents(
247 const blink::WebVector
<blink::WebWaitableEvent
*>& web_events
) {
248 std::vector
<base::WaitableEvent
*> events
;
249 for (size_t i
= 0; i
< web_events
.size(); ++i
)
250 events
.push_back(static_cast<WebWaitableEventImpl
*>(web_events
[i
])->impl());
251 size_t idx
= base::WaitableEvent::WaitMany(
252 vector_as_array(&events
), events
.size());
253 DCHECK_LT(idx
, web_events
.size());
254 return web_events
[idx
];
257 blink::WebGestureCurve
* BlinkPlatformImpl::createFlingAnimationCurve(
258 blink::WebGestureDevice device_source
,
259 const blink::WebFloatPoint
& velocity
,
260 const blink::WebSize
& cumulative_scroll
) {
261 const bool is_main_thread
= true;
262 return ui::WebGestureCurveImpl::CreateFromDefaultPlatformCurve(
263 gfx::Vector2dF(velocity
.x
, velocity
.y
),
264 gfx::Vector2dF(cumulative_scroll
.width
, cumulative_scroll
.height
),
265 is_main_thread
).release();
268 blink::WebCrypto
* BlinkPlatformImpl::crypto() {
272 blink::WebNotificationManager
*
273 BlinkPlatformImpl::notificationManager() {
274 return &web_notification_manager_
;
277 void BlinkPlatformImpl::UpdateWebThreadTLS(blink::WebThread
* thread
) {
278 DCHECK(!current_thread_slot_
.Get());
279 current_thread_slot_
.Set(thread
);
282 } // namespace html_viewer