File: quick_settings_header.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 (429 lines) | stat: -rw-r--r-- 16,419 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
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
// Copyright 2022 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/system/unified/quick_settings_header.h"

#include <memory>

#include "ash/ash_element_identifiers.h"
#include "ash/constants/quick_settings_catalogs.h"
#include "ash/login/ui/lock_screen.h"
#include "ash/public/cpp/ash_view_ids.h"
#include "ash/public/cpp/session/session_observer.h"
#include "ash/public/cpp/system_tray_client.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/ash_color_provider.h"
#include "ash/style/typography.h"
#include "ash/system/channel_indicator/channel_indicator_quick_settings_view.h"
#include "ash/system/channel_indicator/channel_indicator_utils.h"
#include "ash/system/enterprise/enterprise_domain_observer.h"
#include "ash/system/model/enterprise_domain_model.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/model/update_model.h"
#include "ash/system/supervised/supervised_icon_string.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/unified/quick_settings_metrics_util.h"
#include "ash/system/unified/unified_system_tray_controller.h"
#include "ash/system/update/eol_notice_quick_settings_view.h"
#include "ash/system/update/extended_updates_notice_quick_settings_view.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "components/session_manager/session_manager_types.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/devicetype_utils.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"

namespace ash {
namespace {

// The bottom padding is 0 so this view is flush with the feature tiles.
constexpr auto kHeaderPadding = gfx::Insets::TLBR(16, 16, 0, 16);

// Horizontal space between header buttons.
constexpr int kButtonSpacing = 8;

// Header button size when the button is narrow (e.g. two column layout).
constexpr gfx::Size kNarrowButtonSize(180, 32);

// Header button size when the button is wide (e.g. one column layout).
constexpr gfx::Size kWideButtonSize(408, 32);

constexpr int kManagedStateCornerRadius = 16;
constexpr float kManagedStateStrokeWidth = 1.0f;
constexpr auto kManagedStateBorderInsets = gfx::Insets::TLBR(0, 12, 0, 12);
constexpr gfx::Size kManagedStateImageSize(20, 20);

// Shows account settings in OS settings, which includes a link to install or
// open the Family Link app to see supervision settings.
void ShowAccountSettings() {
  quick_settings_metrics_util::RecordQsButtonActivated(
      QsButtonCatalogName::kSupervisedButton);
  Shell::Get()->system_tray_model()->client()->ShowAccountSettings();
}

}  // namespace

class QuickSettingsHeader::ManagedStateView : public views::Button {
  METADATA_HEADER(ManagedStateView, views::Button)

 public:
  ManagedStateView(base::OnceClosure callback,
                   int label_id,
                   const gfx::VectorIcon& icon)
      : views::Button(std::move(callback)), icon_(icon) {
    auto* layout_manager = SetLayoutManager(std::make_unique<views::BoxLayout>(
        views::BoxLayout::Orientation::kHorizontal, gfx::Insets(),
        kUnifiedSystemInfoSpacing));

    // Image goes first.
    image_ = AddChildView(std::make_unique<views::ImageView>());
    label_ = AddChildView(std::make_unique<views::Label>());

    // Inset the icon and label so they aren't too close to the rounded corners.
    layout_manager->set_inside_border_insets(kManagedStateBorderInsets);
    layout_manager->set_main_axis_alignment(
        views::BoxLayout::MainAxisAlignment::kCenter);
    label_->SetAutoColorReadabilityEnabled(false);
    label_->SetSubpixelRenderingEnabled(false);
    label_->SetText(l10n_util::GetStringUTF16(label_id));

    image_->SetPreferredSize(kManagedStateImageSize);
    label_->SetEnabledColor(cros_tokens::kCrosSysOnSurfaceVariant);
    TypographyProvider::Get()->StyleLabel(ash::TypographyToken::kCrosBody2,
                                          *label_);
    SetInstallFocusRingOnFocus(true);
    views::FocusRing::Get(this)->SetColorId(cros_tokens::kCrosSysFocusRing);
    views::InkDrop::Get(this)->SetMode(views::InkDropHost::InkDropMode::ON);
    views::InstallRoundRectHighlightPathGenerator(this, gfx::Insets(),
                                                  kManagedStateCornerRadius);
  }

  ManagedStateView(const ManagedStateView&) = delete;

  ManagedStateView& operator=(const ManagedStateView&) = delete;

  ~ManagedStateView() override = default;

  views::Label* label() { return label_; }

 private:
  // views::Button:
  views::View* GetTooltipHandlerForPoint(const gfx::Point& point) override {
    // Tooltip events should be handled by this top-level view.
    return HitTestPoint(point) ? this : nullptr;
  }

  // views::Button:
  // TODO(b/311234537): consider to remove this override and set color ids.
  void OnThemeChanged() override {
    views::Button::OnThemeChanged();
    const std::pair<SkColor, float> base_color_and_opacity =
        AshColorProvider::Get()->GetInkDropBaseColorAndOpacity();
    views::InkDrop::Get(this)->SetBaseColor(base_color_and_opacity.first);
    image_->SetImage(ui::ImageModel::FromVectorIcon(
        *icon_,
        GetColorProvider()->GetColor(cros_tokens::kCrosSysOnSurfaceVariant)));
  }

  // views::Button:
  void PaintButtonContents(gfx::Canvas* canvas) override {
    // Draw a button outline similar to ChannelIndicatorQuickSettingsView's
    // VersionButton outline.
    cc::PaintFlags flags;
    flags.setColor(
        GetColorProvider()->GetColor(cros_tokens::kCrosSysSeparator));
    flags.setStyle(cc::PaintFlags::kStroke_Style);
    flags.setStrokeWidth(kManagedStateStrokeWidth);
    flags.setAntiAlias(true);
    const float half_stroke_width = kManagedStateStrokeWidth / 2.0f;
    gfx::RectF bounds(GetLocalBounds());
    bounds.Inset(half_stroke_width);
    canvas->DrawRoundRect(bounds, kManagedStateCornerRadius, flags);
  }

  // Owned by views hierarchy.
  raw_ptr<views::Label> label_ = nullptr;
  raw_ptr<views::ImageView> image_ = nullptr;

  const raw_ref<const gfx::VectorIcon> icon_;
};

BEGIN_METADATA(QuickSettingsHeader, ManagedStateView)
END_METADATA

class QuickSettingsHeader::EnterpriseManagedView
    : public ManagedStateView,
      public EnterpriseDomainObserver,
      public SessionObserver {
  METADATA_HEADER(EnterpriseManagedView, ManagedStateView)

 public:
  explicit EnterpriseManagedView(UnifiedSystemTrayController* controller)
      : ManagedStateView(
            base::BindRepeating(
                &QuickSettingsHeader::ShowEnterpriseInfo,
                base::Unretained(controller),
                base::FeatureList::IsEnabled(
                    ash::features::kImprovedManagementDisclosure),
                Shell::Get()->session_controller()->IsUserSessionBlocked(),
                !Shell::Get()
                     ->system_tray_model()
                     ->enterprise_domain()
                     ->enterprise_domain_manager()
                     .empty()),
            IDS_ASH_ENTERPRISE_DEVICE_MANAGED_SHORT,
            kQuickSettingsManagedIcon) {
    DCHECK(Shell::Get());
    SetID(VIEW_ID_QS_MANAGED_BUTTON);
    SetProperty(views::kElementIdentifierKey, kEnterpriseManagedView);
    Shell::Get()->system_tray_model()->enterprise_domain()->AddObserver(this);
    Shell::Get()->session_controller()->AddObserver(this);
    Update();
  }

  EnterpriseManagedView(const EnterpriseManagedView&) = delete;
  EnterpriseManagedView& operator=(const EnterpriseManagedView&) = delete;

  ~EnterpriseManagedView() override {
    Shell::Get()->system_tray_model()->enterprise_domain()->RemoveObserver(
        this);
    Shell::Get()->session_controller()->RemoveObserver(this);
  }

  // Adjusts the layout for a narrower appearance, using a shorter label for
  // the button.
  void SetNarrowLayout(bool narrow) {
    narrow_layout_ = narrow;
    Update();
  }

 private:
  // EnterpriseDomainObserver:
  void OnDeviceEnterpriseInfoChanged() override { Update(); }
  void OnEnterpriseAccountDomainChanged() override { Update(); }

  // SessionObserver:
  void OnLoginStatusChanged(LoginStatus status) override { Update(); }

  // Updates the view visibility and displayed string.
  void Update() {
    EnterpriseDomainModel* model =
        Shell::Get()->system_tray_model()->enterprise_domain();
    SessionControllerImpl* session_controller =
        Shell::Get()->session_controller();
    const std::string enterprise_domain_manager =
        model->enterprise_domain_manager();
    const std::string account_domain_manager = model->account_domain_manager();

    const bool visible = session_controller->ShouldDisplayManagedUI() ||
                         !enterprise_domain_manager.empty() ||
                         !account_domain_manager.empty();
    SetVisible(visible);

    if (!visible) {
      return;
    }

    // Display device and user management based on the enterprise enrollment
    // state.
    std::u16string managed_string;
    if (enterprise_domain_manager.empty() && account_domain_manager.empty()) {
      managed_string = l10n_util::GetStringFUTF16(
          IDS_ASH_ENTERPRISE_DEVICE_MANAGED, ui::GetChromeOSDeviceName());
    } else if (!enterprise_domain_manager.empty() &&
               !account_domain_manager.empty() &&
               enterprise_domain_manager != account_domain_manager) {
      managed_string = l10n_util::GetStringFUTF16(
          IDS_ASH_SHORT_MANAGED_BY_MULTIPLE,
          base::UTF8ToUTF16(enterprise_domain_manager),
          base::UTF8ToUTF16(account_domain_manager));
    } else {
      const std::u16string display_domain_manager =
          enterprise_domain_manager.empty()
              ? base::UTF8ToUTF16(account_domain_manager)
              : base::UTF8ToUTF16(enterprise_domain_manager);
      managed_string = l10n_util::GetStringFUTF16(IDS_ASH_SHORT_MANAGED_BY,
                                                  display_domain_manager);
      // Narrow layout uses the string "Managed" and wide layout uses the full
      // string "Managed by example.com".
      label()->SetText(narrow_layout_
                           ? l10n_util::GetStringUTF16(
                                 IDS_ASH_ENTERPRISE_DEVICE_MANAGED_SHORT)
                           : managed_string);
    }
    SetTooltipText(managed_string);
  }

  // See SetNarrowLayout().
  bool narrow_layout_ = false;
};

BEGIN_METADATA(QuickSettingsHeader, EnterpriseManagedView)
END_METADATA

QuickSettingsHeader::QuickSettingsHeader(
    UnifiedSystemTrayController* controller) {
  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal, kHeaderPadding,
      kButtonSpacing));

  enterprise_managed_view_ =
      AddChildView(std::make_unique<EnterpriseManagedView>(controller));

  // A view that shows whether the user is supervised or a child.
  supervised_view_ = AddChildView(std::make_unique<ManagedStateView>(
      base::BindRepeating(&ShowAccountSettings),
      IDS_ASH_STATUS_TRAY_SUPERVISED_LABEL, GetSupervisedUserIcon()));
  supervised_view_->SetID(VIEW_ID_QS_SUPERVISED_BUTTON);
  const bool visible =
      Shell::Get()->system_tray_model()->IsInUserChildSession();
  supervised_view_->SetVisible(visible);
  if (visible) {
    supervised_view_->SetTooltipText(GetSupervisedUserMessage());
  }

  const bool is_active_state =
      Shell::Get()->session_controller()->GetSessionState() ==
      session_manager::SessionState::ACTIVE;
  if (is_active_state) {
    if (Shell::Get()->system_tray_model()->update_model()->show_eol_notice()) {
      eol_notice_ =
          AddChildView(std::make_unique<EolNoticeQuickSettingsView>());
    } else if (Shell::Get()
                   ->system_tray_model()
                   ->update_model()
                   ->show_extended_updates_notice()) {
      extended_updates_notice_ = AddChildView(
          std::make_unique<ExtendedUpdatesNoticeQuickSettingsView>());
    }
  }

  // If the release track is not "stable" then show the channel indicator UI.
  auto channel = Shell::Get()->shell_delegate()->GetChannel();
  if (channel_indicator_utils::IsDisplayableChannel(channel) && !eol_notice_ &&
      !extended_updates_notice_) {
    channel_view_ =
        AddChildView(std::make_unique<ChannelIndicatorQuickSettingsView>(
            channel, is_active_state && Shell::Get()
                                            ->system_tray_model()
                                            ->client()
                                            ->IsUserFeedbackEnabled()));
  }

  UpdateVisibilityAndLayout();
}

QuickSettingsHeader::~QuickSettingsHeader() = default;

void QuickSettingsHeader::ChildVisibilityChanged(views::View* child) {
  UpdateVisibilityAndLayout();
}

views::View* QuickSettingsHeader::GetManagedButtonForTest() {
  return enterprise_managed_view_;
}

views::View* QuickSettingsHeader::GetSupervisedButtonForTest() {
  return supervised_view_;
}

views::Label* QuickSettingsHeader::GetManagedButtonLabelForTest() {
  return enterprise_managed_view_->label();
}

views::Label* QuickSettingsHeader::GetSupervisedButtonLabelForTest() {
  return supervised_view_->label();
}

views::View* QuickSettingsHeader::GetExtendedUpdatesViewForTest() {
  return extended_updates_notice_;
}

void QuickSettingsHeader::UpdateVisibilityAndLayout() {
  // The managed view and the supervised view are never shown together.
  DCHECK(!enterprise_managed_view_->GetVisible() ||
         !supervised_view_->GetVisible());

  // The notice views are never shown together.
  DCHECK(!!channel_view_ + !!eol_notice_ + !!extended_updates_notice_ <= 1);

  // Make `this` view visible if a child is visible.
  bool managed_view_visible =
      enterprise_managed_view_->GetVisible() || supervised_view_->GetVisible();
  bool notice_view_visible =
      channel_view_ || eol_notice_ || extended_updates_notice_;

  SetVisible(managed_view_visible || notice_view_visible);

  // Update button sizes for one column vs. two columns.
  bool two_columns = managed_view_visible && notice_view_visible;
  gfx::Size size = two_columns ? kNarrowButtonSize : kWideButtonSize;
  enterprise_managed_view_->SetPreferredSize(size);
  supervised_view_->SetPreferredSize(size);
  if (channel_view_) {
    channel_view_->SetPreferredSize(size);
  }
  if (eol_notice_) {
    eol_notice_->SetPreferredSize(size);
  }
  if (extended_updates_notice_) {
    extended_updates_notice_->SetPreferredSize(size);
  }

  // Use custom narrow layouts when two columns are showing.
  enterprise_managed_view_->SetNarrowLayout(two_columns);
  if (channel_view_) {
    channel_view_->SetNarrowLayout(two_columns);
  }
  if (eol_notice_) {
    eol_notice_->SetNarrowLayout(two_columns);
  }
  if (extended_updates_notice_) {
    extended_updates_notice_->SetNarrowLayout(two_columns);
  }
}

// static
void QuickSettingsHeader::ShowEnterpriseInfo(
    UnifiedSystemTrayController* controller,
    bool show_management_disclosure_dialog,
    bool is_user_session_blocked,
    bool has_enterprise_domain_manager) {
  quick_settings_metrics_util::RecordQsButtonActivated(
      QsButtonCatalogName::kManagedButton);
  // Show the new disclosure when on the login/lock screen and feature is
  // enabled.
  if (show_management_disclosure_dialog && is_user_session_blocked &&
      has_enterprise_domain_manager) {
    LockScreen::Get()->ShowManagementDisclosureDialog();
  } else {
    controller->HandleEnterpriseInfoAction();
  }
}

BEGIN_METADATA(QuickSettingsHeader)
END_METADATA

}  // namespace ash