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