1 // Copyright 2019 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/ui/webui/translate_internals/chrome_translate_internals_handler.h"
6 
7 #include <map>
8 #include <utility>
9 #include <vector>
10 
11 #include "base/bind.h"
12 #include "base/callback_helpers.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/translate/chrome_translate_client.h"
18 #include "chrome/browser/translate/translate_service.h"
19 #include "chrome/common/pref_names.h"
20 #include "components/language/core/browser/pref_names.h"
21 #include "components/prefs/pref_service.h"
22 #include "components/translate/core/browser/translate_download_manager.h"
23 #include "components/translate/core/browser/translate_error_details.h"
24 #include "components/translate/core/browser/translate_event_details.h"
25 #include "components/translate/core/browser/translate_pref_names.h"
26 #include "components/translate/core/browser/translate_prefs.h"
27 #include "components/translate/core/common/language_detection_details.h"
28 #include "components/variations/service/variations_service.h"
29 #include "content/public/browser/notification_details.h"
30 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/notification_source.h"
32 #include "content/public/browser/notification_types.h"
33 #include "content/public/browser/web_contents.h"
34 #include "content/public/browser/web_ui.h"
35 
ChromeTranslateInternalsHandler()36 ChromeTranslateInternalsHandler::ChromeTranslateInternalsHandler() {
37   notification_registrar_.Add(this,
38                               chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
39                               content::NotificationService::AllSources());
40 }
41 
~ChromeTranslateInternalsHandler()42 ChromeTranslateInternalsHandler::~ChromeTranslateInternalsHandler() {}
43 
44 translate::TranslateClient*
GetTranslateClient()45 ChromeTranslateInternalsHandler::GetTranslateClient() {
46   return ChromeTranslateClient::FromWebContents(web_ui()->GetWebContents());
47 }
48 
49 variations::VariationsService*
GetVariationsService()50 ChromeTranslateInternalsHandler::GetVariationsService() {
51   return g_browser_process->variations_service();
52 }
53 
RegisterMessageCallback(const std::string & message,const MessageCallback & callback)54 void ChromeTranslateInternalsHandler::RegisterMessageCallback(
55     const std::string& message,
56     const MessageCallback& callback) {
57   web_ui()->RegisterMessageCallback(message, callback);
58 }
59 
CallJavascriptFunction(const std::string & function_name,const std::vector<const base::Value * > & args)60 void ChromeTranslateInternalsHandler::CallJavascriptFunction(
61     const std::string& function_name,
62     const std::vector<const base::Value*>& args) {
63   web_ui()->CallJavascriptFunctionUnsafe(function_name, args);
64 }
65 
RegisterMessages()66 void ChromeTranslateInternalsHandler::RegisterMessages() {
67   RegisterMessageCallbacks();
68 }
69 
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)70 void ChromeTranslateInternalsHandler::Observe(
71     int type,
72     const content::NotificationSource& source,
73     const content::NotificationDetails& details) {
74   DCHECK_EQ(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, type);
75 
76   content::WebContents* web_contents =
77       content::Source<content::WebContents>(source).ptr();
78   const translate::LanguageDetectionDetails* language_detection_details =
79       content::Details<const translate::LanguageDetectionDetails>(details)
80           .ptr();
81   if (web_contents->GetBrowserContext()->IsOffTheRecord() ||
82       !GetTranslateClient()->IsTranslatableURL(
83           language_detection_details->url)) {
84     return;
85   }
86 
87   AddLanguageDetectionDetails(*language_detection_details);
88 }
89