Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_RENDERER_HOST_TRANSLATION_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_RENDERER_HOST_TRANSLATION_SERVICE_H_ |
| 6 #define CHROME_BROWSER_RENDERER_HOST_TRANSLATION_SERVICE_H_ | 6 #define CHROME_BROWSER_RENDERER_HOST_TRANSLATION_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | |
| 8 #include <string> | 9 #include <string> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 12 #include "base/scoped_ptr.h" | |
| 11 #include "base/string16.h" | 13 #include "base/string16.h" |
| 14 #include "chrome/browser/net/url_fetcher.h" | |
| 15 #include "ipc/ipc_message.h" | |
| 16 #include "testing/gtest/include/gtest/gtest_prod.h" | |
| 12 | 17 |
| 13 class ResourceMessageFilter; | 18 class DictionaryValue; |
| 19 class TranslationServiceTest; | |
| 20 class TranslateURLFetcherDelegate; | |
| 21 class URLFetcher; | |
| 14 | 22 |
| 15 // The TranslationService class is used to translate text. | 23 // The TranslationService class is used to translate text. |
| 16 // This temporary implementation only upcases the text sent to it. | 24 // There is one TranslationService is per renderer process. |
|
jungshik at Google
2010/01/30 01:27:17
nit: is per ==> per
| |
| 17 class TranslationService { | 25 // It receives requests to translate text from the different render views of the |
| 26 // render process, provided in lists (text chunks), where the words should be | |
| 27 // translated without changing the chunks order. | |
| 28 // It groups multiple such requests and sends them for translation to the Google | |
| 29 // translation server. When it receives the response, it dispatches it to the | |
| 30 // appropriate render view. | |
| 31 | |
| 32 class TranslationService : public URLFetcher::Delegate { | |
| 18 public: | 33 public: |
| 19 explicit TranslationService(ResourceMessageFilter* resource_msg_filter); | 34 explicit TranslationService(IPC::Message::Sender* message_sender); |
| 35 virtual ~TranslationService(); | |
| 20 | 36 |
| 21 // Translates the passed text chunks and sends a | 37 // Sends the specified text for translation, from |source_language| to |
| 38 // |target_language|. If |secure| is true, a secure connection is used when | |
| 39 // sending the text to the external translation server. | |
| 40 // When the translation results have been received, it sends a | |
| 22 // ViewMsg_TranslateTextReponse message on the renderer at |routing_id|. | 41 // ViewMsg_TranslateTextReponse message on the renderer at |routing_id|. |
| 23 void Translate(int routing_id, | 42 void Translate(int routing_id, |
| 43 int page_id, | |
| 24 int work_id, | 44 int work_id, |
| 25 const std::vector<string16>& text_chunks, | 45 const std::vector<string16>& text_chunks, |
| 26 std::string from_language, | 46 const std::string& source_language, |
| 27 std::string to_language, | 47 const std::string& target_language, |
| 28 bool secure); | 48 bool secure); |
| 29 | 49 |
| 50 // Sends the pending translation request for the specified renderer to the | |
| 51 // translation server. | |
| 52 void SendTranslationRequestForRenderer(int renderer_id, bool secure); | |
| 53 | |
| 54 // URLFetcher::Delegate implementation. | |
| 55 virtual void OnURLFetchComplete(const URLFetcher* source, | |
| 56 const GURL& url, | |
| 57 const URLRequestStatus& status, | |
| 58 int response_code, | |
| 59 const ResponseCookies& cookies, | |
| 60 const std::string& data); | |
| 61 | |
| 62 // Returns true if a page in the language |page_language| (as reported by the | |
| 63 // CLD) should be translated when Chrome is using |chrome_language|. Note that | |
| 64 // this returns false for similar languages, for example it returns false when | |
| 65 // given the values 'en' and 'en-US'. | |
|
jungshik at Google
2010/01/30 01:27:17
A TODO comment about a future enhancement (either
| |
| 66 static bool ShouldTranslatePage(const std::string& page_language, | |
| 67 const std::string& chrome_language); | |
| 68 | |
| 69 // Returns true if the TranslationService is enabled. | |
| 70 static bool IsTranslationEnabled(); | |
| 71 | |
| 72 protected: | |
| 73 // The amount of time in ms after which a pending request is sent if no other | |
| 74 // translation request has been received. | |
| 75 // Overriden in tests. | |
| 76 virtual int GetSendRequestDelay() const; | |
| 77 | |
| 30 private: | 78 private: |
| 31 ResourceMessageFilter* resource_message_filter_; | 79 friend class TranslationServiceTest; |
| 80 friend class TranslateURLFetcherDelegate; | |
| 81 FRIEND_TEST(TranslationServiceTest, MergeTestChunks); | |
| 82 FRIEND_TEST(TranslationServiceTest, SplitTestChunks); | |
| 83 FRIEND_TEST(TranslationServiceTest, RemoveTag); | |
| 84 | |
| 85 struct TranslationRequest; | |
| 86 | |
| 87 // The information necessary to return the translated text to the renderer. | |
| 88 struct RendererRequestInfo { | |
| 89 RendererRequestInfo() : routing_id(0), work_id(0) {} | |
| 90 RendererRequestInfo(int routing_id, int work_id) | |
| 91 : routing_id(routing_id), | |
| 92 work_id(work_id) { | |
| 93 } | |
| 94 int routing_id; | |
| 95 int work_id; | |
| 96 }; | |
| 97 | |
| 98 typedef std::vector<RendererRequestInfo> RendererRequestInfoList; | |
| 99 | |
| 100 typedef std::vector<string16> TextChunks; | |
| 101 typedef std::vector<TextChunks> TextChunksList; | |
| 102 // Maps from a RenderView routing id to the pending request for the | |
| 103 // translation server. | |
| 104 typedef std::map<int, TranslationRequest*> TranslationRequestMap; | |
| 105 | |
| 106 typedef std::map<const URLFetcher*, RendererRequestInfoList*> | |
| 107 RendererRequestInfoMap; | |
| 108 | |
| 109 // Sends the passed request to the translations server. | |
| 110 // Warning the request is deleted when this call returns. | |
|
jungshik at Google
2010/01/30 01:27:17
nit: A colon is necessary after 'Warning', isn't i
| |
| 111 void SendRequestToTranslationServer(TranslationRequest* request); | |
| 112 | |
| 113 // Called by the URLFetcherDelegate when the translation associated with | |
| 114 // |url_fetcher| has been performed. Sends the appropriate message back to | |
| 115 // the renderer and deletes the URLFetcher. | |
| 116 void SendResponseToRenderer(const URLFetcher* url_fetcher, | |
| 117 int error_code, | |
| 118 const TextChunksList& text_chunks_list); | |
| 119 | |
| 120 // Notifies the renderer that we failed to translate the request associated | |
| 121 // with |url_fetcher|. | |
| 122 void TranslationFailed(const URLFetcher* source); | |
| 123 | |
| 124 // Merges all text chunks to be translated into a single string that can be | |
| 125 // sent to the translate server, surrounding each chunk with an anchor tag | |
| 126 // to preserve chunk order in the translated version. | |
| 127 static string16 MergeTextChunks(const TextChunks& text_chunks); | |
| 128 | |
| 129 // Splits the translated text into its original text chunks, removing the | |
| 130 // anchor tags wrapper that were added to preserve order. | |
| 131 static void SplitTextChunks(const string16& translated_text, | |
|
jungshik at Google
2010/01/30 01:27:17
nit: SplitIntoTextChunks seems better.
| |
| 132 TextChunks* text_chunks); | |
| 133 | |
| 134 // Removes the HTML anchor tag surrounding |text| and returns the resulting | |
| 135 // string. | |
| 136 static string16 RemoveTag(const string16& text); | |
| 137 | |
| 138 // Adds |text| to the string request in/out param |request|. If |request| is | |
| 139 // empty, then the source, target language as well as the secure parameters | |
| 140 // are also added. | |
| 141 static void AddTextToRequestString(std::string* request, | |
| 142 const std::string& text, | |
| 143 const std::string& source_language, | |
| 144 const std::string& target_language, | |
| 145 bool secure); | |
| 146 | |
| 147 // The channel used to communicate with the renderer. | |
| 148 IPC::Message::Sender* message_sender_; | |
| 149 | |
| 150 // Map used to retrieve the context of requests when the URLFetcher notifies | |
| 151 // that it got a response. | |
| 152 RendererRequestInfoMap renderer_request_infos_; | |
| 153 | |
| 154 TranslationRequestMap pending_translation_requests_; | |
| 155 TranslationRequestMap pending_secure_translation_requests_; | |
| 32 | 156 |
| 33 DISALLOW_COPY_AND_ASSIGN(TranslationService); | 157 DISALLOW_COPY_AND_ASSIGN(TranslationService); |
| 34 }; | 158 }; |
| 35 | 159 |
| 36 #endif // CHROME_BROWSER_RENDERER_HOST_TRANSLATION_SERVICE_H_ | 160 #endif // CHROME_BROWSER_RENDERER_HOST_TRANSLATION_SERVICE_H_ |
| OLD | NEW |