Add ICU message format support
[chromium-blink-merge.git] / content / renderer / pepper / url_response_info_util.cc
blob736a95c6d2fc756801a204abb06b491929761bb5
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"
7 #include "base/bind.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;
28 namespace content {
30 namespace {
32 class HeaderFlattener : public WebHTTPHeaderVisitor {
33 public:
34 const std::string& buffer() const { return buffer_; }
36 virtual void visitHeader(const WebString& name, const WebString& value) {
37 if (!buffer_.empty())
38 buffer_.append("\n");
39 buffer_.append(name.utf8());
40 buffer_.append(": ");
41 buffer_.append(value.utf8());
44 private:
45 std::string buffer_;
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,
65 std::string(),
66 browser_pending_host_id,
67 renderer_pending_host_id);
68 callback.Run(data);
71 } // namespace
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)) {
82 data.redirect_url =
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,
104 create_msgs,
105 base::Bind(&DidCreateResourceHosts,
106 data,
107 external_path,
108 renderer_pending_host_id,
109 callback));
110 } else {
111 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
112 base::Bind(callback, data));
116 } // namespace content