Chromium Code Reviews| 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/system/data_pipe.h" | 12 #include "mojo/public/cpp/system/data_pipe.h" |
| 13 #include "mojo/services/html_viewer/blink_input_events_type_converters.h" | 13 #include "mojo/services/html_viewer/blink_input_events_type_converters.h" |
| 14 #include "mojo/services/html_viewer/blink_url_request_type_converters.h" | |
| 14 #include "mojo/services/html_viewer/webstoragenamespace_impl.h" | 15 #include "mojo/services/html_viewer/webstoragenamespace_impl.h" |
| 15 #include "mojo/services/html_viewer/weburlloader_impl.h" | 16 #include "mojo/services/html_viewer/weburlloader_impl.h" |
| 16 #include "mojo/services/public/cpp/view_manager/node.h" | 17 #include "mojo/services/public/cpp/view_manager/node.h" |
| 17 #include "mojo/services/public/cpp/view_manager/view.h" | 18 #include "mojo/services/public/cpp/view_manager/view.h" |
| 18 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 19 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
| 19 #include "skia/ext/refptr.h" | 20 #include "skia/ext/refptr.h" |
| 20 #include "third_party/WebKit/public/platform/Platform.h" | 21 #include "third_party/WebKit/public/platform/Platform.h" |
| 21 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" | 22 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" |
| 22 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | 23 #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
| 23 #include "third_party/WebKit/public/web/WebDocument.h" | 24 #include "third_party/WebKit/public/web/WebDocument.h" |
| 24 #include "third_party/WebKit/public/web/WebElement.h" | 25 #include "third_party/WebKit/public/web/WebElement.h" |
| 25 #include "third_party/WebKit/public/web/WebInputEvent.h" | 26 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 26 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 27 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 27 #include "third_party/WebKit/public/web/WebScriptSource.h" | 28 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 28 #include "third_party/WebKit/public/web/WebSettings.h" | 29 #include "third_party/WebKit/public/web/WebSettings.h" |
| 29 #include "third_party/WebKit/public/web/WebView.h" | 30 #include "third_party/WebKit/public/web/WebView.h" |
| 30 #include "third_party/skia/include/core/SkCanvas.h" | 31 #include "third_party/skia/include/core/SkCanvas.h" |
| 31 #include "third_party/skia/include/core/SkColor.h" | 32 #include "third_party/skia/include/core/SkColor.h" |
| 32 #include "third_party/skia/include/core/SkDevice.h" | 33 #include "third_party/skia/include/core/SkDevice.h" |
| 33 | 34 |
| 34 namespace mojo { | 35 namespace mojo { |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 // Ripped from web_url_loader_impl.cc. Why is everything so complicated? | |
| 38 class HeaderFlattener : public blink::WebHTTPHeaderVisitor { | |
| 39 public: | |
| 40 HeaderFlattener() : has_accept_header_(false) {} | |
| 41 | |
| 42 virtual void visitHeader(const blink::WebString& name, | |
| 43 const blink::WebString& value) { | |
| 44 // Headers are latin1. | |
| 45 const std::string& name_latin1 = name.latin1(); | |
| 46 const std::string& value_latin1 = value.latin1(); | |
| 47 | |
| 48 // Skip over referrer headers found in the header map because we already | |
| 49 // pulled it out as a separate parameter. | |
| 50 if (LowerCaseEqualsASCII(name_latin1, "referer")) | |
| 51 return; | |
| 52 | |
| 53 if (LowerCaseEqualsASCII(name_latin1, "accept")) | |
| 54 has_accept_header_ = true; | |
| 55 | |
| 56 buffer_.push_back(name_latin1 + ": " + value_latin1); | |
| 57 } | |
| 58 | |
| 59 Array<String> GetBuffer() { | |
| 60 // In some cases, WebKit doesn't add an Accept header, but not having the | |
| 61 // header confuses some web servers. See bug 808613. | |
| 62 if (!has_accept_header_) { | |
| 63 buffer_.push_back("Accept: */*"); | |
| 64 has_accept_header_ = true; | |
| 65 } | |
| 66 return buffer_.Pass(); | |
| 67 } | |
| 68 | |
| 69 private: | |
| 70 Array<String> buffer_; | |
| 71 bool has_accept_header_; | |
| 72 }; | |
| 73 | |
| 74 void AddRequestBody(NavigationDetails* nav_details, | |
| 75 const blink::WebURLRequest& request) { | |
| 76 if (request.httpBody().isNull()) | |
| 77 return; | |
| 78 | |
| 79 uint32_t i = 0; | |
| 80 blink::WebHTTPBody::Element element; | |
| 81 while (request.httpBody().elementAt(i++, element)) { | |
| 82 switch (element.type) { | |
| 83 case blink::WebHTTPBody::Element::TypeData: | |
| 84 if (!element.data.isEmpty()) { | |
| 85 // WebKit sometimes gives up empty data to append. These aren't | |
| 86 // necessary so we just optimize those out here. | |
| 87 uint32_t num_bytes = static_cast<uint32_t>(element.data.size()); | |
| 88 MojoCreateDataPipeOptions options; | |
| 89 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
| 90 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | |
| 91 options.element_num_bytes = 1; | |
| 92 options.capacity_num_bytes = num_bytes; | |
| 93 DataPipe data_pipe(options); | |
| 94 nav_details->request->body.push_back( | |
| 95 data_pipe.consumer_handle.Pass()); | |
| 96 WriteDataRaw(data_pipe.producer_handle.get(), | |
| 97 element.data.data(), | |
| 98 &num_bytes, | |
| 99 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); | |
| 100 } | |
| 101 break; | |
| 102 case blink::WebHTTPBody::Element::TypeFile: | |
| 103 case blink::WebHTTPBody::Element::TypeFileSystemURL: | |
| 104 case blink::WebHTTPBody::Element::TypeBlob: | |
| 105 // TODO(mpcomplete): handle these. | |
| 106 NOTIMPLEMENTED(); | |
| 107 break; | |
| 108 default: | |
| 109 NOTREACHED(); | |
| 110 } | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void ConfigureSettings(blink::WebSettings* settings) { | 38 void ConfigureSettings(blink::WebSettings* settings) { |
| 115 settings->setAcceleratedCompositingEnabled(false); | 39 settings->setAcceleratedCompositingEnabled(false); |
| 116 settings->setCookieEnabled(true); | 40 settings->setCookieEnabled(true); |
| 117 settings->setDefaultFixedFontSize(13); | 41 settings->setDefaultFixedFontSize(13); |
| 118 settings->setDefaultFontSize(16); | 42 settings->setDefaultFontSize(16); |
| 119 settings->setLoadsImagesAutomatically(true); | 43 settings->setLoadsImagesAutomatically(true); |
| 120 settings->setJavaScriptEnabled(true); | 44 settings->setJavaScriptEnabled(true); |
| 121 } | 45 } |
| 122 | 46 |
| 123 Target WebNavigationPolicyToNavigationTarget( | 47 Target WebNavigationPolicyToNavigationTarget( |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 234 } | 158 } |
| 235 | 159 |
| 236 blink::WebNavigationPolicy HTMLDocumentView::decidePolicyForNavigation( | 160 blink::WebNavigationPolicy HTMLDocumentView::decidePolicyForNavigation( |
| 237 blink::WebLocalFrame* frame, blink::WebDataSource::ExtraData* data, | 161 blink::WebLocalFrame* frame, blink::WebDataSource::ExtraData* data, |
| 238 const blink::WebURLRequest& request, blink::WebNavigationType nav_type, | 162 const blink::WebURLRequest& request, blink::WebNavigationType nav_type, |
| 239 blink::WebNavigationPolicy default_policy, bool is_redirect) { | 163 blink::WebNavigationPolicy default_policy, bool is_redirect) { |
| 240 if (CanNavigateLocally(frame, request)) | 164 if (CanNavigateLocally(frame, request)) |
| 241 return default_policy; | 165 return default_policy; |
| 242 | 166 |
| 243 NavigationDetailsPtr nav_details(NavigationDetails::New()); | 167 NavigationDetailsPtr nav_details(NavigationDetails::New()); |
| 244 nav_details->request->url = request.url().string().utf8(); | 168 nav_details->request = |
| 245 nav_details->request->method = request.httpMethod().utf8(); | 169 TypeConverter<URLRequestPtr, blink::WebURLRequest>::ConvertFrom(request); |
|
darin (slow to review)
2014/08/12 16:23:12
You can just use URLRequest::From(request) here.
Matt Perry
2014/08/12 18:42:53
Done.
| |
| 246 | |
| 247 HeaderFlattener flattener; | |
| 248 request.visitHTTPHeaderFields(&flattener); | |
| 249 nav_details->request->headers = flattener.GetBuffer().Pass(); | |
| 250 | |
| 251 AddRequestBody(nav_details.get(), request); | |
| 252 | 170 |
| 253 navigator_host_->RequestNavigate( | 171 navigator_host_->RequestNavigate( |
| 254 view_->node()->id(), | 172 view_->node()->id(), |
| 255 WebNavigationPolicyToNavigationTarget(default_policy), | 173 WebNavigationPolicyToNavigationTarget(default_policy), |
| 256 nav_details.Pass()); | 174 nav_details.Pass()); |
| 257 | 175 |
| 258 return blink::WebNavigationPolicyIgnore; | 176 return blink::WebNavigationPolicyIgnore; |
| 259 } | 177 } |
| 260 | 178 |
| 261 void HTMLDocumentView::didAddMessageToConsole( | 179 void HTMLDocumentView::didAddMessageToConsole( |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 | 223 |
| 306 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(SkCanvas::NewRaster( | 224 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(SkCanvas::NewRaster( |
| 307 SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType))); | 225 SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType))); |
| 308 | 226 |
| 309 web_view_->paint(canvas.get(), gfx::Rect(0, 0, width, height)); | 227 web_view_->paint(canvas.get(), gfx::Rect(0, 0, width, height)); |
| 310 | 228 |
| 311 view_->SetContents(canvas->getDevice()->accessBitmap(false)); | 229 view_->SetContents(canvas->getDevice()->accessBitmap(false)); |
| 312 } | 230 } |
| 313 | 231 |
| 314 } // namespace mojo | 232 } // namespace mojo |
| OLD | NEW |