File: quick_insert_search_controller.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 (247 lines) | stat: -rw-r--r-- 9,384 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_insert/search/quick_insert_search_controller.h"

#include <algorithm>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>

#include "ash/constants/ash_pref_names.h"
#include "ash/quick_insert/model/quick_insert_search_results_section.h"
#include "ash/quick_insert/quick_insert_category.h"
#include "ash/quick_insert/quick_insert_client.h"
#include "ash/quick_insert/search/quick_insert_search_aggregator.h"
#include "ash/quick_insert/search/quick_insert_search_request.h"
#include "ash/quick_insert/views/quick_insert_view_delegate.h"
#include "base/check_deref.h"
#include "base/containers/contains.h"
#include "base/containers/fixed_flat_map.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "components/language/core/browser/pref_names.h"
#include "components/language/core/common/locale_util.h"
#include "components/prefs/pref_service.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/base/ime/ash/extension_ime_util.h"

namespace ash {
namespace {

constexpr int kMaxEmojiResults = 3;
constexpr int kMaxSymbolResults = 2;
constexpr int kMaxEmoticonResults = 2;

// These are taken from manifest files in:
// https://source.chromium.org/chromium/chromium/src/+/2be4329930cac782779c5083389b83e09a8bcb47:chrome/browser/resources/chromeos/input_method/
constexpr auto kImeToLangCode =
    base::MakeFixedFlatMap<std::string_view, std::string_view>(
        {{"xkb:us::eng", "en"},           {"xkb:us:intl:eng", "en"},
         {"xkb:us:altgr-intl:eng", "en"}, {"xkb:us:dvorak:eng", "en"},
         {"xkb:us:dvp:eng", "en"},        {"xkb:us:colemak:eng", "en"},
         {"xkb:us:workman:eng", "en"},    {"xkb:us:workman-intl:eng", "en"},
         {"xkb:fr::fra", "fr"},           {"xkb:be::fra", "fr"},
         {"xkb:ca::fra", "fr"},           {"xkb:ch:fr:fra", "fr"},
         {"xkb:ca:multix:fra", "fr"},     {"xkb:de::ger", "de"},
         {"xkb:de:neo:ger", "de"},        {"xkb:be::ger", "de"},
         {"xkb:ch::ger", "de"},           {"xkb:jp::jpn", "ja"},
         {"xkb:ca:eng:eng", "en"},        {"xkb:es::spa", "es"},
         {"xkb:dk::dan", "da"},           {"xkb:latam::spa", "es"},
         {"xkb:gb:extd:eng", "en"},       {"xkb:gb:dvorak:eng", "en"},
         {"xkb:fi::fin", "fi"},           {"xkb:no::nob", "no"},
         {"xkb:se::swe", "sv"},           {"nacl_mozc_us", "ja"},
         {"nacl_mozc_jp", "ja"}});

base::span<const emoji::EmojiSearchEntry> FirstNOrFewerElements(
    base::span<const emoji::EmojiSearchEntry> container,
    size_t n) {
  return container.first(std::min(container.size(), n));
}

const base::Value::Dict* LoadEmojiVariantsFromPrefs(PrefService* prefs) {
  if (prefs == nullptr) {
    return nullptr;
  }
  return prefs->GetDict(prefs::kEmojiPickerPreferences)
      .FindDict("preferred_variants");
}

std::vector<std::string> GetLanguageCodesFromPrefs(PrefService* prefs) {
  if (prefs == nullptr) {
    return {"en"};
  }

  // These codes should be in this order.
  // - First: Current active IME
  // - Remaining: All other IMEs that are enabled
  // There should be no duplicates.
  std::vector<std::string> results;

  // Get the currently active IME
  std::string current_ime = extension_ime_util::GetComponentIDByInputMethodID(
      prefs->GetString(prefs::kLanguageCurrentInputMethod));
  if (const auto& it = kImeToLangCode.find(current_ime);
      it != kImeToLangCode.end()) {
    results.push_back(std::string(it->second));
  }

  // Add ui language as second set of results.
  // EmojiSearch expects the "base language" without region e.g. "en" instead of
  // "en-US".
  std::string ui_locale(language::ExtractBaseLanguage(
      prefs->GetString(language::prefs::kApplicationLocale)));

  results.push_back(ui_locale);

  // All enabled engines
  std::vector<std::string> full_ids =
      base::SplitString(prefs->GetString(prefs::kLanguagePreloadEngines), ",",
                        base::WhitespaceHandling::TRIM_WHITESPACE,
                        base::SplitResult::SPLIT_WANT_ALL);

  for (const std::string& id : full_ids) {
    std::string short_ime_id =
        extension_ime_util::GetComponentIDByInputMethodID(id);
    if (const auto& it = kImeToLangCode.find(short_ime_id);
        it != kImeToLangCode.end() && !base::Contains(results, it->second)) {
      results.push_back(std::string(it->second));
    }
  }

  if (results.empty()) {
    // If no languages supported then attempt to use English.
    results.push_back("en");
  }

  return results;
}

}  // namespace

QuickInsertSearchController::QuickInsertSearchController(
    base::TimeDelta burn_in_period)
    : burn_in_period_(burn_in_period) {}

QuickInsertSearchController::~QuickInsertSearchController() = default;

void QuickInsertSearchController::LoadEmojiLanguagesFromPrefs(
    PrefService* prefs) {
  pref_change_registrar_.Reset();
  if (prefs == nullptr) {
    return;
  }
  emoji_search_.LoadEmojiLanguages(GetLanguageCodesFromPrefs(prefs));

  pref_change_registrar_.Init(prefs);
  pref_change_registrar_.Add(
      prefs::kLanguagePreloadEngines,
      base::BindRepeating(&QuickInsertSearchController::LoadEmojiLanguages,
                          weak_ptr_factory_.GetWeakPtr(),
                          pref_change_registrar_.prefs()));
}

void QuickInsertSearchController::LoadEmojiLanguages(PrefService* prefs) {
  if (prefs == nullptr) {
    return;
  }
  emoji_search_.LoadEmojiLanguages(GetLanguageCodesFromPrefs(prefs));
}

void QuickInsertSearchController::StartSearch(
    QuickInsertClient* client,
    std::u16string_view query,
    std::optional<QuickInsertCategory> category,
    base::span<const QuickInsertCategory> available_categories,
    bool caps_lock_state_to_search,
    bool search_case_transforms,
    QuickInsertViewDelegate::SearchResultsCallback callback) {
  StopSearch();
  aggregator_ = std::make_unique<QuickInsertSearchAggregator>(
      burn_in_period_, std::move(callback));

  // TODO: b/348067874 - Hook `done_closure` up to `aggregator_`.
  search_request_ = std::make_unique<QuickInsertSearchRequest>(
      query, std::move(category),
      base::BindRepeating(
          &QuickInsertSearchAggregator::HandleSearchSourceResults,
          aggregator_->GetWeakPtr()),
      base::BindOnce(&QuickInsertSearchAggregator::HandleNoMoreResults,
                     aggregator_->GetWeakPtr()),
      client, available_categories, caps_lock_state_to_search,
      search_case_transforms);
}

void QuickInsertSearchController::StopSearch() {
  // The search request must be reset first so it can let the aggregator know
  // that it has been interrupted.
  search_request_.reset();
  aggregator_.reset();
}

void QuickInsertSearchController::StartEmojiSearch(
    PrefService* prefs,
    std::u16string_view query,
    QuickInsertViewDelegate::EmojiSearchResultsCallback callback) {
  const base::TimeTicks search_start = base::TimeTicks::Now();

  emoji::EmojiSearchResult results = emoji_search_.SearchEmoji(
      query, GetLanguageCodesFromPrefs(prefs), kMaxEmojiResults,
      kMaxSymbolResults, kMaxEmoticonResults);

  base::TimeDelta elapsed = base::TimeTicks::Now() - search_start;
  base::UmaHistogramTimes("Ash.Picker.Search.EmojiProvider.QueryTime", elapsed);

  std::vector<QuickInsertEmojiResult> emoji_results;
  emoji_results.reserve(kMaxEmojiResults + kMaxSymbolResults +
                        kMaxEmoticonResults);

  const base::Value::Dict* emoji_variants = LoadEmojiVariantsFromPrefs(prefs);

  for (const emoji::EmojiSearchEntry& result :
       FirstNOrFewerElements(results.emojis, kMaxEmojiResults)) {
    std::string emoji_string = result.emoji_string;
    if (emoji_variants != nullptr) {
      const std::string* variant_string =
          emoji_variants->FindString(emoji_string);
      if (variant_string != nullptr) {
        emoji_string = *variant_string;
      }
    }
    emoji_results.push_back(QuickInsertEmojiResult::Emoji(
        base::UTF8ToUTF16(emoji_string),
        base::UTF8ToUTF16(emoji_search_.GetEmojiName(emoji_string, "en"))));
  }
  for (const emoji::EmojiSearchEntry& result :
       FirstNOrFewerElements(results.symbols, kMaxSymbolResults)) {
    emoji_results.push_back(QuickInsertEmojiResult::Symbol(
        base::UTF8ToUTF16(result.emoji_string),
        base::UTF8ToUTF16(
            emoji_search_.GetEmojiName(result.emoji_string, "en"))));
  }
  for (const emoji::EmojiSearchEntry& result :
       FirstNOrFewerElements(results.emoticons, kMaxEmoticonResults)) {
    emoji_results.push_back(QuickInsertEmojiResult::Emoticon(
        base::UTF8ToUTF16(result.emoji_string),
        base::UTF8ToUTF16(
            emoji_search_.GetEmojiName(result.emoji_string, "en"))));
  }

  std::move(callback).Run(std::move(emoji_results));
}

std::string QuickInsertSearchController::GetEmojiName(std::string_view emoji) {
  return emoji_search_.GetEmojiName(emoji, "en");
}

}  // namespace ash