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