File: zoom_bubble_view.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (697 lines) | stat: -rw-r--r-- 25,571 bytes parent folder | download | duplicates (4)
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
// Copyright 2012 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/location_bar/zoom_bubble_view.h"

#include <cmath>
#include <memory>

#include "base/auto_reset.h"
#include "base/i18n/number_formatting.h"
#include "base/i18n/rtl.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_actions.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/browser/ui/views/page_action/page_action_view.h"
#include "chrome/browser/ui/views/page_action/zoom_view.h"
#include "chrome/browser/ui/views/zoom/zoom_view_controller.h"
#include "chrome/grit/generated_resources.h"
#include "components/zoom/page_zoom.h"
#include "components/zoom/zoom_controller.h"
#include "extensions/browser/extension_zoom_request_client.h"
#include "extensions/common/api/extension_action/action_info.h"
#include "extensions/common/manifest_handlers/icons_handler.h"
#include "extensions/grit/extensions_browser_resources.h"
#include "third_party/blink/public/common/page/page_zoom.h"
#include "ui/actions/actions.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/base/models/image_model.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/favicon_size.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/text_utils.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/accessibility/view_accessibility.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/highlight_path_generator.h"
#include "ui/views/controls/separator.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/widget/widget.h"

#if BUILDFLAG(IS_MAC)
#include "chrome/browser/ui/fullscreen_util_mac.h"
#endif

namespace {

// The default time that the bubble should stay on the screen if it will close
// automatically.
constexpr base::TimeDelta kBubbleCloseDelayDefault = base::Milliseconds(1500);

// A longer timeout used for how long the bubble should stay on the screen
// before it will close automatically after + or - buttons have been used.
constexpr base::TimeDelta kBubbleCloseDelayLong = base::Milliseconds(5000);

class ZoomButtonHighlightPathGenerator : public views::HighlightPathGenerator {
 public:
  ZoomButtonHighlightPathGenerator() = default;

  SkPath GetHighlightPath(const views::View* view) override {
    constexpr int kCircleRadiusDp = 24 / 2;
    const gfx::Point center = view->GetLocalBounds().CenterPoint();
    return SkPath().addCircle(center.x(), center.y(), kCircleRadiusDp);
  }
};

std::unique_ptr<views::ImageButton> CreateZoomButton(
    views::Button::PressedCallback callback,
    const gfx::VectorIcon& icon,
    int tooltip_id) {
  auto zoom_button =
      views::CreateVectorImageButtonWithNativeTheme(std::move(callback), icon);
  zoom_button->SetTooltipText(l10n_util::GetStringUTF16(tooltip_id));
  views::HighlightPathGenerator::Install(
      zoom_button.get(), std::make_unique<ZoomButtonHighlightPathGenerator>());
  return zoom_button;
}

class ZoomValue : public views::Label {
  METADATA_HEADER(ZoomValue, views::Label)

 public:
  explicit ZoomValue(const content::WebContents* web_contents)
      : Label(std::u16string(),
              views::style::CONTEXT_LABEL,
              views::style::STYLE_PRIMARY),
        max_width_(GetLabelMaxWidth(web_contents)) {
    SetHorizontalAlignment(gfx::ALIGN_LEFT);
  }
  ZoomValue(const ZoomValue&) = delete;
  ZoomValue& operator=(const ZoomValue&) = delete;
  ~ZoomValue() override = default;

  // views::Label:
  gfx::Size CalculatePreferredSize(
      const views::SizeBounds& available_size) const override {
    gfx::Size size =
        views::Label::CalculatePreferredSize(views::SizeBounds(max_width_, {}));
    // When the initial value of the text width is small(eg: 80%), the
    // `ZoomBubbleView` will be smaller. Then after we use a larger value(eg:
    // 200%), the text will not be fully displayed. It needs to be set to the
    // maximum value to ensure that the size of `ZoomBubbleView` is fixed.
    size.set_width(max_width_);
    return size;
  }

 private:
  int GetLabelMaxWidth(const content::WebContents* web_contents) const {
    const int border_width = GetInsets().width();
    int max_w = 0;
    auto* zoom_controller = zoom::ZoomController::FromWebContents(web_contents);
    DCHECK(zoom_controller);
    // Enumerate all zoom factors that can be used in PageZoom::Zoom.
    std::vector<double> zoom_factors =
        zoom::PageZoom::PresetZoomFactors(zoom_controller->GetZoomPercent());
    for (auto zoom : zoom_factors) {
      int w = gfx::GetStringWidth(
          base::FormatPercent(static_cast<int>(std::round(zoom * 100))),
          font_list());
      max_w = std::max(w, max_w);
    }
    return max_w + border_width;
  }

  const int max_width_;
};

BEGIN_METADATA(ZoomValue)
END_METADATA

bool IsBrowserFullscreen(Browser* browser) {
  DCHECK(browser->window() && browser->GetFeatures()
                                  .exclusive_access_manager()
                                  ->fullscreen_controller());
  return browser->window()->IsFullscreen();
}

views::View* GetAnchorViewForBrowser(Browser* browser) {
  BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
#if BUILDFLAG(IS_MAC)
  if (fullscreen_utils::IsInContentFullscreen(browser)) {
    return nullptr;
  }
#endif
  if (!IsBrowserFullscreen(browser) || browser_view->IsToolbarVisible() ||
      browser_view->immersive_mode_controller()->IsRevealed()) {
    // TODO(crbug.com/376284060): An action ID should be created and used here
    // when Zoom is migrated to the new page actions framework.
    return browser_view->toolbar_button_provider()->GetAnchorView(std::nullopt);
  }
  return nullptr;
}

ImmersiveModeController* GetImmersiveModeControllerForBrowser(
    Browser* browser) {
  BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
  return browser_view->immersive_mode_controller();
}

void ParentToBrowser(Browser* browser,
                     ZoomBubbleView* zoom_bubble,
                     views::View* anchor_view,
                     content::WebContents* web_contents) {
  BrowserView* const browser_view =
      BrowserView::GetBrowserViewForBrowser(browser);
  views::Button* button;
  if (IsPageActionMigrated(PageActionIconType::kZoom)) {
    button = browser_view->toolbar_button_provider()->GetPageActionView(
        kActionZoomNormal);
  } else {
    button = browser_view->toolbar_button_provider()->GetPageActionIconView(
        PageActionIconType::kZoom);
  }

  zoom_bubble->SetHighlightedButton(button);

  // If we don't anchor to anything the BrowserView is our parent. This happens
  // in fullscreen cases.
  zoom_bubble->set_parent_window(
      zoom_bubble->anchor_widget()
          ? gfx::NativeView()
          : browser_view->GetWidget()->GetNativeView());

  views::BubbleDialogDelegateView::CreateBubble(zoom_bubble);
}

// Find the extension that initiated the zoom change, if any.
const extensions::ExtensionZoomRequestClient* GetExtensionZoomRequestClient(
    const content::WebContents* web_contents) {
  const zoom::ZoomController* zoom_controller =
      zoom::ZoomController::FromWebContents(web_contents);
  const zoom::ZoomRequestClient* client = zoom_controller->last_client();
  return static_cast<const extensions::ExtensionZoomRequestClient*>(client);
}

void UpdateBubbleVisibilityState(Browser* browser, bool is_bubble_visible) {
  if (!browser) {
    return;
  }

  auto* action_item = actions::ActionManager::Get().FindAction(
      kActionZoomNormal, browser->browser_actions()->root_action_item());
  CHECK(action_item);
  action_item->SetIsShowingBubble(is_bubble_visible);
}

}  // namespace

// static
ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = nullptr;

// static
void ZoomBubbleView::ShowBubble(content::WebContents* web_contents,
                                DisplayReason reason) {
  Browser* browser = chrome::FindBrowserWithTab(web_contents);
  // |web_contents| could have been unloaded if a tab gets closed and a mouse
  // event arrives before the zoom icon gets hidden.
  if (!browser) {
    return;
  }

  if (RefreshBubbleIfShowing(web_contents)) {
    return;
  }

  // If the bubble is already showing but in a different tab, the current
  // bubble must be closed and a new one created.
  CloseCurrentBubble();

  views::View* anchor_view = GetAnchorViewForBrowser(browser);
  ImmersiveModeController* immersive_mode_controller =
      GetImmersiveModeControllerForBrowser(browser);

  zoom_bubble_ = new ZoomBubbleView(anchor_view, web_contents, reason,
                                    immersive_mode_controller);

  const extensions::ExtensionZoomRequestClient* client =
      GetExtensionZoomRequestClient(web_contents);

  // If the zoom change was initiated by an extension, capture the relevent
  // information from it.
  if (client) {
    zoom_bubble_->SetExtensionInfo(client->extension());
  }

  ParentToBrowser(browser, zoom_bubble_, anchor_view, web_contents);

  if (!anchor_view && IsBrowserFullscreen(browser)) {
    zoom_bubble_->AdjustForFullscreen(browser->window()->GetBounds());
  }

  // Do not announce hotkey for refocusing inactive Zoom bubble as it
  // disappears after a short timeout.
  zoom_bubble_->ShowForReason(reason, /* allow_refocus_alert */ false);

  // Update the "bubble is showing" state before we refresh the icon so that
  // UpdateZoomIconVisibility() sees the correct value bubble state value.
  zoom_bubble_->UpdateZoomBubbleStateAndIconVisibility(
      /*is_bubble_visible=*/true);
}

// static
bool ZoomBubbleView::RefreshBubbleIfShowing(
    const content::WebContents* web_contents) {
  if (!CanRefresh(web_contents)) {
    return false;
  }

  DCHECK_EQ(web_contents, zoom_bubble_->web_contents());
  zoom_bubble_->Refresh();

  return true;
}

// static
bool ZoomBubbleView::CanRefresh(const content::WebContents* web_contents) {
  // Can't refresh when there's not already a bubble for this tab.
  if (!zoom_bubble_ || (zoom_bubble_->web_contents() != web_contents)) {
    return false;
  }

  Browser* browser = chrome::FindBrowserWithTab(web_contents);
  if (!browser ||
      (zoom_bubble_->GetAnchorView() != GetAnchorViewForBrowser(browser))) {
    return false;
  }

  const extensions::ExtensionZoomRequestClient* client =
      GetExtensionZoomRequestClient(web_contents);

  // Allow refreshes when the client won't create its own bubble; otherwise
  // the existing bubble would show the wrong zoom value.
  if (client && client->ShouldSuppressBubble()) {
    return true;
  }

  // Allow refreshes when the existing bubble has the same attribution for
  // the zoom change, so only the label needs updating.
  return zoom_bubble_->extension_info_.id ==
         (client ? client->extension()->id() : std::string());
}

// static
void ZoomBubbleView::CloseCurrentBubble() {
  if (zoom_bubble_) {
    zoom_bubble_->CloseBubble();
  }
}

// static
ZoomBubbleView* ZoomBubbleView::GetZoomBubble() {
  return zoom_bubble_;
}

void ZoomBubbleView::Refresh() {
  UpdateZoomPercent();
  StartTimerIfNecessary();
}

ZoomBubbleView::ZoomBubbleView(
    views::View* anchor_view,
    content::WebContents* web_contents,
    DisplayReason reason,
    ImmersiveModeController* immersive_mode_controller)
    : LocationBarBubbleDelegateView(anchor_view, web_contents),
      auto_close_duration_(kBubbleCloseDelayDefault),
      auto_close_(reason == AUTOMATIC),
      immersive_mode_controller_(immersive_mode_controller),
      session_id_(chrome::FindBrowserWithTab(web_contents)->session_id()) {
  SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));

  SetNotifyEnterExitOnChild(true);
  if (immersive_mode_controller_) {
    immersive_mode_controller_->AddObserver(this);
  }
  UseCompactMargins();
}

ZoomBubbleView::~ZoomBubbleView() {
  if (immersive_mode_controller_) {
    immersive_mode_controller_->RemoveObserver(this);
  }
}

std::u16string ZoomBubbleView::GetAccessibleWindowTitle() const {
  Browser* browser = GetBrowser();
  if (!browser) {
    return {};
  }

  ToolbarButtonProvider* provider =
      BrowserView::GetBrowserViewForBrowser(browser)->toolbar_button_provider();

  if (IsPageActionMigrated(PageActionIconType::kZoom)) {
    return provider->GetPageActionView(kActionZoomNormal)->GetAccessibleName();
  }

  return provider->GetPageActionIconView(PageActionIconType::kZoom)
      ->GetTextForTooltipAndAccessibleName();
}

void ZoomBubbleView::OnFocus() {
  LocationBarBubbleDelegateView::OnFocus();
  StopTimer();
}

void ZoomBubbleView::OnBlur() {
  LocationBarBubbleDelegateView::OnBlur();

  const views::FocusManager* focus_manager = GetFocusManager();
  if (focus_manager && Contains(focus_manager->GetFocusedView())) {
    return;
  }

  StartTimerIfNecessary();
}

void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) {
  if (!zoom_bubble_ || !zoom_bubble_->auto_close_ ||
      event->type() != ui::EventType::kGestureTap) {
    return;
  }

  auto_close_ = false;
  StopTimer();
  event->SetHandled();
}

void ZoomBubbleView::OnKeyEvent(ui::KeyEvent* event) {
  if (!zoom_bubble_ || !zoom_bubble_->auto_close_) {
    return;
  }

  const views::FocusManager* focus_manager = GetFocusManager();
  if (focus_manager && Contains(focus_manager->GetFocusedView())) {
    StopTimer();
  } else {
    StartTimerIfNecessary();
  }
}

void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) {
  StopTimer();
}

void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) {
  StartTimerIfNecessary();
}

void ZoomBubbleView::Init() {
  // Set up the layout of the zoom bubble.
  constexpr int kPercentLabelPadding = 64;
  const ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();
  const int spacing =
      provider->GetDistanceMetric(views::DISTANCE_UNRELATED_CONTROL_HORIZONTAL);
  gfx::Insets inset_border_insets =
      provider->GetInsetsMetric(INSETS_TOAST) - margins();
  inset_border_insets.set_top_bottom(0, 0);
  auto box_layout = std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal, inset_border_insets, spacing);
  box_layout->set_main_axis_alignment(
      views::BoxLayout::MainAxisAlignment::kCenter);
  box_layout->set_cross_axis_alignment(
      views::BoxLayout::CrossAxisAlignment::kCenter);
  SetLayoutManager(std::move(box_layout));

  // Calculate child views margins in |this| client view.
  const int label_vertical_spacing =
      provider->GetDistanceMetric(DISTANCE_TOAST_LABEL_VERTICAL);
  const auto label_margin =
      gfx::Insets::TLBR(label_vertical_spacing - margins().top(), 0,
                        label_vertical_spacing - margins().bottom(),
                        kPercentLabelPadding - spacing);

  // Account for the apparent margins that vector buttons have around icons.
  const int control_vertical_spacing =
      provider->GetDistanceMetric(DISTANCE_TOAST_CONTROL_VERTICAL);
  const auto control_vertical_margin =
      gfx::Insets::TLBR(control_vertical_spacing - margins().top(), 0,
                        control_vertical_spacing - margins().bottom(), 0);
  const gfx::Insets vector_button_margin(
      control_vertical_margin -
      provider->GetInsetsMetric(views::INSETS_VECTOR_IMAGE_BUTTON));

  const auto button_pressed_callback = [this](base::RepeatingClosure closure) {
    return base::BindRepeating(&ZoomBubbleView::ButtonPressed,
                               base::Unretained(this), std::move(closure));
  };

  // If this zoom change was initiated by an extension, that extension will be
  // attributed by showing its icon in the zoom bubble.
  if (extension_info_.icon_image) {
    auto image_button = std::make_unique<views::ImageButton>(
        button_pressed_callback(base::BindRepeating(
            &ZoomBubbleView::ImageButtonPressed, base::Unretained(this))));
    image_button->SetTooltipText(
        l10n_util::GetStringFUTF16(IDS_TOOLTIP_ZOOM_EXTENSION_ICON,
                                   base::UTF8ToUTF16(extension_info_.name)));
    image_button->SetImageModel(views::Button::STATE_NORMAL,
                                ui::ImageModel::FromImageSkia(
                                    extension_info_.icon_image->image_skia()));
    image_button_ = AddChildView(std::move(image_button));
  }

  // Add zoom label with the new zoom percent.
  auto label = std::make_unique<ZoomValue>(web_contents());
  label->SetProperty(views::kMarginsKey, gfx::Insets(label_margin));
  label->GetViewAccessibility().SetRole(ax::mojom::Role::kAlert);
  label_ = label.get();
  AddChildView(std::move(label));

  const auto zoom_callback = [button_pressed_callback,
                              web_contents =
                                  web_contents()](content::PageZoom zoom) {
    return button_pressed_callback(base::BindRepeating(
        &zoom::PageZoom::Zoom, base::Unretained(web_contents), zoom));
  };

  // Add Zoom Out ("-") button.
  zoom_out_button_ =
      AddChildView(CreateZoomButton(zoom_callback(content::PAGE_ZOOM_OUT),
                                    kRemoveIcon, IDS_ACCNAME_ZOOM_MINUS2));
  zoom_out_button_->SetProperty(views::kMarginsKey,
                                gfx::Insets(vector_button_margin));

  // Add Zoom In ("+") button.
  zoom_in_button_ = AddChildView(CreateZoomButton(
      zoom_callback(content::PAGE_ZOOM_IN), kAddIcon, IDS_ACCNAME_ZOOM_PLUS2));
  zoom_in_button_->SetProperty(views::kMarginsKey,
                               gfx::Insets(vector_button_margin));

  // Add "Reset" button.
  auto reset_button = std::make_unique<views::MdTextButton>(
      zoom_callback(content::PAGE_ZOOM_RESET),
      l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT));
  reset_button->SetTooltipText(
      l10n_util::GetStringUTF16(IDS_ACCNAME_ZOOM_SET_DEFAULT));
  reset_button_ = AddChildView(std::move(reset_button));

  UpdateZoomPercent();
  StartTimerIfNecessary();
}

void ZoomBubbleView::WindowClosing() {
  // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't
  // call this right away). Only set to nullptr when it's this bubble.
  bool this_bubble = zoom_bubble_ == this;
  if (this_bubble) {
    zoom_bubble_ = nullptr;
  }

  // Clear the "bubble is showing" state before we refresh the icon so that
  // UpdateZoomIconVisibility() sees the correct value bubble state value.
  UpdateZoomBubbleStateAndIconVisibility(/*is_bubble_visible=*/zoom_bubble_ !=
                                         nullptr);
}

void ZoomBubbleView::CloseBubble() {
  Browser* browser = GetBrowser();
  if (ignore_close_bubble_ && browser &&
      GetAnchorViewForBrowser(browser) == GetAnchorView()) {
    return;
  }

  // Widget's Close() is async, but we don't want to use zoom_bubble_ after
  // this. Additionally web_contents() may have been destroyed.
  zoom_bubble_ = nullptr;
  LocationBarBubbleDelegateView::CloseBubble();
}

void ZoomBubbleView::OnImmersiveRevealStarted() {
  CloseBubble();
}

void ZoomBubbleView::OnImmersiveModeControllerDestroyed() {
  immersive_mode_controller_ = nullptr;
}

void ZoomBubbleView::OnExtensionIconImageChanged(
    extensions::IconImage* /* image */) {
  image_button_->SetImageModel(
      views::Button::STATE_NORMAL,
      ui::ImageModel::FromImageSkia(extension_info_.icon_image->image_skia()));
  image_button_->SchedulePaint();
}

void ZoomBubbleView::SetExtensionInfo(const extensions::Extension* extension) {
  DCHECK(extension);
  extension_info_.id = extension->id();
  extension_info_.name = extension->name();

  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  const gfx::ImageSkia& default_extension_icon_image =
      *rb.GetImageSkiaNamed(IDR_EXTENSIONS_FAVICON);
  int icon_size = gfx::kFaviconSize;

  // We give first preference to an icon from the extension's icon set that
  // matches the size of the default. But not all extensions will declare an
  // icon set, or may not have an icon of the default size (we don't want the
  // bubble to display, for example, a very large icon). In that case, if there
  // is an action icon (size-16) this is an acceptable alternative.
  const ExtensionIconSet* icons = &extensions::IconsInfo::GetIcons(extension);
  bool has_default_sized_icon =
      !icons->Get(gfx::kFaviconSize, ExtensionIconSet::Match::kExactly).empty();

  if (!has_default_sized_icon) {
    const extensions::ActionInfo* action =
        extensions::ActionInfo::GetExtensionActionInfo(extension);
    if (!action || action->default_icon.empty()) {
      return;  // Out of options.
    }

    icons = &action->default_icon;
    icon_size = icons->map().begin()->first;
  }

  extension_info_.icon_image = std::make_unique<extensions::IconImage>(
      web_contents()->GetBrowserContext(), extension, *icons, icon_size,
      default_extension_icon_image, this);
}

void ZoomBubbleView::UpdateZoomPercent() {
  label_->SetText(base::FormatPercent(
      zoom::ZoomController::FromWebContents(web_contents())->GetZoomPercent()));
  label_->GetViewAccessibility().SetName(GetAccessibleWindowTitle());
  label_->NotifyAccessibilityEventDeprecated(ax::mojom::Event::kAlert, true);

  // Disable buttons at min, max and default
  auto* zoom_controller = zoom::ZoomController::FromWebContents(web_contents());
  double current_zoom_level = zoom_controller->GetZoomLevel();
  double default_zoom_level = zoom_controller->GetDefaultZoomLevel();
  std::vector<double> zoom_levels =
      zoom::PageZoom::PresetZoomLevels(default_zoom_level);
  DCHECK(zoom_out_button_);
  zoom_out_button_->SetEnabled(
      !blink::ZoomValuesEqual(zoom_levels.front(), current_zoom_level));
  DCHECK(zoom_in_button_);
  zoom_in_button_->SetEnabled(
      !blink::ZoomValuesEqual(zoom_levels.back(), current_zoom_level));
}

void ZoomBubbleView::UpdateZoomBubbleStateAndIconVisibility(
    bool is_bubble_visible) {
  // Note that we can't rely on web_contents() here, as it may have been
  // destroyed by the time we get this call. Also note parent_window() (if set)
  // may also be destroyed: the call to WindowClosing() may be triggered by
  // parent window destruction tearing down its child windows.
  Browser* browser = chrome::FindBrowserWithID(session_id_);
  if (!browser || !browser->window() || !browser->GetActiveTabInterface()) {
    return;
  }

  if (!IsPageActionMigrated(PageActionIconType::kZoom)) {
    browser->window()->UpdatePageActionIcon(PageActionIconType::kZoom);
    return;
  }

  // Update the bubble visibility state before we refresh the icon so that
  // UpdateZoomIconVisibility() sees the correct value bubble state value.
  UpdateBubbleVisibilityState(browser, is_bubble_visible);

  auto* tab_feature = browser->GetActiveTabInterface()->GetTabFeatures();
  CHECK(tab_feature);
  tab_feature->zoom_view_controller()->UpdatePageActionIcon(is_bubble_visible);
}

void ZoomBubbleView::StartTimerIfNecessary() {
  if (!auto_close_) {
    return;
  }

  auto_close_timer_.Start(FROM_HERE, auto_close_duration_, this,
                          &ZoomBubbleView::CloseBubble);
}

void ZoomBubbleView::StopTimer() {
  auto_close_timer_.Stop();
}

void ZoomBubbleView::ButtonPressed(base::RepeatingClosure closure) {
  // No button presses in this dialog should cause the dialog to close,
  // including when the zoom level is set to 100% as a result of a button press.
  base::AutoReset<bool> auto_ignore_close_bubble(&ignore_close_bubble_, true);

  // Extend the timer to give a user more time after any button is pressed.
  auto_close_duration_ = kBubbleCloseDelayLong;
  StartTimerIfNecessary();

  closure.Run();
}

void ZoomBubbleView::ImageButtonPressed() {
  DCHECK(extension_info_.icon_image) << "Invalid button press.";
  Browser* browser = GetBrowser();
  if (browser) {
    chrome::AddSelectedTabWithURL(
        browser,
        GURL(base::StringPrintf("chrome://extensions?id=%s",
                                extension_info_.id.c_str())),
        ui::PAGE_TRANSITION_FROM_API);
  }
}

Browser* ZoomBubbleView::GetBrowser() const {
  return web_contents() ? chrome::FindBrowserWithTab(web_contents()) : nullptr;
}

ZoomBubbleView::ZoomBubbleExtensionInfo::ZoomBubbleExtensionInfo() = default;

ZoomBubbleView::ZoomBubbleExtensionInfo::~ZoomBubbleExtensionInfo() = default;

BEGIN_METADATA(ZoomBubbleView)
END_METADATA