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