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 COMPONENTS_TRANSLATE_CONTENT_BROWSER_CONTENT_TRANSLATE_DRIVER_H_ 6 #define COMPONENTS_TRANSLATE_CONTENT_BROWSER_CONTENT_TRANSLATE_DRIVER_H_ 7 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" 14 15 namespace content { 16 class NavigationController; 17 class WebContents; 18 } 19 20 namespace translate { 21 22 struct LanguageDetectionDetails; 23 class TranslateManager; 24 25 // Content implementation of TranslateDriver. 26 class ContentTranslateDriver : public TranslateDriver, 27 public content::WebContentsObserver { 28 public: 29 30 // The observer for the ContentTranslateDriver. 31 class Observer { 32 public: 33 // Handles when the value of IsPageTranslated is changed. OnIsPageTranslatedChanged(content::WebContents * source)34 virtual void OnIsPageTranslatedChanged(content::WebContents* source) {}; 35 36 // Handles when the value of translate_enabled is changed. OnTranslateEnabledChanged(content::WebContents * source)37 virtual void OnTranslateEnabledChanged(content::WebContents* source) {}; 38 39 // Called when the page language has been determined. OnLanguageDetermined(const translate::LanguageDetectionDetails & details)40 virtual void OnLanguageDetermined( 41 const translate::LanguageDetectionDetails& details) {}; 42 43 // Called when the page has been translated. OnPageTranslated(const std::string & original_lang,const std::string & translated_lang,translate::TranslateErrors::Type error_type)44 virtual void OnPageTranslated( 45 const std::string& original_lang, 46 const std::string& translated_lang, 47 translate::TranslateErrors::Type error_type) {}; 48 49 protected: ~Observer()50 virtual ~Observer() {} 51 }; 52 53 ContentTranslateDriver(content::NavigationController* nav_controller); 54 virtual ~ContentTranslateDriver(); 55 56 // Adds or Removes observers. 57 void AddObserver(Observer* observer); 58 void RemoveObserver(Observer* observer); 59 60 // Number of attempts before waiting for a page to be fully reloaded. set_translate_max_reload_attempts(int attempts)61 void set_translate_max_reload_attempts(int attempts) { 62 max_reload_check_attempts_ = attempts; 63 } 64 65 // Sets the TranslateManager associated with this driver. set_translate_manager(TranslateManager * manager)66 void set_translate_manager(TranslateManager* manager) { 67 translate_manager_ = manager; 68 } 69 70 // Initiates translation once the page is finished loading. 71 void InitiateTranslation(const std::string& page_lang, int attempt); 72 73 // TranslateDriver methods. 74 virtual void OnIsPageTranslatedChanged() OVERRIDE; 75 virtual void OnTranslateEnabledChanged() OVERRIDE; 76 virtual bool IsLinkNavigation() OVERRIDE; 77 virtual 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 virtual void RevertTranslation(int page_seq_no) OVERRIDE; 82 virtual bool IsOffTheRecord() OVERRIDE; 83 virtual const std::string& GetContentsMimeType() OVERRIDE; 84 virtual const GURL& GetLastCommittedURL() OVERRIDE; 85 virtual const GURL& GetActiveURL() OVERRIDE; 86 virtual const GURL& GetVisibleURL() OVERRIDE; 87 virtual bool HasCurrentPage() OVERRIDE; 88 virtual void OpenUrlInNewTab(const GURL& url) OVERRIDE; 89 90 // content::WebContentsObserver implementation. 91 virtual void NavigationEntryCommitted( 92 const content::LoadCommittedDetails& load_details) OVERRIDE; 93 virtual void DidNavigateAnyFrame( 94 const content::LoadCommittedDetails& details, 95 const content::FrameNavigateParams& params) OVERRIDE; 96 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 97 98 // IPC handlers. 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); 105 106 private: 107 // The navigation controller of the tab we are associated with. 108 content::NavigationController* navigation_controller_; 109 110 TranslateManager* translate_manager_; 111 112 ObserverList<Observer, true> observer_list_; 113 114 // Max number of attempts before checking if a page has been reloaded. 115 int max_reload_check_attempts_; 116 117 base::WeakPtrFactory<ContentTranslateDriver> weak_pointer_factory_; 118 119 DISALLOW_COPY_AND_ASSIGN(ContentTranslateDriver); 120 }; 121 122 } // namespace translate 123 124 #endif // COMPONENTS_TRANSLATE_CONTENT_BROWSER_CONTENT_TRANSLATE_DRIVER_H_ 125