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.
5 #include "chrome/browser/translate/translate_tab_helper.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/common/render_messages.h"
9 #include "components/translate/core/browser/page_translated_details.h"
10 #include "components/translate/core/common/language_detection_details.h"
11 #include "content/public/browser/notification_service.h"
12 #include "content/public/browser/web_contents.h"
14 using content::WebContents
;
16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper
);
18 TranslateTabHelper::TranslateTabHelper(WebContents
* web_contents
)
19 : content::WebContentsObserver(web_contents
),
20 language_state_(&web_contents
->GetController()) {
23 TranslateTabHelper::~TranslateTabHelper() {
26 bool TranslateTabHelper::OnMessageReceived(const IPC::Message
& message
) {
28 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper
, message
)
29 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined
,
31 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated
, OnPageTranslated
)
32 IPC_MESSAGE_UNHANDLED(handled
= false)
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
);
45 void TranslateTabHelper::OnLanguageDetermined(
46 const LanguageDetectionDetails
& details
,
47 bool page_needs_translation
) {
48 language_state_
.LanguageDetermined(details
.adopted_language
,
49 page_needs_translation
);
51 content::NotificationService::current()->Notify(
52 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED
,
53 content::Source
<WebContents
>(web_contents()),
54 content::Details
<const LanguageDetectionDetails
>(&details
));
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
));