blob: 723e6579afb681c9aaa400ae233e0e191343d7e7 [file] [log] [blame]
[email protected]b68572a2013-07-10 18:21:511// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]844fecb2012-11-16 20:11:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b68572a2013-07-10 18:21:515#include "content/renderer/pepper/url_response_info_util.h"
[email protected]844fecb2012-11-16 20:11:066
avi1023d012015-12-25 02:39:147#include <stdint.h>
8
[email protected]7c6fcf32013-07-31 07:23:259#include "base/bind.h"
[email protected]35c5feb2013-06-25 04:00:3310#include "base/files/file_path.h"
skyostil2d3b5bd2015-05-27 15:40:5911#include "base/location.h"
12#include "base/single_thread_task_runner.h"
13#include "base/thread_task_runner_handle.h"
[email protected]c6420f082013-09-18 22:42:4114#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]b68572a2013-07-10 18:21:5119#include "ppapi/shared_impl/url_response_info_data.h"
kinukob473f002016-02-22 05:23:1920#include "third_party/WebKit/public/platform/FilePathConversion.h"
[email protected]b68572a2013-07-10 18:21:5121#include "third_party/WebKit/public/platform/WebCString.h"
[email protected]c10884462013-05-30 00:22:0922#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]844fecb2012-11-16 20:11:0626
[email protected]180ef242013-11-07 06:50:4627using blink::WebHTTPHeaderVisitor;
28using blink::WebString;
29using blink::WebURLResponse;
[email protected]844fecb2012-11-16 20:11:0630
[email protected]b68572a2013-07-10 18:21:5131namespace content {
[email protected]844fecb2012-11-16 20:11:0632
33namespace {
34
35class HeaderFlattener : public WebHTTPHeaderVisitor {
36 public:
37 const std::string& buffer() const { return buffer_; }
38
avi5c77d212015-09-25 20:08:2539 void visitHeader(const WebString& name, const WebString& value) override {
[email protected]844fecb2012-11-16 20:11:0640 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]ad63b5c2014-04-11 21:12:3651bool IsRedirect(int32_t status) { return status >= 300 && status <= 399; }
[email protected]844fecb2012-11-16 20:11:0652
[email protected]c6420f082013-09-18 22:42:4153void 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]2e0ec622014-04-11 07:18:3258 DCHECK_EQ(1U, browser_pending_host_ids.size());
[email protected]c6420f082013-09-18 22:42:4159 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]ad63b5c2014-04-11 21:12:3666 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]c6420f082013-09-18 22:42:4171 callback.Run(data);
72}
73
[email protected]844fecb2012-11-16 20:11:0674} // namespace
75
[email protected]c6420f082013-09-18 22:42:4176void DataFromWebURLResponse(RendererPpapiHostImpl* host_impl,
77 PP_Instance pp_instance,
[email protected]7c6fcf32013-07-31 07:23:2578 const WebURLResponse& response,
79 const DataFromWebURLResponseCallback& callback) {
[email protected]b68572a2013-07-10 18:21:5180 ppapi::URLResponseInfoData data;
kinukof19cde72015-12-18 23:15:2581 data.url = response.url().string().utf8();
[email protected]844fecb2012-11-16 20:11:0682 data.status_code = response.httpStatusCode();
83 data.status_text = response.httpStatusText().utf8();
84 if (IsRedirect(data.status_code)) {
[email protected]ad63b5c2014-04-11 21:12:3685 data.redirect_url =
86 response.httpHeaderField(WebString::fromUTF8("Location")).utf8();
[email protected]844fecb2012-11-16 20:11:0687 }
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()) {
kinukob473f002016-02-22 05:23:1995 base::FilePath external_path = blink::WebStringToFilePath(file_path);
[email protected]c6420f082013-09-18 22:42:4196 // 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]76bf34a2013-09-05 23:36:16103
[email protected]c6420f082013-09-18 22:42:41104 std::vector<IPC::Message> create_msgs;
[email protected]2e0ec622014-04-11 07:18:32105 create_msgs.push_back(PpapiHostMsg_FileRef_CreateForRawFS(external_path));
[email protected]ad63b5c2014-04-11 21:12:36106 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]c6420f082013-09-18 22:42:41113 } else {
skyostil2d3b5bd2015-05-27 15:40:59114 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
115 base::Bind(callback, data));
[email protected]c6420f082013-09-18 22:42:41116 }
[email protected]844fecb2012-11-16 20:11:06117}
118
[email protected]b68572a2013-07-10 18:21:51119} // namespace content