Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / chrome / renderer / extensions / extension_localization_peer.cc
blob8c20331d8ab7007dc7ef566f5b23d43311ff0709
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() {
29 // static
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
37 // scheme.
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) {
45 NOTREACHED();
48 bool ExtensionLocalizationPeer::OnReceivedRedirect(
49 const net::RedirectInfo& redirect_info,
50 const content::ResourceResponseInfo& info) {
51 NOTREACHED();
52 return false;
55 void ExtensionLocalizationPeer::OnReceivedResponse(
56 const content::ResourceResponseInfo& info) {
57 response_info_ = info;
60 void ExtensionLocalizationPeer::OnReceivedData(const char* data,
61 int data_length,
62 int encoded_data_length) {
63 data_.append(data, data_length);
66 void ExtensionLocalizationPeer::OnCompletedRequest(
67 int error_code,
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,
82 completion_time,
83 total_transfer_size);
84 return;
87 ReplaceMessages();
89 original_peer_->OnReceivedResponse(response_info_);
90 if (!data_.empty())
91 original_peer_->OnReceivedData(data_.data(),
92 static_cast<int>(data_.size()),
93 -1);
94 original_peer_->OnCompletedRequest(error_code, was_ignored_by_handler,
95 stale_copy_in_cache,
96 security_info, completion_time,
97 total_transfer_size);
100 void ExtensionLocalizationPeer::ReplaceMessages() {
101 if (!message_sender_ || data_.empty())
102 return;
104 if (!request_url_.is_valid())
105 return;
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);
124 std::string error;
125 if (extensions::MessageBundle::ReplaceMessagesWithExternalDictionary(
126 *l10n_messages, &data_, &error)) {
127 data_.resize(data_.size());