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 COMPONENTS_TRANSLATE_CONTENT_BROWSER_CONTENT_TRANSLATE_DRIVER_H_
6 #define COMPONENTS_TRANSLATE_CONTENT_BROWSER_CONTENT_TRANSLATE_DRIVER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/observer_list.h"
11 #include "components/translate/core/browser/translate_driver.h"
12 #include "components/translate/core/common/translate_errors.h"
13 #include "content/public/browser/web_contents_observer.h"
16 class NavigationController
;
22 struct LanguageDetectionDetails
;
23 class TranslateManager
;
25 // Content implementation of TranslateDriver.
26 class ContentTranslateDriver
: public TranslateDriver
,
27 public content::WebContentsObserver
{
30 // The observer for the ContentTranslateDriver.
33 // Handles when the value of IsPageTranslated is changed.
34 virtual void OnIsPageTranslatedChanged(content::WebContents
* source
) {};
36 // Handles when the value of translate_enabled is changed.
37 virtual void OnTranslateEnabledChanged(content::WebContents
* source
) {};
39 // Called when the page language has been determined.
40 virtual void OnLanguageDetermined(
41 const translate::LanguageDetectionDetails
& details
) {};
43 // Called when the page has been translated.
44 virtual void OnPageTranslated(
45 const std::string
& original_lang
,
46 const std::string
& translated_lang
,
47 translate::TranslateErrors::Type error_type
) {};
50 virtual ~Observer() {}
53 ContentTranslateDriver(content::NavigationController
* nav_controller
);
54 ~ContentTranslateDriver() override
;
56 // Adds or Removes observers.
57 void AddObserver(Observer
* observer
);
58 void RemoveObserver(Observer
* observer
);
60 // Number of attempts before waiting for a page to be fully reloaded.
61 void set_translate_max_reload_attempts(int attempts
) {
62 max_reload_check_attempts_
= attempts
;
65 // Sets the TranslateManager associated with this driver.
66 void set_translate_manager(TranslateManager
* manager
) {
67 translate_manager_
= manager
;
70 // Initiates translation once the page is finished loading.
71 void InitiateTranslation(const std::string
& page_lang
, int attempt
);
73 // TranslateDriver methods.
74 void OnIsPageTranslatedChanged() override
;
75 void OnTranslateEnabledChanged() override
;
76 bool IsLinkNavigation() override
;
77 void TranslatePage(int page_seq_no
,
78 const std::string
& translate_script
,
79 const std::string
& source_lang
,
80 const std::string
& target_lang
) override
;
81 void RevertTranslation(int page_seq_no
) override
;
82 bool IsOffTheRecord() override
;
83 const std::string
& GetContentsMimeType() override
;
84 const GURL
& GetLastCommittedURL() override
;
85 const GURL
& GetActiveURL() override
;
86 const GURL
& GetVisibleURL() override
;
87 bool HasCurrentPage() override
;
88 void OpenUrlInNewTab(const GURL
& url
) override
;
90 // content::WebContentsObserver implementation.
91 void NavigationEntryCommitted(
92 const content::LoadCommittedDetails
& load_details
) override
;
93 void DidNavigateAnyFrame(content::RenderFrameHost
* render_frame_host
,
94 const content::LoadCommittedDetails
& details
,
95 const content::FrameNavigateParams
& params
) override
;
96 bool OnMessageReceived(const IPC::Message
& message
) override
;
99 void OnTranslateAssignedSequenceNumber(int page_seq_no
);
100 void OnLanguageDetermined(const LanguageDetectionDetails
& details
,
101 bool page_needs_translation
);
102 void OnPageTranslated(const std::string
& original_lang
,
103 const std::string
& translated_lang
,
104 TranslateErrors::Type error_type
);
107 // The navigation controller of the tab we are associated with.
108 content::NavigationController
* navigation_controller_
;
110 TranslateManager
* translate_manager_
;
112 ObserverList
<Observer
, true> observer_list_
;
114 // Max number of attempts before checking if a page has been reloaded.
115 int max_reload_check_attempts_
;
117 base::WeakPtrFactory
<ContentTranslateDriver
> weak_pointer_factory_
;
119 DISALLOW_COPY_AND_ASSIGN(ContentTranslateDriver
);
122 } // namespace translate
124 #endif // COMPONENTS_TRANSLATE_CONTENT_BROWSER_CONTENT_TRANSLATE_DRIVER_H_