1 // Copyright (c) 2011 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 #include "chrome/browser/translate/translate_tab_helper.h"
6
7 #include "chrome/browser/translate/page_translated_details.h"
8 #include "chrome/common/render_messages.h"
9 #include "content/browser/tab_contents/tab_contents.h"
10 #include "content/common/notification_service.h"
11
TranslateTabHelper(TabContents * tab_contents)12 TranslateTabHelper::TranslateTabHelper(TabContents* tab_contents)
13 : TabContentsObserver(tab_contents),
14 language_state_(&tab_contents->controller()) {
15 }
16
~TranslateTabHelper()17 TranslateTabHelper::~TranslateTabHelper() {
18 }
19
OnMessageReceived(const IPC::Message & message)20 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) {
21 bool handled = true;
22 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message)
23 IPC_MESSAGE_HANDLER(ViewHostMsg_TranslateLanguageDetermined,
24 OnLanguageDetermined)
25 IPC_MESSAGE_HANDLER(ViewHostMsg_PageTranslated, OnPageTranslated)
26 IPC_MESSAGE_UNHANDLED(handled = false)
27 IPC_END_MESSAGE_MAP()
28
29 return handled;
30 }
31
DidNavigateAnyFramePostCommit(const NavigationController::LoadCommittedDetails & details,const ViewHostMsg_FrameNavigate_Params & params)32 void TranslateTabHelper::DidNavigateAnyFramePostCommit(
33 const NavigationController::LoadCommittedDetails& details,
34 const ViewHostMsg_FrameNavigate_Params& params) {
35 // Let the LanguageState clear its state.
36 language_state_.DidNavigate(details);
37 }
38
OnLanguageDetermined(const std::string & language,bool page_translatable)39 void TranslateTabHelper::OnLanguageDetermined(const std::string& language,
40 bool page_translatable) {
41 language_state_.LanguageDetermined(language, page_translatable);
42
43 std::string lang = language;
44 NotificationService::current()->Notify(
45 NotificationType::TAB_LANGUAGE_DETERMINED,
46 Source<TabContents>(tab_contents()),
47 Details<std::string>(&lang));
48 }
49
OnPageTranslated(int32 page_id,const std::string & original_lang,const std::string & translated_lang,TranslateErrors::Type error_type)50 void TranslateTabHelper::OnPageTranslated(int32 page_id,
51 const std::string& original_lang,
52 const std::string& translated_lang,
53 TranslateErrors::Type error_type) {
54 language_state_.set_current_language(translated_lang);
55 language_state_.set_translation_pending(false);
56 PageTranslatedDetails details(original_lang, translated_lang, error_type);
57 NotificationService::current()->Notify(
58 NotificationType::PAGE_TRANSLATED,
59 Source<TabContents>(tab_contents()),
60 Details<PageTranslatedDetails>(&details));
61 }
62