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. 4 5 #include "chrome/renderer/extensions/extension_localization_peer.h" 6 7 #include "base/memory/scoped_ptr.h" 8 #include "base/strings/string_util.h" 9 #include "chrome/common/extensions/extension_messages.h" 10 #include "chrome/common/extensions/message_bundle.h" 11 #include "chrome/common/url_constants.h" 12 #include "extensions/common/constants.h" 13 #include "grit/generated_resources.h" 14 #include "net/base/net_errors.h" 15 #include "net/http/http_response_headers.h" 16 17 ExtensionLocalizationPeer::ExtensionLocalizationPeer( 18 webkit_glue::ResourceLoaderBridge::Peer* 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) { 24 } 25 26 ExtensionLocalizationPeer::~ExtensionLocalizationPeer() { 27 } 28 29 // static 30 ExtensionLocalizationPeer* 31 ExtensionLocalizationPeer::CreateExtensionLocalizationPeer( 32 webkit_glue::ResourceLoaderBridge::Peer* 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; 41 } 42 43 void ExtensionLocalizationPeer::OnUploadProgress( 44 uint64 position, uint64 size) { 45 NOTREACHED(); 46 } 47 48 bool ExtensionLocalizationPeer::OnReceivedRedirect( 49 const GURL& new_url, 50 const webkit_glue::ResourceResponseInfo& info, 51 bool* has_new_first_party_for_cookies, 52 GURL* new_first_party_for_cookies) { 53 NOTREACHED(); 54 return false; 55 } 56 57 void ExtensionLocalizationPeer::OnReceivedResponse( 58 const webkit_glue::ResourceResponseInfo& info) { 59 response_info_ = info; 60 } 61 62 void ExtensionLocalizationPeer::OnReceivedData(const char* data, 63 int data_length, 64 int encoded_data_length) { 65 data_.append(data, data_length); 66 } 67 68 void ExtensionLocalizationPeer::OnCompletedRequest( 69 int error_code, 70 bool was_ignored_by_handler, 71 const std::string& security_info, 72 const base::TimeTicks& completion_time) { 73 // Make sure we delete ourselves at the end of this call. 74 scoped_ptr<ExtensionLocalizationPeer> this_deleter(this); 75 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, security_info, 81 completion_time); 82 return; 83 } 84 85 ReplaceMessages(); 86 87 original_peer_->OnReceivedResponse(response_info_); 88 if (!data_.empty()) 89 original_peer_->OnReceivedData(data_.data(), 90 static_cast<int>(data_.size()), 91 -1); 92 original_peer_->OnCompletedRequest(error_code, was_ignored_by_handler, 93 security_info, completion_time); 94 } 95 96 void ExtensionLocalizationPeer::ReplaceMessages() { 97 if (!message_sender_ || data_.empty()) 98 return; 99 100 if (!request_url_.is_valid()) 101 return; 102 103 std::string extension_id = request_url_.host(); 104 extensions::L10nMessagesMap* l10n_messages = 105 extensions::GetL10nMessagesMap(extension_id); 106 if (!l10n_messages) { 107 extensions::L10nMessagesMap messages; 108 message_sender_->Send(new ExtensionHostMsg_GetMessageBundle( 109 extension_id, &messages)); 110 111 // Save messages we got, so we don't have to ask again. 112 // Messages map is never empty, it contains at least @@extension_id value. 113 extensions::ExtensionToL10nMessagesMap& l10n_messages_map = 114 *extensions::GetExtensionToL10nMessagesMap(); 115 l10n_messages_map[extension_id] = messages; 116 117 l10n_messages = extensions::GetL10nMessagesMap(extension_id); 118 } 119 120 std::string error; 121 if (extensions::MessageBundle::ReplaceMessagesWithExternalDictionary( 122 *l10n_messages, &data_, &error)) { 123 data_.resize(data_.size()); 124 } 125 } 126