| 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 "components/html_viewer/blink_platform_impl.h" | |
| 6 | |
| 7 #include <cmath> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/rand_util.h" | |
| 13 #include "base/synchronization/waitable_event.h" | |
| 14 #include "base/thread_task_runner_handle.h" | |
| 15 #include "base/threading/platform_thread.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "base/trace_event/trace_event.h" | |
| 18 #include "components/html_viewer/blink_resource_constants.h" | |
| 19 #include "components/html_viewer/global_state.h" | |
| 20 #include "components/html_viewer/web_clipboard_impl.h" | |
| 21 #include "components/html_viewer/web_cookie_jar_impl.h" | |
| 22 #include "components/html_viewer/web_graphics_context_3d_command_buffer_impl.h" | |
| 23 #include "components/html_viewer/web_socket_handle_impl.h" | |
| 24 #include "components/html_viewer/web_url_loader_impl.h" | |
| 25 #include "components/message_port/web_message_port_channel_impl.h" | |
| 26 #include "components/mime_util/mime_util.h" | |
| 27 #include "components/scheduler/child/webthread_impl_for_worker_scheduler.h" | |
| 28 #include "components/scheduler/renderer/renderer_scheduler.h" | |
| 29 #include "components/scheduler/renderer/webthread_impl_for_renderer_scheduler.h" | |
| 30 #include "mojo/common/user_agent.h" | |
| 31 #include "mojo/shell/public/cpp/shell.h" | |
| 32 #include "net/base/data_url.h" | |
| 33 #include "net/base/ip_address_number.h" | |
| 34 #include "net/base/net_errors.h" | |
| 35 #include "net/base/net_util.h" | |
| 36 #include "third_party/WebKit/public/platform/URLConversion.h" | |
| 37 #include "third_party/WebKit/public/platform/WebWaitableEvent.h" | |
| 38 #include "ui/base/resource/resource_bundle.h" | |
| 39 #include "ui/events/gestures/blink/web_gesture_curve_impl.h" | |
| 40 #include "url/gurl.h" | |
| 41 | |
| 42 namespace html_viewer { | |
| 43 namespace { | |
| 44 | |
| 45 // Allows overriding user agent scring. | |
| 46 const char kUserAgentSwitch[] = "user-agent"; | |
| 47 | |
| 48 class WebWaitableEventImpl : public blink::WebWaitableEvent { | |
| 49 public: | |
| 50 WebWaitableEventImpl(ResetPolicy policy, InitialState state) { | |
| 51 bool manual_reset = policy == ResetPolicy::Manual; | |
| 52 bool initially_signaled = state == InitialState::Signaled; | |
| 53 impl_.reset(new base::WaitableEvent(manual_reset, initially_signaled)); | |
| 54 } | |
| 55 ~WebWaitableEventImpl() override {} | |
| 56 | |
| 57 void reset() override { impl_->Reset(); } | |
| 58 void wait() override { impl_->Wait(); } | |
| 59 void signal() override { impl_->Signal(); } | |
| 60 | |
| 61 base::WaitableEvent* impl() { | |
| 62 return impl_.get(); | |
| 63 } | |
| 64 | |
| 65 private: | |
| 66 scoped_ptr<base::WaitableEvent> impl_; | |
| 67 DISALLOW_COPY_AND_ASSIGN(WebWaitableEventImpl); | |
| 68 }; | |
| 69 | |
| 70 } // namespace | |
| 71 | |
| 72 BlinkPlatformImpl::BlinkPlatformImpl( | |
| 73 GlobalState* global_state, | |
| 74 mojo::Shell* shell, | |
| 75 scheduler::RendererScheduler* renderer_scheduler) | |
| 76 : global_state_(global_state), | |
| 77 shell_(shell), | |
| 78 main_thread_task_runner_(renderer_scheduler->DefaultTaskRunner()), | |
| 79 main_thread_(renderer_scheduler->CreateMainThread()) { | |
| 80 if (shell) { | |
| 81 scoped_ptr<mojo::Connection> connection = | |
| 82 shell->Connect("mojo:network_service"); | |
| 83 connection->ConnectToService(&web_socket_factory_); | |
| 84 connection->ConnectToService(&url_loader_factory_); | |
| 85 | |
| 86 mojo::CookieStorePtr cookie_store; | |
| 87 connection->ConnectToService(&cookie_store); | |
| 88 cookie_jar_.reset(new WebCookieJarImpl(std::move(cookie_store))); | |
| 89 | |
| 90 mojo::ClipboardPtr clipboard; | |
| 91 shell->ConnectToService("mojo:clipboard", &clipboard); | |
| 92 clipboard_.reset(new WebClipboardImpl(std::move(clipboard))); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 BlinkPlatformImpl::~BlinkPlatformImpl() { | |
| 97 } | |
| 98 | |
| 99 blink::WebCookieJar* BlinkPlatformImpl::cookieJar() { | |
| 100 return cookie_jar_.get(); | |
| 101 } | |
| 102 | |
| 103 blink::WebClipboard* BlinkPlatformImpl::clipboard() { | |
| 104 return clipboard_.get(); | |
| 105 } | |
| 106 | |
| 107 blink::WebMimeRegistry* BlinkPlatformImpl::mimeRegistry() { | |
| 108 return &mime_registry_; | |
| 109 } | |
| 110 | |
| 111 blink::WebThemeEngine* BlinkPlatformImpl::themeEngine() { | |
| 112 return &theme_engine_; | |
| 113 } | |
| 114 | |
| 115 blink::WebString BlinkPlatformImpl::defaultLocale() { | |
| 116 return blink::WebString::fromUTF8("en-US"); | |
| 117 } | |
| 118 | |
| 119 blink::WebBlobRegistry* BlinkPlatformImpl::blobRegistry() { | |
| 120 return &blob_registry_; | |
| 121 } | |
| 122 | |
| 123 double BlinkPlatformImpl::currentTimeSeconds() { | |
| 124 return base::Time::Now().ToDoubleT(); | |
| 125 } | |
| 126 | |
| 127 double BlinkPlatformImpl::monotonicallyIncreasingTimeSeconds() { | |
| 128 return base::TimeTicks::Now().ToInternalValue() / | |
| 129 static_cast<double>(base::Time::kMicrosecondsPerSecond); | |
| 130 } | |
| 131 | |
| 132 bool BlinkPlatformImpl::isThreadedCompositingEnabled() { | |
| 133 return true; | |
| 134 } | |
| 135 | |
| 136 blink::WebCompositorSupport* BlinkPlatformImpl::compositorSupport() { | |
| 137 return &compositor_support_; | |
| 138 } | |
| 139 | |
| 140 uint32_t BlinkPlatformImpl::getUniqueIdForProcess() { | |
| 141 // TODO(rickyz): Replace this with base::GetUniqueIdForProcess when that's | |
| 142 // ready. | |
| 143 return base::trace_event::TraceLog::GetInstance()->process_id(); | |
| 144 } | |
| 145 | |
| 146 void BlinkPlatformImpl::createMessageChannel( | |
| 147 blink::WebMessagePortChannel** channel1, | |
| 148 blink::WebMessagePortChannel** channel2) { | |
| 149 message_port::WebMessagePortChannelImpl::CreatePair(channel1, channel2); | |
| 150 } | |
| 151 | |
| 152 blink::WebScrollbarBehavior* BlinkPlatformImpl::scrollbarBehavior() { | |
| 153 return &scrollbar_behavior_; | |
| 154 } | |
| 155 | |
| 156 blink::WebGraphicsContext3D* | |
| 157 BlinkPlatformImpl::createOffscreenGraphicsContext3D( | |
| 158 const blink::WebGraphicsContext3D::Attributes& attributes, | |
| 159 blink::WebGraphicsContext3D* share_context) { | |
| 160 blink::WebGraphicsContext3D::WebGraphicsInfo gl_info; | |
| 161 return createOffscreenGraphicsContext3D(attributes, share_context, &gl_info); | |
| 162 } | |
| 163 | |
| 164 blink::WebGraphicsContext3D* | |
| 165 BlinkPlatformImpl::createOffscreenGraphicsContext3D( | |
| 166 const blink::WebGraphicsContext3D::Attributes& attributes, | |
| 167 blink::WebGraphicsContext3D* share_context, | |
| 168 blink::WebGraphicsContext3D::WebGraphicsInfo* gl_info) { | |
| 169 // TODO(penghuang): Use the shell from the right HTMLDocument. | |
| 170 return WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext( | |
| 171 global_state_, shell_, blink::WebStringToGURL(attributes.topDocumentURL), | |
| 172 attributes, share_context, gl_info); | |
| 173 } | |
| 174 | |
| 175 blink::WebGraphicsContext3D* | |
| 176 BlinkPlatformImpl::createOffscreenGraphicsContext3D( | |
| 177 const blink::WebGraphicsContext3D::Attributes& attributes) { | |
| 178 return createOffscreenGraphicsContext3D(attributes, nullptr, nullptr); | |
| 179 } | |
| 180 | |
| 181 blink::WebGraphicsContext3DProvider* | |
| 182 BlinkPlatformImpl::createSharedOffscreenGraphicsContext3DProvider() { | |
| 183 return nullptr; | |
| 184 } | |
| 185 | |
| 186 blink::WebData BlinkPlatformImpl::loadResource(const char* resource) { | |
| 187 for (size_t i = 0; i < arraysize(kDataResources); ++i) { | |
| 188 if (!strcmp(resource, kDataResources[i].name)) { | |
| 189 base::StringPiece data = | |
| 190 ResourceBundle::GetSharedInstance().GetRawDataResourceForScale( | |
| 191 kDataResources[i].id, ui::SCALE_FACTOR_100P); | |
| 192 return blink::WebData(data.data(), data.size()); | |
| 193 } | |
| 194 } | |
| 195 NOTREACHED() << "Requested resource is unavailable: " << resource; | |
| 196 return blink::WebData(); | |
| 197 } | |
| 198 | |
| 199 blink::WebURLLoader* BlinkPlatformImpl::createURLLoader() { | |
| 200 return new WebURLLoaderImpl(url_loader_factory_.get(), &blob_registry_); | |
| 201 } | |
| 202 | |
| 203 blink::WebSocketHandle* BlinkPlatformImpl::createWebSocketHandle() { | |
| 204 return new WebSocketHandleImpl(web_socket_factory_.get()); | |
| 205 } | |
| 206 | |
| 207 blink::WebString BlinkPlatformImpl::userAgent() { | |
| 208 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 209 if (command_line->HasSwitch(kUserAgentSwitch)) { | |
| 210 return blink::WebString::fromUTF8( | |
| 211 command_line->GetSwitchValueASCII(kUserAgentSwitch)); | |
| 212 } | |
| 213 return blink::WebString::fromUTF8(mojo::common::GetUserAgent()); | |
| 214 } | |
| 215 | |
| 216 blink::WebData BlinkPlatformImpl::parseDataURL( | |
| 217 const blink::WebURL& url, | |
| 218 blink::WebString& mimetype_out, | |
| 219 blink::WebString& charset_out) { | |
| 220 std::string mimetype, charset, data; | |
| 221 if (net::DataURL::Parse(url, &mimetype, &charset, &data) && | |
| 222 mime_util::IsSupportedMimeType(mimetype)) { | |
| 223 mimetype_out = blink::WebString::fromUTF8(mimetype); | |
| 224 charset_out = blink::WebString::fromUTF8(charset); | |
| 225 return data; | |
| 226 } | |
| 227 return blink::WebData(); | |
| 228 } | |
| 229 | |
| 230 blink::WebURLError BlinkPlatformImpl::cancelledError(const blink::WebURL& url) | |
| 231 const { | |
| 232 blink::WebURLError error; | |
| 233 error.domain = blink::WebString::fromUTF8(net::kErrorDomain); | |
| 234 error.reason = net::ERR_ABORTED; | |
| 235 error.unreachableURL = url; | |
| 236 error.staleCopyInCache = false; | |
| 237 error.isCancellation = true; | |
| 238 return error; | |
| 239 } | |
| 240 | |
| 241 bool BlinkPlatformImpl::isReservedIPAddress( | |
| 242 const blink::WebString& host) const { | |
| 243 net::IPAddressNumber address; | |
| 244 if (!net::ParseURLHostnameToNumber(host.utf8(), &address)) | |
| 245 return false; | |
| 246 return net::IsIPAddressReserved(address); | |
| 247 } | |
| 248 | |
| 249 blink::WebThread* BlinkPlatformImpl::createThread(const char* name) { | |
| 250 scheduler::WebThreadImplForWorkerScheduler* thread = | |
| 251 new scheduler::WebThreadImplForWorkerScheduler(name); | |
| 252 thread->Init(); | |
| 253 thread->TaskRunner()->PostTask( | |
| 254 FROM_HERE, base::Bind(&BlinkPlatformImpl::UpdateWebThreadTLS, | |
| 255 base::Unretained(this), thread)); | |
| 256 return thread; | |
| 257 } | |
| 258 | |
| 259 blink::WebThread* BlinkPlatformImpl::currentThread() { | |
| 260 if (main_thread_->isCurrentThread()) | |
| 261 return main_thread_.get(); | |
| 262 return static_cast<blink::WebThread*>(current_thread_slot_.Get()); | |
| 263 } | |
| 264 | |
| 265 void BlinkPlatformImpl::yieldCurrentThread() { | |
| 266 base::PlatformThread::YieldCurrentThread(); | |
| 267 } | |
| 268 | |
| 269 blink::WebWaitableEvent* BlinkPlatformImpl::createWaitableEvent( | |
| 270 blink::WebWaitableEvent::ResetPolicy policy, | |
| 271 blink::WebWaitableEvent::InitialState state) { | |
| 272 return new WebWaitableEventImpl(policy, state); | |
| 273 } | |
| 274 | |
| 275 blink::WebWaitableEvent* BlinkPlatformImpl::waitMultipleEvents( | |
| 276 const blink::WebVector<blink::WebWaitableEvent*>& web_events) { | |
| 277 std::vector<base::WaitableEvent*> events; | |
| 278 for (size_t i = 0; i < web_events.size(); ++i) | |
| 279 events.push_back(static_cast<WebWaitableEventImpl*>(web_events[i])->impl()); | |
| 280 size_t idx = base::WaitableEvent::WaitMany(events.data(), events.size()); | |
| 281 DCHECK_LT(idx, web_events.size()); | |
| 282 return web_events[idx]; | |
| 283 } | |
| 284 | |
| 285 blink::WebGestureCurve* BlinkPlatformImpl::createFlingAnimationCurve( | |
| 286 blink::WebGestureDevice device_source, | |
| 287 const blink::WebFloatPoint& velocity, | |
| 288 const blink::WebSize& cumulative_scroll) { | |
| 289 const bool is_main_thread = true; | |
| 290 return ui::WebGestureCurveImpl::CreateFromDefaultPlatformCurve( | |
| 291 gfx::Vector2dF(velocity.x, velocity.y), | |
| 292 gfx::Vector2dF(cumulative_scroll.width, cumulative_scroll.height), | |
| 293 is_main_thread).release(); | |
| 294 } | |
| 295 | |
| 296 blink::WebCrypto* BlinkPlatformImpl::crypto() { | |
| 297 return &web_crypto_; | |
| 298 } | |
| 299 | |
| 300 blink::WebNotificationManager* | |
| 301 BlinkPlatformImpl::notificationManager() { | |
| 302 return &web_notification_manager_; | |
| 303 } | |
| 304 | |
| 305 void BlinkPlatformImpl::UpdateWebThreadTLS(blink::WebThread* thread) { | |
| 306 DCHECK(!current_thread_slot_.Get()); | |
| 307 current_thread_slot_.Set(thread); | |
| 308 } | |
| 309 | |
| 310 } // namespace html_viewer | |
| OLD | NEW |