Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / components / translate / ios / browser / ios_translate_driver.h
blob194f3e632d83fc3af0c6de6eebbb718f5c9090a0
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_
8 #include <string>
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;
19 namespace web {
20 class NavigationManager;
21 class WebState;
24 namespace translate {
26 class TranslateManager;
28 // Content implementation of TranslateDriver.
29 class IOSTranslateDriver : public TranslateDriver,
30 public TranslateController::Observer,
31 public web::WebStateObserver {
32 public:
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& GetVisibleURL() override;
63 bool HasCurrentPage() override;
64 void OpenUrlInNewTab(const GURL& url) override;
66 private:
67 // Called when the translation was successfull.
68 void TranslationDidSucceed(const std::string& source_lang,
69 const std::string& target_lang,
70 int page_seq_no,
71 const std::string& original_page_language,
72 double translation_time);
73 // Checks if the current running page translation is finished or errored and
74 // notifies the browser accordingly. If the translation has not terminated,
75 // posts a task to check again later.
76 // Similar to TranslateHelper::CheckTranslateStatus on desktop.
77 void CheckTranslateStatus(const std::string& source_language,
78 const std::string& target_language,
79 int page_seq_no);
81 // Returns true if the user has not navigated away and the the page is not
82 // being destroyed.
83 bool IsPageValid(int page_seq_no) const;
85 // Callback for LanguageDetectionController.
86 void OnLanguageDetermined(
87 const LanguageDetectionController::DetectionDetails& details);
89 // TranslateController::Observer methods.
90 void OnTranslateScriptReady(bool success,
91 double load_time,
92 double ready_time) override;
93 void OnTranslateComplete(bool success,
94 const std::string& original_language,
95 double translation_time) override;
97 // The navigation manager of the tab we are associated with.
98 web::NavigationManager* navigation_manager_;
100 base::WeakPtr<TranslateManager> translate_manager_;
101 scoped_ptr<TranslateController> translate_controller_;
102 scoped_ptr<LanguageDetectionController> language_detection_controller_;
103 scoped_ptr<LanguageDetectionController::CallbackList::Subscription>
104 language_detection_callback_subscription_;
106 // An ever-increasing sequence number of the current page, used to match up
107 // translation requests with responses.
108 // This matches the similar field in TranslateHelper in the renderer on other
109 // platforms.
110 int page_seq_no_;
112 // When a translation is in progress, its page sequence number is stored in
113 // |pending_page_seq_no_|.
114 int pending_page_seq_no_;
116 // Parameters of the current translation.
117 std::string source_language_;
118 std::string target_language_;
120 base::WeakPtrFactory<IOSTranslateDriver> weak_method_factory_;
122 DISALLOW_COPY_AND_ASSIGN(IOSTranslateDriver);
125 } // namespace translate
127 #endif // COMPONENTS_TRANSLATE_IOS_BROWSER_IOS_TRANSLATE_DRIVER_H_