Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: mojo/services/html_viewer/html_document_view.cc

Issue 453133002: Mojo: Generalize blink::URLRequest -> mojo::URLRequest conversion, and use it (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: darin Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/view.h" 17 #include "mojo/services/public/cpp/view_manager/view.h"
17 #include "skia/ext/refptr.h" 18 #include "skia/ext/refptr.h"
18 #include "third_party/WebKit/public/platform/Platform.h" 19 #include "third_party/WebKit/public/platform/Platform.h"
19 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" 20 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h"
20 #include "third_party/WebKit/public/web/WebConsoleMessage.h" 21 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
21 #include "third_party/WebKit/public/web/WebDocument.h" 22 #include "third_party/WebKit/public/web/WebDocument.h"
22 #include "third_party/WebKit/public/web/WebElement.h" 23 #include "third_party/WebKit/public/web/WebElement.h"
23 #include "third_party/WebKit/public/web/WebInputEvent.h" 24 #include "third_party/WebKit/public/web/WebInputEvent.h"
24 #include "third_party/WebKit/public/web/WebLocalFrame.h" 25 #include "third_party/WebKit/public/web/WebLocalFrame.h"
25 #include "third_party/WebKit/public/web/WebScriptSource.h" 26 #include "third_party/WebKit/public/web/WebScriptSource.h"
26 #include "third_party/WebKit/public/web/WebSettings.h" 27 #include "third_party/WebKit/public/web/WebSettings.h"
27 #include "third_party/WebKit/public/web/WebView.h" 28 #include "third_party/WebKit/public/web/WebView.h"
28 #include "third_party/skia/include/core/SkCanvas.h" 29 #include "third_party/skia/include/core/SkCanvas.h"
29 #include "third_party/skia/include/core/SkColor.h" 30 #include "third_party/skia/include/core/SkColor.h"
30 #include "third_party/skia/include/core/SkDevice.h" 31 #include "third_party/skia/include/core/SkDevice.h"
31 32
32 namespace mojo { 33 namespace mojo {
33 namespace { 34 namespace {
34 35
35 // Ripped from web_url_loader_impl.cc. Why is everything so complicated?
36 class HeaderFlattener : public blink::WebHTTPHeaderVisitor {
37 public:
38 HeaderFlattener() : has_accept_header_(false) {}
39
40 virtual void visitHeader(const blink::WebString& name,
41 const blink::WebString& value) {
42 // Headers are latin1.
43 const std::string& name_latin1 = name.latin1();
44 const std::string& value_latin1 = value.latin1();
45
46 // Skip over referrer headers found in the header map because we already
47 // pulled it out as a separate parameter.
48 if (LowerCaseEqualsASCII(name_latin1, "referer"))
49 return;
50
51 if (LowerCaseEqualsASCII(name_latin1, "accept"))
52 has_accept_header_ = true;
53
54 buffer_.push_back(name_latin1 + ": " + value_latin1);
55 }
56
57 Array<String> GetBuffer() {
58 // In some cases, WebKit doesn't add an Accept header, but not having the
59 // header confuses some web servers. See bug 808613.
60 if (!has_accept_header_) {
61 buffer_.push_back("Accept: */*");
62 has_accept_header_ = true;
63 }
64 return buffer_.Pass();
65 }
66
67 private:
68 Array<String> buffer_;
69 bool has_accept_header_;
70 };
71
72 void AddRequestBody(NavigationDetails* nav_details,
73 const blink::WebURLRequest& request) {
74 if (request.httpBody().isNull())
75 return;
76
77 uint32_t i = 0;
78 blink::WebHTTPBody::Element element;
79 while (request.httpBody().elementAt(i++, element)) {
80 switch (element.type) {
81 case blink::WebHTTPBody::Element::TypeData:
82 if (!element.data.isEmpty()) {
83 // WebKit sometimes gives up empty data to append. These aren't
84 // necessary so we just optimize those out here.
85 uint32_t num_bytes = static_cast<uint32_t>(element.data.size());
86 MojoCreateDataPipeOptions options;
87 options.struct_size = sizeof(MojoCreateDataPipeOptions);
88 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
89 options.element_num_bytes = 1;
90 options.capacity_num_bytes = num_bytes;
91 DataPipe data_pipe(options);
92 nav_details->request->body.push_back(
93 data_pipe.consumer_handle.Pass());
94 WriteDataRaw(data_pipe.producer_handle.get(),
95 element.data.data(),
96 &num_bytes,
97 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE);
98 }
99 break;
100 case blink::WebHTTPBody::Element::TypeFile:
101 case blink::WebHTTPBody::Element::TypeFileSystemURL:
102 case blink::WebHTTPBody::Element::TypeBlob:
103 // TODO(mpcomplete): handle these.
104 NOTIMPLEMENTED();
105 break;
106 default:
107 NOTREACHED();
108 }
109 }
110 }
111
112 void ConfigureSettings(blink::WebSettings* settings) { 36 void ConfigureSettings(blink::WebSettings* settings) {
113 settings->setAcceleratedCompositingEnabled(false); 37 settings->setAcceleratedCompositingEnabled(false);
114 settings->setCookieEnabled(true); 38 settings->setCookieEnabled(true);
115 settings->setDefaultFixedFontSize(13); 39 settings->setDefaultFixedFontSize(13);
116 settings->setDefaultFontSize(16); 40 settings->setDefaultFontSize(16);
117 settings->setLoadsImagesAutomatically(true); 41 settings->setLoadsImagesAutomatically(true);
118 settings->setJavaScriptEnabled(true); 42 settings->setJavaScriptEnabled(true);
119 } 43 }
120 44
121 Target WebNavigationPolicyToNavigationTarget( 45 Target WebNavigationPolicyToNavigationTarget(
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 } 152 }
229 153
230 blink::WebNavigationPolicy HTMLDocumentView::decidePolicyForNavigation( 154 blink::WebNavigationPolicy HTMLDocumentView::decidePolicyForNavigation(
231 blink::WebLocalFrame* frame, blink::WebDataSource::ExtraData* data, 155 blink::WebLocalFrame* frame, blink::WebDataSource::ExtraData* data,
232 const blink::WebURLRequest& request, blink::WebNavigationType nav_type, 156 const blink::WebURLRequest& request, blink::WebNavigationType nav_type,
233 blink::WebNavigationPolicy default_policy, bool is_redirect) { 157 blink::WebNavigationPolicy default_policy, bool is_redirect) {
234 if (CanNavigateLocally(frame, request)) 158 if (CanNavigateLocally(frame, request))
235 return default_policy; 159 return default_policy;
236 160
237 NavigationDetailsPtr nav_details(NavigationDetails::New()); 161 NavigationDetailsPtr nav_details(NavigationDetails::New());
238 nav_details->request->url = request.url().string().utf8(); 162 nav_details->request = URLRequest::From(request);
239 nav_details->request->method = request.httpMethod().utf8();
240
241 HeaderFlattener flattener;
242 request.visitHTTPHeaderFields(&flattener);
243 nav_details->request->headers = flattener.GetBuffer().Pass();
244
245 AddRequestBody(nav_details.get(), request);
246 163
247 navigator_host_->RequestNavigate( 164 navigator_host_->RequestNavigate(
248 root_->id(), 165 root_->id(),
249 WebNavigationPolicyToNavigationTarget(default_policy), 166 WebNavigationPolicyToNavigationTarget(default_policy),
250 nav_details.Pass()); 167 nav_details.Pass());
251 168
252 return blink::WebNavigationPolicyIgnore; 169 return blink::WebNavigationPolicyIgnore;
253 } 170 }
254 171
255 void HTMLDocumentView::didAddMessageToConsole( 172 void HTMLDocumentView::didAddMessageToConsole(
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 215
299 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(SkCanvas::NewRaster( 216 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(SkCanvas::NewRaster(
300 SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType))); 217 SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType)));
301 218
302 web_view_->paint(canvas.get(), gfx::Rect(0, 0, width, height)); 219 web_view_->paint(canvas.get(), gfx::Rect(0, 0, width, height));
303 220
304 root_->SetContents(canvas->getDevice()->accessBitmap(false)); 221 root_->SetContents(canvas->getDevice()->accessBitmap(false));
305 } 222 }
306 223
307 } // namespace mojo 224 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/services/html_viewer/blink_url_request_type_converters.cc ('k') | mojo/services/html_viewer/weburlloader_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698