1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_TRANSLATE_TRANSLATE_INTERNALS_TRANSLATE_INTERNALS_HANDLER_H_
#define COMPONENTS_TRANSLATE_TRANSLATE_INTERNALS_TRANSLATE_INTERNALS_HANDLER_H_
#include <string_view>
#include "base/callback_list.h"
#include "base/functional/callback.h"
#include "components/translate/core/browser/translate_client.h"
#include "components/translate/core/browser/translate_language_list.h"
#include "components/translate/core/browser/translate_manager.h"
#include "components/variations/service/variations_service.h"
namespace base {
class Value;
class ValueView;
} // namespace base
namespace translate {
struct LanguageDetectionDetails;
struct TranslateErrorDetails;
struct TranslateEventDetails;
struct TranslateInitDetails;
// The handler class for chrome://translate-internals page operations.
class TranslateInternalsHandler {
public:
TranslateInternalsHandler();
TranslateInternalsHandler(const TranslateInternalsHandler&) = delete;
TranslateInternalsHandler& operator=(const TranslateInternalsHandler&) =
delete;
~TranslateInternalsHandler();
// Returns a dictionary of languages where each key is a language
// code and each value is a language name in the locale.
static base::Value::Dict GetLanguages();
virtual TranslateClient* GetTranslateClient() = 0;
virtual variations::VariationsService* GetVariationsService() = 0;
// Registers to handle |message| from JavaScript with |callback|.
using MessageCallback =
base::RepeatingCallback<void(const base::Value::List&)>;
virtual void RegisterMessageCallback(std::string_view message,
MessageCallback callback) = 0;
// Calls a Javascript function with the given name and arguments.
virtual void CallJavascriptFunction(
std::string_view function_name,
base::span<const base::ValueView> args) = 0;
protected:
// Subclasses should call this in order to handle messages from JavaScript.
void RegisterMessageCallbacks();
// Subclasses should call this when language detection details are available.
void AddLanguageDetectionDetails(const LanguageDetectionDetails& details);
private:
// Callback for translate errors.
void OnTranslateError(const TranslateErrorDetails& details);
// Callback for translate initialisations.
virtual void OnTranslateInit(const translate::TranslateInitDetails& details);
// Callback for translate events.
virtual void OnTranslateEvent(const TranslateEventDetails& details);
// Handles the Javascript message 'removePrefItem'. This message is sent
// when UI requests to remove an item in the preference.
void OnRemovePrefItem(const base::Value::List& args);
// Handles the JavaScript message 'setRecentTargetLanguage'. This message is
// sent when the UI requests to change the 'translate_recent_target'
// preference.
void OnSetRecentTargetLanguage(const base::Value::List& args);
// Handles the Javascript message 'overrideCountry'. This message is sent
// when UI requests to override the stored country.
void OnOverrideCountry(const base::Value::List& country);
// Handles the Javascript message 'requestInfo'. This message is sent
// when UI needs to show information concerned with the translation.
// For now, this returns only prefs to Javascript.
// |args| is not used.
void OnRequestInfo(const base::Value::List& args);
// Sends a message to Javascript.
void SendMessageToJs(std::string_view message,
const base::Value::Dict& value);
// Sends the current preference to Javascript.
void SendPrefsToJs();
// Sends the languages currently supported by the server to JavaScript.
void SendSupportedLanguagesToJs();
// Sends the stored permanent country to Javascript.
// |was_updated| tells Javascript if the country has been updated or not.
void SendCountryToJs(bool was_updated);
// Subscription for translate events coming from the translate language list.
base::CallbackListSubscription event_subscription_;
// Subscription for translate errors coming from the translate manager.
base::CallbackListSubscription error_subscription_;
// Subscription for translate initialization event.
base::CallbackListSubscription init_subscription_;
};
} // namespace translate
#endif // COMPONENTS_TRANSLATE_TRANSLATE_INTERNALS_TRANSLATE_INTERNALS_HANDLER_H_
|