File: accessibility_private_hooks_delegate.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (91 lines) | stat: -rw-r--r-- 3,446 bytes parent folder | download | duplicates (11)
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
// 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.

#include "chrome/renderer/extensions/api/accessibility_private_hooks_delegate.h"

#include "base/i18n/unicodestring.h"
#include "base/strings/utf_string_conversions.h"
#include "extensions/common/extension.h"
#include "extensions/renderer/bindings/api_signature.h"
#include "extensions/renderer/get_script_context.h"
#include "extensions/renderer/script_context.h"
#include "gin/converter.h"
#include "ui/base/l10n/l10n_util.h"

namespace extensions {

namespace {
constexpr char kGetDisplayNameForLocale[] =
    "accessibilityPrivate.getDisplayNameForLocale";
constexpr char kUndeterminedLocale[] = "und";
}  // namespace

using RequestResult = APIBindingHooks::RequestResult;

AccessibilityPrivateHooksDelegate::AccessibilityPrivateHooksDelegate() =
    default;
AccessibilityPrivateHooksDelegate::~AccessibilityPrivateHooksDelegate() =
    default;

RequestResult AccessibilityPrivateHooksDelegate::HandleRequest(
    const std::string& method_name,
    const APISignature* signature,
    v8::Local<v8::Context> context,
    v8::LocalVector<v8::Value>* arguments,
    const APITypeReferenceMap& refs) {
  // Error checks.
  // Ensure we would like to call the GetDisplayNameForLocale function.
  if (method_name != kGetDisplayNameForLocale)
    return RequestResult(RequestResult::NOT_HANDLED);
  // Ensure arguments are successfully parsed and converted.
  APISignature::V8ParseResult parse_result =
      signature->ParseArgumentsToV8(context, *arguments, refs);
  if (!parse_result.succeeded()) {
    RequestResult result(RequestResult::INVALID_INVOCATION);
    result.error = std::move(*parse_result.error);
    return result;
  }
  return HandleGetDisplayNameForLocale(
      GetScriptContextFromV8ContextChecked(context), *parse_result.arguments);
}

RequestResult AccessibilityPrivateHooksDelegate::HandleGetDisplayNameForLocale(
    ScriptContext* script_context,
    const v8::LocalVector<v8::Value>& parsed_arguments) {
  DCHECK(script_context->extension());
  DCHECK_EQ(2u, parsed_arguments.size());
  DCHECK(parsed_arguments[0]->IsString());
  DCHECK(parsed_arguments[1]->IsString());

  const std::string locale =
      gin::V8ToString(script_context->isolate(), parsed_arguments[0]);
  const std::string display_locale =
      gin::V8ToString(script_context->isolate(), parsed_arguments[1]);

  bool found_valid_result = false;
  std::string locale_result;
  if (l10n_util::IsValidLocaleSyntax(locale) &&
      l10n_util::IsValidLocaleSyntax(display_locale)) {
    locale_result = base::UTF16ToUTF8(l10n_util::GetDisplayNameForLocale(
        locale, display_locale, true /* is_ui */));
    // Check for valid locales before getting the display name.
    // The ICU Locale class returns "und" for undetermined locales, and
    // returns the locale string directly if it has no translation.
    // Treat these cases as invalid results.
    found_valid_result =
        locale_result != kUndeterminedLocale && locale_result != locale;
  }

  // We return an empty string to communicate that we could not determine
  // the display locale.
  if (!found_valid_result)
    locale_result = std::string();

  RequestResult result(RequestResult::HANDLED);
  result.return_value =
      gin::StringToV8(script_context->isolate(), locale_result);
  return result;
}

}  // namespace extensions