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
3 // found in the LICENSE file.
5 #include "chrome/renderer/extensions/extension_localization_peer.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_util.h"
9 #include "chrome/common/url_constants.h"
10 #include "extensions/common/constants.h"
11 #include "extensions/common/extension_messages.h"
12 #include "extensions/common/message_bundle.h"
13 #include "ipc/ipc_sender.h"
14 #include "net/base/net_errors.h"
15 #include "net/http/http_response_headers.h"
17 ExtensionLocalizationPeer::ExtensionLocalizationPeer(
18 content::RequestPeer
* peer
,
19 IPC::Sender
* message_sender
,
20 const GURL
& request_url
)
21 : original_peer_(peer
),
22 message_sender_(message_sender
),
23 request_url_(request_url
) {
26 ExtensionLocalizationPeer::~ExtensionLocalizationPeer() {
30 ExtensionLocalizationPeer
*
31 ExtensionLocalizationPeer::CreateExtensionLocalizationPeer(
32 content::RequestPeer
* peer
,
33 IPC::Sender
* message_sender
,
34 const std::string
& mime_type
,
35 const GURL
& request_url
) {
36 // Return NULL if content is not text/css or it doesn't belong to extension
38 return (request_url
.SchemeIs(extensions::kExtensionScheme
) &&
39 StartsWithASCII(mime_type
, "text/css", false)) ?
40 new ExtensionLocalizationPeer(peer
, message_sender
, request_url
) : NULL
;
43 void ExtensionLocalizationPeer::OnUploadProgress(
44 uint64 position
, uint64 size
) {
48 bool ExtensionLocalizationPeer::OnReceivedRedirect(
49 const net::RedirectInfo
& redirect_info
,
50 const content::ResourceResponseInfo
& info
) {
55 void ExtensionLocalizationPeer::OnReceivedResponse(
56 const content::ResourceResponseInfo
& info
) {
57 response_info_
= info
;
60 void ExtensionLocalizationPeer::OnReceivedData(const char* data
,
62 int encoded_data_length
) {
63 data_
.append(data
, data_length
);
66 void ExtensionLocalizationPeer::OnCompletedRequest(
68 bool was_ignored_by_handler
,
69 bool stale_copy_in_cache
,
70 const std::string
& security_info
,
71 const base::TimeTicks
& completion_time
,
72 int64 total_transfer_size
) {
73 // Make sure we delete ourselves at the end of this call.
74 scoped_ptr
<ExtensionLocalizationPeer
> this_deleter(this);
76 // Give sub-classes a chance at altering the data.
77 if (error_code
!= net::OK
) {
78 // We failed to load the resource.
79 original_peer_
->OnReceivedResponse(response_info_
);
80 original_peer_
->OnCompletedRequest(net::ERR_ABORTED
, false,
81 stale_copy_in_cache
, security_info
,
89 original_peer_
->OnReceivedResponse(response_info_
);
91 original_peer_
->OnReceivedData(data_
.data(),
92 static_cast<int>(data_
.size()),
94 original_peer_
->OnCompletedRequest(error_code
, was_ignored_by_handler
,
96 security_info
, completion_time
,
100 void ExtensionLocalizationPeer::ReplaceMessages() {
101 if (!message_sender_
|| data_
.empty())
104 if (!request_url_
.is_valid())
107 std::string extension_id
= request_url_
.host();
108 extensions::L10nMessagesMap
* l10n_messages
=
109 extensions::GetL10nMessagesMap(extension_id
);
110 if (!l10n_messages
) {
111 extensions::L10nMessagesMap messages
;
112 message_sender_
->Send(new ExtensionHostMsg_GetMessageBundle(
113 extension_id
, &messages
));
115 // Save messages we got, so we don't have to ask again.
116 // Messages map is never empty, it contains at least @@extension_id value.
117 extensions::ExtensionToL10nMessagesMap
& l10n_messages_map
=
118 *extensions::GetExtensionToL10nMessagesMap();
119 l10n_messages_map
[extension_id
] = messages
;
121 l10n_messages
= extensions::GetL10nMessagesMap(extension_id
);
125 if (extensions::MessageBundle::ReplaceMessagesWithExternalDictionary(
126 *l10n_messages
, &data_
, &error
)) {
127 data_
.resize(data_
.size());