| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/services/html_viewer/html_document_view.h" | 5 #include "mojo/services/html_viewer/html_document_view.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | |
| 10 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 11 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 12 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 13 #include "mojo/public/cpp/application/connect.h" | 12 #include "mojo/public/cpp/application/connect.h" |
| 14 #include "mojo/public/cpp/application/service_provider_impl.h" | 13 #include "mojo/public/cpp/application/service_provider_impl.h" |
| 15 #include "mojo/public/cpp/system/data_pipe.h" | 14 #include "mojo/public/cpp/system/data_pipe.h" |
| 16 #include "mojo/public/interfaces/application/shell.mojom.h" | 15 #include "mojo/public/interfaces/application/shell.mojom.h" |
| 17 #include "mojo/services/html_viewer/blink_input_events_type_converters.h" | 16 #include "mojo/services/html_viewer/blink_input_events_type_converters.h" |
| 18 #include "mojo/services/html_viewer/blink_url_request_type_converters.h" | 17 #include "mojo/services/html_viewer/blink_url_request_type_converters.h" |
| 19 #include "mojo/services/html_viewer/weblayertreeview_impl.h" | |
| 20 #include "mojo/services/html_viewer/webstoragenamespace_impl.h" | 18 #include "mojo/services/html_viewer/webstoragenamespace_impl.h" |
| 21 #include "mojo/services/html_viewer/weburlloader_impl.h" | 19 #include "mojo/services/html_viewer/weburlloader_impl.h" |
| 22 #include "mojo/services/public/cpp/view_manager/view.h" | 20 #include "mojo/services/public/cpp/view_manager/view.h" |
| 23 #include "mojo/services/public/interfaces/surfaces/surfaces_service.mojom.h" | |
| 24 #include "skia/ext/refptr.h" | 21 #include "skia/ext/refptr.h" |
| 25 #include "third_party/WebKit/public/platform/Platform.h" | 22 #include "third_party/WebKit/public/platform/Platform.h" |
| 26 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" | 23 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" |
| 27 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | 24 #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
| 28 #include "third_party/WebKit/public/web/WebDocument.h" | 25 #include "third_party/WebKit/public/web/WebDocument.h" |
| 29 #include "third_party/WebKit/public/web/WebElement.h" | 26 #include "third_party/WebKit/public/web/WebElement.h" |
| 30 #include "third_party/WebKit/public/web/WebInputEvent.h" | 27 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 31 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 28 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 32 #include "third_party/WebKit/public/web/WebScriptSource.h" | 29 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 33 #include "third_party/WebKit/public/web/WebSettings.h" | 30 #include "third_party/WebKit/public/web/WebSettings.h" |
| 34 #include "third_party/WebKit/public/web/WebView.h" | 31 #include "third_party/WebKit/public/web/WebView.h" |
| 35 #include "third_party/skia/include/core/SkCanvas.h" | 32 #include "third_party/skia/include/core/SkCanvas.h" |
| 36 #include "third_party/skia/include/core/SkColor.h" | 33 #include "third_party/skia/include/core/SkColor.h" |
| 37 #include "third_party/skia/include/core/SkDevice.h" | 34 #include "third_party/skia/include/core/SkDevice.h" |
| 38 | 35 |
| 39 namespace mojo { | 36 namespace mojo { |
| 40 namespace { | 37 namespace { |
| 41 | 38 |
| 42 void ConfigureSettings(blink::WebSettings* settings) { | 39 void ConfigureSettings(blink::WebSettings* settings) { |
| 40 settings->setAcceleratedCompositingEnabled(false); |
| 43 settings->setCookieEnabled(true); | 41 settings->setCookieEnabled(true); |
| 44 settings->setDefaultFixedFontSize(13); | 42 settings->setDefaultFixedFontSize(13); |
| 45 settings->setDefaultFontSize(16); | 43 settings->setDefaultFontSize(16); |
| 46 settings->setLoadsImagesAutomatically(true); | 44 settings->setLoadsImagesAutomatically(true); |
| 47 settings->setJavaScriptEnabled(true); | 45 settings->setJavaScriptEnabled(true); |
| 48 } | 46 } |
| 49 | 47 |
| 50 Target WebNavigationPolicyToNavigationTarget( | 48 Target WebNavigationPolicyToNavigationTarget( |
| 51 blink::WebNavigationPolicy policy) { | 49 blink::WebNavigationPolicy policy) { |
| 52 switch (policy) { | 50 switch (policy) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 79 // Otherwise we don't know if we're the right app to handle this request. Ask | 77 // Otherwise we don't know if we're the right app to handle this request. Ask |
| 80 // host to do the navigation for us. | 78 // host to do the navigation for us. |
| 81 return false; | 79 return false; |
| 82 } | 80 } |
| 83 | 81 |
| 84 } // namespace | 82 } // namespace |
| 85 | 83 |
| 86 HTMLDocumentView::HTMLDocumentView( | 84 HTMLDocumentView::HTMLDocumentView( |
| 87 URLResponsePtr response, | 85 URLResponsePtr response, |
| 88 InterfaceRequest<ServiceProvider> service_provider_request, | 86 InterfaceRequest<ServiceProvider> service_provider_request, |
| 89 Shell* shell, | 87 Shell* shell) |
| 90 scoped_refptr<base::MessageLoopProxy> compositor_thread) | |
| 91 : shell_(shell), | 88 : shell_(shell), |
| 92 web_view_(NULL), | 89 web_view_(NULL), |
| 93 root_(NULL), | 90 root_(NULL), |
| 94 view_manager_client_factory_(shell, this), | 91 view_manager_client_factory_(shell, this), |
| 95 compositor_thread_(compositor_thread), | 92 repaint_pending_(false), |
| 96 weak_factory_(this) { | 93 weak_factory_(this) { |
| 97 ServiceProviderImpl* exported_services = new ServiceProviderImpl(); | 94 ServiceProviderImpl* exported_services = new ServiceProviderImpl(); |
| 98 exported_services->AddService(&view_manager_client_factory_); | 95 exported_services->AddService(&view_manager_client_factory_); |
| 99 BindToRequest(exported_services, &service_provider_request); | 96 BindToRequest(exported_services, &service_provider_request); |
| 100 Load(response.Pass()); | 97 Load(response.Pass()); |
| 101 } | 98 } |
| 102 | 99 |
| 103 HTMLDocumentView::~HTMLDocumentView() { | 100 HTMLDocumentView::~HTMLDocumentView() { |
| 104 if (web_view_) | 101 if (web_view_) |
| 105 web_view_->close(); | 102 web_view_->close(); |
| 106 if (root_) | 103 if (root_) |
| 107 root_->RemoveObserver(this); | 104 root_->RemoveObserver(this); |
| 108 } | 105 } |
| 109 | 106 |
| 110 void HTMLDocumentView::OnEmbed( | 107 void HTMLDocumentView::OnEmbed( |
| 111 ViewManager* view_manager, | 108 ViewManager* view_manager, |
| 112 View* root, | 109 View* root, |
| 113 ServiceProviderImpl* embedee_service_provider_impl, | 110 ServiceProviderImpl* embedee_service_provider_impl, |
| 114 scoped_ptr<ServiceProvider> embedder_service_provider) { | 111 scoped_ptr<ServiceProvider> embedder_service_provider) { |
| 115 root_ = root; | 112 root_ = root; |
| 116 embedder_service_provider_ = embedder_service_provider.Pass(); | 113 embedder_service_provider_ = embedder_service_provider.Pass(); |
| 117 navigator_host_.set_service_provider(embedder_service_provider_.get()); | 114 navigator_host_.set_service_provider(embedder_service_provider_.get()); |
| 118 | 115 |
| 116 root_->SetColor(SK_ColorCYAN); // Dummy background color. |
| 119 web_view_->resize(root_->bounds().size()); | 117 web_view_->resize(root_->bounds().size()); |
| 120 web_layer_tree_view_impl_->setViewportSize(root_->bounds().size()); | |
| 121 web_layer_tree_view_impl_->set_view(root_); | |
| 122 root_->AddObserver(this); | 118 root_->AddObserver(this); |
| 123 } | 119 } |
| 124 | 120 |
| 125 void HTMLDocumentView::OnViewManagerDisconnected(ViewManager* view_manager) { | 121 void HTMLDocumentView::OnViewManagerDisconnected(ViewManager* view_manager) { |
| 126 // TODO(aa): Need to figure out how shutdown works. | 122 // TODO(aa): Need to figure out how shutdown works. |
| 127 } | 123 } |
| 128 | 124 |
| 129 void HTMLDocumentView::Load(URLResponsePtr response) { | 125 void HTMLDocumentView::Load(URLResponsePtr response) { |
| 130 web_view_ = blink::WebView::create(this); | 126 web_view_ = blink::WebView::create(this); |
| 131 web_layer_tree_view_impl_->set_widget(web_view_); | |
| 132 ConfigureSettings(web_view_->settings()); | 127 ConfigureSettings(web_view_->settings()); |
| 133 web_view_->setMainFrame(blink::WebLocalFrame::create(this)); | 128 web_view_->setMainFrame(blink::WebLocalFrame::create(this)); |
| 134 | 129 |
| 135 GURL url(response->url); | 130 GURL url(response->url); |
| 136 | 131 |
| 137 WebURLRequestExtraData* extra_data = new WebURLRequestExtraData; | 132 WebURLRequestExtraData* extra_data = new WebURLRequestExtraData; |
| 138 extra_data->synthetic_response = response.Pass(); | 133 extra_data->synthetic_response = response.Pass(); |
| 139 | 134 |
| 140 blink::WebURLRequest web_request; | 135 blink::WebURLRequest web_request; |
| 141 web_request.initialize(); | 136 web_request.initialize(); |
| 142 web_request.setURL(url); | 137 web_request.setURL(url); |
| 143 web_request.setExtraData(extra_data); | 138 web_request.setExtraData(extra_data); |
| 144 | 139 |
| 145 web_view_->mainFrame()->loadRequest(web_request); | 140 web_view_->mainFrame()->loadRequest(web_request); |
| 146 } | 141 } |
| 147 | 142 |
| 148 blink::WebStorageNamespace* HTMLDocumentView::createSessionStorageNamespace() { | 143 blink::WebStorageNamespace* HTMLDocumentView::createSessionStorageNamespace() { |
| 149 return new WebStorageNamespaceImpl(); | 144 return new WebStorageNamespaceImpl(); |
| 150 } | 145 } |
| 151 | 146 |
| 152 void HTMLDocumentView::initializeLayerTreeView() { | 147 void HTMLDocumentView::didInvalidateRect(const blink::WebRect& rect) { |
| 153 ServiceProviderPtr surfaces_service_provider; | 148 if (!repaint_pending_) { |
| 154 shell_->ConnectToApplication("mojo:mojo_surfaces_service", | 149 repaint_pending_ = true; |
| 155 Get(&surfaces_service_provider)); | 150 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 156 InterfacePtr<SurfacesService> surfaces_service; | 151 FROM_HERE, |
| 157 ConnectToService(surfaces_service_provider.get(), &surfaces_service); | 152 base::Bind(&HTMLDocumentView::Repaint, weak_factory_.GetWeakPtr())); |
| 158 | 153 } |
| 159 ServiceProviderPtr gpu_service_provider; | |
| 160 // TODO(jamesr): Should be mojo:mojo_gpu_service | |
| 161 shell_->ConnectToApplication("mojo:mojo_native_viewport_service", | |
| 162 Get(&gpu_service_provider)); | |
| 163 InterfacePtr<Gpu> gpu_service; | |
| 164 ConnectToService(gpu_service_provider.get(), &gpu_service); | |
| 165 web_layer_tree_view_impl_.reset(new WebLayerTreeViewImpl( | |
| 166 compositor_thread_, surfaces_service.Pass(), gpu_service.Pass())); | |
| 167 } | 154 } |
| 168 | 155 |
| 169 blink::WebLayerTreeView* HTMLDocumentView::layerTreeView() { | 156 bool HTMLDocumentView::allowsBrokenNullLayerTreeView() const { |
| 170 return web_layer_tree_view_impl_.get(); | 157 // TODO(darin): Switch to using compositor bindings. |
| 158 // |
| 159 // NOTE: Note to Blink maintainers, feel free to break this code if it is the |
| 160 // last NOT using compositor bindings and you want to delete this code path. |
| 161 // |
| 162 return true; |
| 171 } | 163 } |
| 172 | 164 |
| 173 blink::WebFrame* HTMLDocumentView::createChildFrame( | 165 blink::WebFrame* HTMLDocumentView::createChildFrame( |
| 174 blink::WebLocalFrame* parent, | 166 blink::WebLocalFrame* parent, |
| 175 const blink::WebString& frameName) { | 167 const blink::WebString& frameName) { |
| 176 blink::WebLocalFrame* web_frame = blink::WebLocalFrame::create(this); | 168 blink::WebLocalFrame* web_frame = blink::WebLocalFrame::create(this); |
| 177 parent->appendChild(web_frame); | 169 parent->appendChild(web_frame); |
| 178 return web_frame; | 170 return web_frame; |
| 179 } | 171 } |
| 180 | 172 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 root_ = NULL; | 224 root_ = NULL; |
| 233 } | 225 } |
| 234 | 226 |
| 235 void HTMLDocumentView::OnViewInputEvent(View* view, const EventPtr& event) { | 227 void HTMLDocumentView::OnViewInputEvent(View* view, const EventPtr& event) { |
| 236 scoped_ptr<blink::WebInputEvent> web_event = | 228 scoped_ptr<blink::WebInputEvent> web_event = |
| 237 event.To<scoped_ptr<blink::WebInputEvent> >(); | 229 event.To<scoped_ptr<blink::WebInputEvent> >(); |
| 238 if (web_event) | 230 if (web_event) |
| 239 web_view_->handleInputEvent(*web_event); | 231 web_view_->handleInputEvent(*web_event); |
| 240 } | 232 } |
| 241 | 233 |
| 234 void HTMLDocumentView::Repaint() { |
| 235 repaint_pending_ = false; |
| 236 |
| 237 if (!root_) |
| 238 return; |
| 239 |
| 240 web_view_->animate(0.0); |
| 241 web_view_->layout(); |
| 242 |
| 243 int width = web_view_->size().width; |
| 244 int height = web_view_->size().height; |
| 245 |
| 246 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(SkCanvas::NewRaster( |
| 247 SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType))); |
| 248 |
| 249 web_view_->paint(canvas.get(), gfx::Rect(0, 0, width, height)); |
| 250 |
| 251 root_->SetContents(canvas->getDevice()->accessBitmap(false)); |
| 252 } |
| 253 |
| 242 } // namespace mojo | 254 } // namespace mojo |
| OLD | NEW |