OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 COMPONENTS_TRANSLATE_CONTENT_RENDERER_TRANSLATE_HELPER_H_ | 5 #ifndef COMPONENTS_TRANSLATE_CONTENT_RENDERER_TRANSLATE_HELPER_H_ |
6 #define COMPONENTS_TRANSLATE_CONTENT_RENDERER_TRANSLATE_HELPER_H_ | 6 #define COMPONENTS_TRANSLATE_CONTENT_RENDERER_TRANSLATE_HELPER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "components/translate/content/common/translate.mojom.h" |
15 #include "components/translate/core/common/translate_errors.h" | 16 #include "components/translate/core/common/translate_errors.h" |
16 #include "content/public/renderer/render_frame_observer.h" | 17 #include "content/public/renderer/render_frame_observer.h" |
| 18 #include "mojo/public/cpp/bindings/binding.h" |
17 #include "url/gurl.h" | 19 #include "url/gurl.h" |
18 | 20 |
19 namespace blink { | 21 namespace blink { |
20 class WebDocument; | 22 class WebDocument; |
21 class WebLocalFrame; | 23 class WebLocalFrame; |
22 } | 24 } |
23 | 25 |
24 namespace translate { | 26 namespace translate { |
25 | 27 |
26 // This class deals with page translation. | 28 // This class deals with page translation. |
27 // There is one TranslateHelper per RenderView. | 29 // There is one TranslateHelper per RenderView. |
28 class TranslateHelper : public content::RenderFrameObserver { | 30 class TranslateHelper : public content::RenderFrameObserver, |
| 31 public mojom::Page { |
29 public: | 32 public: |
30 explicit TranslateHelper(content::RenderFrame* render_frame, | 33 TranslateHelper(content::RenderFrame* render_frame, |
31 int world_id, | 34 int world_id, |
32 int extension_group, | 35 int extension_group, |
33 const std::string& extension_scheme); | 36 const std::string& extension_scheme); |
34 ~TranslateHelper() override; | 37 ~TranslateHelper() override; |
35 | 38 |
36 // Informs us that the page's text has been extracted. | 39 // Informs us that the page's text has been extracted. |
37 void PageCaptured(const base::string16& contents); | 40 void PageCaptured(const base::string16& contents); |
38 | 41 |
39 // Lets the translation system know that we are preparing to navigate to | 42 // Lets the translation system know that we are preparing to navigate to |
40 // the specified URL. If there is anything that can or should be done before | 43 // the specified URL. If there is anything that can or should be done before |
41 // this URL loads, this is the time to prepare for it. | 44 // this URL loads, this is the time to prepare for it. |
42 void PrepareForUrl(const GURL& url); | 45 void PrepareForUrl(const GURL& url); |
43 | 46 |
| 47 // mojom::Page implementation. |
| 48 void Translate(const std::string& translate_script, |
| 49 const std::string& source_lang, |
| 50 const std::string& target_lang, |
| 51 const TranslateCallback& callback) override; |
| 52 void RevertTranslation() override; |
| 53 |
44 protected: | 54 protected: |
45 // The following methods are protected so they can be overridden in | |
46 // unit-tests. | |
47 void OnTranslatePage(int page_seq_no, | |
48 const std::string& translate_script, | |
49 const std::string& source_lang, | |
50 const std::string& target_lang); | |
51 void OnRevertTranslation(int page_seq_no); | |
52 | |
53 // Returns true if the translate library is available, meaning the JavaScript | 55 // Returns true if the translate library is available, meaning the JavaScript |
54 // has already been injected in that page. | 56 // has already been injected in that page. |
55 virtual bool IsTranslateLibAvailable(); | 57 virtual bool IsTranslateLibAvailable(); |
56 | 58 |
57 // Returns true if the translate library has been initialized successfully. | 59 // Returns true if the translate library has been initialized successfully. |
58 virtual bool IsTranslateLibReady(); | 60 virtual bool IsTranslateLibReady(); |
59 | 61 |
60 // Returns true if the translation script has finished translating the page. | 62 // Returns true if the translation script has finished translating the page. |
61 virtual bool HasTranslationFinished(); | 63 virtual bool HasTranslationFinished(); |
62 | 64 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 | 97 |
96 // Executes the JavaScript code in |script| in the main frame of RenderView. | 98 // Executes the JavaScript code in |script| in the main frame of RenderView. |
97 // and returns the number returned by the script evaluation if the script was | 99 // and returns the number returned by the script evaluation if the script was |
98 // run successfully. Otherwise, returns 0.0. | 100 // run successfully. Otherwise, returns 0.0. |
99 virtual double ExecuteScriptAndGetDoubleResult(const std::string& script); | 101 virtual double ExecuteScriptAndGetDoubleResult(const std::string& script); |
100 | 102 |
101 private: | 103 private: |
102 // Converts language code to the one used in server supporting list. | 104 // Converts language code to the one used in server supporting list. |
103 static void ConvertLanguageCodeSynonym(std::string* code); | 105 static void ConvertLanguageCodeSynonym(std::string* code); |
104 | 106 |
| 107 const mojom::ContentTranslateDriverPtr& GetTranslateDriver(); |
| 108 |
105 // RenderFrameObserver implementation. | 109 // RenderFrameObserver implementation. |
106 bool OnMessageReceived(const IPC::Message& message) override; | |
107 void OnDestruct() override; | 110 void OnDestruct() override; |
108 | 111 |
109 // Informs us that the page's text has been extracted. | |
110 void PageCapturedImpl(int page_seq_no, const base::string16& contents); | |
111 | |
112 // Cancels any translation that is currently being performed. This does not | 112 // Cancels any translation that is currently being performed. This does not |
113 // revert existing translations. | 113 // revert existing translations. |
114 void CancelPendingTranslation(); | 114 void CancelPendingTranslation(); |
115 | 115 |
116 // Checks if the current running page translation is finished or errored and | 116 // Checks if the current running page translation is finished or errored and |
117 // notifies the browser accordingly. If the translation has not terminated, | 117 // notifies the browser accordingly. If the translation has not terminated, |
118 // posts a task to check again later. | 118 // posts a task to check again later. |
119 void CheckTranslateStatus(int page_seq_no); | 119 void CheckTranslateStatus(); |
120 | 120 |
121 // Called by TranslatePage to do the actual translation. |count| is used to | 121 // Called by TranslatePage to do the actual translation. |count| is used to |
122 // limit the number of retries. | 122 // limit the number of retries. |
123 void TranslatePageImpl(int page_seq_no, int count); | 123 void TranslatePageImpl(int count); |
124 | 124 |
125 // Sends a message to the browser to notify it that the translation failed | 125 // Sends a message to the browser to notify it that the translation failed |
126 // with |error|. | 126 // with |error|. |
127 void NotifyBrowserTranslationFailed(TranslateErrors::Type error); | 127 void NotifyBrowserTranslationFailed(TranslateErrors::Type error); |
128 | 128 |
129 // Convenience method to access the main frame. Can return NULL, typically | 129 // Convenience method to access the main frame. Can return NULL, typically |
130 // if the page is being closed. | 130 // if the page is being closed. |
131 blink::WebLocalFrame* GetMainFrame(); | 131 blink::WebLocalFrame* GetMainFrame(); |
132 | 132 |
133 // An ever-increasing sequence number of the current page, used to match up | |
134 // translation requests with responses. | |
135 int page_seq_no_; | |
136 | |
137 // The states associated with the current translation. | 133 // The states associated with the current translation. |
138 bool translation_pending_; | 134 TranslateCallback translate_callback_pending_; |
139 std::string source_lang_; | 135 std::string source_lang_; |
140 std::string target_lang_; | 136 std::string target_lang_; |
141 | 137 |
142 // Time when a page langauge is determined. This is used to know a duration | 138 // Time when a page langauge is determined. This is used to know a duration |
143 // time from showing infobar to requesting translation. | 139 // time from showing infobar to requesting translation. |
144 base::TimeTicks language_determined_time_; | 140 base::TimeTicks language_determined_time_; |
145 | 141 |
146 // The world ID to use for script execution. | 142 // The world ID to use for script execution. |
147 int world_id_; | 143 int world_id_; |
148 | 144 |
149 // The extension group. | 145 // The extension group. |
150 int extension_group_; | 146 int extension_group_; |
151 | 147 |
152 // The URL scheme for translate extensions. | 148 // The URL scheme for translate extensions. |
153 std::string extension_scheme_; | 149 std::string extension_scheme_; |
154 | 150 |
| 151 mojom::ContentTranslateDriverPtr translate_driver_; |
| 152 |
| 153 mojo::Binding<mojom::Page> binding_; |
| 154 |
155 // Method factory used to make calls to TranslatePageImpl. | 155 // Method factory used to make calls to TranslatePageImpl. |
156 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; | 156 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; |
157 | 157 |
158 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); | 158 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); |
159 }; | 159 }; |
160 | 160 |
161 } // namespace translate | 161 } // namespace translate |
162 | 162 |
163 #endif // COMPONENTS_TRANSLATE_CONTENT_RENDERER_TRANSLATE_HELPER_H_ | 163 #endif // COMPONENTS_TRANSLATE_CONTENT_RENDERER_TRANSLATE_HELPER_H_ |
OLD | NEW |