| 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 #include "components/translate/content/browser/content_translate_driver.h" | 5 #include "components/translate/content/browser/content_translate_driver.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "components/translate/content/common/translate_messages.h" | 8 #include "components/translate/content/common/translate_messages.h" |
| 9 #include "content/public/browser/browser_context.h" | 9 #include "content/public/browser/browser_context.h" |
| 10 #include "content/public/browser/navigation_controller.h" | 10 #include "content/public/browser/navigation_controller.h" |
| 11 #include "content/public/browser/navigation_details.h" | |
| 12 #include "content/public/browser/navigation_entry.h" | 11 #include "content/public/browser/navigation_entry.h" |
| 13 #include "content/public/browser/render_view_host.h" | 12 #include "content/public/browser/render_view_host.h" |
| 14 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 15 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 16 | 15 |
| 17 ContentTranslateDriver::ContentTranslateDriver( | 16 ContentTranslateDriver::ContentTranslateDriver( |
| 18 content::NavigationController* nav_controller) | 17 content::NavigationController* nav_controller) |
| 19 : navigation_controller_(nav_controller), | 18 : navigation_controller_(nav_controller), |
| 20 language_state_(this), | |
| 21 observer_(NULL) { | 19 observer_(NULL) { |
| 22 DCHECK(navigation_controller_); | 20 DCHECK(navigation_controller_); |
| 23 } | 21 } |
| 24 | 22 |
| 25 ContentTranslateDriver::~ContentTranslateDriver() {} | 23 ContentTranslateDriver::~ContentTranslateDriver() {} |
| 26 | 24 |
| 27 void ContentTranslateDriver::DidNavigate( | |
| 28 const content::LoadCommittedDetails& details) { | |
| 29 const bool reload = | |
| 30 details.entry->GetTransitionType() == content::PAGE_TRANSITION_RELOAD || | |
| 31 details.type == content::NAVIGATION_TYPE_SAME_PAGE; | |
| 32 language_state_.DidNavigate( | |
| 33 details.is_in_page, details.is_main_frame, reload); | |
| 34 } | |
| 35 | |
| 36 // TranslateDriver methods | 25 // TranslateDriver methods |
| 37 | 26 |
| 38 bool ContentTranslateDriver::IsLinkNavigation() { | 27 bool ContentTranslateDriver::IsLinkNavigation() { |
| 39 return navigation_controller_ && navigation_controller_->GetActiveEntry() && | 28 return navigation_controller_ && navigation_controller_->GetActiveEntry() && |
| 40 navigation_controller_->GetActiveEntry()->GetTransitionType() == | 29 navigation_controller_->GetActiveEntry()->GetTransitionType() == |
| 41 content::PAGE_TRANSITION_LINK; | 30 content::PAGE_TRANSITION_LINK; |
| 42 } | 31 } |
| 43 | 32 |
| 44 void ContentTranslateDriver::OnTranslateEnabledChanged() { | 33 void ContentTranslateDriver::OnTranslateEnabledChanged() { |
| 45 if (observer_) { | 34 if (observer_) { |
| 46 content::WebContents* web_contents = | 35 content::WebContents* web_contents = |
| 47 navigation_controller_->GetWebContents(); | 36 navigation_controller_->GetWebContents(); |
| 48 observer_->OnTranslateEnabledChanged(web_contents); | 37 observer_->OnTranslateEnabledChanged(web_contents); |
| 49 } | 38 } |
| 50 } | 39 } |
| 51 | 40 |
| 52 void ContentTranslateDriver::OnIsPageTranslatedChanged() { | 41 void ContentTranslateDriver::OnIsPageTranslatedChanged() { |
| 53 if (observer_) { | 42 if (observer_) { |
| 54 content::WebContents* web_contents = | 43 content::WebContents* web_contents = |
| 55 navigation_controller_->GetWebContents(); | 44 navigation_controller_->GetWebContents(); |
| 56 observer_->OnIsPageTranslatedChanged(web_contents); | 45 observer_->OnIsPageTranslatedChanged(web_contents); |
| 57 } | 46 } |
| 58 } | 47 } |
| 59 | 48 |
| 60 LanguageState& ContentTranslateDriver::GetLanguageState() { | |
| 61 return language_state_; | |
| 62 } | |
| 63 | |
| 64 void ContentTranslateDriver::TranslatePage(const std::string& translate_script, | 49 void ContentTranslateDriver::TranslatePage(const std::string& translate_script, |
| 65 const std::string& source_lang, | 50 const std::string& source_lang, |
| 66 const std::string& target_lang) { | 51 const std::string& target_lang) { |
| 67 content::NavigationEntry* entry = navigation_controller_->GetActiveEntry(); | 52 content::NavigationEntry* entry = navigation_controller_->GetActiveEntry(); |
| 68 if (!entry) { | 53 if (!entry) { |
| 69 NOTREACHED(); | 54 NOTREACHED(); |
| 70 return; | 55 return; |
| 71 } | 56 } |
| 72 | 57 |
| 73 content::WebContents* web_contents = navigation_controller_->GetWebContents(); | 58 content::WebContents* web_contents = navigation_controller_->GetWebContents(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 101 |
| 117 bool ContentTranslateDriver::HasCurrentPage() { | 102 bool ContentTranslateDriver::HasCurrentPage() { |
| 118 return (navigation_controller_->GetActiveEntry() != NULL); | 103 return (navigation_controller_->GetActiveEntry() != NULL); |
| 119 } | 104 } |
| 120 | 105 |
| 121 int ContentTranslateDriver::GetCurrentPageID() { | 106 int ContentTranslateDriver::GetCurrentPageID() { |
| 122 DCHECK(HasCurrentPage()); | 107 DCHECK(HasCurrentPage()); |
| 123 content::NavigationEntry* entry = navigation_controller_->GetActiveEntry(); | 108 content::NavigationEntry* entry = navigation_controller_->GetActiveEntry(); |
| 124 return entry->GetPageID(); | 109 return entry->GetPageID(); |
| 125 } | 110 } |
| OLD | NEW |