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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_
6 #define CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/ui/translate/translate_bubble_model.h"
12 #include "components/translate/content/browser/browser_cld_data_provider.h"
13 #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_step.h"
16 #include "components/translate/core/common/translate_errors.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "content/public/browser/web_contents_user_data.h"
23 } // namespace content
26 class ScopedCLDDynamicDataHarness;
33 class TranslateAcceptLanguages;
35 class TranslateManager;
36 } // namespace translate
38 class ChromeTranslateClient
39 : public translate::TranslateClient,
40 public translate::ContentTranslateDriver::Observer,
41 public content::WebContentsObserver,
42 public content::WebContentsUserData<ChromeTranslateClient> {
44 virtual ~ChromeTranslateClient();
46 // Gets the LanguageState associated with the page.
47 translate::LanguageState& GetLanguageState();
49 // Returns the ContentTranslateDriver instance associated with this
51 translate::ContentTranslateDriver& translate_driver() {
52 return translate_driver_;
55 // Helper method to return a new TranslatePrefs instance.
56 static scoped_ptr<translate::TranslatePrefs> CreateTranslatePrefs(
59 // Helper method to return the TranslateAcceptLanguages instance associated
60 // with |browser_context|.
61 static translate::TranslateAcceptLanguages* GetTranslateAcceptLanguages(
62 content::BrowserContext* browser_context);
64 // Helper method to return the TranslateManager instance associated with
65 // |web_contents|, or NULL if there is no such associated instance.
66 static translate::TranslateManager* GetManagerFromWebContents(
67 content::WebContents* web_contents);
69 // Gets |source| and |target| language for translation.
70 static void GetTranslateLanguages(content::WebContents* web_contents,
74 // Gets the associated TranslateManager.
75 translate::TranslateManager* GetTranslateManager();
77 // Gets the associated WebContents. Returns NULL if the WebContents is being
79 content::WebContents* GetWebContents();
81 // TranslateClient implementation.
82 virtual translate::TranslateDriver* GetTranslateDriver() OVERRIDE;
83 virtual PrefService* GetPrefs() OVERRIDE;
84 virtual scoped_ptr<translate::TranslatePrefs> GetTranslatePrefs() OVERRIDE;
85 virtual translate::TranslateAcceptLanguages* GetTranslateAcceptLanguages()
87 virtual int GetInfobarIconID() const OVERRIDE;
88 virtual scoped_ptr<infobars::InfoBar> CreateInfoBar(
89 scoped_ptr<translate::TranslateInfoBarDelegate> delegate) const OVERRIDE;
90 virtual void ShowTranslateUI(translate::TranslateStep step,
91 const std::string source_language,
92 const std::string target_language,
93 translate::TranslateErrors::Type error_type,
94 bool triggered_from_menu) OVERRIDE;
95 virtual bool IsTranslatableURL(const GURL& url) OVERRIDE;
96 virtual void ShowReportLanguageDetectionErrorUI(
97 const GURL& report_url) OVERRIDE;
99 // ContentTranslateDriver::Observer implementation.
100 virtual void OnLanguageDetermined(
101 const translate::LanguageDetectionDetails& details) OVERRIDE;
102 virtual void OnPageTranslated(
103 const std::string& original_lang,
104 const std::string& translated_lang,
105 translate::TranslateErrors::Type error_type) OVERRIDE;
108 explicit ChromeTranslateClient(content::WebContents* web_contents);
109 friend class content::WebContentsUserData<ChromeTranslateClient>;
111 // content::WebContentsObserver implementation.
112 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
113 virtual void WebContentsDestroyed() OVERRIDE;
115 // Shows the translate bubble.
116 void ShowBubble(translate::TranslateStep step,
117 translate::TranslateErrors::Type error_type);
119 translate::ContentTranslateDriver translate_driver_;
120 scoped_ptr<translate::TranslateManager> translate_manager_;
122 // Provides CLD data for this process.
123 scoped_ptr<translate::BrowserCldDataProvider> cld_data_provider_;
125 DISALLOW_COPY_AND_ASSIGN(ChromeTranslateClient);
128 #endif // CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_