[email protected] | b68572a | 2013-07-10 18:21:51 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | b68572a | 2013-07-10 18:21:51 | [diff] [blame] | 5 | #include "content/renderer/pepper/url_response_info_util.h" |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 6 | |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | 7c6fcf3 | 2013-07-31 07:23:25 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 35c5feb | 2013-06-25 04:00:33 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
skyostil | 2d3b5bd | 2015-05-27 15:40:59 | [diff] [blame] | 11 | #include "base/location.h" |
| 12 | #include "base/single_thread_task_runner.h" |
| 13 | #include "base/thread_task_runner_handle.h" |
[email protected] | c6420f08 | 2013-09-18 22:42:41 | [diff] [blame] | 14 | #include "content/public/renderer/renderer_ppapi_host.h" |
| 15 | #include "content/renderer/pepper/pepper_file_ref_renderer_host.h" |
| 16 | #include "content/renderer/pepper/renderer_ppapi_host_impl.h" |
| 17 | #include "ipc/ipc_message.h" |
| 18 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | b68572a | 2013-07-10 18:21:51 | [diff] [blame] | 19 | #include "ppapi/shared_impl/url_response_info_data.h" |
kinuko | b473f00 | 2016-02-22 05:23:19 | [diff] [blame] | 20 | #include "third_party/WebKit/public/platform/FilePathConversion.h" |
[email protected] | b68572a | 2013-07-10 18:21:51 | [diff] [blame] | 21 | #include "third_party/WebKit/public/platform/WebCString.h" |
[email protected] | c1088446 | 2013-05-30 00:22:09 | [diff] [blame] | 22 | #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" |
| 23 | #include "third_party/WebKit/public/platform/WebString.h" |
| 24 | #include "third_party/WebKit/public/platform/WebURL.h" |
| 25 | #include "third_party/WebKit/public/platform/WebURLResponse.h" |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 26 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 27 | using blink::WebHTTPHeaderVisitor; |
| 28 | using blink::WebString; |
| 29 | using blink::WebURLResponse; |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 30 | |
[email protected] | b68572a | 2013-07-10 18:21:51 | [diff] [blame] | 31 | namespace content { |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 32 | |
| 33 | namespace { |
| 34 | |
| 35 | class HeaderFlattener : public WebHTTPHeaderVisitor { |
| 36 | public: |
| 37 | const std::string& buffer() const { return buffer_; } |
| 38 | |
avi | 5c77d21 | 2015-09-25 20:08:25 | [diff] [blame] | 39 | void visitHeader(const WebString& name, const WebString& value) override { |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 40 | if (!buffer_.empty()) |
| 41 | buffer_.append("\n"); |
| 42 | buffer_.append(name.utf8()); |
| 43 | buffer_.append(": "); |
| 44 | buffer_.append(value.utf8()); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | std::string buffer_; |
| 49 | }; |
| 50 | |
[email protected] | ad63b5c | 2014-04-11 21:12:36 | [diff] [blame] | 51 | bool IsRedirect(int32_t status) { return status >= 300 && status <= 399; } |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 52 | |
[email protected] | c6420f08 | 2013-09-18 22:42:41 | [diff] [blame] | 53 | void DidCreateResourceHosts(const ppapi::URLResponseInfoData& in_data, |
| 54 | const base::FilePath& external_path, |
| 55 | int renderer_pending_host_id, |
| 56 | const DataFromWebURLResponseCallback& callback, |
| 57 | const std::vector<int>& browser_pending_host_ids) { |
[email protected] | 2e0ec62 | 2014-04-11 07:18:32 | [diff] [blame] | 58 | DCHECK_EQ(1U, browser_pending_host_ids.size()); |
[email protected] | c6420f08 | 2013-09-18 22:42:41 | [diff] [blame] | 59 | int browser_pending_host_id = 0; |
| 60 | |
| 61 | if (browser_pending_host_ids.size() == 1) |
| 62 | browser_pending_host_id = browser_pending_host_ids[0]; |
| 63 | |
| 64 | ppapi::URLResponseInfoData data = in_data; |
| 65 | |
[email protected] | ad63b5c | 2014-04-11 21:12:36 | [diff] [blame] | 66 | data.body_as_file_ref = |
| 67 | ppapi::MakeExternalFileRefCreateInfo(external_path, |
| 68 | std::string(), |
| 69 | browser_pending_host_id, |
| 70 | renderer_pending_host_id); |
[email protected] | c6420f08 | 2013-09-18 22:42:41 | [diff] [blame] | 71 | callback.Run(data); |
| 72 | } |
| 73 | |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 74 | } // namespace |
| 75 | |
[email protected] | c6420f08 | 2013-09-18 22:42:41 | [diff] [blame] | 76 | void DataFromWebURLResponse(RendererPpapiHostImpl* host_impl, |
| 77 | PP_Instance pp_instance, |
[email protected] | 7c6fcf3 | 2013-07-31 07:23:25 | [diff] [blame] | 78 | const WebURLResponse& response, |
| 79 | const DataFromWebURLResponseCallback& callback) { |
[email protected] | b68572a | 2013-07-10 18:21:51 | [diff] [blame] | 80 | ppapi::URLResponseInfoData data; |
kinuko | f19cde7 | 2015-12-18 23:15:25 | [diff] [blame] | 81 | data.url = response.url().string().utf8(); |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 82 | data.status_code = response.httpStatusCode(); |
| 83 | data.status_text = response.httpStatusText().utf8(); |
| 84 | if (IsRedirect(data.status_code)) { |
[email protected] | ad63b5c | 2014-04-11 21:12:36 | [diff] [blame] | 85 | data.redirect_url = |
| 86 | response.httpHeaderField(WebString::fromUTF8("Location")).utf8(); |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | HeaderFlattener flattener; |
| 90 | response.visitHTTPHeaderFields(&flattener); |
| 91 | data.headers = flattener.buffer(); |
| 92 | |
| 93 | WebString file_path = response.downloadFilePath(); |
| 94 | if (!file_path.isEmpty()) { |
kinuko | b473f00 | 2016-02-22 05:23:19 | [diff] [blame] | 95 | base::FilePath external_path = blink::WebStringToFilePath(file_path); |
[email protected] | c6420f08 | 2013-09-18 22:42:41 | [diff] [blame] | 96 | // TODO(teravest): Write a utility function to create resource hosts in the |
| 97 | // renderer and browser. |
| 98 | PepperFileRefRendererHost* renderer_host = |
| 99 | new PepperFileRefRendererHost(host_impl, pp_instance, 0, external_path); |
| 100 | int renderer_pending_host_id = |
| 101 | host_impl->GetPpapiHost()->AddPendingResourceHost( |
| 102 | scoped_ptr<ppapi::host::ResourceHost>(renderer_host)); |
[email protected] | 76bf34a | 2013-09-05 23:36:16 | [diff] [blame] | 103 | |
[email protected] | c6420f08 | 2013-09-18 22:42:41 | [diff] [blame] | 104 | std::vector<IPC::Message> create_msgs; |
[email protected] | 2e0ec62 | 2014-04-11 07:18:32 | [diff] [blame] | 105 | create_msgs.push_back(PpapiHostMsg_FileRef_CreateForRawFS(external_path)); |
[email protected] | ad63b5c | 2014-04-11 21:12:36 | [diff] [blame] | 106 | host_impl->CreateBrowserResourceHosts(pp_instance, |
| 107 | create_msgs, |
| 108 | base::Bind(&DidCreateResourceHosts, |
| 109 | data, |
| 110 | external_path, |
| 111 | renderer_pending_host_id, |
| 112 | callback)); |
[email protected] | c6420f08 | 2013-09-18 22:42:41 | [diff] [blame] | 113 | } else { |
skyostil | 2d3b5bd | 2015-05-27 15:40:59 | [diff] [blame] | 114 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 115 | base::Bind(callback, data)); |
[email protected] | c6420f08 | 2013-09-18 22:42:41 | [diff] [blame] | 116 | } |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | b68572a | 2013-07-10 18:21:51 | [diff] [blame] | 119 | } // namespace content |