[Autofill] remove *MultiInfo* functions
[chromium-blink-merge.git] / components / html_viewer / blink_platform_impl.cc
blob7c37000e818f2954a3ab521608d0089ab1047af0
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 "components/html_viewer/blink_resource_constants.h"
17 #include "components/html_viewer/web_clipboard_impl.h"
18 #include "components/html_viewer/web_cookie_jar_impl.h"
19 #include "components/html_viewer/web_message_port_channel_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/mime_util/mime_util.h"
23 #include "components/scheduler/child/webthread_impl_for_worker_scheduler.h"
24 #include "components/scheduler/renderer/renderer_scheduler.h"
25 #include "components/scheduler/renderer/webthread_impl_for_renderer_scheduler.h"
26 #include "mojo/application/public/cpp/application_impl.h"
27 #include "mojo/application/public/cpp/connect.h"
28 #include "mojo/common/user_agent.h"
29 #include "net/base/data_url.h"
30 #include "net/base/ip_address_number.h"
31 #include "net/base/net_errors.h"
32 #include "net/base/net_util.h"
33 #include "third_party/WebKit/public/platform/WebWaitableEvent.h"
34 #include "ui/events/gestures/blink/web_gesture_curve_impl.h"
36 namespace html_viewer {
37 namespace {
39 // Allows overriding user agent scring.
40 const char kUserAgentSwitch[] = "user-agent";
42 class WebWaitableEventImpl : public blink::WebWaitableEvent {
43 public:
44 WebWaitableEventImpl() : impl_(new base::WaitableEvent(false, false)) {}
45 ~WebWaitableEventImpl() override {}
47 void wait() override { impl_->Wait(); }
48 void signal() override { impl_->Signal(); }
50 base::WaitableEvent* impl() {
51 return impl_.get();
54 private:
55 scoped_ptr<base::WaitableEvent> impl_;
56 DISALLOW_COPY_AND_ASSIGN(WebWaitableEventImpl);
59 } // namespace
61 BlinkPlatformImpl::BlinkPlatformImpl(
62 mojo::ApplicationImpl* app,
63 scheduler::RendererScheduler* renderer_scheduler)
64 : main_thread_task_runner_(renderer_scheduler->DefaultTaskRunner()),
65 main_thread_(
66 new scheduler::WebThreadImplForRendererScheduler(renderer_scheduler)),
67 shared_timer_func_(NULL),
68 shared_timer_fire_time_(0.0),
69 shared_timer_fire_time_was_set_while_suspended_(false),
70 shared_timer_suspended_(0) {
71 if (app) {
72 mojo::URLRequestPtr request(mojo::URLRequest::New());
73 request->url = mojo::String::From("mojo:network_service");
74 mojo::ApplicationConnection* connection =
75 app->ConnectToApplication(request.Pass());
76 connection->ConnectToService(&network_service_);
77 connection->ConnectToService(&url_loader_factory_);
79 mojo::CookieStorePtr cookie_store;
80 network_service_->GetCookieStore(GetProxy(&cookie_store));
81 cookie_jar_.reset(new WebCookieJarImpl(cookie_store.Pass()));
83 mojo::ClipboardPtr clipboard;
84 mojo::URLRequestPtr request2(mojo::URLRequest::New());
85 request2->url = mojo::String::From("mojo:clipboard");
86 app->ConnectToService(request2.Pass(), &clipboard);
87 clipboard_.reset(new WebClipboardImpl(clipboard.Pass()));
89 shared_timer_.SetTaskRunner(main_thread_task_runner_);
92 BlinkPlatformImpl::~BlinkPlatformImpl() {
95 blink::WebCookieJar* BlinkPlatformImpl::cookieJar() {
96 return cookie_jar_.get();
99 blink::WebClipboard* BlinkPlatformImpl::clipboard() {
100 return clipboard_.get();
103 blink::WebMimeRegistry* BlinkPlatformImpl::mimeRegistry() {
104 return &mime_registry_;
107 blink::WebThemeEngine* BlinkPlatformImpl::themeEngine() {
108 return &theme_engine_;
111 blink::WebString BlinkPlatformImpl::defaultLocale() {
112 return blink::WebString::fromUTF8("en-US");
115 blink::WebBlobRegistry* BlinkPlatformImpl::blobRegistry() {
116 return &blob_registry_;
119 double BlinkPlatformImpl::currentTime() {
120 return base::Time::Now().ToDoubleT();
123 double BlinkPlatformImpl::monotonicallyIncreasingTime() {
124 return base::TimeTicks::Now().ToInternalValue() /
125 static_cast<double>(base::Time::kMicrosecondsPerSecond);
128 void BlinkPlatformImpl::cryptographicallyRandomValues(unsigned char* buffer,
129 size_t length) {
130 base::RandBytes(buffer, length);
133 void BlinkPlatformImpl::setSharedTimerFiredFunction(void (*func)()) {
134 shared_timer_func_ = func;
137 void BlinkPlatformImpl::setSharedTimerFireInterval(
138 double interval_seconds) {
139 shared_timer_fire_time_ = interval_seconds + monotonicallyIncreasingTime();
140 if (shared_timer_suspended_) {
141 shared_timer_fire_time_was_set_while_suspended_ = true;
142 return;
145 // By converting between double and int64 representation, we run the risk
146 // of losing precision due to rounding errors. Performing computations in
147 // microseconds reduces this risk somewhat. But there still is the potential
148 // of us computing a fire time for the timer that is shorter than what we
149 // need.
150 // As the event loop will check event deadlines prior to actually firing
151 // them, there is a risk of needlessly rescheduling events and of
152 // needlessly looping if sleep times are too short even by small amounts.
153 // This results in measurable performance degradation unless we use ceil() to
154 // always round up the sleep times.
155 int64 interval = static_cast<int64>(
156 ceil(interval_seconds * base::Time::kMillisecondsPerSecond)
157 * base::Time::kMicrosecondsPerMillisecond);
159 if (interval < 0)
160 interval = 0;
162 shared_timer_.Stop();
163 shared_timer_.Start(FROM_HERE, base::TimeDelta::FromMicroseconds(interval),
164 this, &BlinkPlatformImpl::DoTimeout);
167 void BlinkPlatformImpl::stopSharedTimer() {
168 shared_timer_.Stop();
171 bool BlinkPlatformImpl::isThreadedCompositingEnabled() {
172 return true;
175 blink::WebCompositorSupport* BlinkPlatformImpl::compositorSupport() {
176 return &compositor_support_;
179 void BlinkPlatformImpl::createMessageChannel(
180 blink::WebMessagePortChannel** channel1,
181 blink::WebMessagePortChannel** channel2) {
182 WebMessagePortChannelImpl::CreatePair(channel1, channel2);
185 blink::WebScrollbarBehavior* BlinkPlatformImpl::scrollbarBehavior() {
186 return &scrollbar_behavior_;
189 const unsigned char* BlinkPlatformImpl::getTraceCategoryEnabledFlag(
190 const char* category_name) {
191 static const unsigned char buf[] = "*";
192 return buf;
195 blink::WebData BlinkPlatformImpl::loadResource(const char* resource) {
196 for (size_t i = 0; i < arraysize(kDataResources); ++i) {
197 if (!strcmp(resource, kDataResources[i].name)) {
198 int length;
199 const unsigned char* data =
200 blink_resource_map_.GetResource(kDataResources[i].id, &length);
201 CHECK(data != nullptr && length > 0);
202 return blink::WebData(reinterpret_cast<const char*>(data), length);
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(network_service_.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 return new WebWaitableEventImpl();
282 blink::WebWaitableEvent* BlinkPlatformImpl::waitMultipleEvents(
283 const blink::WebVector<blink::WebWaitableEvent*>& web_events) {
284 std::vector<base::WaitableEvent*> events;
285 for (size_t i = 0; i < web_events.size(); ++i)
286 events.push_back(static_cast<WebWaitableEventImpl*>(web_events[i])->impl());
287 size_t idx = base::WaitableEvent::WaitMany(
288 vector_as_array(&events), events.size());
289 DCHECK_LT(idx, web_events.size());
290 return web_events[idx];
293 blink::WebGestureCurve* BlinkPlatformImpl::createFlingAnimationCurve(
294 blink::WebGestureDevice device_source,
295 const blink::WebFloatPoint& velocity,
296 const blink::WebSize& cumulative_scroll) {
297 const bool is_main_thread = true;
298 return ui::WebGestureCurveImpl::CreateFromDefaultPlatformCurve(
299 gfx::Vector2dF(velocity.x, velocity.y),
300 gfx::Vector2dF(cumulative_scroll.width, cumulative_scroll.height),
301 is_main_thread).release();
304 blink::WebCrypto* BlinkPlatformImpl::crypto() {
305 return &web_crypto_;
308 blink::WebNotificationManager*
309 BlinkPlatformImpl::notificationManager() {
310 return &web_notification_manager_;
313 void BlinkPlatformImpl::UpdateWebThreadTLS(blink::WebThread* thread) {
314 DCHECK(!current_thread_slot_.Get());
315 current_thread_slot_.Set(thread);
318 } // namespace html_viewer