OLD | NEW |
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 COMPONENTS_TRANSLATE_CONTENT_BROWSER_CONTENT_TRANSLATE_DRIVER_H_ | 5 #ifndef COMPONENTS_TRANSLATE_CONTENT_BROWSER_CONTENT_TRANSLATE_DRIVER_H_ |
6 #define COMPONENTS_TRANSLATE_CONTENT_BROWSER_CONTENT_TRANSLATE_DRIVER_H_ | 6 #define COMPONENTS_TRANSLATE_CONTENT_BROWSER_CONTENT_TRANSLATE_DRIVER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/memory/weak_ptr.h" |
9 #include "components/translate/core/browser/translate_driver.h" | 10 #include "components/translate/core/browser/translate_driver.h" |
| 11 #include "content/public/browser/web_contents_observer.h" |
10 | 12 |
11 namespace content { | 13 namespace content { |
12 class NavigationController; | 14 class NavigationController; |
13 class WebContents; | 15 class WebContents; |
14 } | 16 } |
15 | 17 |
16 namespace translate { | 18 namespace translate { |
17 | 19 |
| 20 class TranslateManager; |
| 21 |
18 // Content implementation of TranslateDriver. | 22 // Content implementation of TranslateDriver. |
19 class ContentTranslateDriver : public TranslateDriver { | 23 class ContentTranslateDriver : public TranslateDriver, |
| 24 public content::WebContentsObserver { |
20 public: | 25 public: |
21 | 26 |
22 // The observer for the ContentTranslateDriver. | 27 // The observer for the ContentTranslateDriver. |
23 class Observer { | 28 class Observer { |
24 public: | 29 public: |
25 // Handles when the value of IsPageTranslated is changed. | 30 // Handles when the value of IsPageTranslated is changed. |
26 virtual void OnIsPageTranslatedChanged(content::WebContents* source) = 0; | 31 virtual void OnIsPageTranslatedChanged(content::WebContents* source) = 0; |
27 | 32 |
28 // Handles when the value of translate_enabled is changed. | 33 // Handles when the value of translate_enabled is changed. |
29 virtual void OnTranslateEnabledChanged(content::WebContents* source) = 0; | 34 virtual void OnTranslateEnabledChanged(content::WebContents* source) = 0; |
30 | 35 |
31 protected: | 36 protected: |
32 virtual ~Observer() {} | 37 virtual ~Observer() {} |
33 }; | 38 }; |
34 | 39 |
35 ContentTranslateDriver(content::NavigationController* nav_controller); | 40 ContentTranslateDriver(content::NavigationController* nav_controller); |
36 virtual ~ContentTranslateDriver(); | 41 virtual ~ContentTranslateDriver(); |
37 | 42 |
38 // Sets the Observer. Calling this method is optional. | 43 // Sets the Observer. Calling this method is optional. |
39 void set_observer(Observer* observer) { observer_ = observer; } | 44 void set_observer(Observer* observer) { observer_ = observer; } |
40 | 45 |
| 46 // Number of attempts before waiting for a page to be fully reloaded. |
| 47 void set_translate_max_reload_attempts(int attempts) { |
| 48 max_reload_check_attempts_ = attempts; |
| 49 } |
| 50 |
| 51 // Sets the TranslateManager associated with this driver. |
| 52 void set_translate_manager(TranslateManager* manager) { |
| 53 translate_manager_ = manager; |
| 54 } |
| 55 |
| 56 // Initiates translation once the page is finished loading. |
| 57 void InitiateTranslation(const std::string& page_lang, int attempt); |
| 58 |
41 // TranslateDriver methods. | 59 // TranslateDriver methods. |
42 virtual void OnIsPageTranslatedChanged() OVERRIDE; | 60 virtual void OnIsPageTranslatedChanged() OVERRIDE; |
43 virtual void OnTranslateEnabledChanged() OVERRIDE; | 61 virtual void OnTranslateEnabledChanged() OVERRIDE; |
44 virtual bool IsLinkNavigation() OVERRIDE; | 62 virtual bool IsLinkNavigation() OVERRIDE; |
45 virtual void TranslatePage(int page_seq_no, | 63 virtual void TranslatePage(int page_seq_no, |
46 const std::string& translate_script, | 64 const std::string& translate_script, |
47 const std::string& source_lang, | 65 const std::string& source_lang, |
48 const std::string& target_lang) OVERRIDE; | 66 const std::string& target_lang) OVERRIDE; |
49 virtual void RevertTranslation(int page_seq_no) OVERRIDE; | 67 virtual void RevertTranslation(int page_seq_no) OVERRIDE; |
50 virtual bool IsOffTheRecord() OVERRIDE; | 68 virtual bool IsOffTheRecord() OVERRIDE; |
51 virtual const std::string& GetContentsMimeType() OVERRIDE; | 69 virtual const std::string& GetContentsMimeType() OVERRIDE; |
52 virtual const GURL& GetLastCommittedURL() OVERRIDE; | 70 virtual const GURL& GetLastCommittedURL() OVERRIDE; |
53 virtual const GURL& GetActiveURL() OVERRIDE; | 71 virtual const GURL& GetActiveURL() OVERRIDE; |
54 virtual const GURL& GetVisibleURL() OVERRIDE; | 72 virtual const GURL& GetVisibleURL() OVERRIDE; |
55 virtual bool HasCurrentPage() OVERRIDE; | 73 virtual bool HasCurrentPage() OVERRIDE; |
56 virtual void OpenUrlInNewTab(const GURL& url) OVERRIDE; | 74 virtual void OpenUrlInNewTab(const GURL& url) OVERRIDE; |
57 | 75 |
| 76 // content::WebContentsObserver implementation. |
| 77 virtual void NavigationEntryCommitted( |
| 78 const content::LoadCommittedDetails& load_details) OVERRIDE; |
| 79 virtual void DidNavigateAnyFrame( |
| 80 const content::LoadCommittedDetails& details, |
| 81 const content::FrameNavigateParams& params) OVERRIDE; |
| 82 |
58 private: | 83 private: |
59 // The navigation controller of the tab we are associated with. | 84 // The navigation controller of the tab we are associated with. |
60 content::NavigationController* navigation_controller_; | 85 content::NavigationController* navigation_controller_; |
61 | 86 |
| 87 TranslateManager* translate_manager_; |
| 88 |
62 Observer* observer_; | 89 Observer* observer_; |
63 | 90 |
| 91 // Max number of attempts before checking if a page has been reloaded. |
| 92 int max_reload_check_attempts_; |
| 93 |
| 94 base::WeakPtrFactory<ContentTranslateDriver> weak_pointer_factory_; |
| 95 |
64 DISALLOW_COPY_AND_ASSIGN(ContentTranslateDriver); | 96 DISALLOW_COPY_AND_ASSIGN(ContentTranslateDriver); |
65 }; | 97 }; |
66 | 98 |
67 } // namespace translate | 99 } // namespace translate |
68 | 100 |
69 #endif // COMPONENTS_TRANSLATE_CONTENT_BROWSER_CONTENT_TRANSLATE_DRIVER_H_ | 101 #endif // COMPONENTS_TRANSLATE_CONTENT_BROWSER_CONTENT_TRANSLATE_DRIVER_H_ |
OLD | NEW |