File: lobster_system_state_provider_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 (249 lines) | stat: -rw-r--r-- 9,785 bytes parent folder | download | duplicates (5)
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
// Copyright 2025 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/lobster/lobster_system_state_provider_impl.h"

#include <array>

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/generative_ai_country_restrictions.h"
#include "ash/public/cpp/lobster/lobster_enums.h"
#include "ash/public/cpp/lobster/lobster_system_state.h"
#include "ash/public/cpp/lobster/lobster_text_input_context.h"
#include "base/containers/fixed_flat_set.h"
#include "base/types/cxx23_to_underlying.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/components/editor_menu/public/cpp/editor_consent_status.h"
#include "chromeos/ash/components/specialized_features/feature_access_checker.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "net/base/network_change_notifier.h"
#include "ui/base/ime/ash/extension_ime_util.h"
#include "ui/base/ime/ash/input_method_manager.h"
#include "ui/display/screen.h"
#include "ui/display/tablet_state.h"

namespace {

ash::LobsterConsentStatus GetConsentStatusFromInteger(int status_value) {
  switch (status_value) {
    case base::to_underlying(
        chromeos::editor_menu::EditorConsentStatus::kUnset):
    case base::to_underlying(
        chromeos::editor_menu::EditorConsentStatus::kPending):
      return ash::LobsterConsentStatus::kUnset;
    case base::to_underlying(
        chromeos::editor_menu::EditorConsentStatus::kApproved):
      return ash::LobsterConsentStatus::kApproved;
    case base::to_underlying(
        chromeos::editor_menu::EditorConsentStatus::kDeclined):
      return ash::LobsterConsentStatus::kDeclined;
    default:
      LOG(ERROR) << "Invalid consent status: " << status_value;
      // For any of the invalid states, treat the consent status as unset.
      return ash::LobsterConsentStatus::kUnset;
  }
}

std::string GetCurrentImeEngineId() {
  ash::input_method::InputMethodManager* input_method_manager =
      ash::input_method::InputMethodManager::Get();
  if (input_method_manager == nullptr ||
      input_method_manager->GetActiveIMEState() == nullptr) {
    return "";
  }
  return ash::extension_ime_util::GetComponentIDByInputMethodID(
      input_method_manager->GetActiveIMEState()->GetCurrentInputMethod().id());
}

bool IsInputTypeAllowed(ui::TextInputType type) {
  static constexpr auto kTextInputTypeAllowlist =
      base::MakeFixedFlatSet<ui::TextInputType>(
          {ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE, ui::TEXT_INPUT_TYPE_TEXT,
           ui::TEXT_INPUT_TYPE_TEXT_AREA});

  return kTextInputTypeAllowlist.contains(type);
}

bool IsImeAllowed(std::string_view current_ime_id) {
  static constexpr auto kImeAllowlist =
      base::MakeFixedFlatSet<std::string_view>({
          "xkb:ca:eng:eng",           // Canada
          "xkb:gb::eng",              // UK
          "xkb:gb:extd:eng",          // UK Extended
          "xkb:gb:dvorak:eng",        // UK Dvorak
          "xkb:in::eng",              // India
          "xkb:pk::eng",              // Pakistan
          "xkb:us:altgr-intl:eng",    // US Extended
          "xkb:us:colemak:eng",       // US Colemak
          "xkb:us:dvorak:eng",        // US Dvorak
          "xkb:us:dvp:eng",           // US Programmer Dvorak
          "xkb:us:intl_pc:eng",       // US Intl (PC)
          "xkb:us:intl:eng",          // US Intl
          "xkb:us:workman-intl:eng",  // US Workman Intl
          "xkb:us:workman:eng",       // US Workman
          "xkb:us::eng",              // US
          "xkb:za:gb:eng"             // South Africa
      });

  return kImeAllowlist.contains(current_ime_id);
}

specialized_features::FeatureAccessConfig CreateFeatureAccessConfig() {
  specialized_features::FeatureAccessConfig config;
  config.disabled_in_kiosk_mode = true;

  // Dogfood devices ignore all other checks.
  if (base::FeatureList::IsEnabled(ash::features::kLobsterDogfood)) {
    return config;
  }

  config.feature_flag = &ash::features::kLobster;
  config.feature_management_flag = &ash::features::kFeatureManagementLobster;
  config.capability_callback =
      base::BindRepeating([](AccountCapabilities capabilities) {
        return capabilities.can_use_manta_service();
      });
  config.country_codes = ash::GetGenerativeAiCountryAllowlist();
  return config;
}

}  // namespace

LobsterSystemStateProviderImpl::LobsterSystemStateProviderImpl(
    PrefService* pref,
    signin::IdentityManager* identity_manager,
    bool is_in_demo_mode)
    : pref_(pref),
      access_checker_(CreateFeatureAccessConfig(),
                      pref_,
                      identity_manager,
                      /*variations_service_callback=*/base::BindRepeating([]() {
                        return g_browser_process->variations_service();
                      })),
      is_in_demo_mode_(is_in_demo_mode) {}

LobsterSystemStateProviderImpl::~LobsterSystemStateProviderImpl() = default;

ash::LobsterSystemState LobsterSystemStateProviderImpl::GetSystemState(
    const ash::LobsterTextInputContext& text_input_context) {
  ash::LobsterSystemState system_state(ash::LobsterStatus::kEnabled,
                                       /*failed_checks=*/{});

  specialized_features::FeatureAccessFailureSet access_checker_failure_set =
      access_checker_.Check();

  // Performs feature flag check
  if (access_checker_failure_set.Has(
          specialized_features::FeatureAccessFailure::kFeatureFlagDisabled)) {
    system_state.status = ash::LobsterStatus::kBlocked;
    system_state.failed_checks.Put(
        ash::LobsterSystemCheck::kInvalidFeatureFlags);
  }

  // Performs a hardware check
  if (access_checker_failure_set.Has(
          specialized_features::FeatureAccessFailure::
              kFeatureManagementCheckFailed)) {
    system_state.status = ash::LobsterStatus::kBlocked;
    system_state.failed_checks.Put(
        ash::LobsterSystemCheck::kUnsupportedHardware);
  }

  // Performs a location check
  if (access_checker_failure_set.Has(
          specialized_features::FeatureAccessFailure::kCountryCheckFailed)) {
    system_state.status = ash::LobsterStatus::kBlocked;
    system_state.failed_checks.Put(ash::LobsterSystemCheck::kInvalidRegion);
  }

  // TODO: b:406915099 - Migrate demo mode check into the shared feature checker module.
  // Performs account capabilities check in non-demo mode only
  if (!is_in_demo_mode_ && access_checker_failure_set.Has(
                               specialized_features::FeatureAccessFailure::
                                   kAccountCapabilitiesCheckFailed)) {
    system_state.status = ash::LobsterStatus::kBlocked;
    system_state.failed_checks.Put(
        ash::LobsterSystemCheck::kInvalidAccountCapabilities);
  }

  // Performs a kiosk mode check
  if (access_checker_failure_set.Has(
          specialized_features::FeatureAccessFailure::
              kDisabledInKioskModeCheckFailed)) {
    system_state.status = ash::LobsterStatus::kBlocked;
    system_state.failed_checks.Put(
        ash::LobsterSystemCheck::kUnsupportedInKioskMode);
  }

  if (!IsInputTypeAllowed(text_input_context.text_input_type)) {
    system_state.status = ash::LobsterStatus::kBlocked;
    system_state.failed_checks.Put(ash::LobsterSystemCheck::kInvalidInputField);
  }

  if (!ash::features::IsLobsterEnabledForManagedUsers() &&
      pref_->IsManagedPreference(
          ash::prefs::kLobsterEnterprisePolicySettings)) {
    system_state.status = ash::LobsterStatus::kBlocked;
    system_state.failed_checks.Put(
        ash::LobsterSystemCheck::kForcedDisabledOnManagedUsers);
  }

  if (pref_->GetInteger(ash::prefs::kLobsterEnterprisePolicySettings) ==
      base::to_underlying(ash::LobsterEnterprisePolicyValue::kDisabled)) {
    system_state.status = ash::LobsterStatus::kBlocked;
    system_state.failed_checks.Put(ash::LobsterSystemCheck::kUnsupportedPolicy);
  }

  if (!pref_->GetBoolean(ash::prefs::kLobsterEnabled)) {
    system_state.status = ash::LobsterStatus::kBlocked;
    system_state.failed_checks.Put(ash::LobsterSystemCheck::kSettingsOff);
  }

  // Performs a network check
  if (net::NetworkChangeNotifier::IsOffline()) {
    system_state.status = ash::LobsterStatus::kBlocked;
    system_state.failed_checks.Put(
        ash::LobsterSystemCheck::kNoInternetConnection);
  }

  // Performs a tablet mode check
  if (is_in_tablet_mode_) {
    system_state.status = ash::LobsterStatus::kBlocked;
    system_state.failed_checks.Put(
        ash::LobsterSystemCheck::kUnsupportedFormFactor);
  }

  // Performs an IME check
  if (ash::features::IsLobsterDisabledByInvalidIME() &&
      !IsImeAllowed(GetCurrentImeEngineId())) {
    system_state.status = ash::LobsterStatus::kBlocked;
    system_state.failed_checks.Put(
        ash::LobsterSystemCheck::kInvalidInputMethod);
  }

  ash::LobsterConsentStatus consent_status = GetConsentStatusFromInteger(
      pref_->GetInteger(ash::prefs::kOrcaConsentStatus));

  if (consent_status == ash::LobsterConsentStatus::kDeclined) {
    system_state.failed_checks.Put(ash::LobsterSystemCheck::kInvalidConsent);
  }

  if (!system_state.failed_checks.empty()) {
    system_state.status = ash::LobsterStatus::kBlocked;
  } else if (consent_status == ash::LobsterConsentStatus::kUnset) {
    system_state.status = ash::LobsterStatus::kConsentNeeded;
  } else {
    system_state.status = ash::LobsterStatus::kEnabled;
  }

  return system_state;
}

void LobsterSystemStateProviderImpl::OnDisplayTabletStateChanged(
    display::TabletState state) {
  is_in_tablet_mode_ = (state == display::TabletState::kInTabletMode);
}