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