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.
4
5#ifndef CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_
6#define CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_
7
8#include <string>
9
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"
19
20namespace content {
21class BrowserContext;
22class WebContents;
23}  // namespace content
24
25namespace test {
26class ScopedCLDDynamicDataHarness;
27}  // namespace test
28
29class PrefService;
30
31namespace translate {
32class LanguageState;
33class TranslateAcceptLanguages;
34class TranslatePrefs;
35class TranslateManager;
36}  // namespace translate
37
38class ChromeTranslateClient
39    : public translate::TranslateClient,
40      public translate::ContentTranslateDriver::Observer,
41      public content::WebContentsObserver,
42      public content::WebContentsUserData<ChromeTranslateClient> {
43 public:
44  virtual ~ChromeTranslateClient();
45
46  // Gets the LanguageState associated with the page.
47  translate::LanguageState& GetLanguageState();
48
49  // Returns the ContentTranslateDriver instance associated with this
50  // WebContents.
51  translate::ContentTranslateDriver& translate_driver() {
52    return translate_driver_;
53  }
54
55  // Helper method to return a new TranslatePrefs instance.
56  static scoped_ptr<translate::TranslatePrefs> CreateTranslatePrefs(
57      PrefService* prefs);
58
59  // Helper method to return the TranslateAcceptLanguages instance associated
60  // with |browser_context|.
61  static translate::TranslateAcceptLanguages* GetTranslateAcceptLanguages(
62      content::BrowserContext* browser_context);
63
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);
68
69  // Gets |source| and |target| language for translation.
70  static void GetTranslateLanguages(content::WebContents* web_contents,
71                                    std::string* source,
72                                    std::string* target);
73
74  // Gets the associated TranslateManager.
75  translate::TranslateManager* GetTranslateManager();
76
77  // Gets the associated WebContents. Returns NULL if the WebContents is being
78  // destroyed.
79  content::WebContents* GetWebContents();
80
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()
86      OVERRIDE;
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;
98
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;
106
107 private:
108  explicit ChromeTranslateClient(content::WebContents* web_contents);
109  friend class content::WebContentsUserData<ChromeTranslateClient>;
110
111  // content::WebContentsObserver implementation.
112  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
113  virtual void WebContentsDestroyed() OVERRIDE;
114
115  // Shows the translate bubble.
116  void ShowBubble(translate::TranslateStep step,
117                  translate::TranslateErrors::Type error_type);
118
119  translate::ContentTranslateDriver translate_driver_;
120  scoped_ptr<translate::TranslateManager> translate_manager_;
121
122  // Provides CLD data for this process.
123  scoped_ptr<translate::BrowserCldDataProvider> cld_data_provider_;
124
125  DISALLOW_COPY_AND_ASSIGN(ChromeTranslateClient);
126};
127
128#endif  // CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_
129