Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: chrome/browser/translate/chrome_translate_client.h

Issue 333603002: Modularize Compact Language Detector 2 (CLD2) data sources (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge https://codereview.chromium.org/326383005 Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 const std::string target_language, 95 const std::string target_language,
102 TranslateErrors::Type error_type, 96 TranslateErrors::Type error_type,
103 bool triggered_from_menu) OVERRIDE; 97 bool triggered_from_menu) OVERRIDE;
104 virtual bool IsTranslatableURL(const GURL& url) OVERRIDE; 98 virtual bool IsTranslatableURL(const GURL& url) OVERRIDE;
105 virtual void ShowReportLanguageDetectionErrorUI( 99 virtual void ShowReportLanguageDetectionErrorUI(
106 const GURL& report_url) OVERRIDE; 100 const GURL& report_url) OVERRIDE;
107 101
108 private: 102 private:
109 explicit ChromeTranslateClient(content::WebContents* web_contents); 103 explicit ChromeTranslateClient(content::WebContents* web_contents);
110 friend class content::WebContentsUserData<ChromeTranslateClient>; 104 friend class content::WebContentsUserData<ChromeTranslateClient>;
111 friend class test::ScopedCLDDynamicDataHarness; // For cleaning static state.
112 105
113 // content::WebContentsObserver implementation. 106 // content::WebContentsObserver implementation.
114 virtual void NavigationEntryCommitted( 107 virtual void NavigationEntryCommitted(
115 const content::LoadCommittedDetails& load_details) OVERRIDE; 108 const content::LoadCommittedDetails& load_details) OVERRIDE;
116 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 109 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
117 virtual void DidNavigateAnyFrame( 110 virtual void DidNavigateAnyFrame(
118 const content::LoadCommittedDetails& details, 111 const content::LoadCommittedDetails& details,
119 const content::FrameNavigateParams& params) OVERRIDE; 112 const content::FrameNavigateParams& params) OVERRIDE;
120 virtual void WebContentsDestroyed() OVERRIDE; 113 virtual void WebContentsDestroyed() OVERRIDE;
121 114
122 // Initiates translation once the page is finished loading. 115 // Initiates translation once the page is finished loading.
123 void InitiateTranslation(const std::string& page_lang, int attempt); 116 void InitiateTranslation(const std::string& page_lang, int attempt);
124 void OnLanguageDetermined(const LanguageDetectionDetails& details, 117 void OnLanguageDetermined(const LanguageDetectionDetails& details,
125 bool page_needs_translation); 118 bool page_needs_translation);
126 void OnPageTranslated(int32 page_id, 119 void OnPageTranslated(int32 page_id,
127 const std::string& original_lang, 120 const std::string& original_lang,
128 const std::string& translated_lang, 121 const std::string& translated_lang,
129 TranslateErrors::Type error_type); 122 TranslateErrors::Type error_type);
130 123
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. 124 // Shows the translate bubble.
167 void ShowBubble(translate::TranslateStep step, 125 void ShowBubble(translate::TranslateStep step,
168 TranslateErrors::Type error_type); 126 TranslateErrors::Type error_type);
169 127
170 // Max number of attempts before checking if a page has been reloaded. 128 // Max number of attempts before checking if a page has been reloaded.
171 int max_reload_check_attempts_; 129 int max_reload_check_attempts_;
172 130
173 ContentTranslateDriver translate_driver_; 131 ContentTranslateDriver translate_driver_;
174 scoped_ptr<TranslateManager> translate_manager_; 132 scoped_ptr<TranslateManager> translate_manager_;
175 133
176 // Necessary for binding the callback to HandleCLDDataRequest on the blocking 134 // Provides CLD data for this process.
177 // pool and for delaying translation initialization until the page has 135 scoped_ptr<translate::BrowserCldDataProvider> cld_data_provider_;
178 // finished loading on a reload. 136
179 base::WeakPtrFactory<ChromeTranslateClient> weak_pointer_factory_; 137 base::WeakPtrFactory<ChromeTranslateClient> weak_pointer_factory_;
180 138
181 DISALLOW_COPY_AND_ASSIGN(ChromeTranslateClient); 139 DISALLOW_COPY_AND_ASSIGN(ChromeTranslateClient);
182 }; 140 };
183 141
184 #endif // CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_ 142 #endif // CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_
OLDNEW
« no previous file with comments | « chrome/browser/policy/policy_browsertest.cc ('k') | chrome/browser/translate/chrome_translate_client.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698