OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_ | 5 #ifndef CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_ |
6 #define CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_ | 6 #define CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "chrome/browser/ui/translate/translate_bubble_model.h" | 12 #include "chrome/browser/ui/translate/translate_bubble_model.h" |
13 #include "components/translate/content/browser/browser_cld_data_provider.h" | |
13 #include "components/translate/content/browser/content_translate_driver.h" | 14 #include "components/translate/content/browser/content_translate_driver.h" |
14 #include "components/translate/core/browser/translate_client.h" | 15 #include "components/translate/core/browser/translate_client.h" |
15 #include "components/translate/core/browser/translate_step.h" | 16 #include "components/translate/core/browser/translate_step.h" |
16 #include "components/translate/core/common/translate_errors.h" | 17 #include "components/translate/core/common/translate_errors.h" |
17 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
18 #include "content/public/browser/web_contents_user_data.h" | 19 #include "content/public/browser/web_contents_user_data.h" |
19 | 20 |
20 #if defined(CLD2_DYNAMIC_MODE) | |
21 #include "base/basictypes.h" | |
22 #include "base/lazy_instance.h" | |
23 #include "base/synchronization/lock.h" | |
24 #include "base/task_runner.h" | |
25 #endif | |
26 | |
27 namespace base { | 21 namespace base { |
28 class File; | 22 class File; |
29 } // namespace base | 23 } // namespace base |
30 | 24 |
31 namespace content { | 25 namespace content { |
32 class BrowserContext; | 26 class BrowserContext; |
33 class WebContents; | 27 class WebContents; |
34 } // namespace content | 28 } // namespace content |
35 | 29 |
36 namespace test { | 30 namespace test { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 | 115 |
122 // Initiates translation once the page is finished loading. | 116 // Initiates translation once the page is finished loading. |
123 void InitiateTranslation(const std::string& page_lang, int attempt); | 117 void InitiateTranslation(const std::string& page_lang, int attempt); |
124 void OnLanguageDetermined(const LanguageDetectionDetails& details, | 118 void OnLanguageDetermined(const LanguageDetectionDetails& details, |
125 bool page_needs_translation); | 119 bool page_needs_translation); |
126 void OnPageTranslated(int32 page_id, | 120 void OnPageTranslated(int32 page_id, |
127 const std::string& original_lang, | 121 const std::string& original_lang, |
128 const std::string& translated_lang, | 122 const std::string& translated_lang, |
129 TranslateErrors::Type error_type); | 123 TranslateErrors::Type error_type); |
130 | 124 |
131 #if defined(CLD2_DYNAMIC_MODE) | |
132 // Called when we receive ChromeViewHostMsg_NeedCLDData from a renderer. | |
133 // If we have already cached the data, responds immediately; else, enqueues | |
134 // a HandleCLDDataRequest on the blocking pool to cache the data. | |
135 // Acquires and releases s_file_lock_ in a non-blocking manner; queries | |
136 // handled while the file is being cached will gracefully and immediately | |
137 // fail. | |
138 // It is up to the originator of the message to poll again later if required; | |
139 // no "negative response" will be generated. | |
140 void OnCLDDataRequested(); | |
141 | |
142 // Invoked on the blocking pool in order to cache the data. When successful, | |
143 // immediately responds to the request that initiated OnCLDDataRequested. | |
144 // Holds s_file_lock_ while the file is being cached. | |
145 static void HandleCLDDataRequest(); | |
146 | |
147 // If the CLD data is ready, send it to the renderer. Briefly checks the lock. | |
148 void MaybeSendCLDDataAvailable(); | |
149 | |
150 // Sends the renderer a response containing the data file handle. No locking. | |
151 void SendCLDDataAvailable(const base::File* handle, | |
152 const uint64 data_offset, | |
153 const uint64 data_length); | |
154 | |
155 // The data file, cached as long as the process stays alive. | |
156 // We also track the offset at which the data starts, and its length. | |
157 static base::File* s_cached_file_; // guarded by file_lock_ | |
158 static uint64 s_cached_data_offset_; // guarded by file_lock_ | |
159 static uint64 s_cached_data_length_; // guarded by file_lock_ | |
160 | |
161 // Guards s_cached_file_ | |
162 static base::LazyInstance<base::Lock> s_file_lock_; | |
163 | |
164 #endif | |
165 | |
166 // Shows the translate bubble. | 125 // Shows the translate bubble. |
167 void ShowBubble(translate::TranslateStep step, | 126 void ShowBubble(translate::TranslateStep step, |
168 TranslateErrors::Type error_type); | 127 TranslateErrors::Type error_type); |
169 | 128 |
170 // Max number of attempts before checking if a page has been reloaded. | 129 // Max number of attempts before checking if a page has been reloaded. |
171 int max_reload_check_attempts_; | 130 int max_reload_check_attempts_; |
172 | 131 |
173 ContentTranslateDriver translate_driver_; | 132 ContentTranslateDriver translate_driver_; |
174 scoped_ptr<TranslateManager> translate_manager_; | 133 scoped_ptr<TranslateManager> translate_manager_; |
175 | 134 |
176 // Necessary for binding the callback to HandleCLDDataRequest on the blocking | 135 // Provides CLD data for this process. |
Takashi Toyoshima
2014/06/19 10:20:34
Why do you remove these comments for WeakPtrFactor
Andrew Hayden (chromium.org)
2014/06/19 13:40:19
Because this factory was here before HandleCLDData
| |
177 // pool and for delaying translation initialization until the page has | 136 scoped_ptr<content::BrowserCldDataProvider> cld_data_provider_; |
178 // finished loading on a reload. | 137 |
179 base::WeakPtrFactory<ChromeTranslateClient> weak_pointer_factory_; | 138 base::WeakPtrFactory<ChromeTranslateClient> weak_pointer_factory_; |
180 | 139 |
181 DISALLOW_COPY_AND_ASSIGN(ChromeTranslateClient); | 140 DISALLOW_COPY_AND_ASSIGN(ChromeTranslateClient); |
182 }; | 141 }; |
183 | 142 |
184 #endif // CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_ | 143 #endif // CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_ |
OLD | NEW |