| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/renderer/security_filter_peer.h" | 5 #include "chrome/renderer/security_filter_peer.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "chrome/grit/generated_resources.h" | 9 #include "chrome/grit/generated_resources.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/http/http_response_headers.h" | 11 #include "net/http/http_response_headers.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | 12 #include "ui/base/l10n/l10n_util.h" |
| 13 | 13 |
| 14 namespace { |
| 15 |
| 16 class StringData final : public content::RequestPeer::ReceivedData { |
| 17 public: |
| 18 explicit StringData(const std::string& data) : data_(data) {} |
| 19 |
| 20 const char* payload() const override { return data_.c_str(); } |
| 21 int length() const override { return data_.size(); } |
| 22 int encoded_length() const override { return -1; } |
| 23 |
| 24 private: |
| 25 std::string data_; |
| 26 |
| 27 DISALLOW_COPY_AND_ASSIGN(StringData); |
| 28 }; |
| 29 |
| 30 } // namespace |
| 31 |
| 14 SecurityFilterPeer::SecurityFilterPeer(content::RequestPeer* peer) | 32 SecurityFilterPeer::SecurityFilterPeer(content::RequestPeer* peer) |
| 15 : original_peer_(peer) { | 33 : original_peer_(peer) { |
| 16 } | 34 } |
| 17 | 35 |
| 18 SecurityFilterPeer::~SecurityFilterPeer() { | 36 SecurityFilterPeer::~SecurityFilterPeer() { |
| 19 } | 37 } |
| 20 | 38 |
| 21 // static | 39 // static |
| 22 SecurityFilterPeer* | 40 SecurityFilterPeer* |
| 23 SecurityFilterPeer::CreateSecurityFilterPeerForDeniedRequest( | 41 SecurityFilterPeer::CreateSecurityFilterPeerForDeniedRequest( |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 const content::ResourceResponseInfo& info) { | 91 const content::ResourceResponseInfo& info) { |
| 74 NOTREACHED(); | 92 NOTREACHED(); |
| 75 return false; | 93 return false; |
| 76 } | 94 } |
| 77 | 95 |
| 78 void SecurityFilterPeer::OnReceivedResponse( | 96 void SecurityFilterPeer::OnReceivedResponse( |
| 79 const content::ResourceResponseInfo& info) { | 97 const content::ResourceResponseInfo& info) { |
| 80 NOTREACHED(); | 98 NOTREACHED(); |
| 81 } | 99 } |
| 82 | 100 |
| 83 void SecurityFilterPeer::OnReceivedData(const char* data, | 101 void SecurityFilterPeer::OnReceivedData(scoped_ptr<ReceivedData> data) { |
| 84 int data_length, | |
| 85 int encoded_data_length) { | |
| 86 NOTREACHED(); | 102 NOTREACHED(); |
| 87 } | 103 } |
| 88 | 104 |
| 89 void SecurityFilterPeer::OnCompletedRequest( | 105 void SecurityFilterPeer::OnCompletedRequest( |
| 90 int error_code, | 106 int error_code, |
| 91 bool was_ignored_by_handler, | 107 bool was_ignored_by_handler, |
| 92 bool stale_copy_in_cache, | 108 bool stale_copy_in_cache, |
| 93 const std::string& security_info, | 109 const std::string& security_info, |
| 94 const base::TimeTicks& completion_time, | 110 const base::TimeTicks& completion_time, |
| 95 int64 total_transfer_size) { | 111 int64 total_transfer_size) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 : SecurityFilterPeer(peer), mime_type_(mime_type) {} | 148 : SecurityFilterPeer(peer), mime_type_(mime_type) {} |
| 133 | 149 |
| 134 BufferedPeer::~BufferedPeer() { | 150 BufferedPeer::~BufferedPeer() { |
| 135 } | 151 } |
| 136 | 152 |
| 137 void BufferedPeer::OnReceivedResponse( | 153 void BufferedPeer::OnReceivedResponse( |
| 138 const content::ResourceResponseInfo& info) { | 154 const content::ResourceResponseInfo& info) { |
| 139 ProcessResponseInfo(info, &response_info_, mime_type_); | 155 ProcessResponseInfo(info, &response_info_, mime_type_); |
| 140 } | 156 } |
| 141 | 157 |
| 142 void BufferedPeer::OnReceivedData(const char* data, | 158 void BufferedPeer::OnReceivedData(scoped_ptr<ReceivedData> data) { |
| 143 int data_length, | 159 data_.append(data->payload(), data->length()); |
| 144 int encoded_data_length) { | |
| 145 data_.append(data, data_length); | |
| 146 } | 160 } |
| 147 | 161 |
| 148 void BufferedPeer::OnCompletedRequest(int error_code, | 162 void BufferedPeer::OnCompletedRequest(int error_code, |
| 149 bool was_ignored_by_handler, | 163 bool was_ignored_by_handler, |
| 150 bool stale_copy_in_cache, | 164 bool stale_copy_in_cache, |
| 151 const std::string& security_info, | 165 const std::string& security_info, |
| 152 const base::TimeTicks& completion_time, | 166 const base::TimeTicks& completion_time, |
| 153 int64 total_transfer_size) { | 167 int64 total_transfer_size) { |
| 154 // Make sure we delete ourselves at the end of this call. | 168 // Make sure we delete ourselves at the end of this call. |
| 155 scoped_ptr<BufferedPeer> this_deleter(this); | 169 scoped_ptr<BufferedPeer> this_deleter(this); |
| 156 | 170 |
| 157 // Give sub-classes a chance at altering the data. | 171 // Give sub-classes a chance at altering the data. |
| 158 if (error_code != net::OK || !DataReady()) { | 172 if (error_code != net::OK || !DataReady()) { |
| 159 // Pretend we failed to load the resource. | 173 // Pretend we failed to load the resource. |
| 160 original_peer_->OnReceivedResponse(response_info_); | 174 original_peer_->OnReceivedResponse(response_info_); |
| 161 original_peer_->OnCompletedRequest(net::ERR_ABORTED, false, | 175 original_peer_->OnCompletedRequest(net::ERR_ABORTED, false, |
| 162 stale_copy_in_cache, | 176 stale_copy_in_cache, |
| 163 security_info, completion_time, | 177 security_info, completion_time, |
| 164 total_transfer_size); | 178 total_transfer_size); |
| 165 return; | 179 return; |
| 166 } | 180 } |
| 167 | 181 |
| 168 original_peer_->OnReceivedResponse(response_info_); | 182 original_peer_->OnReceivedResponse(response_info_); |
| 169 if (!data_.empty()) | 183 if (!data_.empty()) |
| 170 original_peer_->OnReceivedData(data_.data(), | 184 original_peer_->OnReceivedData(make_scoped_ptr(new StringData(data_))); |
| 171 static_cast<int>(data_.size()), | |
| 172 -1); | |
| 173 original_peer_->OnCompletedRequest(error_code, was_ignored_by_handler, | 185 original_peer_->OnCompletedRequest(error_code, was_ignored_by_handler, |
| 174 stale_copy_in_cache, security_info, | 186 stale_copy_in_cache, security_info, |
| 175 completion_time, total_transfer_size); | 187 completion_time, total_transfer_size); |
| 176 } | 188 } |
| 177 | 189 |
| 178 //////////////////////////////////////////////////////////////////////////////// | 190 //////////////////////////////////////////////////////////////////////////////// |
| 179 // ReplaceContentPeer | 191 // ReplaceContentPeer |
| 180 | 192 |
| 181 ReplaceContentPeer::ReplaceContentPeer(content::RequestPeer* peer, | 193 ReplaceContentPeer::ReplaceContentPeer(content::RequestPeer* peer, |
| 182 const std::string& mime_type, | 194 const std::string& mime_type, |
| 183 const std::string& data) | 195 const std::string& data) |
| 184 : SecurityFilterPeer(peer), | 196 : SecurityFilterPeer(peer), |
| 185 mime_type_(mime_type), | 197 mime_type_(mime_type), |
| 186 data_(data) {} | 198 data_(data) {} |
| 187 | 199 |
| 188 ReplaceContentPeer::~ReplaceContentPeer() { | 200 ReplaceContentPeer::~ReplaceContentPeer() { |
| 189 } | 201 } |
| 190 | 202 |
| 191 void ReplaceContentPeer::OnReceivedResponse( | 203 void ReplaceContentPeer::OnReceivedResponse( |
| 192 const content::ResourceResponseInfo& info) { | 204 const content::ResourceResponseInfo& info) { |
| 193 // Ignore this, we'll serve some alternate content in OnCompletedRequest. | 205 // Ignore this, we'll serve some alternate content in OnCompletedRequest. |
| 194 } | 206 } |
| 195 | 207 |
| 196 void ReplaceContentPeer::OnReceivedData(const char* data, | 208 void ReplaceContentPeer::OnReceivedData(scoped_ptr<ReceivedData> data) { |
| 197 int data_length, | |
| 198 int encoded_data_length) { | |
| 199 // Ignore this, we'll serve some alternate content in OnCompletedRequest. | 209 // Ignore this, we'll serve some alternate content in OnCompletedRequest. |
| 200 } | 210 } |
| 201 | 211 |
| 202 void ReplaceContentPeer::OnCompletedRequest( | 212 void ReplaceContentPeer::OnCompletedRequest( |
| 203 int error_code, | 213 int error_code, |
| 204 bool was_ignored_by_handler, | 214 bool was_ignored_by_handler, |
| 205 bool stale_copy_in_cache, | 215 bool stale_copy_in_cache, |
| 206 const std::string& security_info, | 216 const std::string& security_info, |
| 207 const base::TimeTicks& completion_time, | 217 const base::TimeTicks& completion_time, |
| 208 int64 total_transfer_size) { | 218 int64 total_transfer_size) { |
| 209 content::ResourceResponseInfo info; | 219 content::ResourceResponseInfo info; |
| 210 ProcessResponseInfo(info, &info, mime_type_); | 220 ProcessResponseInfo(info, &info, mime_type_); |
| 211 info.security_info = security_info; | 221 info.security_info = security_info; |
| 212 info.content_length = static_cast<int>(data_.size()); | 222 info.content_length = static_cast<int>(data_.size()); |
| 213 original_peer_->OnReceivedResponse(info); | 223 original_peer_->OnReceivedResponse(info); |
| 214 if (!data_.empty()) | 224 if (!data_.empty()) |
| 215 original_peer_->OnReceivedData(data_.data(), | 225 original_peer_->OnReceivedData(make_scoped_ptr(new StringData(data_))); |
| 216 static_cast<int>(data_.size()), | |
| 217 -1); | |
| 218 original_peer_->OnCompletedRequest(net::OK, | 226 original_peer_->OnCompletedRequest(net::OK, |
| 219 false, | 227 false, |
| 220 stale_copy_in_cache, | 228 stale_copy_in_cache, |
| 221 security_info, | 229 security_info, |
| 222 completion_time, | 230 completion_time, |
| 223 total_transfer_size); | 231 total_transfer_size); |
| 224 | 232 |
| 225 // The request processing is complete, we must delete ourselves. | 233 // The request processing is complete, we must delete ourselves. |
| 226 delete this; | 234 delete this; |
| 227 } | 235 } |
| OLD | NEW |