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/chrome_notification_types.h"
8 #include "chrome/browser/translate/page_translated_details.h"
9 #include "chrome/common/render_messages.h"
10 #include "chrome/common/translate/language_detection_details.h"
11 #include "content/public/browser/notification_service.h"
12 #include "content/public/browser/web_contents.h"
13
14 using content::WebContents;
15
16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper);
17
TranslateTabHelper(WebContents * web_contents)18 TranslateTabHelper::TranslateTabHelper(WebContents* web_contents)
19 : content::WebContentsObserver(web_contents),
20 language_state_(&web_contents->GetController()) {
21 }
22
~TranslateTabHelper()23 TranslateTabHelper::~TranslateTabHelper() {
24 }
25
OnMessageReceived(const IPC::Message & message)26 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) {
27 bool handled = true;
28 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message)
29 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined,
30 OnLanguageDetermined)
31 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated)
32 IPC_MESSAGE_UNHANDLED(handled = false)
33 IPC_END_MESSAGE_MAP()
34
35 return handled;
36 }
37
DidNavigateAnyFrame(const content::LoadCommittedDetails & details,const content::FrameNavigateParams & params)38 void TranslateTabHelper::DidNavigateAnyFrame(
39 const content::LoadCommittedDetails& details,
40 const content::FrameNavigateParams& params) {
41 // Let the LanguageState clear its state.
42 language_state_.DidNavigate(details);
43 }
44
OnLanguageDetermined(const LanguageDetectionDetails & details,bool page_needs_translation)45 void TranslateTabHelper::OnLanguageDetermined(
46 const LanguageDetectionDetails& details,
47 bool page_needs_translation) {
48 language_state_.LanguageDetermined(details.adopted_language,
49 page_needs_translation);
50
51 content::NotificationService::current()->Notify(
52 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
53 content::Source<WebContents>(web_contents()),
54 content::Details<const LanguageDetectionDetails>(&details));
55 }
56
OnPageTranslated(int32 page_id,const std::string & original_lang,const std::string & translated_lang,TranslateErrors::Type error_type)57 void TranslateTabHelper::OnPageTranslated(int32 page_id,
58 const std::string& original_lang,
59 const std::string& translated_lang,
60 TranslateErrors::Type error_type) {
61 language_state_.SetCurrentLanguage(translated_lang);
62 language_state_.set_translation_pending(false);
63 PageTranslatedDetails details;
64 details.source_language = original_lang;
65 details.target_language = translated_lang;
66 details.error_type = error_type;
67 content::NotificationService::current()->Notify(
68 chrome::NOTIFICATION_PAGE_TRANSLATED,
69 content::Source<WebContents>(web_contents()),
70 content::Details<PageTranslatedDetails>(&details));
71 }
72