OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "webkit/glue/plugins/pepper_url_request_info.h" | 5 #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
10 #include "net/http/http_util.h" | 10 #include "net/http/http_util.h" |
11 #include "ppapi/c/pp_var.h" | 11 #include "ppapi/c/pp_var.h" |
12 #include "third_party/WebKit/WebKit/chromium/public/WebData.h" | 12 #include "third_party/WebKit/WebKit/chromium/public/WebData.h" |
13 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" | 13 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" |
14 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | 14 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
15 #include "third_party/WebKit/WebKit/chromium/public/WebHTTPBody.h" | 15 #include "third_party/WebKit/WebKit/chromium/public/WebHTTPBody.h" |
16 #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" | 16 #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" |
17 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" | 17 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" |
18 #include "webkit/glue/plugins/pepper_common.h" | 18 #include "webkit/plugins/ppapi/common.h" |
19 #include "webkit/glue/plugins/pepper_file_ref.h" | 19 #include "webkit/plugins/ppapi/plugin_module.h" |
20 #include "webkit/glue/plugins/pepper_plugin_module.h" | 20 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
21 #include "webkit/glue/plugins/pepper_string.h" | 21 #include "webkit/plugins/ppapi/string.h" |
22 #include "webkit/glue/plugins/pepper_var.h" | 22 #include "webkit/plugins/ppapi/var.h" |
23 #include "webkit/glue/webkit_glue.h" | 23 #include "webkit/glue/webkit_glue.h" |
24 | 24 |
25 using WebKit::WebData; | 25 using WebKit::WebData; |
26 using WebKit::WebHTTPBody; | 26 using WebKit::WebHTTPBody; |
27 using WebKit::WebString; | 27 using WebKit::WebString; |
28 using WebKit::WebFrame; | 28 using WebKit::WebFrame; |
29 using WebKit::WebURL; | 29 using WebKit::WebURL; |
30 using WebKit::WebURLRequest; | 30 using WebKit::WebURLRequest; |
31 | 31 |
32 namespace pepper { | 32 namespace webkit { |
| 33 namespace ppapi { |
33 | 34 |
34 namespace { | 35 namespace { |
35 | 36 |
36 // If any of these request headers are specified, they will not be sent. | 37 // If any of these request headers are specified, they will not be sent. |
37 // TODO(darin): Add more based on security considerations? | 38 // TODO(darin): Add more based on security considerations? |
38 const char* const kIgnoredRequestHeaders[] = { | 39 const char* const kIgnoredRequestHeaders[] = { |
39 "content-length" | 40 "content-length" |
40 }; | 41 }; |
41 | 42 |
42 PP_Bool IsIgnoredRequestHeader(const std::string& name) { | 43 PP_Bool IsIgnoredRequestHeader(const std::string& name) { |
43 for (size_t i = 0; i < arraysize(kIgnoredRequestHeaders); ++i) { | 44 for (size_t i = 0; i < arraysize(kIgnoredRequestHeaders); ++i) { |
44 if (LowerCaseEqualsASCII(name, kIgnoredRequestHeaders[i])) | 45 if (LowerCaseEqualsASCII(name, kIgnoredRequestHeaders[i])) |
45 return PP_TRUE; | 46 return PP_TRUE; |
46 } | 47 } |
47 return PP_FALSE; | 48 return PP_FALSE; |
48 } | 49 } |
49 | 50 |
50 PP_Resource Create(PP_Module module_id) { | 51 PP_Resource Create(PP_Module module_id) { |
51 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); | 52 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); |
52 if (!module) | 53 if (!module) |
53 return 0; | 54 return 0; |
54 | 55 |
55 URLRequestInfo* request = new URLRequestInfo(module); | 56 PPB_URLRequestInfo_Impl* request = new PPB_URLRequestInfo_Impl(module); |
56 | 57 |
57 return request->GetReference(); | 58 return request->GetReference(); |
58 } | 59 } |
59 | 60 |
60 PP_Bool IsURLRequestInfo(PP_Resource resource) { | 61 PP_Bool IsURLRequestInfo(PP_Resource resource) { |
61 return BoolToPPBool(!!Resource::GetAs<URLRequestInfo>(resource)); | 62 return BoolToPPBool(!!Resource::GetAs<PPB_URLRequestInfo_Impl>(resource)); |
62 } | 63 } |
63 | 64 |
64 PP_Bool SetProperty(PP_Resource request_id, | 65 PP_Bool SetProperty(PP_Resource request_id, |
65 PP_URLRequestProperty property, | 66 PP_URLRequestProperty property, |
66 PP_Var var) { | 67 PP_Var var) { |
67 scoped_refptr<URLRequestInfo> request( | 68 scoped_refptr<PPB_URLRequestInfo_Impl> request( |
68 Resource::GetAs<URLRequestInfo>(request_id)); | 69 Resource::GetAs<PPB_URLRequestInfo_Impl>(request_id)); |
69 if (!request) | 70 if (!request) |
70 return PP_FALSE; | 71 return PP_FALSE; |
71 | 72 |
72 if (var.type == PP_VARTYPE_BOOL) { | 73 if (var.type == PP_VARTYPE_BOOL) { |
73 return BoolToPPBool( | 74 return BoolToPPBool( |
74 request->SetBooleanProperty(property, | 75 request->SetBooleanProperty(property, |
75 PPBoolToBool(var.value.as_bool))); | 76 PPBoolToBool(var.value.as_bool))); |
76 } | 77 } |
77 | 78 |
78 if (var.type == PP_VARTYPE_STRING) { | 79 if (var.type == PP_VARTYPE_STRING) { |
79 scoped_refptr<StringVar> string(StringVar::FromPPVar(var)); | 80 scoped_refptr<StringVar> string(StringVar::FromPPVar(var)); |
80 if (string) { | 81 if (string) { |
81 return BoolToPPBool(request->SetStringProperty(property, | 82 return BoolToPPBool(request->SetStringProperty(property, |
82 string->value())); | 83 string->value())); |
83 } | 84 } |
84 } | 85 } |
85 | 86 |
86 return PP_FALSE; | 87 return PP_FALSE; |
87 } | 88 } |
88 | 89 |
89 PP_Bool AppendDataToBody(PP_Resource request_id, | 90 PP_Bool AppendDataToBody(PP_Resource request_id, |
90 const char* data, | 91 const char* data, |
91 uint32_t len) { | 92 uint32_t len) { |
92 scoped_refptr<URLRequestInfo> request( | 93 scoped_refptr<PPB_URLRequestInfo_Impl> request( |
93 Resource::GetAs<URLRequestInfo>(request_id)); | 94 Resource::GetAs<PPB_URLRequestInfo_Impl>(request_id)); |
94 if (!request) | 95 if (!request) |
95 return PP_FALSE; | 96 return PP_FALSE; |
96 | 97 |
97 return BoolToPPBool(request->AppendDataToBody(std::string(data, len))); | 98 return BoolToPPBool(request->AppendDataToBody(std::string(data, len))); |
98 } | 99 } |
99 | 100 |
100 PP_Bool AppendFileToBody(PP_Resource request_id, | 101 PP_Bool AppendFileToBody(PP_Resource request_id, |
101 PP_Resource file_ref_id, | 102 PP_Resource file_ref_id, |
102 int64_t start_offset, | 103 int64_t start_offset, |
103 int64_t number_of_bytes, | 104 int64_t number_of_bytes, |
104 PP_Time expected_last_modified_time) { | 105 PP_Time expected_last_modified_time) { |
105 scoped_refptr<URLRequestInfo> request( | 106 scoped_refptr<PPB_URLRequestInfo_Impl> request( |
106 Resource::GetAs<URLRequestInfo>(request_id)); | 107 Resource::GetAs<PPB_URLRequestInfo_Impl>(request_id)); |
107 if (!request) | 108 if (!request) |
108 return PP_FALSE; | 109 return PP_FALSE; |
109 | 110 |
110 scoped_refptr<FileRef> file_ref(Resource::GetAs<FileRef>(file_ref_id)); | 111 scoped_refptr<PPB_FileRef_Impl> file_ref( |
| 112 Resource::GetAs<PPB_FileRef_Impl>(file_ref_id)); |
111 if (!file_ref) | 113 if (!file_ref) |
112 return PP_FALSE; | 114 return PP_FALSE; |
113 | 115 |
114 return BoolToPPBool(request->AppendFileToBody(file_ref, | 116 return BoolToPPBool(request->AppendFileToBody(file_ref, |
115 start_offset, | 117 start_offset, |
116 number_of_bytes, | 118 number_of_bytes, |
117 expected_last_modified_time)); | 119 expected_last_modified_time)); |
118 } | 120 } |
119 | 121 |
120 const PPB_URLRequestInfo ppb_urlrequestinfo = { | 122 const PPB_URLRequestInfo ppb_urlrequestinfo = { |
121 &Create, | 123 &Create, |
122 &IsURLRequestInfo, | 124 &IsURLRequestInfo, |
123 &SetProperty, | 125 &SetProperty, |
124 &AppendDataToBody, | 126 &AppendDataToBody, |
125 &AppendFileToBody | 127 &AppendFileToBody |
126 }; | 128 }; |
127 | 129 |
128 } // namespace | 130 } // namespace |
129 | 131 |
130 struct URLRequestInfo::BodyItem { | 132 struct PPB_URLRequestInfo_Impl::BodyItem { |
131 BodyItem(const std::string& data) | 133 BodyItem(const std::string& data) |
132 : data(data), | 134 : data(data), |
133 start_offset(0), | 135 start_offset(0), |
134 number_of_bytes(-1), | 136 number_of_bytes(-1), |
135 expected_last_modified_time(0.0) { | 137 expected_last_modified_time(0.0) { |
136 } | 138 } |
137 | 139 |
138 BodyItem(FileRef* file_ref, | 140 BodyItem(PPB_FileRef_Impl* file_ref, |
139 int64_t start_offset, | 141 int64_t start_offset, |
140 int64_t number_of_bytes, | 142 int64_t number_of_bytes, |
141 PP_Time expected_last_modified_time) | 143 PP_Time expected_last_modified_time) |
142 : file_ref(file_ref), | 144 : file_ref(file_ref), |
143 start_offset(start_offset), | 145 start_offset(start_offset), |
144 number_of_bytes(number_of_bytes), | 146 number_of_bytes(number_of_bytes), |
145 expected_last_modified_time(expected_last_modified_time) { | 147 expected_last_modified_time(expected_last_modified_time) { |
146 } | 148 } |
147 | 149 |
148 std::string data; | 150 std::string data; |
149 scoped_refptr<FileRef> file_ref; | 151 scoped_refptr<PPB_FileRef_Impl> file_ref; |
150 int64_t start_offset; | 152 int64_t start_offset; |
151 int64_t number_of_bytes; | 153 int64_t number_of_bytes; |
152 PP_Time expected_last_modified_time; | 154 PP_Time expected_last_modified_time; |
153 }; | 155 }; |
154 | 156 |
155 URLRequestInfo::URLRequestInfo(PluginModule* module) | 157 PPB_URLRequestInfo_Impl::PPB_URLRequestInfo_Impl(PluginModule* module) |
156 : Resource(module), | 158 : Resource(module), |
157 stream_to_file_(false), | 159 stream_to_file_(false), |
158 follow_redirects_(true), | 160 follow_redirects_(true), |
159 record_download_progress_(false), | 161 record_download_progress_(false), |
160 record_upload_progress_(false) { | 162 record_upload_progress_(false) { |
161 } | 163 } |
162 | 164 |
163 URLRequestInfo::~URLRequestInfo() { | 165 PPB_URLRequestInfo_Impl::~PPB_URLRequestInfo_Impl() { |
164 } | 166 } |
165 | 167 |
166 // static | 168 // static |
167 const PPB_URLRequestInfo* URLRequestInfo::GetInterface() { | 169 const PPB_URLRequestInfo* PPB_URLRequestInfo_Impl::GetInterface() { |
168 return &ppb_urlrequestinfo; | 170 return &ppb_urlrequestinfo; |
169 } | 171 } |
170 | 172 |
171 URLRequestInfo* URLRequestInfo::AsURLRequestInfo() { | 173 PPB_URLRequestInfo_Impl* PPB_URLRequestInfo_Impl::AsPPB_URLRequestInfo_Impl() { |
172 return this; | 174 return this; |
173 } | 175 } |
174 | 176 |
175 bool URLRequestInfo::SetBooleanProperty(PP_URLRequestProperty property, | 177 bool PPB_URLRequestInfo_Impl::SetBooleanProperty(PP_URLRequestProperty property, |
176 bool value) { | 178 bool value) { |
177 switch (property) { | 179 switch (property) { |
178 case PP_URLREQUESTPROPERTY_STREAMTOFILE: | 180 case PP_URLREQUESTPROPERTY_STREAMTOFILE: |
179 stream_to_file_ = value; | 181 stream_to_file_ = value; |
180 return true; | 182 return true; |
181 case PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS: | 183 case PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS: |
182 follow_redirects_ = value; | 184 follow_redirects_ = value; |
183 return true; | 185 return true; |
184 case PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS: | 186 case PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS: |
185 record_download_progress_ = value; | 187 record_download_progress_ = value; |
186 return true; | 188 return true; |
187 case PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS: | 189 case PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS: |
188 record_upload_progress_ = value; | 190 record_upload_progress_ = value; |
189 return true; | 191 return true; |
190 default: | 192 default: |
191 //NOTIMPLEMENTED(); // TODO(darin): Implement me! | 193 //NOTIMPLEMENTED(); // TODO(darin): Implement me! |
192 return false; | 194 return false; |
193 } | 195 } |
194 } | 196 } |
195 | 197 |
196 bool URLRequestInfo::SetStringProperty(PP_URLRequestProperty property, | 198 bool PPB_URLRequestInfo_Impl::SetStringProperty(PP_URLRequestProperty property, |
197 const std::string& value) { | 199 const std::string& value) { |
198 // TODO(darin): Validate input. Perhaps at a different layer? | 200 // TODO(darin): Validate input. Perhaps at a different layer? |
199 switch (property) { | 201 switch (property) { |
200 case PP_URLREQUESTPROPERTY_URL: | 202 case PP_URLREQUESTPROPERTY_URL: |
201 url_ = value; // NOTE: This may be a relative URL. | 203 url_ = value; // NOTE: This may be a relative URL. |
202 return true; | 204 return true; |
203 case PP_URLREQUESTPROPERTY_METHOD: | 205 case PP_URLREQUESTPROPERTY_METHOD: |
204 method_ = value; | 206 method_ = value; |
205 return true; | 207 return true; |
206 case PP_URLREQUESTPROPERTY_HEADERS: | 208 case PP_URLREQUESTPROPERTY_HEADERS: |
207 headers_ = value; | 209 headers_ = value; |
208 return true; | 210 return true; |
209 default: | 211 default: |
210 return false; | 212 return false; |
211 } | 213 } |
212 } | 214 } |
213 | 215 |
214 bool URLRequestInfo::AppendDataToBody(const std::string& data) { | 216 bool PPB_URLRequestInfo_Impl::AppendDataToBody(const std::string& data) { |
215 if (!data.empty()) | 217 if (!data.empty()) |
216 body_.push_back(BodyItem(data)); | 218 body_.push_back(BodyItem(data)); |
217 return true; | 219 return true; |
218 } | 220 } |
219 | 221 |
220 bool URLRequestInfo::AppendFileToBody(FileRef* file_ref, | 222 bool PPB_URLRequestInfo_Impl::AppendFileToBody( |
221 int64_t start_offset, | 223 PPB_FileRef_Impl* file_ref, |
222 int64_t number_of_bytes, | 224 int64_t start_offset, |
223 PP_Time expected_last_modified_time) { | 225 int64_t number_of_bytes, |
| 226 PP_Time expected_last_modified_time) { |
224 // Ignore a call to append nothing. | 227 // Ignore a call to append nothing. |
225 if (number_of_bytes == 0) | 228 if (number_of_bytes == 0) |
226 return true; | 229 return true; |
227 | 230 |
228 // Check for bad values. (-1 means read until end of file.) | 231 // Check for bad values. (-1 means read until end of file.) |
229 if (start_offset < 0 || number_of_bytes < -1) | 232 if (start_offset < 0 || number_of_bytes < -1) |
230 return false; | 233 return false; |
231 | 234 |
232 body_.push_back(BodyItem(file_ref, | 235 body_.push_back(BodyItem(file_ref, |
233 start_offset, | 236 start_offset, |
234 number_of_bytes, | 237 number_of_bytes, |
235 expected_last_modified_time)); | 238 expected_last_modified_time)); |
236 return true; | 239 return true; |
237 } | 240 } |
238 | 241 |
239 WebURLRequest URLRequestInfo::ToWebURLRequest(WebFrame* frame) const { | 242 WebURLRequest PPB_URLRequestInfo_Impl::ToWebURLRequest(WebFrame* frame) const { |
240 WebURLRequest web_request; | 243 WebURLRequest web_request; |
241 web_request.initialize(); | 244 web_request.initialize(); |
242 web_request.setURL(frame->document().completeURL(WebString::fromUTF8(url_))); | 245 web_request.setURL(frame->document().completeURL(WebString::fromUTF8(url_))); |
243 web_request.setDownloadToFile(stream_to_file_); | 246 web_request.setDownloadToFile(stream_to_file_); |
244 | 247 |
245 if (!method_.empty()) | 248 if (!method_.empty()) |
246 web_request.setHTTPMethod(WebString::fromUTF8(method_)); | 249 web_request.setHTTPMethod(WebString::fromUTF8(method_)); |
247 | 250 |
248 if (!headers_.empty()) { | 251 if (!headers_.empty()) { |
249 net::HttpUtil::HeadersIterator it(headers_.begin(), headers_.end(), "\n"); | 252 net::HttpUtil::HeadersIterator it(headers_.begin(), headers_.end(), "\n"); |
(...skipping 22 matching lines...) Expand all Loading... |
272 http_body.appendData(WebData(body_[i].data)); | 275 http_body.appendData(WebData(body_[i].data)); |
273 } | 276 } |
274 } | 277 } |
275 web_request.setHTTPBody(http_body); | 278 web_request.setHTTPBody(http_body); |
276 } | 279 } |
277 | 280 |
278 frame->setReferrerForRequest(web_request, WebURL()); // Use default. | 281 frame->setReferrerForRequest(web_request, WebURL()); // Use default. |
279 return web_request; | 282 return web_request; |
280 } | 283 } |
281 | 284 |
282 } // namespace pepper | 285 } // namespace ppapi |
| 286 } // namespace webkit |
| 287 |
OLD | NEW |