File: input_components_handler.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (168 lines) | stat: -rw-r--r-- 5,793 bytes parent folder | download | duplicates (3)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/common/manifest_handlers/input_components_handler.h"

#include <stddef.h>

#include <memory>
#include <utility>

#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/options_page_info.h"

namespace extensions {

namespace keys = manifest_keys;
namespace errors = manifest_errors;

InputComponentInfo::InputComponentInfo() = default;

InputComponentInfo::InputComponentInfo(const InputComponentInfo& other) =
    default;

InputComponentInfo::~InputComponentInfo() = default;

InputComponents::InputComponents() = default;
InputComponents::~InputComponents() = default;

// static
const std::vector<InputComponentInfo>* InputComponents::GetInputComponents(
    const Extension* extension) {
  InputComponents* info = static_cast<InputComponents*>(
      extension->GetManifestData(keys::kInputComponents));
  return info ? &info->input_components : nullptr;
}

InputComponentsHandler::InputComponentsHandler() = default;

InputComponentsHandler::~InputComponentsHandler() = default;

bool InputComponentsHandler::Parse(Extension* extension,
                                   std::u16string* error) {
  const base::Value* list_value;
  if (!extension->manifest()->GetList(keys::kInputComponents, &list_value)) {
    *error = errors::kInvalidInputComponents16;
    return false;
  }

  auto info = std::make_unique<InputComponents>();
  for (size_t i = 0; i < list_value->GetList().size(); ++i) {
    const base::Value::Dict* module_value =
        list_value->GetList()[i].GetIfDict();
    if (!module_value) {
      *error = errors::kInvalidInputComponents16;
      return false;
    }

    // Get input_components[i].name.
    const std::string* name_str = module_value->FindString(keys::kName);
    if (!name_str) {
      *error = ErrorUtils::FormatErrorMessageUTF16(
          errors::kInvalidInputComponentName, base::NumberToString(i));
      return false;
    }

    // Get input_components[i].id.
    std::string id_str;
    const std::string* maybe_id_str = module_value->FindString(keys::kId);
    if (maybe_id_str) {
      id_str = *maybe_id_str;
    }

    // Get input_components[i].language.
    // Both string and list of string are allowed to be compatibile with old
    // input_ime manifest specification.
    std::set<std::string> languages;
    const base::Value* language_value = module_value->Find(keys::kLanguage);
    if (language_value) {
      if (language_value->is_string()) {
        languages.insert(language_value->GetString());
      } else if (language_value->is_list()) {
        for (const auto& language : language_value->GetList()) {
          if (language.is_string()) {
            languages.insert(language.GetString());
          }
        }
      }
    }

    std::set<std::string> layouts;
    const base::Value::List* layouts_value =
        module_value->FindList(keys::kLayouts);
    if (layouts_value) {
      for (size_t j = 0; j < layouts_value->size(); ++j) {
        const base::Value& layout = (*layouts_value)[j];
        if (!layout.is_string()) {
          *error = ErrorUtils::FormatErrorMessageUTF16(
              errors::kInvalidInputComponentLayoutName, base::NumberToString(i),
              base::NumberToString(j));
          return false;
        }
        layouts.insert(layout.GetString());
      }
    }

    // Get input_components[i].input_view_url.
    // Note: 'input_view' is optional in manifest.
    GURL input_view_url;
    const std::string* input_view_str =
        module_value->FindString(keys::kInputView);
    if (input_view_str) {
      input_view_url = extension->ResolveExtensionURL(*input_view_str);
      if (!input_view_url.is_valid()) {
        *error = ErrorUtils::FormatErrorMessageUTF16(errors::kInvalidInputView,
                                                     base::NumberToString(i));
        return false;
      }
    }

    // Get input_components[i].options_page_url.
    // Note: 'options_page' is optional in manifest.
    GURL options_page_url;
    const std::string* options_page_str =
        module_value->FindString(keys::kImeOptionsPage);
    if (options_page_str) {
      options_page_url = extension->ResolveExtensionURL(*options_page_str);
      if (!options_page_url.is_valid()) {
        *error = ErrorUtils::FormatErrorMessageUTF16(
            errors::kInvalidOptionsPage, base::NumberToString(i));
        return false;
      }
    } else {
      // Fall back to extension's options page for backward compatibility.
      options_page_url = extensions::OptionsPageInfo::GetOptionsPage(extension);
    }

    InputComponentInfo component;
    component.name = *name_str;
    component.id = std::move(id_str);
    component.languages = std::move(languages);
    component.layouts = std::move(layouts);
    component.options_page_url = std::move(options_page_url);
    component.input_view_url = std::move(input_view_url);
    info->input_components.push_back(std::move(component));
  }
  extension->SetManifestData(keys::kInputComponents, std::move(info));
  return true;
}

const std::vector<std::string>
InputComponentsHandler::PrerequisiteKeys() const {
  return SingleKey(keys::kOptionsPage);
}

base::span<const char* const> InputComponentsHandler::Keys() const {
  static constexpr const char* kKeys[] = {keys::kInputComponents};
  return kKeys;
}

}  // namespace extensions