| 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 plugins { |
| 24 namespace ppapi { |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 class HeaderFlattener : public WebHTTPHeaderVisitor { | 28 class HeaderFlattener : public WebHTTPHeaderVisitor { |
| 27 public: | 29 public: |
| 28 const std::string& buffer() const { return buffer_; } | 30 const std::string& buffer() const { return buffer_; } |
| 29 | 31 |
| 30 virtual void visitHeader(const WebString& name, const WebString& value) { | 32 virtual void visitHeader(const WebString& name, const WebString& value) { |
| 31 if (!buffer_.empty()) | 33 if (!buffer_.empty()) |
| 32 buffer_.append("\n"); | 34 buffer_.append("\n"); |
| 33 buffer_.append(name.utf8()); | 35 buffer_.append(name.utf8()); |
| 34 buffer_.append(": "); | 36 buffer_.append(": "); |
| 35 buffer_.append(value.utf8()); | 37 buffer_.append(value.utf8()); |
| 36 } | 38 } |
| 37 | 39 |
| 38 private: | 40 private: |
| 39 std::string buffer_; | 41 std::string buffer_; |
| 40 }; | 42 }; |
| 41 | 43 |
| 42 PP_Bool IsURLResponseInfo(PP_Resource resource) { | 44 PP_Bool IsURLResponseInfo(PP_Resource resource) { |
| 43 return BoolToPPBool(!!Resource::GetAs<URLResponseInfo>(resource)); | 45 return BoolToPPBool(!!Resource::GetAs<PPB_URLResponseInfo_Impl>(resource)); |
| 44 } | 46 } |
| 45 | 47 |
| 46 PP_Var GetProperty(PP_Resource response_id, | 48 PP_Var GetProperty(PP_Resource response_id, |
| 47 PP_URLResponseProperty property) { | 49 PP_URLResponseProperty property) { |
| 48 scoped_refptr<URLResponseInfo> response( | 50 scoped_refptr<PPB_URLResponseInfo_Impl> response( |
| 49 Resource::GetAs<URLResponseInfo>(response_id)); | 51 Resource::GetAs<PPB_URLResponseInfo_Impl>(response_id)); |
| 50 if (!response) | 52 if (!response) |
| 51 return PP_MakeUndefined(); | 53 return PP_MakeUndefined(); |
| 52 | 54 |
| 53 return response->GetProperty(property); | 55 return response->GetProperty(property); |
| 54 } | 56 } |
| 55 | 57 |
| 56 PP_Resource GetBody(PP_Resource response_id) { | 58 PP_Resource GetBody(PP_Resource response_id) { |
| 57 scoped_refptr<URLResponseInfo> response( | 59 scoped_refptr<PPB_URLResponseInfo_Impl> response( |
| 58 Resource::GetAs<URLResponseInfo>(response_id)); | 60 Resource::GetAs<PPB_URLResponseInfo_Impl>(response_id)); |
| 59 if (!response.get()) | 61 if (!response.get()) |
| 60 return 0; | 62 return 0; |
| 61 | 63 |
| 62 FileRef* body = response->body(); | 64 PPB_FileRef_Impl* body = response->body(); |
| 63 if (!body) | 65 if (!body) |
| 64 return 0; | 66 return 0; |
| 65 body->AddRef(); // AddRef for the caller. | 67 body->AddRef(); // AddRef for the caller. |
| 66 | 68 |
| 67 return body->GetReference(); | 69 return body->GetReference(); |
| 68 } | 70 } |
| 69 | 71 |
| 70 const PPB_URLResponseInfo ppb_urlresponseinfo = { | 72 const PPB_URLResponseInfo ppb_urlresponseinfo = { |
| 71 &IsURLResponseInfo, | 73 &IsURLResponseInfo, |
| 72 &GetProperty, | 74 &GetProperty, |
| 73 &GetBody | 75 &GetBody |
| 74 }; | 76 }; |
| 75 | 77 |
| 76 bool IsRedirect(int32_t status) { | 78 bool IsRedirect(int32_t status) { |
| 77 return status >= 300 && status <= 399; | 79 return status >= 300 && status <= 399; |
| 78 } | 80 } |
| 79 | 81 |
| 80 } // namespace | 82 } // namespace |
| 81 | 83 |
| 82 URLResponseInfo::URLResponseInfo(PluginModule* module) | 84 PPB_URLResponseInfo_Impl::PPB_URLResponseInfo_Impl(PluginModule* module) |
| 83 : Resource(module), | 85 : Resource(module), |
| 84 status_code_(-1) { | 86 status_code_(-1) { |
| 85 } | 87 } |
| 86 | 88 |
| 87 URLResponseInfo::~URLResponseInfo() { | 89 PPB_URLResponseInfo_Impl::~PPB_URLResponseInfo_Impl() { |
| 88 } | 90 } |
| 89 | 91 |
| 90 // static | 92 // static |
| 91 const PPB_URLResponseInfo* URLResponseInfo::GetInterface() { | 93 const PPB_URLResponseInfo* PPB_URLResponseInfo_Impl::GetInterface() { |
| 92 return &ppb_urlresponseinfo; | 94 return &ppb_urlresponseinfo; |
| 93 } | 95 } |
| 94 | 96 |
| 95 URLResponseInfo* URLResponseInfo::AsURLResponseInfo() { | 97 PPB_URLResponseInfo_Impl* PPB_URLResponseInfo_Impl::AsURLResponseInfo() { |
| 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 plugins |
| 146 } // namespace webkit |
| 147 |
| OLD | NEW |