| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/extension_localization_peer.h" | 5 #include "chrome/renderer/extensions/extension_localization_peer.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 14 #include "extensions/common/constants.h" | 14 #include "extensions/common/constants.h" |
| 15 #include "extensions/common/extension_messages.h" | 15 #include "extensions/common/extension_messages.h" |
| 16 #include "extensions/common/message_bundle.h" | 16 #include "extensions/common/message_bundle.h" |
| 17 #include "ipc/ipc_sender.h" | 17 #include "ipc/ipc_sender.h" |
| 18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 class StringData final : public content::RequestPeer::ReceivedData { | 23 class StringData final : public content::RequestPeer::ReceivedData { |
| 24 public: | 24 public: |
| 25 explicit StringData(const std::string& data) : data_(data) {} | 25 explicit StringData(const std::string& data) : data_(data) {} |
| 26 | 26 |
| 27 const char* payload() const override { return data_.data(); } | 27 const char* payload() const override { return data_.data(); } |
| 28 int length() const override { return data_.size(); } | 28 int length() const override { return data_.size(); } |
| 29 int encoded_data_length() const override { return -1; } | 29 int encoded_data_length() const override { return -1; } |
| 30 // The original data has substitutions applied, so the original | |
| 31 // encoded_body_length no longer applies. | |
| 32 int encoded_body_length() const override { return data_.size(); } | |
| 33 | 30 |
| 34 private: | 31 private: |
| 35 const std::string data_; | 32 const std::string data_; |
| 36 | 33 |
| 37 DISALLOW_COPY_AND_ASSIGN(StringData); | 34 DISALLOW_COPY_AND_ASSIGN(StringData); |
| 38 }; | 35 }; |
| 39 | 36 |
| 40 } // namespace | 37 } // namespace |
| 41 | 38 |
| 42 ExtensionLocalizationPeer::ExtensionLocalizationPeer( | 39 ExtensionLocalizationPeer::ExtensionLocalizationPeer( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 void ExtensionLocalizationPeer::OnReceivedData( | 84 void ExtensionLocalizationPeer::OnReceivedData( |
| 88 std::unique_ptr<ReceivedData> data) { | 85 std::unique_ptr<ReceivedData> data) { |
| 89 data_.append(data->payload(), data->length()); | 86 data_.append(data->payload(), data->length()); |
| 90 } | 87 } |
| 91 | 88 |
| 92 void ExtensionLocalizationPeer::OnCompletedRequest( | 89 void ExtensionLocalizationPeer::OnCompletedRequest( |
| 93 int error_code, | 90 int error_code, |
| 94 bool was_ignored_by_handler, | 91 bool was_ignored_by_handler, |
| 95 bool stale_copy_in_cache, | 92 bool stale_copy_in_cache, |
| 96 const base::TimeTicks& completion_time, | 93 const base::TimeTicks& completion_time, |
| 97 int64_t total_transfer_size) { | 94 int64_t total_transfer_size, |
| 95 int64_t encoded_body_size) { |
| 98 // Give sub-classes a chance at altering the data. | 96 // Give sub-classes a chance at altering the data. |
| 99 if (error_code != net::OK) { | 97 if (error_code != net::OK) { |
| 100 // We failed to load the resource. | 98 // We failed to load the resource. |
| 101 original_peer_->OnReceivedResponse(response_info_); | 99 original_peer_->OnReceivedResponse(response_info_); |
| 102 original_peer_->OnCompletedRequest(net::ERR_ABORTED, false, | 100 original_peer_->OnCompletedRequest(net::ERR_ABORTED, false, |
| 103 stale_copy_in_cache, completion_time, | 101 stale_copy_in_cache, completion_time, |
| 104 total_transfer_size); | 102 total_transfer_size, encoded_body_size); |
| 105 return; | 103 return; |
| 106 } | 104 } |
| 107 | 105 |
| 108 ReplaceMessages(); | 106 ReplaceMessages(); |
| 109 | 107 |
| 110 original_peer_->OnReceivedResponse(response_info_); | 108 original_peer_->OnReceivedResponse(response_info_); |
| 111 if (!data_.empty()) | 109 if (!data_.empty()) |
| 112 original_peer_->OnReceivedData(base::MakeUnique<StringData>(data_)); | 110 original_peer_->OnReceivedData(base::MakeUnique<StringData>(data_)); |
| 113 original_peer_->OnCompletedRequest(error_code, was_ignored_by_handler, | 111 original_peer_->OnCompletedRequest(error_code, was_ignored_by_handler, |
| 114 stale_copy_in_cache, completion_time, | 112 stale_copy_in_cache, completion_time, |
| 115 total_transfer_size); | 113 total_transfer_size, encoded_body_size); |
| 116 } | 114 } |
| 117 | 115 |
| 118 void ExtensionLocalizationPeer::ReplaceMessages() { | 116 void ExtensionLocalizationPeer::ReplaceMessages() { |
| 119 if (!message_sender_ || data_.empty()) | 117 if (!message_sender_ || data_.empty()) |
| 120 return; | 118 return; |
| 121 | 119 |
| 122 if (!request_url_.is_valid()) | 120 if (!request_url_.is_valid()) |
| 123 return; | 121 return; |
| 124 | 122 |
| 125 std::string extension_id = request_url_.host(); | 123 std::string extension_id = request_url_.host(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 138 | 136 |
| 139 l10n_messages = extensions::GetL10nMessagesMap(extension_id); | 137 l10n_messages = extensions::GetL10nMessagesMap(extension_id); |
| 140 } | 138 } |
| 141 | 139 |
| 142 std::string error; | 140 std::string error; |
| 143 if (extensions::MessageBundle::ReplaceMessagesWithExternalDictionary( | 141 if (extensions::MessageBundle::ReplaceMessagesWithExternalDictionary( |
| 144 *l10n_messages, &data_, &error)) { | 142 *l10n_messages, &data_, &error)) { |
| 145 data_.resize(data_.size()); | 143 data_.resize(data_.size()); |
| 146 } | 144 } |
| 147 } | 145 } |
| OLD | NEW |