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

Side by Side Diff: webkit/plugins/ppapi/url_request_info_util.cc

Issue 20165002: Move webkit/plugins/ppapi to content/renderer/pepper. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 5 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
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "webkit/plugins/ppapi/url_request_info_util.h"
6
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "net/http/http_util.h"
10 #include "ppapi/shared_impl/url_request_info_data.h"
11 #include "ppapi/shared_impl/var.h"
12 #include "ppapi/thunk/enter.h"
13 #include "third_party/WebKit/public/platform/WebData.h"
14 #include "third_party/WebKit/public/platform/WebHTTPBody.h"
15 #include "third_party/WebKit/public/platform/WebURL.h"
16 #include "third_party/WebKit/public/platform/WebURLRequest.h"
17 #include "third_party/WebKit/public/web/WebDocument.h"
18 #include "third_party/WebKit/public/web/WebFrame.h"
19 #include "url/gurl.h"
20 #include "url/url_util.h"
21 #include "webkit/child/weburlrequest_extradata_impl.h"
22 #include "webkit/plugins/ppapi/common.h"
23 #include "webkit/plugins/ppapi/plugin_module.h"
24 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h"
25 #include "webkit/plugins/ppapi/resource_helper.h"
26
27 using ppapi::URLRequestInfoData;
28 using ppapi::Resource;
29 using ppapi::thunk::EnterResourceNoLock;
30 using ppapi::thunk::PPB_FileRef_API;
31 using WebKit::WebData;
32 using WebKit::WebHTTPBody;
33 using WebKit::WebString;
34 using WebKit::WebFrame;
35 using WebKit::WebURL;
36 using WebKit::WebURLRequest;
37
38 namespace webkit {
39 namespace ppapi {
40
41 namespace {
42
43 // Appends the file ref given the Resource pointer associated with it to the
44 // given HTTP body, returning true on success.
45 bool AppendFileRefToBody(
46 Resource* file_ref_resource,
47 int64_t start_offset,
48 int64_t number_of_bytes,
49 PP_Time expected_last_modified_time,
50 WebHTTPBody *http_body) {
51 // Get the underlying file ref impl.
52 if (!file_ref_resource)
53 return false;
54 PPB_FileRef_API* file_ref_api = file_ref_resource->AsPPB_FileRef_API();
55 if (!file_ref_api)
56 return false;
57 const PPB_FileRef_Impl* file_ref =
58 static_cast<PPB_FileRef_Impl*>(file_ref_api);
59
60 PluginDelegate* plugin_delegate =
61 ResourceHelper::GetPluginDelegate(file_ref_resource);
62 if (!plugin_delegate)
63 return false;
64
65 base::FilePath platform_path;
66 switch (file_ref->GetFileSystemType()) {
67 case PP_FILESYSTEMTYPE_LOCALTEMPORARY:
68 case PP_FILESYSTEMTYPE_LOCALPERSISTENT:
69 // TODO(kinuko): remove this sync IPC when we fully support
70 // AppendURLRange for FileSystem URL.
71 plugin_delegate->SyncGetFileSystemPlatformPath(
72 file_ref->GetFileSystemURL(), &platform_path);
73 break;
74 case PP_FILESYSTEMTYPE_EXTERNAL:
75 platform_path = file_ref->GetSystemPath();
76 break;
77 default:
78 NOTREACHED();
79 }
80 http_body->appendFileRange(
81 platform_path.AsUTF16Unsafe(),
82 start_offset,
83 number_of_bytes,
84 expected_last_modified_time);
85 return true;
86 }
87
88 // Checks that the request data is valid. Returns false on failure. Note that
89 // method and header validation is done by the URL loader when the request is
90 // opened, and any access errors are returned asynchronously.
91 bool ValidateURLRequestData(const ::ppapi::URLRequestInfoData& data) {
92 if (data.prefetch_buffer_lower_threshold < 0 ||
93 data.prefetch_buffer_upper_threshold < 0 ||
94 data.prefetch_buffer_upper_threshold <=
95 data.prefetch_buffer_lower_threshold) {
96 return false;
97 }
98 return true;
99 }
100
101 // Ensures that the file_ref members of the given request info data are
102 // populated from the resource IDs. Returns true on success.
103 bool EnsureFileRefObjectsPopulated(::ppapi::URLRequestInfoData* data) {
104 // Get the Resource objects for any file refs with only host resource (this
105 // is the state of the request as it comes off IPC).
106 for (size_t i = 0; i < data->body.size(); ++i) {
107 URLRequestInfoData::BodyItem& item = data->body[i];
108 if (item.is_file && !item.file_ref.get()) {
109 EnterResourceNoLock<PPB_FileRef_API> enter(
110 item.file_ref_host_resource.host_resource(), false);
111 if (!enter.succeeded())
112 return false;
113 item.file_ref = enter.resource();
114 }
115 }
116 return true;
117 }
118
119 } // namespace
120
121 bool CreateWebURLRequest(::ppapi::URLRequestInfoData* data,
122 WebFrame* frame,
123 WebURLRequest* dest) {
124 // In the out-of-process case, we've received the URLRequestInfoData
125 // from the untrusted plugin and done no validation on it. We need to be
126 // sure it's not being malicious by checking everything for consistency.
127 if (!ValidateURLRequestData(*data) || !EnsureFileRefObjectsPopulated(data))
128 return false;
129
130 dest->initialize();
131 dest->setTargetType(WebURLRequest::TargetIsObject);
132 dest->setURL(frame->document().completeURL(WebString::fromUTF8(
133 data->url)));
134 dest->setDownloadToFile(data->stream_to_file);
135 dest->setReportUploadProgress(data->record_upload_progress);
136
137 if (!data->method.empty())
138 dest->setHTTPMethod(WebString::fromUTF8(data->method));
139
140 dest->setFirstPartyForCookies(frame->document().firstPartyForCookies());
141
142 const std::string& headers = data->headers;
143 if (!headers.empty()) {
144 net::HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\n\r");
145 while (it.GetNext()) {
146 dest->addHTTPHeaderField(
147 WebString::fromUTF8(it.name()),
148 WebString::fromUTF8(it.values()));
149 }
150 }
151
152 // Append the upload data.
153 if (!data->body.empty()) {
154 WebHTTPBody http_body;
155 http_body.initialize();
156 for (size_t i = 0; i < data->body.size(); ++i) {
157 const URLRequestInfoData::BodyItem& item = data->body[i];
158 if (item.is_file) {
159 if (!AppendFileRefToBody(item.file_ref.get(),
160 item.start_offset,
161 item.number_of_bytes,
162 item.expected_last_modified_time,
163 &http_body))
164 return false;
165 } else {
166 DCHECK(!item.data.empty());
167 http_body.appendData(WebData(item.data));
168 }
169 }
170 dest->setHTTPBody(http_body);
171 }
172
173 // Add the "Referer" header if there is a custom referrer. Such requests
174 // require universal access. For all other requests, "Referer" will be set
175 // after header security checks are done in AssociatedURLLoader.
176 if (data->has_custom_referrer_url && !data->custom_referrer_url.empty())
177 frame->setReferrerForRequest(*dest, GURL(data->custom_referrer_url));
178
179 if (data->has_custom_content_transfer_encoding &&
180 !data->custom_content_transfer_encoding.empty()) {
181 dest->addHTTPHeaderField(
182 WebString::fromUTF8("Content-Transfer-Encoding"),
183 WebString::fromUTF8(data->custom_content_transfer_encoding));
184 }
185
186 if (data->has_custom_user_agent) {
187 dest->setExtraData(new webkit_glue::WebURLRequestExtraDataImpl(
188 WebKit::WebReferrerPolicyDefault, // Ignored.
189 WebString::fromUTF8(data->custom_user_agent)));
190 }
191
192 return true;
193 }
194
195 bool URLRequestRequiresUniversalAccess(
196 const ::ppapi::URLRequestInfoData& data) {
197 return
198 data.has_custom_referrer_url ||
199 data.has_custom_content_transfer_encoding ||
200 data.has_custom_user_agent ||
201 url_util::FindAndCompareScheme(data.url, "javascript", NULL);
202 }
203
204 } // namespace ppapi
205 } // namespace webkit
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698