1 // Copyright 2013 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.
5 #include "content/renderer/pepper/url_response_info_util.h"
8 #include "base/files/file_path.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "content/public/renderer/renderer_ppapi_host.h"
13 #include "content/renderer/pepper/pepper_file_ref_renderer_host.h"
14 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
15 #include "ipc/ipc_message.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17 #include "ppapi/shared_impl/url_response_info_data.h"
18 #include "third_party/WebKit/public/platform/WebCString.h"
19 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h"
20 #include "third_party/WebKit/public/platform/WebString.h"
21 #include "third_party/WebKit/public/platform/WebURL.h"
22 #include "third_party/WebKit/public/platform/WebURLResponse.h"
24 using blink::WebHTTPHeaderVisitor
;
25 using blink::WebString
;
26 using blink::WebURLResponse
;
32 class HeaderFlattener
: public WebHTTPHeaderVisitor
{
34 const std::string
& buffer() const { return buffer_
; }
36 virtual void visitHeader(const WebString
& name
, const WebString
& value
) {
39 buffer_
.append(name
.utf8());
41 buffer_
.append(value
.utf8());
48 bool IsRedirect(int32_t status
) { return status
>= 300 && status
<= 399; }
50 void DidCreateResourceHosts(const ppapi::URLResponseInfoData
& in_data
,
51 const base::FilePath
& external_path
,
52 int renderer_pending_host_id
,
53 const DataFromWebURLResponseCallback
& callback
,
54 const std::vector
<int>& browser_pending_host_ids
) {
55 DCHECK_EQ(1U, browser_pending_host_ids
.size());
56 int browser_pending_host_id
= 0;
58 if (browser_pending_host_ids
.size() == 1)
59 browser_pending_host_id
= browser_pending_host_ids
[0];
61 ppapi::URLResponseInfoData data
= in_data
;
63 data
.body_as_file_ref
=
64 ppapi::MakeExternalFileRefCreateInfo(external_path
,
66 browser_pending_host_id
,
67 renderer_pending_host_id
);
73 void DataFromWebURLResponse(RendererPpapiHostImpl
* host_impl
,
74 PP_Instance pp_instance
,
75 const WebURLResponse
& response
,
76 const DataFromWebURLResponseCallback
& callback
) {
77 ppapi::URLResponseInfoData data
;
78 data
.url
= response
.url().spec();
79 data
.status_code
= response
.httpStatusCode();
80 data
.status_text
= response
.httpStatusText().utf8();
81 if (IsRedirect(data
.status_code
)) {
83 response
.httpHeaderField(WebString::fromUTF8("Location")).utf8();
86 HeaderFlattener flattener
;
87 response
.visitHTTPHeaderFields(&flattener
);
88 data
.headers
= flattener
.buffer();
90 WebString file_path
= response
.downloadFilePath();
91 if (!file_path
.isEmpty()) {
92 base::FilePath external_path
= base::FilePath::FromUTF16Unsafe(file_path
);
93 // TODO(teravest): Write a utility function to create resource hosts in the
94 // renderer and browser.
95 PepperFileRefRendererHost
* renderer_host
=
96 new PepperFileRefRendererHost(host_impl
, pp_instance
, 0, external_path
);
97 int renderer_pending_host_id
=
98 host_impl
->GetPpapiHost()->AddPendingResourceHost(
99 scoped_ptr
<ppapi::host::ResourceHost
>(renderer_host
));
101 std::vector
<IPC::Message
> create_msgs
;
102 create_msgs
.push_back(PpapiHostMsg_FileRef_CreateForRawFS(external_path
));
103 host_impl
->CreateBrowserResourceHosts(pp_instance
,
105 base::Bind(&DidCreateResourceHosts
,
108 renderer_pending_host_id
,
111 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
112 base::Bind(callback
, data
));
116 } // namespace content