1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_
#define CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/translate/translate_bubble_model.h"
#include "components/translate/content/browser/content_translate_driver.h"
#include "components/translate/core/browser/translate_client.h"
#include "components/translate/core/browser/translate_step.h"
#include "components/translate/core/common/translate_errors.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#if defined(CLD2_DYNAMIC_MODE)
#include "base/basictypes.h"
#include "base/lazy_instance.h"
#include "base/synchronization/lock.h"
#include "base/task_runner.h"
#endif
namespace base {
class File;
}
namespace content {
class BrowserContext;
class WebContents;
}
struct LanguageDetectionDetails;
class PrefService;
class TranslateAcceptLanguages;
class TranslatePrefs;
class TranslateManager;
class TranslateTabHelper
: public TranslateClient,
public content::WebContentsObserver,
public content::WebContentsUserData<TranslateTabHelper> {
public:
virtual ~TranslateTabHelper();
// Gets the LanguageState associated with the page.
LanguageState& GetLanguageState();
// Returns the ContentTranslateDriver instance associated with this
// WebContents.
ContentTranslateDriver& translate_driver() { return translate_driver_; }
// Helper method to return a new TranslatePrefs instance.
static scoped_ptr<TranslatePrefs> CreateTranslatePrefs(PrefService* prefs);
// Helper method to return the TranslateAcceptLanguages instance associated
// with |browser_context|.
static TranslateAcceptLanguages* GetTranslateAcceptLanguages(
content::BrowserContext* browser_context);
// Helper method to return the TranslateManager instance associated with
// |web_contents|, or NULL if there is no such associated instance.
static TranslateManager* GetManagerFromWebContents(
content::WebContents* web_contents);
// Gets |source| and |target| language for translation.
static void GetTranslateLanguages(content::WebContents* web_contents,
std::string* source,
std::string* target);
// Gets the associated TranslateManager.
TranslateManager* GetTranslateManager();
// Gets the associated WebContents. Returns NULL if the WebContents is being
// destroyed.
content::WebContents* GetWebContents();
// Number of attempts before waiting for a page to be fully reloaded.
void set_translate_max_reload_attempts(int attempts) {
max_reload_check_attempts_ = attempts;
}
// TranslateClient implementation.
virtual TranslateDriver* GetTranslateDriver() OVERRIDE;
virtual PrefService* GetPrefs() OVERRIDE;
virtual scoped_ptr<TranslatePrefs> GetTranslatePrefs() OVERRIDE;
virtual TranslateAcceptLanguages* GetTranslateAcceptLanguages() OVERRIDE;
virtual void ShowTranslateUI(translate::TranslateStep step,
const std::string source_language,
const std::string target_language,
TranslateErrors::Type error_type,
bool triggered_from_menu) OVERRIDE;
virtual bool IsTranslatableURL(const GURL& url) OVERRIDE;
virtual void ShowReportLanguageDetectionErrorUI(
const GURL& report_url) OVERRIDE;
private:
explicit TranslateTabHelper(content::WebContents* web_contents);
friend class content::WebContentsUserData<TranslateTabHelper>;
// content::WebContentsObserver implementation.
virtual void NavigationEntryCommitted(
const content::LoadCommittedDetails& load_details) OVERRIDE;
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
virtual void DidNavigateAnyFrame(
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) OVERRIDE;
virtual void WebContentsDestroyed(
content::WebContents* web_contents) OVERRIDE;
// Initiates translation once the page is finished loading.
void InitiateTranslation(const std::string& page_lang, int attempt);
void OnLanguageDetermined(const LanguageDetectionDetails& details,
bool page_needs_translation);
void OnPageTranslated(int32 page_id,
const std::string& original_lang,
const std::string& translated_lang,
TranslateErrors::Type error_type);
#if defined(CLD2_DYNAMIC_MODE)
// Called when we receive ChromeViewHostMsg_NeedCLDData from a renderer.
// If we have already cached the data, responds immediately; else, enqueues
// a HandleCLDDataRequest on the blocking pool to cache the data.
// Acquires and releases s_file_lock_ in a non-blocking manner; queries
// handled while the file is being cached will gracefully and immediately
// fail.
// It is up to the originator of the message to poll again later if required;
// no "negative response" will be generated.
void OnCLDDataRequested();
// Invoked on the blocking pool in order to cache the data. When successful,
// immediately responds to the request that initiated OnCLDDataRequested.
// Holds s_file_lock_ while the file is being cached.
static void HandleCLDDataRequest();
// If the CLD data is ready, send it to the renderer. Briefly checks the lock.
void MaybeSendCLDDataAvailable();
// Sends the renderer a response containing the data file handle. No locking.
void SendCLDDataAvailable(const base::File* handle,
const uint64 data_offset,
const uint64 data_length);
// The data file, cached as long as the process stays alive.
// We also track the offset at which the data starts, and its length.
static base::File* s_cached_file_; // guarded by file_lock_
static uint64 s_cached_data_offset_; // guarded by file_lock_
static uint64 s_cached_data_length_; // guarded by file_lock_
// Guards s_cached_file_
static base::LazyInstance<base::Lock> s_file_lock_;
#endif
// Shows the translate bubble.
void ShowBubble(translate::TranslateStep step,
TranslateErrors::Type error_type);
// Max number of attempts before checking if a page has been reloaded.
int max_reload_check_attempts_;
ContentTranslateDriver translate_driver_;
scoped_ptr<TranslateManager> translate_manager_;
// Necessary for binding the callback to HandleCLDDataRequest on the blocking
// pool and for delaying translation initialization until the page has
// finished loading on a reload.
base::WeakPtrFactory<TranslateTabHelper> weak_pointer_factory_;
DISALLOW_COPY_AND_ASSIGN(TranslateTabHelper);
};
#endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_TAB_HELPER_H_
|