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_IOS_BROWSER_IOS_TRANSLATE_DRIVER_H_
6 #define COMPONENTS_TRANSLATE_IOS_BROWSER_IOS_TRANSLATE_DRIVER_H_
10 #include "base/basictypes.h"
11 #include "base/memory/weak_ptr.h"
12 #include "components/translate/core/browser/translate_driver.h"
13 #include "components/translate/ios/browser/language_detection_controller.h"
14 #include "components/translate/ios/browser/translate_controller.h"
15 #include "ios/web/public/web_state/web_state_observer.h"
17 @
class CRWJSInjectionReceiver
;
20 class NavigationManager
;
26 class TranslateManager
;
28 // Content implementation of TranslateDriver.
29 class IOSTranslateDriver
: public TranslateDriver
,
30 public TranslateController::Observer
,
31 public web::WebStateObserver
{
33 IOSTranslateDriver(web::WebState
* web_state
,
34 web::NavigationManager
* navigation_manager
,
35 TranslateManager
* translate_manager
);
36 ~IOSTranslateDriver() override
;
38 LanguageDetectionController
* language_detection_controller() {
39 return language_detection_controller_
.get();
42 TranslateController
* translate_controller() {
43 return translate_controller_
.get();
46 // web::WebStateObserver methods.
47 void NavigationItemCommitted(
48 const web::LoadCommittedDetails
& load_details
) override
;
50 // TranslateDriver methods.
51 void OnIsPageTranslatedChanged() override
;
52 void OnTranslateEnabledChanged() override
;
53 bool IsLinkNavigation() override
;
54 void TranslatePage(int page_seq_no
,
55 const std::string
& translate_script
,
56 const std::string
& source_lang
,
57 const std::string
& target_lang
) override
;
58 void RevertTranslation(int page_seq_no
) override
;
59 bool IsOffTheRecord() override
;
60 const std::string
& GetContentsMimeType() override
;
61 const GURL
& GetLastCommittedURL() override
;
62 const GURL
& GetActiveURL() override
;
63 const GURL
& GetVisibleURL() override
;
64 bool HasCurrentPage() override
;
65 void OpenUrlInNewTab(const GURL
& url
) override
;
68 // Called when the translation was successfull.
69 void TranslationDidSucceed(const std::string
& source_lang
,
70 const std::string
& target_lang
,
72 const std::string
& original_page_language
,
73 double translation_time
);
74 // Checks if the current running page translation is finished or errored and
75 // notifies the browser accordingly. If the translation has not terminated,
76 // posts a task to check again later.
77 // Similar to TranslateHelper::CheckTranslateStatus on desktop.
78 void CheckTranslateStatus(const std::string
& source_language
,
79 const std::string
& target_language
,
82 // Returns true if the user has not navigated away and the the page is not
84 bool IsPageValid(int page_seq_no
) const;
86 // Callback for LanguageDetectionController.
87 void OnLanguageDetermined(
88 const LanguageDetectionController::DetectionDetails
& details
);
90 // TranslateController::Observer methods.
91 void OnTranslateScriptReady(bool success
,
93 double ready_time
) override
;
94 void OnTranslateComplete(bool success
,
95 const std::string
& original_language
,
96 double translation_time
) override
;
98 // The navigation manager of the tab we are associated with.
99 web::NavigationManager
* navigation_manager_
;
101 base::WeakPtr
<TranslateManager
> translate_manager_
;
102 scoped_ptr
<TranslateController
> translate_controller_
;
103 scoped_ptr
<LanguageDetectionController
> language_detection_controller_
;
104 scoped_ptr
<LanguageDetectionController::CallbackList::Subscription
>
105 language_detection_callback_subscription_
;
107 // An ever-increasing sequence number of the current page, used to match up
108 // translation requests with responses.
109 // This matches the similar field in TranslateHelper in the renderer on other
113 // When a translation is in progress, its page sequence number is stored in
114 // |pending_page_seq_no_|.
115 int pending_page_seq_no_
;
117 // Parameters of the current translation.
118 std::string source_language_
;
119 std::string target_language_
;
121 base::WeakPtrFactory
<IOSTranslateDriver
> weak_method_factory_
;
123 DISALLOW_COPY_AND_ASSIGN(IOSTranslateDriver
);
126 } // namespace translate
128 #endif // COMPONENTS_TRANSLATE_IOS_BROWSER_IOS_TRANSLATE_DRIVER_H_