File: extensions_menu_main_page_view.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (784 lines) | stat: -rw-r--r-- 35,924 bytes parent folder | download
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
// 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 "chrome/browser/ui/views/extensions/extensions_menu_main_page_view.h"

#include <memory>
#include <string>

#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/metrics/user_metrics.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/extensions/extension_action_view_controller.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/browser/ui/views/extensions/extensions_dialogs_utils.h"
#include "chrome/browser/ui/views/extensions/extensions_menu_handler.h"
#include "chrome/browser/ui/views/extensions/extensions_menu_item_view.h"
#include "chrome/grit/generated_resources.h"
#include "components/vector_icons/vector_icons.h"
#include "extensions/common/extension_id.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/text_constants.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/bubble/tooltip_icon.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/image_button_factory.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/button/toggle_button.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/separator.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/flex_layout_view.h"
#include "ui/views/layout/layout_types.h"
#include "ui/views/style/typography.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#include "extensions/common/extension_urls.h"
#endif

namespace {

// Updates the `toggle_button` text based on its state.
std::u16string GetSiteSettingToggleText(bool is_on) {
  int label_id = is_on ? IDS_EXTENSIONS_MENU_SITE_SETTINGS_TOGGLE_ON_TOOLTIP
                       : IDS_EXTENSIONS_MENU_SITE_SETTINGS_TOGGLE_OFF_TOOLTIP;
  return l10n_util::GetStringUTF16(label_id);
}

// Converts a view to a ExtensionMenuItemView. This cannot be used to
// *determine* if a view is an ExtensionMenuItemView (it should only be used
// when the view is known to be one). It is only used as an extra measure to
// prevent bad static casts.
ExtensionMenuItemView* GetAsMenuItem(views::View* view) {
  DCHECK(views::IsViewClass<ExtensionMenuItemView>(view));
  return views::AsViewClass<ExtensionMenuItemView>(view);
}

// Returns the ExtensionMenuItemView corresponding to `action_id` if
// it is a children of `parent_view`. The children of the parent view must be
// ExtensionMenuItemView, otherwise it will DCHECK.
ExtensionMenuItemView* GetMenuItem(
    views::View* parent_view,
    const ToolbarActionsModel::ActionId& action_id) {
  for (auto* view : parent_view->children()) {
    auto* item_view = GetAsMenuItem(view);
    if (item_view->view_controller()->GetId() == action_id) {
      return item_view;
    }
  }
  return nullptr;
}

}  // namespace

// View that contains a special message inside the extensions menu main page
// depending on its state.
class MessageSection : public views::BoxLayoutView {
 public:
  // The type of view container in the message section. At most one of this is
  // visible at all times.
  enum class ContainerType {
    // Container with a label.
    kTextContainer,
    // Container with labels and reload button.
    kReloadContainer,
    // Container with extensions requesting access.
    kRequestsAccessContainer
  };

  MessageSection(base::RepeatingCallback<void()> reload_callback,
                 base::RepeatingCallback<void(const extensions::ExtensionId&)>
                     allow_callback,
                 base::RepeatingCallback<void(const extensions::ExtensionId&)>
                     dismiss_callback);
  MessageSection(const MessageSection&) = delete;
  const MessageSection& operator=(const MessageSection&) = delete;
  ~MessageSection() override = default;

  // Updates the views contents and visibility given `state` and
  // `has_enterprise_extensions`.
  void Update(ExtensionsMenuMainPageView::MessageSectionState state,
              bool has_enterprise_extensions);

  // Adds an entry in `extensions_container_` for the extension with `id`,
  // `name` and `icon` at `index`. If the extension is already present, it
  // updates the entry. Shows the sections if it's the first extension entry.
  // Note that `state_` must be `kUserCustomizedAccess`.
  void AddOrUpdateExtension(const extensions::ExtensionId& id,
                            const std::u16string& name,
                            const ui::ImageModel& icon,
                            int index);

  // Removes the entry corresponding to `id`, if existent. Hides the section if
  // no extension entries are remaining. Note that `state_` must be
  // `kUserCustomizedAccess`.
  void RemoveExtension(const extensions::ExtensionId& id);

  // Accessors used by tests:
  views::View* GetTextContainerForTesting() { return text_container_; }
  views::View* GetReloadContainerForTesting() { return reload_container_; }
  views::View* GetRequestsAccessContainerForTesting() {
    return requests_access_container_;
  }
  std::vector<extensions::ExtensionId> GetExtensionsForTesting();
  views::View* GetExtensionEntryForTesting(
      const extensions::ExtensionId& extension_id);

 private:
  static constexpr int kTextContainerTextIndex = 0;
  static constexpr int kTextContainerTooltipIconIndex = 1;
  static constexpr int kReloadContainerMainTextIndex = 0;
  static constexpr int kExtensionItemsContainerIndex = 1;
  static constexpr int kExtensionItemIconIndex = 0;
  static constexpr int kExtensionItemLabelIndex = 1;

  // Removes all extension entries.
  void ClearExtensions();

  // Updates the visibility of the view based on `state_` and
  // `extension_entries_`.
  void UpdateVisibility();

  // Updates the containers visibility and content given a `container_type`,
  // `label_id` and `show_label_tooltip`. At most only one of the "containers"
  // will be visible.
  void UpdateContainer(ContainerType container_type,
                       int label_id = -1,
                       bool show_label_tooltip = false);

  // The current state of the section.
  ExtensionsMenuMainPageView::MessageSectionState state_;

  // Text container.
  raw_ptr<views::View> text_container_;

  // Reload container.
  raw_ptr<views::View> reload_container_;
  // Callback for the reload button in `reload_container_`.
  base::RepeatingCallback<void()> reload_callback_;

  // Request access container
  raw_ptr<views::View> requests_access_container_;
  // A collection of all the extension entries in the request access container.
  std::map<extensions::ExtensionId, views::View*> extension_entries_;

  // Callback for the buttons in the extension entries.
  base::RepeatingCallback<void(const extensions::ExtensionId&)> allow_callback_;
  base::RepeatingCallback<void(const extensions::ExtensionId&)>
      dismiss_callback_;
};

BEGIN_VIEW_BUILDER(/* No Export */, MessageSection, views::BoxLayoutView)
END_VIEW_BUILDER

DEFINE_VIEW_BUILDER(/* No Export */, MessageSection)

MessageSection::MessageSection(
    base::RepeatingCallback<void()> reload_callback,
    base::RepeatingCallback<void(const extensions::ExtensionId&)>
        allow_callback,
    base::RepeatingCallback<void(const extensions::ExtensionId&)>
        dismiss_callback)
    : reload_callback_(std::move(reload_callback)),
      allow_callback_(std::move(allow_callback)),
      dismiss_callback_(std::move(dismiss_callback)) {
  auto* layout_provider = ChromeLayoutProvider::Get();
  const int section_vertical_margin = layout_provider->GetDistanceMetric(
      DISTANCE_UNRELATED_CONTROL_VERTICAL_LARGE);
  const int section_horizontal_margin = layout_provider->GetDistanceMetric(
      DISTANCE_UNRELATED_CONTROL_HORIZONTAL_LARGE);
  const int control_vertical_margin = layout_provider->GetDistanceMetric(
      DISTANCE_RELATED_CONTROL_VERTICAL_SMALL);

  views::Builder<MessageSection>(this)
      .SetOrientation(views::BoxLayout::Orientation::kVertical)
      // TODO(crbug.com/1390952): After adding margins, compute radius from a
      // variable or create a const variable.
      .SetBackground(views::CreateThemedRoundedRectBackground(
          kColorExtensionsMenuHighlightedBackground, 4))
      .SetInsideBorderInsets(
          gfx::Insets::VH(section_vertical_margin, section_horizontal_margin))
      .AddChildren(
          // Text container.
          views::Builder<views::FlexLayoutView>()
              .CopyAddressTo(&text_container_)
              .SetVisible(false)
              .SetOrientation(views::LayoutOrientation::kHorizontal)
              .SetMainAxisAlignment(views::LayoutAlignment::kCenter)
              .AddChildren(
                  // Main text.
                  views::Builder<views::Label>()
                      .SetTextContext(
                          ChromeTextContext::CONTEXT_DIALOG_BODY_TEXT_SMALL)
                      .SetHorizontalAlignment(gfx::ALIGN_CENTER),
                  // Enterprise info tooltip.
                  views::Builder<views::TooltipIcon>(
                      std::make_unique<
                          views::TooltipIcon>(l10n_util::GetStringUTF16(
                          IDS_EXTENSIONS_MENU_MESSAGE_SECTION_ENTERPRISE_TOOLTIP_ICON_TEXT)))
                      .SetBubbleWidth(layout_provider->GetDistanceMetric(
                          views::DISTANCE_BUBBLE_PREFERRED_WIDTH))
                      .SetAnchorPointArrow(
                          views::BubbleBorder::Arrow::TOP_LEFT)),
          // Reload container.
          views::Builder<views::BoxLayoutView>()
              .CopyAddressTo(&reload_container_)
              .SetVisible(false)
              .SetOrientation(views::BoxLayout::Orientation::kVertical)
              .SetCrossAxisAlignment(
                  views::BoxLayout::CrossAxisAlignment::kCenter)
              .AddChildren(
                  // Main text.
                  views::Builder<views::Label>()
                      // Text will be set based on the `state_`.
                      .SetTextContext(
                          ChromeTextContext::CONTEXT_DIALOG_BODY_TEXT_SMALL)
                      .SetTextStyle(views::style::STYLE_EMPHASIZED),
                  // Description text.
                  views::Builder<views::Label>()
                      .SetText(l10n_util::GetStringUTF16(
                          IDS_EXTENSIONS_MENU_MESSAGE_SECTION_RELOAD_CONTAINER_DESCRIPTION_TEXT))
                      .SetTextContext(
                          ChromeTextContext::CONTEXT_DIALOG_BODY_TEXT_SMALL)
                      .SetMultiLine(true),
                  // Reload button.
                  views::Builder<views::MdTextButton>()
                      .SetCallback(base::BindRepeating(reload_callback_))
                      .SetProperty(
                          views::kMarginsKey,
                          gfx::Insets::TLBR(control_vertical_margin, 0, 0, 0))
                      .SetText(l10n_util::GetStringUTF16(
                          IDS_EXTENSIONS_MENU_MESSAGE_SECTION_RELOAD_CONTAINER_BUTTON_TEXT))
                      .SetTooltipText(l10n_util::GetStringUTF16(
                          IDS_EXTENSIONS_MENU_MESSAGE_SECTION_RELOAD_CONTAINER_BUTTON_TOOLTIP))),
          // Requests access container.
          views::Builder<views::BoxLayoutView>()
              .CopyAddressTo(&requests_access_container_)
              .SetVisible(false)
              .SetOrientation(views::BoxLayout::Orientation::kVertical)
              .AddChildren(
                  // Header.
                  views::Builder<views::Label>()
                      .SetText(l10n_util::GetStringUTF16(
                          IDS_EXTENSIONS_MENU_REQUESTS_ACCESS_SECTION_TITLE))
                      .SetTextContext(
                          ChromeTextContext::CONTEXT_DIALOG_BODY_TEXT_SMALL)
                      .SetTextStyle(views::style::STYLE_EMPHASIZED)
                      .SetHorizontalAlignment(gfx::ALIGN_LEFT),
                  // Empty container for the extensions requesting access.
                  views::Builder<views::BoxLayoutView>().SetOrientation(
                      views::BoxLayout::Orientation::kVertical)))
      .BuildChildren();
}

void MessageSection::Update(
    ExtensionsMenuMainPageView::MessageSectionState state,
    bool has_enterprise_extensions) {
  state_ = state;
  ContainerType container_type;
  int label_id;
  bool show_label_tooltip;

  switch (state_) {
    case ExtensionsMenuMainPageView::MessageSectionState::kRestrictedAccess:
      container_type = ContainerType::kTextContainer;
      label_id = IDS_EXTENSIONS_MENU_MESSAGE_SECTION_RESTRICTED_ACCESS_TEXT;
      show_label_tooltip = false;
      break;
    case ExtensionsMenuMainPageView::MessageSectionState::kUserCustomizedAccess:
      container_type = ContainerType::kRequestsAccessContainer;
      // This state has a static label, thus we don't need to pass a label id.
      label_id = -1;
      show_label_tooltip = false;
      break;
    case ExtensionsMenuMainPageView::MessageSectionState::
        kUserCustomizedAccessReload:
      container_type = ContainerType::kReloadContainer;
      label_id =
          IDS_EXTENSIONS_MENU_MESSAGE_SECTION_USER_CUSTOMIZED_ACCESS_TEXT;
      show_label_tooltip = false;
      break;
    case ExtensionsMenuMainPageView::MessageSectionState::kUserBlockedAccess:
      container_type = ContainerType::kTextContainer;
      label_id = IDS_EXTENSIONS_MENU_MESSAGE_SECTION_USER_BLOCKED_ACCESS_TEXT;
      // Tooltip can only be visible on this state, and if there are any
      // enterprise extensions installed.
      show_label_tooltip = has_enterprise_extensions;
      break;
    case ExtensionsMenuMainPageView::MessageSectionState::
        kUserBlockedAccessReload:
      container_type = ContainerType::kReloadContainer;
      label_id = IDS_EXTENSIONS_MENU_MESSAGE_SECTION_USER_BLOCKED_ACCESS_TEXT;
      show_label_tooltip = false;
      break;
  }

  UpdateContainer(container_type, label_id, show_label_tooltip);
  UpdateVisibility();
}

void MessageSection::UpdateContainer(ContainerType container_type,
                                     int label_id,
                                     bool show_label_tooltip) {
  switch (container_type) {
    case ContainerType::kTextContainer:
      DCHECK_NE(label_id, -1);
      text_container_->SetVisible(true);
      reload_container_->SetVisible(false);
      requests_access_container_->SetVisible(false);
      views::AsViewClass<views::Label>(
          text_container_->children()[kTextContainerTextIndex])
          ->SetText(l10n_util::GetStringUTF16(label_id));
      text_container_->children()[kTextContainerTooltipIconIndex]->SetVisible(
          show_label_tooltip);
      ClearExtensions();
      break;
    case ContainerType::kReloadContainer:
      DCHECK_NE(label_id, -1);
      text_container_->SetVisible(false);
      reload_container_->SetVisible(true);
      requests_access_container_->SetVisible(false);
      views::AsViewClass<views::Label>(
          reload_container_->children()[kReloadContainerMainTextIndex])
          ->SetText(l10n_util::GetStringUTF16(label_id));
      break;
    case ContainerType::kRequestsAccessContainer:
      DCHECK_EQ(label_id, -1);
      text_container_->SetVisible(false);
      reload_container_->SetVisible(false);
      requests_access_container_->SetVisible(!extension_entries_.empty());
      break;
  }
}

void MessageSection::AddOrUpdateExtension(const extensions::ExtensionId& id,
                                          const std::u16string& name,
                                          const ui::ImageModel& icon,
                                          int index) {
  CHECK_EQ(
      state_,
      ExtensionsMenuMainPageView::MessageSectionState::kUserCustomizedAccess);
  auto extension_iter = extension_entries_.find(id);

  if (extension_iter == extension_entries_.end()) {
    // Add new extension entry.
    auto* layout_provider = ChromeLayoutProvider::Get();
    const int control_vertical_margin = layout_provider->GetDistanceMetric(
        DISTANCE_RELATED_CONTROL_VERTICAL_SMALL);
    const int related_control_horizontal_margin =
        layout_provider->GetDistanceMetric(
            DISTANCE_RELATED_LABEL_HORIZONTAL_LIST);

    auto item =
        views::Builder<views::FlexLayoutView>()
            .SetOrientation(views::LayoutOrientation::kHorizontal)
            .SetProperty(views::kMarginsKey,
                         gfx::Insets::TLBR(control_vertical_margin, 0, 0, 0))
            .AddChildren(
                views::Builder<views::ImageView>().SetImage(icon),
                views::Builder<views::Label>()
                    .SetText(name)
                    .SetHorizontalAlignment(gfx::ALIGN_LEFT)
                    .SetProperty(views::kFlexBehaviorKey,
                                 views::FlexSpecification(
                                     views::MinimumFlexSizeRule::kScaleToZero,
                                     views::MaximumFlexSizeRule::kUnbounded)),
                views::Builder<views::MdTextButton>()
                    .SetCallback(base::BindRepeating(dismiss_callback_, id))
                    .SetText(l10n_util::GetStringUTF16(
                        IDS_EXTENSIONS_MENU_REQUESTS_ACCESS_SECTION_DISMISS_BUTTON_TEXT))
                    .SetTooltipText(l10n_util::GetStringUTF16(
                        IDS_EXTENSIONS_MENU_REQUESTS_ACCESS_SECTION_DISMISS_BUTTON_TOOLTIP)),
                views::Builder<views::MdTextButton>()
                    .SetCallback(base::BindRepeating(allow_callback_, id))
                    .SetText(l10n_util::GetStringUTF16(
                        IDS_EXTENSIONS_MENU_REQUESTS_ACCESS_SECTION_ALLOW_BUTTON_TEXT))
                    .SetTooltipText(l10n_util::GetStringUTF16(
                        IDS_EXTENSIONS_MENU_REQUESTS_ACCESS_SECTION_ALLOW_BUTTON_TOOLTIP))
                    .SetProperty(
                        views::kMarginsKey,
                        gfx::Insets::TLBR(0, related_control_horizontal_margin,
                                          0, 0)))
            .Build();
    extension_entries_.insert({id, item.get()});
    requests_access_container_->children()[1]->AddChildViewAt(std::move(item),
                                                              index);
    requests_access_container_->SetVisible(!extension_entries_.empty());
    UpdateVisibility();
  } else {
    // Update extension entry.
    std::vector<View*> extension_items = extension_iter->second->children();
    views::AsViewClass<views::ImageView>(
        extension_items[kExtensionItemIconIndex])
        ->SetImage(icon);
    views::AsViewClass<views::Label>(extension_items[kExtensionItemLabelIndex])
        ->SetText(name);
    requests_access_container_->children()[kExtensionItemsContainerIndex]
        ->ReorderChildView(extension_iter->second, index);
  }
}

void MessageSection::RemoveExtension(const extensions::ExtensionId& id) {
  CHECK_EQ(
      state_,
      ExtensionsMenuMainPageView::MessageSectionState::kUserCustomizedAccess);
  auto extension_iter = extension_entries_.find(id);
  if (extension_iter == extension_entries_.end()) {
    return;
  }

  requests_access_container_->children()[kExtensionItemsContainerIndex]
      ->RemoveChildViewT(extension_iter->second);
  extension_entries_.erase(extension_iter);

  requests_access_container_->SetVisible(!extension_entries_.empty());
  UpdateVisibility();
}

void MessageSection::ClearExtensions() {
  requests_access_container_->children()[kExtensionItemsContainerIndex]
      ->RemoveAllChildViews();
  extension_entries_.clear();
}

void MessageSection::UpdateVisibility() {
  // Section is always visible unless state is "user customized access" and no
  // extension is requesting site access.
  bool is_visible = state_ == ExtensionsMenuMainPageView::MessageSectionState::
                                  kUserCustomizedAccess
                        ? !extension_entries_.empty()
                        : true;
  SetVisible(is_visible);
}

std::vector<extensions::ExtensionId> MessageSection::GetExtensionsForTesting() {
  std::vector<extensions::ExtensionId> extensions;
  extensions.reserve(extension_entries_.size());
  for (auto entry : extension_entries_) {
    extensions.push_back(entry.first);
  }
  return extensions;
}

views::View* MessageSection::GetExtensionEntryForTesting(
    const extensions::ExtensionId& extension_id) {
  auto iter = extension_entries_.find(extension_id);
  return iter == extension_entries_.end() ? nullptr : iter->second;
}

ExtensionsMenuMainPageView::ExtensionsMenuMainPageView(
    Browser* browser,
    ExtensionsMenuHandler* menu_handler)
    : browser_(browser), menu_handler_(menu_handler) {
  // This is set so that the extensions menu doesn't fall outside the monitor in
  // a maximized window in 1024x768. See https://crbug.com/1096630.
  // TODO(crbug.com/1413883): Consider making the height dynamic.
  constexpr int kMaxExtensionButtonsHeightDp = 448;
  views::FlexSpecification stretch_specification =
      views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToZero,
                               views::MaximumFlexSizeRule::kUnbounded,
                               /*adjust_height_for_width =*/true)
          .WithWeight(1);

  ChromeLayoutProvider* const chrome_layout_provider =
      ChromeLayoutProvider::Get();
  const int vertical_spacing = chrome_layout_provider->GetDistanceMetric(
      DISTANCE_UNRELATED_CONTROL_VERTICAL_LARGE);
  const int horizontal_spacing = chrome_layout_provider->GetDistanceMetric(
      DISTANCE_RELATED_CONTROL_HORIZONTAL_SMALL);
  // This value must be the same as the `HoverButton` vertical margin.
  const int hover_button_vertical_spacing =
      chrome_layout_provider->GetDistanceMetric(
          DISTANCE_CONTROL_LIST_VERTICAL) /
      2;
  const int icon_size = chrome_layout_provider->GetDistanceMetric(
      DISTANCE_EXTENSIONS_MENU_BUTTON_ICON_SIZE);

  views::LayoutProvider* layout_provider = views::LayoutProvider::Get();
  const gfx::Insets dialog_insets =
      layout_provider->GetInsetsMetric(views::InsetsMetric::INSETS_DIALOG);

  // Views that need configuration after construction (e.g access size after a
  // separate view is constructed).
  views::Label* subheader_title;

  views::Builder<ExtensionsMenuMainPageView>(this)
      .SetLayoutManager(std::make_unique<views::BoxLayout>(
          views::BoxLayout::Orientation::kVertical))
      // TODO(crbug.com/1390952): Add margins after adding the menu
      // items, to make sure all items are aligned.
      .AddChildren(
          // Subheader section.
          views::Builder<views::FlexLayoutView>()
              .SetCrossAxisAlignment(views::LayoutAlignment::kCenter)
              // Add top dialog margins, since its the first element, and
              // horizontal dialog margins. Bottom margin will be added by the
              // next view (in general, vertical margins should be added by the
              // bottom view).
              .SetInteriorMargin(gfx::Insets::TLBR(dialog_insets.top(),
                                                   dialog_insets.left(), 0,
                                                   dialog_insets.right()))
              .SetProperty(views::kFlexBehaviorKey, stretch_specification)
              .SetVisible(true)
              .AddChildren(
                  views::Builder<views::FlexLayoutView>()
                      .SetOrientation(views::LayoutOrientation::kVertical)
                      .SetCrossAxisAlignment(views::LayoutAlignment::kStretch)
                      .SetProperty(views::kFlexBehaviorKey,
                                   stretch_specification)
                      .AddChildren(
                          views::Builder<views::Label>()
                              .CopyAddressTo(&subheader_title)
                              .SetText(l10n_util::GetStringUTF16(
                                  IDS_EXTENSIONS_MENU_TITLE))
                              .SetHorizontalAlignment(gfx::ALIGN_LEFT)
                              .SetTextContext(
                                  views::style::CONTEXT_DIALOG_TITLE)
                              .SetTextStyle(views::style::STYLE_SECONDARY),
                          views::Builder<views::Label>()
                              .CopyAddressTo(&subheader_subtitle_)
                              .SetHorizontalAlignment(gfx::ALIGN_LEFT)
                              .SetTextContext(views::style::CONTEXT_LABEL)
                              .SetTextStyle(views::style::STYLE_SECONDARY)
                              .SetAllowCharacterBreak(true)
                              .SetMultiLine(true)
                              .SetProperty(views::kFlexBehaviorKey,
                                           stretch_specification)),
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
                  views::Builder<views::ImageButton>(
                      views::CreateVectorImageButtonWithNativeTheme(
                          base::BindRepeating(
                              &chrome::ShowWebStore, browser_,
                              extension_urls::kExtensionsMenuUtmSource),
                          vector_icons::kGoogleChromeWebstoreIcon, icon_size))
                      .SetAccessibleName(l10n_util::GetStringUTF16(
                          IDS_EXTENSIONS_MENU_MAIN_PAGE_OPEN_CHROME_WEBSTORE_ACCESSIBLE_NAME))
                      .CustomConfigure(
                          base::BindOnce([](views::ImageButton* view) {
                            view->SizeToPreferredSize();
                            InstallCircleHighlightPathGenerator(view);
                          })),
#endif
                  // Setting button.
                  views::Builder<views::ImageButton>(
                      views::CreateVectorImageButtonWithNativeTheme(
                          base::BindRepeating(
                              [](Browser* browser) {
                                base::RecordAction(base::UserMetricsAction(
                                    "Extensions.Menu."
                                    "ExtensionsSettingsOpened"));
                                chrome::ShowExtensions(browser);
                              },
                              browser_),
                          features::IsChromeRefresh2023()
                              ? vector_icons::kSettingsChromeRefreshIcon
                              : vector_icons::kSettingsIcon,
                          icon_size))
                      .SetProperty(
                          views::kMarginsKey,
                          gfx::Insets::TLBR(0, horizontal_spacing, 0, 0))
                      .SetTooltipText(
                          l10n_util::GetStringUTF16(IDS_MANAGE_EXTENSIONS))
                      .CustomConfigure(
                          base::BindOnce([](views::ImageButton* view) {
                            view->SizeToPreferredSize();
                            InstallCircleHighlightPathGenerator(view);
                          })),
                  // Toggle site settings button.
                  views::Builder<views::ToggleButton>()
                      .CopyAddressTo(&site_settings_toggle_)
                      .SetProperty(
                          views::kMarginsKey,
                          gfx::Insets::TLBR(0, horizontal_spacing, 0, 0))
                      .SetAccessibleName(l10n_util::GetStringUTF16(
                          IDS_EXTENSIONS_MENU_SITE_SETTINGS_TOGGLE_ACCESSIBLE_NAME))
                      .SetCallback(base::BindRepeating(
                          [](views::ToggleButton* toggle_button,
                             base::RepeatingCallback<void(bool)> callback) {
                            callback.Run(toggle_button->GetIsOn());
                          },
                          site_settings_toggle_,
                          base::BindRepeating(
                              &ExtensionsMenuHandler::
                                  OnSiteSettingsToggleButtonPressed,
                              base::Unretained(menu_handler))))),
          // Contents.
          views::Builder<views::Separator>().SetProperty(
              views::kMarginsKey, gfx::Insets::VH(vertical_spacing, 0)),
          views::Builder<views::ScrollView>()
              .ClipHeightTo(0, kMaxExtensionButtonsHeightDp)
              .SetDrawOverflowIndicator(false)
              .SetHorizontalScrollBarMode(
                  views::ScrollView::ScrollBarMode::kDisabled)
              .SetContents(
                  views::Builder<views::BoxLayoutView>()
                      .SetOrientation(views::BoxLayout::Orientation::kVertical)
                      // Horizontal dialog margins are added inside the scroll
                      // view contents to have the scroll bar by the dialog
                      // border.
                      .SetInsideBorderInsets(
                          gfx::Insets::VH(0, dialog_insets.left()))
                      .AddChildren(
                          // Message section.
                          views::Builder<MessageSection>(
                              std::make_unique<MessageSection>(
                                  base::BindRepeating(
                                      &ExtensionsMenuHandler::
                                          OnReloadPageButtonClicked,
                                      base::Unretained(menu_handler_)),
                                  base::BindRepeating(
                                      &ExtensionsMenuHandler::
                                          OnAllowExtensionClicked,
                                      base::Unretained(menu_handler_)),
                                  base::BindRepeating(
                                      &ExtensionsMenuHandler::
                                          OnDismissExtensionClicked,
                                      base::Unretained(menu_handler_))))
                              .CopyAddressTo(&message_section_),
                          // Menu items section.
                          views::Builder<views::BoxLayoutView>()
                              .CopyAddressTo(&menu_items_)
                              // Add bottom dialog margins since it's the last
                              // element.
                              .SetProperty(
                                  views::kMarginsKey,
                                  gfx::Insets::TLBR(
                                      0, 0,
                                      dialog_insets.bottom() -
                                          hover_button_vertical_spacing,
                                      0))
                              .SetOrientation(
                                  views::BoxLayout::Orientation::kVertical))))

      .BuildChildren();

  // Align the site setting toggle vertically with the subheader title by
  // getting the label height after construction.
  site_settings_toggle_->SetPreferredSize(
      gfx::Size(site_settings_toggle_->GetPreferredSize().width(),
                subheader_title->GetLineHeight()));
}

void ExtensionsMenuMainPageView::CreateAndInsertMenuItem(
    std::unique_ptr<ExtensionActionViewController> action_controller,
    extensions::ExtensionId extension_id,
    bool is_enterprise,
    ExtensionMenuItemView::SiteAccessToggleState site_access_toggle_state,
    ExtensionMenuItemView::SitePermissionsButtonState
        site_permissions_button_state,
    ExtensionMenuItemView::SitePermissionsButtonAccess
        site_permissions_button_access,
    int index) {
  // base::Unretained() below is safe because `menu_handler_` lifetime is
  // tied to this view lifetime by the extensions menu coordinator.
  auto item = std::make_unique<ExtensionMenuItemView>(
      browser_, is_enterprise, std::move(action_controller),
      base::BindRepeating(&ExtensionsMenuHandler::OnExtensionToggleSelected,
                          base::Unretained(menu_handler_), extension_id),
      base::BindRepeating(&ExtensionsMenuHandler::OpenSitePermissionsPage,
                          base::Unretained(menu_handler_), extension_id));
  item->Update(site_access_toggle_state, site_permissions_button_state,
               site_permissions_button_access);
  menu_items_->AddChildViewAt(std::move(item), index);
}

void ExtensionsMenuMainPageView::RemoveMenuItem(
    const ToolbarActionsModel::ActionId& action_id) {
  views::View* item = GetMenuItem(menu_items_, action_id);
  menu_items_->RemoveChildViewT(item);
}

void ExtensionsMenuMainPageView::UpdateSubheader(
    const std::u16string& current_site,
    bool is_site_settings_toggle_visible,
    bool is_site_settings_toggle_on) {
  subheader_subtitle_->SetText(current_site);

  site_settings_toggle_->SetVisible(is_site_settings_toggle_visible);
  site_settings_toggle_->SetIsOn(is_site_settings_toggle_on);
  site_settings_toggle_->SetTooltipText(
      GetSiteSettingToggleText(is_site_settings_toggle_on));
}

void ExtensionsMenuMainPageView::UpdateMessageSection(
    MessageSectionState state,
    bool has_enterprise_extensions) {
  message_section_->Update(state, has_enterprise_extensions);
}

void ExtensionsMenuMainPageView::AddOrUpdateExtensionRequestingAccess(
    const extensions::ExtensionId& id,
    const std::u16string& name,
    const ui::ImageModel& icon,
    int index) {
  message_section_->AddOrUpdateExtension(id, name, icon, index);
}

void ExtensionsMenuMainPageView::RemoveExtensionRequestingAccess(
    const extensions::ExtensionId& id) {
  message_section_->RemoveExtension(id);
  SizeToPreferredSize();
}

std::vector<ExtensionMenuItemView*> ExtensionsMenuMainPageView::GetMenuItems()
    const {
  std::vector<ExtensionMenuItemView*> menu_item_views;
  for (views::View* view : menu_items_->children()) {
    menu_item_views.push_back(GetAsMenuItem(view));
  }
  return menu_item_views;
}

const std::u16string&
ExtensionsMenuMainPageView::GetSubheaderSubtitleTextForTesting() const {
  return subheader_subtitle_->GetText();
}

views::View* ExtensionsMenuMainPageView::GetTextContainerForTesting() {
  return message_section_->GetTextContainerForTesting();  // IN-TEST
}

views::View* ExtensionsMenuMainPageView::GetReloadContainerForTesting() {
  return message_section_->GetReloadContainerForTesting();  // IN-TEST
}

views::View*
ExtensionsMenuMainPageView::GetRequestsAccessContainerForTesting() {
  return message_section_->GetRequestsAccessContainerForTesting();  // IN-TEST
}

std::vector<extensions::ExtensionId>
ExtensionsMenuMainPageView::GetExtensionsRequestingAccessForTesting() {
  return message_section_->GetExtensionsForTesting();  // IN-TEST
}

views::View*
ExtensionsMenuMainPageView::GetExtensionRequestingAccessEntryForTesting(
    const extensions::ExtensionId& extension_id) {
  return message_section_->GetExtensionEntryForTesting(
      extension_id);  // IN-TEST
}

content::WebContents* ExtensionsMenuMainPageView::GetActiveWebContents() const {
  return browser_->tab_strip_model()->GetActiveWebContents();
}

BEGIN_METADATA(ExtensionsMenuMainPageView, views::View)
END_METADATA