OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 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 CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ | 5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ |
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ | 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
| 10 #if defined(CLD2_DYNAMIC_MODE) |
| 11 #include "base/basictypes.h" |
| 12 #include "base/lazy_instance.h" |
| 13 #endif |
10 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #if defined(CLD2_DYNAMIC_MODE) |
| 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/platform_file.h" |
| 18 #include "base/synchronization/lock.h" |
| 19 #include "base/task_runner.h" |
| 20 #endif |
11 #include "chrome/browser/ui/translate/translate_bubble_model.h" | 21 #include "chrome/browser/ui/translate/translate_bubble_model.h" |
12 #include "components/translate/content/browser/content_translate_driver.h" | 22 #include "components/translate/content/browser/content_translate_driver.h" |
13 #include "components/translate/core/browser/translate_client.h" | 23 #include "components/translate/core/browser/translate_client.h" |
14 #include "components/translate/core/common/translate_errors.h" | 24 #include "components/translate/core/common/translate_errors.h" |
15 #include "content/public/browser/web_contents_observer.h" | 25 #include "content/public/browser/web_contents_observer.h" |
16 #include "content/public/browser/web_contents_user_data.h" | 26 #include "content/public/browser/web_contents_user_data.h" |
17 | 27 |
18 namespace content { | 28 namespace content { |
19 class BrowserContext; | 29 class BrowserContext; |
20 class WebContents; | 30 class WebContents; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 virtual void WebContentsDestroyed( | 104 virtual void WebContentsDestroyed( |
95 content::WebContents* web_contents) OVERRIDE; | 105 content::WebContents* web_contents) OVERRIDE; |
96 | 106 |
97 void OnLanguageDetermined(const LanguageDetectionDetails& details, | 107 void OnLanguageDetermined(const LanguageDetectionDetails& details, |
98 bool page_needs_translation); | 108 bool page_needs_translation); |
99 void OnPageTranslated(int32 page_id, | 109 void OnPageTranslated(int32 page_id, |
100 const std::string& original_lang, | 110 const std::string& original_lang, |
101 const std::string& translated_lang, | 111 const std::string& translated_lang, |
102 TranslateErrors::Type error_type); | 112 TranslateErrors::Type error_type); |
103 | 113 |
| 114 #if defined(CLD2_DYNAMIC_MODE) |
| 115 // Called when we receive ChromeViewHostMsg_NeedCLDData from a renderer. |
| 116 // If we have already cached the data, responds immediately; else, enqueues |
| 117 // a HandleCLDDataRequest on the blocking pool to cache the data. |
| 118 // Acquires and releases s_file_lock_ in a non-blocking manner; queries |
| 119 // handled while the file is being cached will gracefully and immediately |
| 120 // fail. |
| 121 // It is up to the originator of the message to poll again later if required; |
| 122 // no "negative response" will be generated. |
| 123 void OnCLDDataRequested(); |
| 124 |
| 125 // Invoked on the blocking pool in order to cache the data. When successful, |
| 126 // immediately responds to the request that initiated OnCLDDataRequested. |
| 127 // Holds s_file_lock_ while the file is being cached. |
| 128 static void HandleCLDDataRequest(); |
| 129 |
| 130 // If the CLD data is ready, send it to the renderer. Briefly checks the lock. |
| 131 void MaybeSendCLDDataAvailable(); |
| 132 |
| 133 // Sends the renderer a response containing the data file handle. No locking. |
| 134 void SendCLDDataAvailable(const base::PlatformFile handle, |
| 135 const uint64 data_offset, |
| 136 const uint64 data_length); |
| 137 |
| 138 // Necessary for binding the callback to HandleCLDDataRequest on the blocking |
| 139 // pool. |
| 140 base::WeakPtrFactory<TranslateTabHelper> weak_pointer_factory_; |
| 141 |
| 142 // The data file, cached as long as the process stays alive. |
| 143 // We also track the offset at which the data starts, and its length. |
| 144 static base::PlatformFile s_cached_platform_file_; // guarded by file_lock_ |
| 145 static uint64 s_cached_data_offset_; // guarded by file_lock_ |
| 146 static uint64 s_cached_data_length_; // guarded by file_lock_ |
| 147 |
| 148 // Guards s_cached_platform_file_ |
| 149 static base::LazyInstance<base::Lock> s_file_lock_; |
| 150 |
| 151 #endif |
| 152 |
104 // Shows the translate bubble. | 153 // Shows the translate bubble. |
105 void ShowBubble(TranslateStep step, TranslateErrors::Type error_type); | 154 void ShowBubble(TranslateStep step, TranslateErrors::Type error_type); |
106 | 155 |
107 ContentTranslateDriver translate_driver_; | 156 ContentTranslateDriver translate_driver_; |
108 scoped_ptr<TranslateManager> translate_manager_; | 157 scoped_ptr<TranslateManager> translate_manager_; |
109 | 158 |
110 DISALLOW_COPY_AND_ASSIGN(TranslateTabHelper); | 159 DISALLOW_COPY_AND_ASSIGN(TranslateTabHelper); |
111 }; | 160 }; |
112 | 161 |
113 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ | 162 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_ |
OLD | NEW |