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/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "mojo/public/cpp/application/connect.h" |
| 13 #include "mojo/public/cpp/application/service_provider_impl.h" |
12 #include "mojo/public/cpp/system/data_pipe.h" | 14 #include "mojo/public/cpp/system/data_pipe.h" |
| 15 #include "mojo/public/interfaces/application/shell.mojom.h" |
13 #include "mojo/services/html_viewer/blink_input_events_type_converters.h" | 16 #include "mojo/services/html_viewer/blink_input_events_type_converters.h" |
14 #include "mojo/services/html_viewer/blink_url_request_type_converters.h" | 17 #include "mojo/services/html_viewer/blink_url_request_type_converters.h" |
15 #include "mojo/services/html_viewer/webstoragenamespace_impl.h" | 18 #include "mojo/services/html_viewer/webstoragenamespace_impl.h" |
16 #include "mojo/services/html_viewer/weburlloader_impl.h" | 19 #include "mojo/services/html_viewer/weburlloader_impl.h" |
17 #include "mojo/services/public/cpp/view_manager/view.h" | 20 #include "mojo/services/public/cpp/view_manager/view.h" |
18 #include "skia/ext/refptr.h" | 21 #include "skia/ext/refptr.h" |
19 #include "third_party/WebKit/public/platform/Platform.h" | 22 #include "third_party/WebKit/public/platform/Platform.h" |
20 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" | 23 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" |
21 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | 24 #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
22 #include "third_party/WebKit/public/web/WebDocument.h" | 25 #include "third_party/WebKit/public/web/WebDocument.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 if (request.extraData()) | 74 if (request.extraData()) |
72 return true; | 75 return true; |
73 | 76 |
74 // 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 |
75 // host to do the navigation for us. | 78 // host to do the navigation for us. |
76 return false; | 79 return false; |
77 } | 80 } |
78 | 81 |
79 } // namespace | 82 } // namespace |
80 | 83 |
81 HTMLDocumentView::HTMLDocumentView(ServiceProvider* service_provider, | 84 HTMLDocumentView::HTMLDocumentView( |
82 ViewManager* view_manager) | 85 URLResponsePtr response, |
83 : view_manager_(view_manager), | 86 scoped_ptr<ServiceProvider> imported_services, |
84 navigator_host_(service_provider), | 87 ServiceProviderImpl* exported_services, |
| 88 Shell* shell) |
| 89 : imported_services_(imported_services.Pass()), |
| 90 shell_(shell), |
85 web_view_(NULL), | 91 web_view_(NULL), |
86 root_(NULL), | 92 root_(NULL), |
| 93 view_manager_client_factory_(shell, this), |
87 repaint_pending_(false), | 94 repaint_pending_(false), |
88 weak_factory_(this) { | 95 weak_factory_(this) { |
| 96 exported_services->AddService(&view_manager_client_factory_); |
| 97 Load(response.Pass()); |
89 } | 98 } |
90 | 99 |
91 HTMLDocumentView::~HTMLDocumentView() { | 100 HTMLDocumentView::~HTMLDocumentView() { |
92 if (web_view_) | 101 if (web_view_) |
93 web_view_->close(); | 102 web_view_->close(); |
94 if (root_) | 103 if (root_) |
95 root_->RemoveObserver(this); | 104 root_->RemoveObserver(this); |
96 } | 105 } |
97 | 106 |
98 void HTMLDocumentView::AttachToView(View* view) { | 107 void HTMLDocumentView::OnEmbed(ViewManager* view_manager, |
99 root_ = view; | 108 View* root, |
| 109 ServiceProviderImpl* exported_services, |
| 110 scoped_ptr<ServiceProvider> imported_services) { |
| 111 root_ = root; |
100 root_->SetColor(SK_ColorCYAN); // Dummy background color. | 112 root_->SetColor(SK_ColorCYAN); // Dummy background color. |
| 113 web_view_->resize(root_->bounds().size()); |
| 114 root_->AddObserver(this); |
| 115 } |
101 | 116 |
| 117 void HTMLDocumentView::OnViewManagerDisconnected(ViewManager* view_manager) { |
| 118 // TODO(aa): Need to figure out how shutdown works. |
| 119 } |
| 120 |
| 121 void HTMLDocumentView::Load(URLResponsePtr response) { |
102 web_view_ = blink::WebView::create(this); | 122 web_view_ = blink::WebView::create(this); |
103 ConfigureSettings(web_view_->settings()); | 123 ConfigureSettings(web_view_->settings()); |
104 web_view_->setMainFrame(blink::WebLocalFrame::create(this)); | 124 web_view_->setMainFrame(blink::WebLocalFrame::create(this)); |
105 web_view_->resize(root_->bounds().size()); | |
106 | |
107 root_->AddObserver(this); | |
108 } | |
109 | |
110 void HTMLDocumentView::Load(URLResponsePtr response) { | |
111 DCHECK(web_view_); | |
112 | 125 |
113 GURL url(response->url); | 126 GURL url(response->url); |
114 | 127 |
115 WebURLRequestExtraData* extra_data = new WebURLRequestExtraData; | 128 WebURLRequestExtraData* extra_data = new WebURLRequestExtraData; |
116 extra_data->synthetic_response = response.Pass(); | 129 extra_data->synthetic_response = response.Pass(); |
117 | 130 |
118 blink::WebURLRequest web_request; | 131 blink::WebURLRequest web_request; |
119 web_request.initialize(); | 132 web_request.initialize(); |
120 web_request.setURL(url); | 133 web_request.setURL(url); |
121 web_request.setExtraData(extra_data); | 134 web_request.setExtraData(extra_data); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 blink::WebNavigationPolicy HTMLDocumentView::decidePolicyForNavigation( | 183 blink::WebNavigationPolicy HTMLDocumentView::decidePolicyForNavigation( |
171 blink::WebLocalFrame* frame, blink::WebDataSource::ExtraData* data, | 184 blink::WebLocalFrame* frame, blink::WebDataSource::ExtraData* data, |
172 const blink::WebURLRequest& request, blink::WebNavigationType nav_type, | 185 const blink::WebURLRequest& request, blink::WebNavigationType nav_type, |
173 blink::WebNavigationPolicy default_policy, bool is_redirect) { | 186 blink::WebNavigationPolicy default_policy, bool is_redirect) { |
174 if (CanNavigateLocally(frame, request)) | 187 if (CanNavigateLocally(frame, request)) |
175 return default_policy; | 188 return default_policy; |
176 | 189 |
177 NavigationDetailsPtr nav_details(NavigationDetails::New()); | 190 NavigationDetailsPtr nav_details(NavigationDetails::New()); |
178 nav_details->request = URLRequest::From(request); | 191 nav_details->request = URLRequest::From(request); |
179 | 192 |
180 navigator_host_->RequestNavigate( | 193 GetNavigatorHost()->RequestNavigate( |
181 root_->id(), | 194 root_->id(), |
182 WebNavigationPolicyToNavigationTarget(default_policy), | 195 WebNavigationPolicyToNavigationTarget(default_policy), |
183 nav_details.Pass()); | 196 nav_details.Pass()); |
184 | 197 |
185 return blink::WebNavigationPolicyIgnore; | 198 return blink::WebNavigationPolicyIgnore; |
186 } | 199 } |
187 | 200 |
188 void HTMLDocumentView::didAddMessageToConsole( | 201 void HTMLDocumentView::didAddMessageToConsole( |
189 const blink::WebConsoleMessage& message, | 202 const blink::WebConsoleMessage& message, |
190 const blink::WebString& source_name, | 203 const blink::WebString& source_name, |
191 unsigned source_line, | 204 unsigned source_line, |
192 const blink::WebString& stack_trace) { | 205 const blink::WebString& stack_trace) { |
193 } | 206 } |
194 | 207 |
195 void HTMLDocumentView::didNavigateWithinPage( | 208 void HTMLDocumentView::didNavigateWithinPage( |
196 blink::WebLocalFrame* frame, const blink::WebHistoryItem& history_item, | 209 blink::WebLocalFrame* frame, const blink::WebHistoryItem& history_item, |
197 blink::WebHistoryCommitType commit_type) { | 210 blink::WebHistoryCommitType commit_type) { |
198 navigator_host_->DidNavigateLocally(root_->id(), | 211 GetNavigatorHost()->DidNavigateLocally(root_->id(), |
199 history_item.urlString().utf8()); | 212 history_item.urlString().utf8()); |
200 } | 213 } |
201 | 214 |
202 void HTMLDocumentView::OnViewBoundsChanged(View* view, | 215 void HTMLDocumentView::OnViewBoundsChanged(View* view, |
203 const gfx::Rect& old_bounds, | 216 const gfx::Rect& old_bounds, |
204 const gfx::Rect& new_bounds) { | 217 const gfx::Rect& new_bounds) { |
205 DCHECK_EQ(view, root_); | 218 DCHECK_EQ(view, root_); |
206 web_view_->resize(view->bounds().size()); | 219 web_view_->resize(view->bounds().size()); |
207 } | 220 } |
208 | 221 |
209 void HTMLDocumentView::OnViewDestroyed(View* view) { | 222 void HTMLDocumentView::OnViewDestroyed(View* view) { |
210 DCHECK_EQ(view, root_); | 223 DCHECK_EQ(view, root_); |
211 view->RemoveObserver(this); | 224 view->RemoveObserver(this); |
212 root_ = NULL; | 225 root_ = NULL; |
213 } | 226 } |
214 | 227 |
215 void HTMLDocumentView::OnViewInputEvent(View* view, const EventPtr& event) { | 228 void HTMLDocumentView::OnViewInputEvent(View* view, const EventPtr& event) { |
216 scoped_ptr<blink::WebInputEvent> web_event = | 229 scoped_ptr<blink::WebInputEvent> web_event = |
217 event.To<scoped_ptr<blink::WebInputEvent> >(); | 230 event.To<scoped_ptr<blink::WebInputEvent> >(); |
218 if (web_event) | 231 if (web_event) |
219 web_view_->handleInputEvent(*web_event); | 232 web_view_->handleInputEvent(*web_event); |
220 } | 233 } |
221 | 234 |
222 void HTMLDocumentView::Repaint() { | 235 void HTMLDocumentView::Repaint() { |
223 repaint_pending_ = false; | 236 repaint_pending_ = false; |
224 | 237 |
| 238 if (!root_) |
| 239 return; |
| 240 |
225 web_view_->animate(0.0); | 241 web_view_->animate(0.0); |
226 web_view_->layout(); | 242 web_view_->layout(); |
227 | 243 |
228 int width = web_view_->size().width; | 244 int width = web_view_->size().width; |
229 int height = web_view_->size().height; | 245 int height = web_view_->size().height; |
230 | 246 |
231 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(SkCanvas::NewRaster( | 247 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(SkCanvas::NewRaster( |
232 SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType))); | 248 SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType))); |
233 | 249 |
234 web_view_->paint(canvas.get(), gfx::Rect(0, 0, width, height)); | 250 web_view_->paint(canvas.get(), gfx::Rect(0, 0, width, height)); |
235 | 251 |
236 root_->SetContents(canvas->getDevice()->accessBitmap(false)); | 252 root_->SetContents(canvas->getDevice()->accessBitmap(false)); |
237 } | 253 } |
238 | 254 |
| 255 NavigatorHost* HTMLDocumentView::GetNavigatorHost() { |
| 256 if (!navigator_host_.get()) { |
| 257 // TODO(aa): This should come via |imported_services| in OnEmbed(). |
| 258 InterfacePtr<ServiceProvider> sp; |
| 259 shell_->ConnectToApplication("mojo:mojo_window_manager", Get(&sp)); |
| 260 ConnectToService(sp.get(), &navigator_host_); |
| 261 } |
| 262 return navigator_host_.get(); |
| 263 } |
| 264 |
239 } // namespace mojo | 265 } // namespace mojo |
OLD | NEW |