File: component_extension_ime_manager_delegate_impl.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 (490 lines) | stat: -rw-r--r-- 17,201 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Copyright 2020 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/browser/ash/input_method/component_extension_ime_manager_delegate_impl.h"

#include <stddef.h>

#include <algorithm>
#include <optional>
#include <string_view>

#include "ash/constants/ash_features.h"
#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "base/json/json_string_value_serializer.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/to_string.h"
#include "base/system/sys_info.h"
#include "base/task/thread_pool.h"
#include "base/trace_event/trace_event.h"
#include "build/branding_buildflags.h"
#include "chrome/browser/extensions/component_loader.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/grit/browser_resources.h"
#include "chromeos/ash/components/settings/cros_settings.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "chromeos/constants/chromeos_features.h"
#include "chromeos/ime/input_methods.h"
#include "extensions/browser/extension_pref_value_map.h"
#include "extensions/browser/extension_pref_value_map_factory.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h"
#include "net/base/url_util.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/base/ime/ash/extension_ime_util.h"
#include "ui/base/resource/resource_bundle.h"

namespace ash {
namespace input_method {

namespace {

struct AllowlistedComponentExtensionIME {
  const char* id;
  int manifest_resource_id;
} allowlisted_component_extensions[] = {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
    {
        // Official Google ChromeOS 1P Input.
        extension_ime_util::kXkbExtensionId,
        IDR_GOOGLE_XKB_MANIFEST,
    },
#else
    {
        // Open-sourced ChromiumOS xkb extension.
        extension_ime_util::kXkbExtensionId,
        IDR_XKB_MANIFEST,
    },
    {
        // Open-sourced ChromiumOS Keyboards extension.
        extension_ime_util::kM17nExtensionId,
        IDR_M17N_MANIFEST,
    },
    {
        // Open-sourced Pinyin Chinese Input Method.
        extension_ime_util::kChinesePinyinExtensionId,
        IDR_PINYIN_MANIFEST,
    },
    {
        // Open-sourced Zhuyin Chinese Input Method.
        extension_ime_util::kChineseZhuyinExtensionId,
        IDR_ZHUYIN_MANIFEST,
    },
    {
        // Open-sourced Cangjie Chinese Input Method.
        extension_ime_util::kChineseCangjieExtensionId,
        IDR_CANGJIE_MANIFEST,
    },
    {
        // Open-sourced Japanese Mozc Input.
        extension_ime_util::kMozcExtensionId,
        IDR_MOZC_MANIFEST,
    },
    {
        // Open-sourced Hangul Input.
        extension_ime_util::kHangulExtensionId,
        IDR_HANGUL_MANIFEST,
    },
#endif
    {
        // Braille hardware keyboard IME that works together with ChromeVox.
        extension_ime_util::kBrailleImeExtensionId,
        IDR_BRAILLE_MANIFEST,
    },
};

const char kImePathKeyName[] = "ime_path";

void DoLoadExtension(content::BrowserContext* context,
                     const std::string& extension_id,
                     const std::string& manifest,
                     const base::FilePath& file_path) {
  TRACE_EVENT1("ime", "DoLoadExtension", "ext_id", extension_id);
  extensions::ExtensionRegistry* extension_registry =
      extensions::ExtensionRegistry::Get(context);
  DCHECK(extension_registry);
  if (extension_registry->enabled_extensions().GetByID(extension_id)) {
    VLOG(1) << "the IME extension(id=\"" << extension_id
            << "\") is already enabled";
    return;
  }
  const std::string loaded_extension_id =
      extensions::ComponentLoader::Get(context)->Add(manifest, file_path);
  if (loaded_extension_id.empty()) {
    LOG(ERROR) << "Failed to add an IME extension(id=\"" << extension_id
               << ", path=\"" << file_path.LossyDisplayName()
               << "\") to ComponentLoader";
    return;
  }
  // Register IME extension with ExtensionPrefValueMap.
  ExtensionPrefValueMapFactory::GetForBrowserContext(context)
      ->RegisterExtension(extension_id,
                          base::Time(),  // install_time.
                          true,          // is_enabled.
                          true);         // is_incognito_enabled.
  DCHECK_EQ(loaded_extension_id, extension_id);
  auto* registrar = extensions::ExtensionRegistrar::Get(context);
  DCHECK(registrar);
  if (!registrar->IsExtensionEnabled(loaded_extension_id)) {
    LOG(ERROR) << "An IME extension(id=\"" << loaded_extension_id
               << "\") is not enabled after loading";
  }
}

bool CheckFilePath(const base::FilePath* file_path) {
  return base::PathExists(*file_path);
}

void OnFilePathChecked(content::BrowserContext* context,
                       const std::string* extension_id,
                       const std::string* manifest,
                       const base::FilePath* file_path,
                       bool result) {
  if (result) {
    DoLoadExtension(context, *extension_id, *manifest, *file_path);
  } else {
    LOG_IF(ERROR, base::SysInfo::IsRunningOnChromeOS())
        << "IME extension file path does not exist: " << file_path->value();
  }
}

}  // namespace

ComponentExtensionIMEManagerDelegateImpl::
    ComponentExtensionIMEManagerDelegateImpl() {
  ReadComponentExtensionsInfo(&component_extension_list_);
  login_layout_set_.insert(
      std::begin(chromeos::input_method::kLoginXkbLayoutIds),
      std::end(chromeos::input_method::kLoginXkbLayoutIds));
}

ComponentExtensionIMEManagerDelegateImpl::
    ~ComponentExtensionIMEManagerDelegateImpl() = default;

std::vector<ComponentExtensionIME>
ComponentExtensionIMEManagerDelegateImpl::ListIME() {
  return component_extension_list_;
}

void ComponentExtensionIMEManagerDelegateImpl::Load(
    content::BrowserContext* context,
    const std::string& extension_id,
    const std::string& manifest,
    const base::FilePath& file_path) {
  TRACE_EVENT0("ime", "ComponentExtensionIMEManagerDelegateImpl::Load");
  std::string* manifest_cp = new std::string(manifest);
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  // Skip checking the path of the Chrome OS IME component extension when it's
  // Google Chrome brand, since it is bundled resource on Chrome OS image. This
  // will improve the IME extension load latency a lot.
  // See http://b/192032670 for more details.
  if (extension_id == extension_ime_util::kXkbExtensionId) {
    DoLoadExtension(context, extension_id, *manifest_cp, file_path);
    return;
  }
#endif
  // Check the existence of file path to avoid unnecessary extension loading
  // and InputMethodEngine creation, so that the virtual keyboard web content
  // url won't be override by IME component extensions.
  auto* copied_file_path = new base::FilePath(file_path);
  base::ThreadPool::PostTaskAndReplyWithResult(
      // USER_BLOCKING because it is on the critical path of displaying the
      // virtual keyboard. See https://crbug.com/976542
      FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_BLOCKING},
      base::BindOnce(&CheckFilePath, base::Unretained(copied_file_path)),
      base::BindOnce(&OnFilePathChecked, base::Unretained(context),
                     base::Owned(new std::string(extension_id)),
                     base::Owned(manifest_cp), base::Owned(copied_file_path)));
}

bool ComponentExtensionIMEManagerDelegateImpl::IsInLoginLayoutAllowlist(
    const std::string& layout) {
  return login_layout_set_.find(layout) != login_layout_set_.end();
}

std::optional<base::Value::Dict>
ComponentExtensionIMEManagerDelegateImpl::ParseManifest(
    std::string_view manifest_string) {
  base::JSONReader::Result result =
      base::JSONReader::ReadAndReturnValueWithError(manifest_string);
  if (!result.has_value()) {
    LOG(ERROR) << "Failed to parse manifest: " << result.error().message
               << " at line " << result.error().line << " column "
               << result.error().column;
    return std::nullopt;
  }
  if (!result.value().is_dict()) {
    LOG(ERROR) << "Failed to parse manifest: parsed value is not a dictionary";
    return std::nullopt;
  }
  return std::make_optional(std::move(result.value()).TakeDict());
}

// static
bool ComponentExtensionIMEManagerDelegateImpl::IsIMEExtensionID(
    const std::string& id) {
  for (auto& extension : allowlisted_component_extensions) {
    if (base::EqualsCaseInsensitiveASCII(id, extension.id)) {
      return true;
    }
  }
  return false;
}

// static
bool ComponentExtensionIMEManagerDelegateImpl::ReadEngineComponent(
    const ComponentExtensionIME& component_extension,
    const base::Value::Dict& dict,
    ComponentExtensionEngine* out) {
  DCHECK(out);
  const std::string* engine_id =
      dict.FindString(extensions::manifest_keys::kId);
  if (!engine_id) {
    return false;
  }
  out->engine_id = *engine_id;

  const std::string* display_name =
      dict.FindString(extensions::manifest_keys::kName);
  if (!display_name) {
    return false;
  }
  out->display_name = *display_name;

  const std::string* indicator =
      dict.FindString(extensions::manifest_keys::kIndicator);
  out->indicator = indicator ? *indicator : "";

  std::set<std::string> languages;
  const base::Value* language_value =
      dict.Find(extensions::manifest_keys::kLanguage);
  if (language_value) {
    if (language_value->is_string()) {
      languages.insert(language_value->GetString());
    } else if (language_value->is_list()) {
      for (const base::Value& elem : language_value->GetList()) {
        if (elem.is_string()) {
          languages.insert(elem.GetString());
        }
      }
    }
  }
  DCHECK(!languages.empty());
  out->language_codes.assign(languages.begin(), languages.end());

  // For legacy reasons, multiple physical keyboard XKB layouts can be specified
  // in the IME extension manifest for each input method. However, CrOS only
  // supports one layout per input method. Thus use the "first" layout if
  // specified, else default to "us". CrOS IME extension manifests should
  // specify one and only one layout per input method to avoid confusion.
  const base::Value::List* layouts =
      dict.FindList(extensions::manifest_keys::kLayouts);
  if (!layouts) {
    return false;
  }

  if (*engine_id == "ko-t-i0-und" &&
      base::FeatureList::IsEnabled(
          features::kImeKoreanOnlyModeSwitchOnRightAlt)) {
    out->layout = "kr(cros)";
  } else if (!layouts->empty() && layouts->front().is_string()) {
    out->layout = layouts->front().GetString();
  } else {
    out->layout = "us";
  }

  std::string url_string;
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  bool is_global_emoji_preferences_enabled = base::FeatureList::IsEnabled(
      features::kVirtualKeyboardGlobalEmojiPreferences);
  GURL url = extensions::Extension::ResolveExtensionURL(
      extensions::Extension::GetBaseURLFromExtensionId(component_extension.id),
      "inputview.html");
  url = net::AppendOrReplaceQueryParameter(url, "jelly", "true");
  url = net::AppendOrReplaceQueryParameter(
      url, "globalemojipreferences",
      base::ToString(is_global_emoji_preferences_enabled));
  // Information is managed on VK extension side so just use a default value
  // here.
  url = net::AppendOrReplaceRef(url, "id=default");
  if (!url.is_valid()) {
    return false;
  }
  out->input_view_url = url;
#else
  const std::string* input_view =
      dict.FindString(extensions::manifest_keys::kInputView);
  if (input_view) {
    url_string = *input_view;
    GURL url = extensions::Extension::ResolveExtensionURL(
        extensions::Extension::GetBaseURLFromExtensionId(
            component_extension.id),
        url_string);
    if (!url.is_valid()) {
      return false;
    }
    out->input_view_url = url;
  }
#endif

  const std::string* option_page =
      dict.FindString(extensions::manifest_keys::kOptionsPage);

  bool flag_allows_settings_page =
      (*engine_id != "vkd_vi_vni" && *engine_id != "vkd_vi_telex") ||
      base::FeatureList::IsEnabled(features::kFirstPartyVietnameseInput);

  if (option_page && flag_allows_settings_page) {
    url_string = *option_page;
    GURL options_page_url = extensions::Extension::ResolveExtensionURL(
        extensions::Extension::GetBaseURLFromExtensionId(
            component_extension.id),
        url_string);
    if (!options_page_url.is_valid()) {
      return false;
    }
    out->options_page_url = options_page_url;
  } else {
    // Fallback to extension level options page.
    out->options_page_url = component_extension.options_page_url;
  }

  const std::string* handwriting_language =
      dict.FindString(extensions::manifest_keys::kHandwritingLanguage);

  if (handwriting_language != nullptr) {
    out->handwriting_language = *handwriting_language;
  } else {
    out->handwriting_language = std::nullopt;
  }

  return true;
}

// static
bool ComponentExtensionIMEManagerDelegateImpl::ReadExtensionInfo(
    const base::Value::Dict& manifest,
    const std::string& extension_id,
    ComponentExtensionIME* out) {
  const std::string* description =
      manifest.FindString(extensions::manifest_keys::kDescription);
  if (!description) {
    return false;
  }
  out->description = *description;

  const std::string* path = manifest.FindString(kImePathKeyName);
  if (path) {
    out->path = base::FilePath(*path);
  }
  const std::string* url_string =
      manifest.FindString(extensions::manifest_keys::kOptionsPage);
  if (url_string) {
    GURL url = extensions::Extension::ResolveExtensionURL(
        extensions::Extension::GetBaseURLFromExtensionId(extension_id),
        *url_string);
    if (!url.is_valid()) {
      return false;
    }
    out->options_page_url = url;
  }
  // It's okay to return true on no option page and/or input view page case.
  return true;
}

// static
void ComponentExtensionIMEManagerDelegateImpl::ReadComponentExtensionsInfo(
    std::vector<ComponentExtensionIME>* out_imes) {
  DCHECK(out_imes);
  for (auto& extension : allowlisted_component_extensions) {
    // TODO(crbug.com/384675323): Remove this check and update
    // `allowlisted_component_extensions` when flag is removed.
    if (extension.manifest_resource_id == IDR_BRAILLE_MANIFEST &&
        ::features::IsAccessibilityManifestV3EnabledForBrailleIme()) {
      extension.manifest_resource_id = IDR_BRAILLE_MANIFEST_MV3;
    }

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
    if (extension.manifest_resource_id == IDR_GOOGLE_XKB_MANIFEST &&
        base::FeatureList::IsEnabled(features::kImeManifestV3)) {
      extension.manifest_resource_id = IDR_GOOGLE_XKB_MANIFEST_V3;
    }
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

    ComponentExtensionIME component_ime;
    component_ime.manifest =
        ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
            extension.manifest_resource_id);
    if (component_ime.manifest.empty()) {
      LOG(ERROR) << "Couldn't get manifest from resource_id("
                 << extension.manifest_resource_id << ")";
      continue;
    }

    std::optional<base::Value::Dict> maybe_manifest =
        ParseManifest(component_ime.manifest);
    if (!maybe_manifest.has_value()) {
      LOG(ERROR) << "Failed to load invalid manifest: "
                 << component_ime.manifest;
      continue;
    }
    const base::Value::Dict& manifest = maybe_manifest.value();

    if (!ReadExtensionInfo(manifest, extension.id, &component_ime)) {
      LOG(ERROR) << "manifest doesn't have needed information for IME.";
      continue;
    }

    component_ime.id = extension.id;

    if (!component_ime.path.IsAbsolute()) {
      base::FilePath resources_path;
      if (!base::PathService::Get(chrome::DIR_RESOURCES, &resources_path)) {
        NOTREACHED();
      }
      component_ime.path = resources_path.Append(component_ime.path);
    }

    const base::Value::List* component_list =
        manifest.FindList(extensions::manifest_keys::kInputComponents);
    if (!component_list) {
      LOG(ERROR) << "No input_components is found in manifest.";
      continue;
    }

    for (const base::Value& value : *component_list) {
      if (!value.is_dict()) {
        continue;
      }

      const base::Value::Dict& dictionary = value.GetDict();
      ComponentExtensionEngine engine;
      ReadEngineComponent(component_ime, dictionary, &engine);

      const char* kHindiInscriptEngineId = "vkd_hi_inscript";
      if (engine.engine_id == kHindiInscriptEngineId &&
          !base::FeatureList::IsEnabled(features::kHindiInscriptLayout)) {
        bool policy_value = false;
        CrosSettings::Get()->GetBoolean(kDeviceHindiInscriptLayoutEnabled,
                                        &policy_value);
        if (!policy_value) {
          continue;
        }
      }

      component_ime.engines.push_back(engine);
    }
    out_imes->push_back(component_ime);
  }
}

}  // namespace input_method
}  // namespace ash