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