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_response_info.h" | 5 #include "webkit/plugins/ppapi/ppb_url_response_info_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ppapi/c/pp_var.h" | 8 #include "ppapi/c/pp_var.h" |
9 #include "third_party/WebKit/WebKit/chromium/public/WebHTTPHeaderVisitor.h" | 9 #include "third_party/WebKit/WebKit/chromium/public/WebHTTPHeaderVisitor.h" |
10 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" | 10 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" |
11 #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" | 11 #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" |
12 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" | 12 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" |
13 #include "webkit/glue/plugins/pepper_common.h" | 13 #include "webkit/plugins/ppapi/common.h" |
14 #include "webkit/glue/plugins/pepper_file_ref.h" | 14 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
15 #include "webkit/glue/plugins/pepper_var.h" | 15 #include "webkit/plugins/ppapi/var.h" |
16 #include "webkit/glue/webkit_glue.h" | 16 #include "webkit/glue/webkit_glue.h" |
17 | 17 |
18 using WebKit::WebHTTPHeaderVisitor; | 18 using WebKit::WebHTTPHeaderVisitor; |
19 using WebKit::WebString; | 19 using WebKit::WebString; |
20 using WebKit::WebURLResponse; | 20 using WebKit::WebURLResponse; |
21 | 21 |
22 namespace pepper { | 22 namespace webkit { |
| 23 namespace ppapi { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 class HeaderFlattener : public WebHTTPHeaderVisitor { | 27 class HeaderFlattener : public WebHTTPHeaderVisitor { |
27 public: | 28 public: |
28 const std::string& buffer() const { return buffer_; } | 29 const std::string& buffer() const { return buffer_; } |
29 | 30 |
30 virtual void visitHeader(const WebString& name, const WebString& value) { | 31 virtual void visitHeader(const WebString& name, const WebString& value) { |
31 if (!buffer_.empty()) | 32 if (!buffer_.empty()) |
32 buffer_.append("\n"); | 33 buffer_.append("\n"); |
33 buffer_.append(name.utf8()); | 34 buffer_.append(name.utf8()); |
34 buffer_.append(": "); | 35 buffer_.append(": "); |
35 buffer_.append(value.utf8()); | 36 buffer_.append(value.utf8()); |
36 } | 37 } |
37 | 38 |
38 private: | 39 private: |
39 std::string buffer_; | 40 std::string buffer_; |
40 }; | 41 }; |
41 | 42 |
42 PP_Bool IsURLResponseInfo(PP_Resource resource) { | 43 PP_Bool IsURLResponseInfo(PP_Resource resource) { |
43 return BoolToPPBool(!!Resource::GetAs<URLResponseInfo>(resource)); | 44 return BoolToPPBool(!!Resource::GetAs<PPB_URLResponseInfo_Impl>(resource)); |
44 } | 45 } |
45 | 46 |
46 PP_Var GetProperty(PP_Resource response_id, | 47 PP_Var GetProperty(PP_Resource response_id, |
47 PP_URLResponseProperty property) { | 48 PP_URLResponseProperty property) { |
48 scoped_refptr<URLResponseInfo> response( | 49 scoped_refptr<PPB_URLResponseInfo_Impl> response( |
49 Resource::GetAs<URLResponseInfo>(response_id)); | 50 Resource::GetAs<PPB_URLResponseInfo_Impl>(response_id)); |
50 if (!response) | 51 if (!response) |
51 return PP_MakeUndefined(); | 52 return PP_MakeUndefined(); |
52 | 53 |
53 return response->GetProperty(property); | 54 return response->GetProperty(property); |
54 } | 55 } |
55 | 56 |
56 PP_Resource GetBody(PP_Resource response_id) { | 57 PP_Resource GetBody(PP_Resource response_id) { |
57 scoped_refptr<URLResponseInfo> response( | 58 scoped_refptr<PPB_URLResponseInfo_Impl> response( |
58 Resource::GetAs<URLResponseInfo>(response_id)); | 59 Resource::GetAs<PPB_URLResponseInfo_Impl>(response_id)); |
59 if (!response.get()) | 60 if (!response.get()) |
60 return 0; | 61 return 0; |
61 | 62 |
62 FileRef* body = response->body(); | 63 PPB_FileRef_Impl* body = response->body(); |
63 if (!body) | 64 if (!body) |
64 return 0; | 65 return 0; |
65 body->AddRef(); // AddRef for the caller. | 66 body->AddRef(); // AddRef for the caller. |
66 | 67 |
67 return body->GetReference(); | 68 return body->GetReference(); |
68 } | 69 } |
69 | 70 |
70 const PPB_URLResponseInfo ppb_urlresponseinfo = { | 71 const PPB_URLResponseInfo ppb_urlresponseinfo = { |
71 &IsURLResponseInfo, | 72 &IsURLResponseInfo, |
72 &GetProperty, | 73 &GetProperty, |
73 &GetBody | 74 &GetBody |
74 }; | 75 }; |
75 | 76 |
76 bool IsRedirect(int32_t status) { | 77 bool IsRedirect(int32_t status) { |
77 return status >= 300 && status <= 399; | 78 return status >= 300 && status <= 399; |
78 } | 79 } |
79 | 80 |
80 } // namespace | 81 } // namespace |
81 | 82 |
82 URLResponseInfo::URLResponseInfo(PluginModule* module) | 83 PPB_URLResponseInfo_Impl::PPB_URLResponseInfo_Impl(PluginModule* module) |
83 : Resource(module), | 84 : Resource(module), |
84 status_code_(-1) { | 85 status_code_(-1) { |
85 } | 86 } |
86 | 87 |
87 URLResponseInfo::~URLResponseInfo() { | 88 PPB_URLResponseInfo_Impl::~PPB_URLResponseInfo_Impl() { |
88 } | 89 } |
89 | 90 |
90 // static | 91 // static |
91 const PPB_URLResponseInfo* URLResponseInfo::GetInterface() { | 92 const PPB_URLResponseInfo* PPB_URLResponseInfo_Impl::GetInterface() { |
92 return &ppb_urlresponseinfo; | 93 return &ppb_urlresponseinfo; |
93 } | 94 } |
94 | 95 |
95 URLResponseInfo* URLResponseInfo::AsURLResponseInfo() { | 96 PPB_URLResponseInfo_Impl* |
| 97 PPB_URLResponseInfo_Impl::AsPPB_URLResponseInfo_Impl() { |
96 return this; | 98 return this; |
97 } | 99 } |
98 | 100 |
99 PP_Var URLResponseInfo::GetProperty(PP_URLResponseProperty property) { | 101 PP_Var PPB_URLResponseInfo_Impl::GetProperty(PP_URLResponseProperty property) { |
100 switch (property) { | 102 switch (property) { |
101 case PP_URLRESPONSEPROPERTY_URL: | 103 case PP_URLRESPONSEPROPERTY_URL: |
102 return StringVar::StringToPPVar(module(), url_); | 104 return StringVar::StringToPPVar(module(), url_); |
103 case PP_URLRESPONSEPROPERTY_REDIRECTURL: | 105 case PP_URLRESPONSEPROPERTY_REDIRECTURL: |
104 if (IsRedirect(status_code_)) | 106 if (IsRedirect(status_code_)) |
105 return StringVar::StringToPPVar(module(), redirect_url_); | 107 return StringVar::StringToPPVar(module(), redirect_url_); |
106 break; | 108 break; |
107 case PP_URLRESPONSEPROPERTY_REDIRECTMETHOD: | 109 case PP_URLRESPONSEPROPERTY_REDIRECTMETHOD: |
108 if (IsRedirect(status_code_)) | 110 if (IsRedirect(status_code_)) |
109 return StringVar::StringToPPVar(module(), status_text_); | 111 return StringVar::StringToPPVar(module(), status_text_); |
110 break; | 112 break; |
111 case PP_URLRESPONSEPROPERTY_STATUSCODE: | 113 case PP_URLRESPONSEPROPERTY_STATUSCODE: |
112 return PP_MakeInt32(status_code_); | 114 return PP_MakeInt32(status_code_); |
113 case PP_URLRESPONSEPROPERTY_STATUSLINE: | 115 case PP_URLRESPONSEPROPERTY_STATUSLINE: |
114 return StringVar::StringToPPVar(module(), status_text_); | 116 return StringVar::StringToPPVar(module(), status_text_); |
115 case PP_URLRESPONSEPROPERTY_HEADERS: | 117 case PP_URLRESPONSEPROPERTY_HEADERS: |
116 return StringVar::StringToPPVar(module(), headers_); | 118 return StringVar::StringToPPVar(module(), headers_); |
117 } | 119 } |
118 // The default is to return an undefined PP_Var. | 120 // The default is to return an undefined PP_Var. |
119 return PP_MakeUndefined(); | 121 return PP_MakeUndefined(); |
120 } | 122 } |
121 | 123 |
122 bool URLResponseInfo::Initialize(const WebURLResponse& response) { | 124 bool PPB_URLResponseInfo_Impl::Initialize(const WebURLResponse& response) { |
123 url_ = response.url().spec(); | 125 url_ = response.url().spec(); |
124 status_code_ = response.httpStatusCode(); | 126 status_code_ = response.httpStatusCode(); |
125 status_text_ = response.httpStatusText().utf8(); | 127 status_text_ = response.httpStatusText().utf8(); |
126 if (IsRedirect(status_code_)) { | 128 if (IsRedirect(status_code_)) { |
127 redirect_url_ = response.httpHeaderField( | 129 redirect_url_ = response.httpHeaderField( |
128 WebString::fromUTF8("Location")).utf8(); | 130 WebString::fromUTF8("Location")).utf8(); |
129 } | 131 } |
130 | 132 |
131 HeaderFlattener flattener; | 133 HeaderFlattener flattener; |
132 response.visitHTTPHeaderFields(&flattener); | 134 response.visitHTTPHeaderFields(&flattener); |
133 headers_ = flattener.buffer(); | 135 headers_ = flattener.buffer(); |
134 | 136 |
135 WebString file_path = response.downloadFilePath(); | 137 WebString file_path = response.downloadFilePath(); |
136 if (!file_path.isEmpty()) | 138 if (!file_path.isEmpty()) |
137 body_ = new FileRef(module(), webkit_glue::WebStringToFilePath(file_path)); | 139 body_ = new PPB_FileRef_Impl(module(), |
| 140 webkit_glue::WebStringToFilePath(file_path)); |
138 return true; | 141 return true; |
139 } | 142 } |
140 | 143 |
141 } // namespace pepper | 144 } // namespace ppapi |
| 145 } // namespace webkit |
| 146 |
OLD | NEW |