OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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_response_info_util.h" | 5 #include "content/renderer/pepper/url_response_info_util.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "content/public/renderer/renderer_ppapi_host.h" | 10 #include "content/renderer/pepper/ppb_file_ref_impl.h" |
11 #include "content/renderer/pepper/renderer_ppapi_host_impl.h" | |
12 #include "ipc/ipc_message.h" | |
13 #include "ppapi/proxy/ppapi_messages.h" | |
14 #include "ppapi/shared_impl/url_response_info_data.h" | 11 #include "ppapi/shared_impl/url_response_info_data.h" |
15 #include "third_party/WebKit/public/platform/WebCString.h" | 12 #include "third_party/WebKit/public/platform/WebCString.h" |
16 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" | 13 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" |
17 #include "third_party/WebKit/public/platform/WebString.h" | 14 #include "third_party/WebKit/public/platform/WebString.h" |
18 #include "third_party/WebKit/public/platform/WebURL.h" | 15 #include "third_party/WebKit/public/platform/WebURL.h" |
19 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 16 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
20 | 17 |
21 using WebKit::WebHTTPHeaderVisitor; | 18 using WebKit::WebHTTPHeaderVisitor; |
22 using WebKit::WebString; | 19 using WebKit::WebString; |
23 using WebKit::WebURLResponse; | 20 using WebKit::WebURLResponse; |
(...skipping 15 matching lines...) Expand all Loading... |
39 } | 36 } |
40 | 37 |
41 private: | 38 private: |
42 std::string buffer_; | 39 std::string buffer_; |
43 }; | 40 }; |
44 | 41 |
45 bool IsRedirect(int32_t status) { | 42 bool IsRedirect(int32_t status) { |
46 return status >= 300 && status <= 399; | 43 return status >= 300 && status <= 399; |
47 } | 44 } |
48 | 45 |
49 void DidCreateResourceHosts(const ppapi::URLResponseInfoData& in_data, | |
50 const base::FilePath& external_path, | |
51 const DataFromWebURLResponseCallback& callback, | |
52 const std::vector<int>& pending_resource_ids) { | |
53 DCHECK(pending_resource_ids.size() == 1); | |
54 int pending_resource_id = 0; | |
55 | |
56 if (pending_resource_ids.size() == 1) | |
57 pending_resource_id = pending_resource_ids[0]; | |
58 | |
59 ppapi::URLResponseInfoData data = in_data; | |
60 data.body_as_file_ref = ppapi::MakeExternalFileRefCreateInfo( | |
61 external_path, std::string(), pending_resource_id); | |
62 callback.Run(data); | |
63 } | |
64 | |
65 } // namespace | 46 } // namespace |
66 | 47 |
67 void DataFromWebURLResponse(RendererPpapiHostImpl* host_impl, | 48 void DataFromWebURLResponse(PP_Instance pp_instance, |
68 PP_Instance pp_instance, | |
69 const WebURLResponse& response, | 49 const WebURLResponse& response, |
70 const DataFromWebURLResponseCallback& callback) { | 50 const DataFromWebURLResponseCallback& callback) { |
71 ppapi::URLResponseInfoData data; | 51 ppapi::URLResponseInfoData data; |
| 52 |
72 data.url = response.url().spec(); | 53 data.url = response.url().spec(); |
73 data.status_code = response.httpStatusCode(); | 54 data.status_code = response.httpStatusCode(); |
74 data.status_text = response.httpStatusText().utf8(); | 55 data.status_text = response.httpStatusText().utf8(); |
75 if (IsRedirect(data.status_code)) { | 56 if (IsRedirect(data.status_code)) { |
76 data.redirect_url = response.httpHeaderField( | 57 data.redirect_url = response.httpHeaderField( |
77 WebString::fromUTF8("Location")).utf8(); | 58 WebString::fromUTF8("Location")).utf8(); |
78 } | 59 } |
79 | 60 |
80 HeaderFlattener flattener; | 61 HeaderFlattener flattener; |
81 response.visitHTTPHeaderFields(&flattener); | 62 response.visitHTTPHeaderFields(&flattener); |
82 data.headers = flattener.buffer(); | 63 data.headers = flattener.buffer(); |
83 | 64 |
84 WebString file_path = response.downloadFilePath(); | 65 WebString file_path = response.downloadFilePath(); |
85 if (!file_path.isEmpty()) { | 66 if (!file_path.isEmpty()) { |
86 base::FilePath external_path = base::FilePath::FromUTF16Unsafe(file_path); | 67 scoped_refptr<PPB_FileRef_Impl> file_ref( |
87 std::vector<IPC::Message> create_msgs; | 68 PPB_FileRef_Impl::CreateExternal( |
88 create_msgs.push_back(PpapiHostMsg_FileRef_CreateExternal(external_path)); | 69 pp_instance, |
89 host_impl->CreateBrowserResourceHosts( | 70 base::FilePath::FromUTF16Unsafe(file_path), |
90 pp_instance, | 71 std::string())); |
91 create_msgs, | 72 data.body_as_file_ref = file_ref->GetCreateInfo(); |
92 base::Bind(&DidCreateResourceHosts, data, external_path, callback)); | 73 file_ref->GetReference(); // The returned data has one ref for the plugin. |
93 } else { | |
94 base::MessageLoop::current()->PostTask( | |
95 FROM_HERE, | |
96 base::Bind(callback, data)); | |
97 } | 74 } |
| 75 |
| 76 // We post data to a callback instead of returning it here because the new |
| 77 // implementation for FileRef is asynchronous when creating the resource. |
| 78 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, data)); |
98 } | 79 } |
99 | 80 |
100 } // namespace content | 81 } // namespace content |
OLD | NEW |