OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "mojo/examples/html_viewer/blink_platform_impl.h" | |
6 | |
7 #include <math.h> | |
jamesr
2014/06/20 05:58:07
do we do <cmath> ?
| |
8 | |
9 #include "base/rand_util.h" | |
10 #include "base/stl_util.h" | |
11 #include "base/synchronization/waitable_event.h" | |
12 #include "base/time/time.h" | |
13 #include "mojo/examples/html_viewer/webthread_impl.h" | |
14 #include "mojo/examples/html_viewer/weburlloader_impl.h" | |
15 #include "mojo/public/cpp/application/application.h" | |
16 #include "net/base/data_url.h" | |
17 #include "net/base/mime_util.h" | |
18 #include "net/base/net_errors.h" | |
19 #include "third_party/WebKit/public/platform/WebWaitableEvent.h" | |
20 | |
21 namespace mojo { | |
22 namespace examples { | |
23 namespace { | |
24 | |
25 // TODO(darin): Figure out what our UA should really be. | |
26 const char kUserAgentString[] = | |
27 "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " | |
28 "Chrome/35.0.1916.153 Safari/537.36"; | |
29 | |
30 class WebWaitableEventImpl : public blink::WebWaitableEvent { | |
31 public: | |
32 WebWaitableEventImpl() : impl_(new base::WaitableEvent(false, false)) {} | |
33 virtual ~WebWaitableEventImpl() {} | |
34 | |
35 virtual void wait() { impl_->Wait(); } | |
36 virtual void signal() { impl_->Signal(); } | |
37 | |
38 base::WaitableEvent* impl() { | |
39 return impl_.get(); | |
40 } | |
41 | |
42 private: | |
43 scoped_ptr<base::WaitableEvent> impl_; | |
44 DISALLOW_COPY_AND_ASSIGN(WebWaitableEventImpl); | |
45 }; | |
46 | |
47 } // namespace | |
48 | |
49 BlinkPlatformImpl::BlinkPlatformImpl(Application* app) | |
50 : main_loop_(base::MessageLoop::current()), | |
51 shared_timer_func_(NULL), | |
52 shared_timer_fire_time_(0.0), | |
53 shared_timer_fire_time_was_set_while_suspended_(false), | |
54 shared_timer_suspended_(0), | |
55 current_thread_slot_(&DestroyCurrentThread) { | |
56 app->ConnectTo("mojo:mojo_network_service", &network_service_); | |
57 } | |
58 | |
59 blink::WebMimeRegistry* BlinkPlatformImpl::mimeRegistry() { | |
60 return &mime_registry_; | |
61 } | |
62 | |
63 blink::WebThemeEngine* BlinkPlatformImpl::themeEngine() { | |
64 return &dummy_theme_engine_; | |
65 } | |
66 | |
67 blink::WebString BlinkPlatformImpl::defaultLocale() { | |
68 return blink::WebString::fromUTF8("en-US"); | |
69 } | |
70 | |
71 double BlinkPlatformImpl::currentTime() { | |
72 return base::Time::Now().ToDoubleT(); | |
73 } | |
74 | |
75 double BlinkPlatformImpl::monotonicallyIncreasingTime() { | |
76 return base::TimeTicks::Now().ToInternalValue() / | |
77 static_cast<double>(base::Time::kMicrosecondsPerSecond); | |
78 } | |
79 | |
80 void BlinkPlatformImpl::cryptographicallyRandomValues(unsigned char* buffer, | |
81 size_t length) { | |
82 base::RandBytes(buffer, length); | |
83 } | |
84 | |
85 void BlinkPlatformImpl::setSharedTimerFiredFunction(void (*func)()) { | |
86 shared_timer_func_ = func; | |
87 } | |
88 | |
89 void BlinkPlatformImpl::setSharedTimerFireInterval( | |
90 double interval_seconds) { | |
91 shared_timer_fire_time_ = interval_seconds + monotonicallyIncreasingTime(); | |
92 if (shared_timer_suspended_) { | |
93 shared_timer_fire_time_was_set_while_suspended_ = true; | |
94 return; | |
95 } | |
96 | |
97 // By converting between double and int64 representation, we run the risk | |
98 // of losing precision due to rounding errors. Performing computations in | |
99 // microseconds reduces this risk somewhat. But there still is the potential | |
100 // of us computing a fire time for the timer that is shorter than what we | |
101 // need. | |
102 // As the event loop will check event deadlines prior to actually firing | |
103 // them, there is a risk of needlessly rescheduling events and of | |
104 // needlessly looping if sleep times are too short even by small amounts. | |
105 // This results in measurable performance degradation unless we use ceil() to | |
106 // always round up the sleep times. | |
107 int64 interval = static_cast<int64>( | |
108 ceil(interval_seconds * base::Time::kMillisecondsPerSecond) | |
109 * base::Time::kMicrosecondsPerMillisecond); | |
110 | |
111 if (interval < 0) | |
112 interval = 0; | |
113 | |
114 shared_timer_.Stop(); | |
115 shared_timer_.Start(FROM_HERE, base::TimeDelta::FromMicroseconds(interval), | |
116 this, &BlinkPlatformImpl::DoTimeout); | |
117 } | |
118 | |
119 void BlinkPlatformImpl::stopSharedTimer() { | |
120 shared_timer_.Stop(); | |
121 } | |
122 | |
123 void BlinkPlatformImpl::callOnMainThread( | |
124 void (*func)(void*), void* context) { | |
125 main_loop_->PostTask(FROM_HERE, base::Bind(func, context)); | |
126 } | |
127 | |
128 const unsigned char* BlinkPlatformImpl::getTraceCategoryEnabledFlag( | |
129 const char* category_name) { | |
130 static const unsigned char buf[] = "*"; | |
131 return buf; | |
132 } | |
133 | |
134 blink::WebURLLoader* BlinkPlatformImpl::createURLLoader() { | |
135 return new WebURLLoaderImpl(network_service_.get()); | |
136 } | |
137 | |
138 blink::WebString BlinkPlatformImpl::userAgent() { | |
139 return blink::WebString::fromUTF8(kUserAgentString); | |
140 } | |
141 | |
142 blink::WebData BlinkPlatformImpl::parseDataURL( | |
143 const blink::WebURL& url, | |
144 blink::WebString& mimetype_out, | |
145 blink::WebString& charset_out) { | |
146 std::string mimetype, charset, data; | |
147 if (net::DataURL::Parse(url, &mimetype, &charset, &data) | |
148 && net::IsSupportedMimeType(mimetype)) { | |
149 mimetype_out = blink::WebString::fromUTF8(mimetype); | |
150 charset_out = blink::WebString::fromUTF8(charset); | |
151 return data; | |
152 } | |
153 return blink::WebData(); | |
154 } | |
155 | |
156 blink::WebURLError BlinkPlatformImpl::cancelledError(const blink::WebURL& url) | |
157 const { | |
158 blink::WebURLError error; | |
159 error.domain = blink::WebString::fromUTF8(net::kErrorDomain); | |
160 error.reason = net::ERR_ABORTED; | |
161 error.unreachableURL = url; | |
162 error.staleCopyInCache = false; | |
163 error.isCancellation = true; | |
164 return error; | |
165 } | |
166 | |
167 blink::WebThread* BlinkPlatformImpl::createThread(const char* name) { | |
168 return new WebThreadImpl(name); | |
169 } | |
170 | |
171 blink::WebThread* BlinkPlatformImpl::currentThread() { | |
172 WebThreadImplForMessageLoop* thread = | |
173 static_cast<WebThreadImplForMessageLoop*>(current_thread_slot_.Get()); | |
174 if (thread) | |
175 return (thread); | |
176 | |
177 scoped_refptr<base::MessageLoopProxy> message_loop = | |
178 base::MessageLoopProxy::current(); | |
179 if (!message_loop.get()) | |
180 return NULL; | |
181 | |
182 thread = new WebThreadImplForMessageLoop(message_loop.get()); | |
183 current_thread_slot_.Set(thread); | |
184 return thread; | |
185 } | |
186 | |
187 blink::WebWaitableEvent* BlinkPlatformImpl::createWaitableEvent() { | |
188 return new WebWaitableEventImpl(); | |
189 } | |
190 | |
191 blink::WebWaitableEvent* BlinkPlatformImpl::waitMultipleEvents( | |
192 const blink::WebVector<blink::WebWaitableEvent*>& web_events) { | |
193 std::vector<base::WaitableEvent*> events; | |
194 for (size_t i = 0; i < web_events.size(); ++i) | |
195 events.push_back(static_cast<WebWaitableEventImpl*>(web_events[i])->impl()); | |
196 size_t idx = base::WaitableEvent::WaitMany( | |
197 vector_as_array(&events), events.size()); | |
198 DCHECK_LT(idx, web_events.size()); | |
199 return web_events[idx]; | |
200 } | |
201 | |
202 // static | |
203 void BlinkPlatformImpl::DestroyCurrentThread(void* thread) { | |
204 WebThreadImplForMessageLoop* impl = | |
205 static_cast<WebThreadImplForMessageLoop*>(thread); | |
206 delete impl; | |
207 } | |
208 | |
209 } // namespace examples | |
210 } // namespace mojo | |
OLD | NEW |