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 #ifndef CHROME_RENDERER_EXTENSIONS_EXTENSION_LOCALIZATION_PEER_H_ 6 #define CHROME_RENDERER_EXTENSIONS_EXTENSION_LOCALIZATION_PEER_H_ 7 8 #include <string> 9 10 #include "ipc/ipc_sender.h" 11 #include "webkit/child/resource_loader_bridge.h" 12 13 // The ExtensionLocalizationPeer is a proxy to a 14 // webkit_glue::ResourceLoaderBridge::Peer instance. It is used to pre-process 15 // CSS files requested by extensions to replace localization templates with the 16 // appropriate localized strings. 17 // 18 // Call the factory method CreateExtensionLocalizationPeer() to obtain an 19 // instance of ExtensionLocalizationPeer based on the original Peer. 20 class ExtensionLocalizationPeer 21 : public webkit_glue::ResourceLoaderBridge::Peer { 22 public: 23 virtual ~ExtensionLocalizationPeer(); 24 25 static ExtensionLocalizationPeer* CreateExtensionLocalizationPeer( 26 webkit_glue::ResourceLoaderBridge::Peer* peer, 27 IPC::Sender* message_sender, 28 const std::string& mime_type, 29 const GURL& request_url); 30 31 // ResourceLoaderBridge::Peer methods. 32 virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE; 33 virtual bool OnReceivedRedirect( 34 const GURL& new_url, 35 const webkit_glue::ResourceResponseInfo& info, 36 bool* has_new_first_party_for_cookies, 37 GURL* new_first_party_for_cookies) OVERRIDE; 38 virtual void OnReceivedResponse( 39 const webkit_glue::ResourceResponseInfo& info) OVERRIDE; 40 virtual void OnDownloadedData(int len, int encoded_data_length) OVERRIDE {} 41 virtual void OnReceivedData(const char* data, 42 int data_length, 43 int encoded_data_length) OVERRIDE; 44 virtual void OnCompletedRequest( 45 int error_code, 46 bool was_ignored_by_handler, 47 const std::string& security_info, 48 const base::TimeTicks& completion_time) OVERRIDE; 49 50 private: 51 friend class ExtensionLocalizationPeerTest; 52 53 // Use CreateExtensionLocalizationPeer to create an instance. 54 ExtensionLocalizationPeer( 55 webkit_glue::ResourceLoaderBridge::Peer* peer, 56 IPC::Sender* message_sender, 57 const GURL& request_url); 58 59 // Loads message catalogs, and replaces all __MSG_some_name__ templates within 60 // loaded file. 61 void ReplaceMessages(); 62 63 // Original peer that handles the request once we are done processing data_. 64 webkit_glue::ResourceLoaderBridge::Peer* original_peer_; 65 66 // We just pass though the response info. This holds the copy of the original. 67 webkit_glue::ResourceResponseInfo response_info_; 68 69 // Sends ExtensionHostMsg_GetMessageBundle message to the browser to fetch 70 // message catalog. 71 IPC::Sender* message_sender_; 72 73 // Buffer for incoming data. We wait until OnCompletedRequest before using it. 74 std::string data_; 75 76 // Original request URL. 77 GURL request_url_; 78 79 private: 80 DISALLOW_COPY_AND_ASSIGN(ExtensionLocalizationPeer); 81 }; 82 83 #endif // CHROME_RENDERER_EXTENSIONS_EXTENSION_LOCALIZATION_PEER_H_ 84