forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension_localization_peer.cc
148 lines (123 loc) · 4.88 KB
/
extension_localization_peer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/renderer/extensions/extension_localization_peer.h"
#include <memory>
#include <utility>
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_util.h"
#include "chrome/common/url_constants.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension_messages.h"
#include "extensions/common/message_bundle.h"
#include "ipc/ipc_sender.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
namespace {
class StringData final : public content::RequestPeer::ReceivedData {
public:
explicit StringData(const std::string& data) : data_(data) {}
const char* payload() const override { return data_.data(); }
int length() const override { return data_.size(); }
int encoded_data_length() const override { return -1; }
// The original data has substitutions applied, so the original
// encoded_body_length no longer applies.
int encoded_body_length() const override { return data_.size(); }
private:
const std::string data_;
DISALLOW_COPY_AND_ASSIGN(StringData);
};
} // namespace
ExtensionLocalizationPeer::ExtensionLocalizationPeer(
std::unique_ptr<content::RequestPeer> peer,
IPC::Sender* message_sender,
const GURL& request_url)
: original_peer_(std::move(peer)),
message_sender_(message_sender),
request_url_(request_url) {}
ExtensionLocalizationPeer::~ExtensionLocalizationPeer() {
}
// static
std::unique_ptr<content::RequestPeer>
ExtensionLocalizationPeer::CreateExtensionLocalizationPeer(
std::unique_ptr<content::RequestPeer> peer,
IPC::Sender* message_sender,
const std::string& mime_type,
const GURL& request_url) {
// Return the given |peer| as is if content is not text/css or it doesn't
// belong to extension scheme.
return (request_url.SchemeIs(extensions::kExtensionScheme) &&
base::StartsWith(mime_type, "text/css",
base::CompareCase::INSENSITIVE_ASCII))
? base::WrapUnique(new ExtensionLocalizationPeer(
std::move(peer), message_sender, request_url))
: std::move(peer);
}
void ExtensionLocalizationPeer::OnUploadProgress(uint64_t position,
uint64_t size) {
NOTREACHED();
}
bool ExtensionLocalizationPeer::OnReceivedRedirect(
const net::RedirectInfo& redirect_info,
const content::ResourceResponseInfo& info) {
NOTREACHED();
return false;
}
void ExtensionLocalizationPeer::OnReceivedResponse(
const content::ResourceResponseInfo& info) {
response_info_ = info;
}
void ExtensionLocalizationPeer::OnReceivedData(
std::unique_ptr<ReceivedData> data) {
data_.append(data->payload(), data->length());
}
void ExtensionLocalizationPeer::OnCompletedRequest(
int error_code,
bool was_ignored_by_handler,
bool stale_copy_in_cache,
const std::string& security_info,
const base::TimeTicks& completion_time,
int64_t total_transfer_size) {
// Give sub-classes a chance at altering the data.
if (error_code != net::OK) {
// We failed to load the resource.
original_peer_->OnReceivedResponse(response_info_);
original_peer_->OnCompletedRequest(net::ERR_ABORTED, false,
stale_copy_in_cache, security_info,
completion_time, total_transfer_size);
return;
}
ReplaceMessages();
original_peer_->OnReceivedResponse(response_info_);
if (!data_.empty())
original_peer_->OnReceivedData(base::WrapUnique(new StringData(data_)));
original_peer_->OnCompletedRequest(error_code, was_ignored_by_handler,
stale_copy_in_cache, security_info,
completion_time, total_transfer_size);
}
void ExtensionLocalizationPeer::ReplaceMessages() {
if (!message_sender_ || data_.empty())
return;
if (!request_url_.is_valid())
return;
std::string extension_id = request_url_.host();
extensions::L10nMessagesMap* l10n_messages =
extensions::GetL10nMessagesMap(extension_id);
if (!l10n_messages) {
extensions::L10nMessagesMap messages;
message_sender_->Send(new ExtensionHostMsg_GetMessageBundle(
extension_id, &messages));
// Save messages we got, so we don't have to ask again.
// Messages map is never empty, it contains at least @@extension_id value.
extensions::ExtensionToL10nMessagesMap& l10n_messages_map =
*extensions::GetExtensionToL10nMessagesMap();
l10n_messages_map[extension_id] = messages;
l10n_messages = extensions::GetL10nMessagesMap(extension_id);
}
std::string error;
if (extensions::MessageBundle::ReplaceMessagesWithExternalDictionary(
*l10n_messages, &data_, &error)) {
data_.resize(data_.size());
}
}