OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "webkit/plugins/ppapi/ppb_url_response_info_impl.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "ppapi/c/pp_var.h" | |
9 #include "ppapi/shared_impl/var.h" | |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebHTTPHeade
rVisitor.h" | |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRespon
se.h" | |
14 #include "webkit/base/file_path_string_conversions.h" | |
15 #include "webkit/plugins/ppapi/common.h" | |
16 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
17 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | |
18 #include "webkit/plugins/ppapi/resource_helper.h" | |
19 | |
20 using ppapi::StringVar; | |
21 using ppapi::thunk::PPB_URLResponseInfo_API; | |
22 using WebKit::WebHTTPHeaderVisitor; | |
23 using WebKit::WebString; | |
24 using WebKit::WebURLResponse; | |
25 | |
26 namespace webkit { | |
27 namespace ppapi { | |
28 | |
29 namespace { | |
30 | |
31 class HeaderFlattener : public WebHTTPHeaderVisitor { | |
32 public: | |
33 const std::string& buffer() const { return buffer_; } | |
34 | |
35 virtual void visitHeader(const WebString& name, const WebString& value) { | |
36 if (!buffer_.empty()) | |
37 buffer_.append("\n"); | |
38 buffer_.append(name.utf8()); | |
39 buffer_.append(": "); | |
40 buffer_.append(value.utf8()); | |
41 } | |
42 | |
43 private: | |
44 std::string buffer_; | |
45 }; | |
46 | |
47 bool IsRedirect(int32_t status) { | |
48 return status >= 300 && status <= 399; | |
49 } | |
50 | |
51 } // namespace | |
52 | |
53 PPB_URLResponseInfo_Impl::PPB_URLResponseInfo_Impl(PP_Instance instance) | |
54 : Resource(::ppapi::OBJECT_IS_IMPL, instance), | |
55 status_code_(-1) { | |
56 } | |
57 | |
58 PPB_URLResponseInfo_Impl::~PPB_URLResponseInfo_Impl() { | |
59 } | |
60 | |
61 bool PPB_URLResponseInfo_Impl::Initialize(const WebURLResponse& response) { | |
62 url_ = response.url().spec(); | |
63 status_code_ = response.httpStatusCode(); | |
64 status_text_ = response.httpStatusText().utf8(); | |
65 if (IsRedirect(status_code_)) { | |
66 redirect_url_ = response.httpHeaderField( | |
67 WebString::fromUTF8("Location")).utf8(); | |
68 } | |
69 | |
70 HeaderFlattener flattener; | |
71 response.visitHTTPHeaderFields(&flattener); | |
72 headers_ = flattener.buffer(); | |
73 | |
74 WebString file_path = response.downloadFilePath(); | |
75 if (!file_path.isEmpty()) { | |
76 body_ = PPB_FileRef_Impl::CreateExternal( | |
77 pp_instance(), | |
78 webkit_base::WebStringToFilePath(file_path), | |
79 std::string()); | |
80 } | |
81 return true; | |
82 } | |
83 | |
84 PPB_URLResponseInfo_API* PPB_URLResponseInfo_Impl::AsPPB_URLResponseInfo_API() { | |
85 return this; | |
86 } | |
87 | |
88 PP_Var PPB_URLResponseInfo_Impl::GetProperty(PP_URLResponseProperty property) { | |
89 switch (property) { | |
90 case PP_URLRESPONSEPROPERTY_URL: | |
91 return StringVar::StringToPPVar(url_); | |
92 case PP_URLRESPONSEPROPERTY_REDIRECTURL: | |
93 if (IsRedirect(status_code_)) | |
94 return StringVar::StringToPPVar(redirect_url_); | |
95 break; | |
96 case PP_URLRESPONSEPROPERTY_REDIRECTMETHOD: | |
97 if (IsRedirect(status_code_)) | |
98 return StringVar::StringToPPVar(status_text_); | |
99 break; | |
100 case PP_URLRESPONSEPROPERTY_STATUSCODE: | |
101 return PP_MakeInt32(status_code_); | |
102 case PP_URLRESPONSEPROPERTY_STATUSLINE: | |
103 return StringVar::StringToPPVar(status_text_); | |
104 case PP_URLRESPONSEPROPERTY_HEADERS: | |
105 return StringVar::StringToPPVar(headers_); | |
106 } | |
107 // The default is to return an undefined PP_Var. | |
108 return PP_MakeUndefined(); | |
109 } | |
110 | |
111 PP_Resource PPB_URLResponseInfo_Impl::GetBodyAsFileRef() { | |
112 if (!body_.get()) | |
113 return 0; | |
114 return body_->GetReference(); | |
115 } | |
116 | |
117 } // namespace ppapi | |
118 } // namespace webkit | |
OLD | NEW |