File: browser_window_default_touch_bar.mm

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 (666 lines) | stat: -rw-r--r-- 23,402 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
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/ui/cocoa/touchbar/browser_window_default_touch_bar.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/mac/mac_util.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/sys_string_conversions.h"
#include "build/branding_buildflags.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/command_observer.h"
#include "chrome/browser/command_updater.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
#include "chrome/browser/ui/bookmarks/bookmark_tab_helper_observer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#import "chrome/browser/ui/cocoa/touchbar/browser_window_touch_bar_controller.h"
#include "chrome/browser/ui/fullscreen_util_mac.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "components/omnibox/browser/vector_icons.h"
#include "components/prefs/pref_member.h"
#include "components/search_engines/util.h"
#include "components/strings/grit/components_strings.h"
#include "components/url_formatter/url_formatter.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#import "skia/ext/skia_utils_mac.h"
#import "ui/base/cocoa/touch_bar_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_util_mac.h"
#include "ui/gfx/paint_vector_icon.h"

namespace {

// Touch bar identifiers.
NSString* const kBrowserWindowTouchBarId = @"browser-window";
NSString* const kTabFullscreenTouchBarId = @"tab-fullscreen";

// Touch bar items identifiers.
NSString* const kBackTouchId = @"BACK";
NSString* const kForwardTouchId = @"FORWARD";
NSString* const kReloadOrStopTouchId = @"RELOAD-STOP";
NSString* const kHomeTouchId = @"HOME";
NSString* const kSearchTouchId = @"SEARCH";
NSString* const kStarTouchId = @"BOOKMARK";
NSString* const kNewTabTouchId = @"NEW-TAB";
NSString* const kFullscreenOriginLabelTouchId = @"FULLSCREEN-ORIGIN-LABEL";

// This is a combined back and forward control which can no longer be selected
// but may be in an existing customized Touch Bar. It now represents a group
// containing the back and forward buttons, and adding the back or forward
// buttons to the Touch Bar individually magically decomposes the group.
NSString* const kBackForwardTouchId = @"BACK-FWD";

// Touch bar icon colors values.
const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE;
const SkColor kTouchBarStarActiveColor = gfx::kGoogleBlue500;

const SkColor kTouchBarUrlPathColor = SkColorSetA(SK_ColorWHITE, 0x7F);

// The size of the touch bar icons.
const int kTouchBarIconSize = 16;

// The min width of the search button in the touch bar.
const int kSearchBtnMinWidth = 205;

// Creates an NSImage from the given VectorIcon.
NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon,
                               SkColor color = kTouchBarDefaultIconColor) {
  return NSImageFromImageSkia(
      gfx::CreateVectorIcon(icon, kTouchBarIconSize, color));
}

// Creates an NSButton for the touch bar using an existing NSImage.
NSButton* CreateTouchBarButtonWithImage(NSImage* image,
                                        BrowserWindowDefaultTouchBar* owner,
                                        int command,
                                        int tooltip_id) {
  NSButton* button = [NSButton buttonWithImage:image
                                        target:owner
                                        action:@selector(executeCommand:)];
  button.tag = command;
  button.accessibilityTitle = l10n_util::GetNSString(tooltip_id);
  return button;
}

// Creates an NSButton for the touch bar using a vector icon.
NSButton* CreateTouchBarButton(const gfx::VectorIcon& icon,
                               BrowserWindowDefaultTouchBar* owner,
                               int command,
                               int tooltip_id,
                               SkColor color = kTouchBarDefaultIconColor) {
  return CreateTouchBarButtonWithImage(CreateNSImageFromIcon(icon, color),
                                       owner, command, tooltip_id);
}

// A class registered for C++ notifications. This is used to detect changes in
// the profile preferences and the back/forward commands.
class TouchBarNotificationBridge : public CommandObserver,
                                   public BrowserListObserver,
                                   public BookmarkTabHelperObserver,
                                   public TabStripModelObserver,
                                   public content::WebContentsObserver {
 public:
  TouchBarNotificationBridge(BrowserWindowDefaultTouchBar* owner,
                             Browser* browser)
      : owner_(owner), browser_(browser), contents_(nullptr) {
    TabStripModel* model = browser_->tab_strip_model();
    DCHECK(model);
    model->AddObserver(this);
    UpdateWebContents(model->GetActiveWebContents());

    auto* command_controller = browser->command_controller();
    command_controller->AddCommandObserver(IDC_BACK, this);
    owner.canGoBack = command_controller->IsCommandEnabled(IDC_BACK);
    command_controller->AddCommandObserver(IDC_FORWARD, this);
    owner.canGoForward = command_controller->IsCommandEnabled(IDC_FORWARD);

    auto* profile = browser->profile();
    auto* prefs = profile->GetPrefs();
    show_home_button_.Init(
        prefs::kShowHomeButton, prefs,
        base::BindRepeating(&TouchBarNotificationBridge::UpdateTouchBar,
                            base::Unretained(this)));

    profile_pref_registrar_.Init(prefs);
    profile_pref_registrar_.Add(
        DefaultSearchManager::kDefaultSearchProviderDataPrefName,
        base::BindRepeating(&TouchBarNotificationBridge::UpdateTouchBar,
                            base::Unretained(this)));

    BrowserList::AddObserver(this);
  }

  bool show_home_button() { return show_home_button_.GetValue(); }

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

  ~TouchBarNotificationBridge() override {
    BrowserList::RemoveObserver(this);
    browser_->tab_strip_model()->RemoveObserver(this);
    UpdateWebContents(nullptr);
  }

  void UpdateTouchBar() { [[owner_ controller] invalidateTouchBar]; }

  void UpdateWebContents(content::WebContents* new_contents) {
    if (contents_ == new_contents) {
      return;
    }
    if (contents_) {
      BookmarkTabHelper::FromWebContents(contents_)->RemoveObserver(this);
    }

    contents_ = new_contents;

    // Stop observing the old WebContents and start observing the new one (if
    // nonnull).
    Observe(contents_);

    BookmarkTabHelper* bookmark_helper =
        contents_ ? BookmarkTabHelper::FromWebContents(contents_) : nullptr;
    if (bookmark_helper) {
      bookmark_helper->AddObserver(this);
    }

    owner_.isPageLoading = contents_ && contents_->IsLoading();
    owner_.isStarred = bookmark_helper && bookmark_helper->is_starred();
    UpdateTouchBar();
  }

  // BookmarkTabHelperObserver:
  void URLStarredChanged(content::WebContents* web_contents,
                         bool starred) override {
    DCHECK(web_contents == contents_);
    owner_.isStarred = starred;
  }

 protected:
  // CommandObserver:
  void EnabledStateChangedForCommand(int command, bool enabled) override {
    DCHECK(command == IDC_BACK || command == IDC_FORWARD);
    if (command == IDC_BACK) {
      owner_.canGoBack = enabled;
    } else if (command == IDC_FORWARD) {
      owner_.canGoForward = enabled;
    }
  }

  // TabStripModelObserver:
  void OnTabStripModelChanged(
      TabStripModel* tab_strip_model,
      const TabStripModelChange& change,
      const TabStripSelectionChange& selection) override {
    UpdateWebContents(selection.new_contents);
  }

  void OnBrowserRemoved(Browser* browser) override {
    if (browser == owner_.browser) {
      owner_.browser = nullptr;
    }
  }

  // WebContentsObserver:
  void DidToggleFullscreenModeForTab(bool entered_fullscreen,
                                     bool will_cause_resize) override {
    UpdateTouchBar();
  }

  void DidStartLoading() override {
    DCHECK(contents_ && contents_->IsLoading());
    owner_.isPageLoading = YES;
  }

  void DidStopLoading() override {
    DCHECK(contents_ && !contents_->IsLoading());
    owner_.isPageLoading = NO;
  }

  void WebContentsDestroyed() override { UpdateWebContents(nullptr); }

 private:
  BrowserWindowDefaultTouchBar* __weak owner_;
  raw_ptr<Browser> browser_;                // Weak.
  raw_ptr<content::WebContents> contents_;  // Weak.

  // Used to monitor the optional home button pref.
  BooleanPrefMember show_home_button_;

  PrefChangeRegistrar profile_pref_registrar_;
};

}  // namespace

@interface BrowserWindowDefaultTouchBar () {
  // Used to receive and handle notifications.
  std::unique_ptr<TouchBarNotificationBridge> _notificationBridge;

  // The stop/reload button in the touch bar.
  NSButton* __strong _reloadStopButton;

  // The starred button in the touch bar.
  NSButton* __strong _starredButton;

  // The search button in the touch bar.
  NSButton* __strong _searchButton;

  // The last created BrowserWindowDefaultTouchBar (cached until it needs a
  // rebuild).
  NSTouchBar* __strong _touchBar;

  // The existence of the Home button in the Touch Bar.
  bool _touchBarHasHomeButton;
}

// Creates and returns a touch bar for tab non-fullscreen mode.
- (NSTouchBar*)createTabTouchBar;

// Creates and returns a touch bar for tab fullscreen mode.
- (NSTouchBar*)createTabFullscreenTouchBar;

// Updates the starred button in the touch bar.
- (void)updateStarredButton;

// Updates the reload / stop button in the touch bar.
- (void)updateReloadStopButton;

// Updates the search button in the touch bar.
- (void)updateSearchTouchBarButton;

@end

@implementation BrowserWindowDefaultTouchBar

@synthesize isPageLoading = _isPageLoading;
@synthesize isStarred = _isStarred;
@synthesize canGoBack = _canGoBack;
@synthesize canGoForward = _canGoForward;
@synthesize controller = _controller;
@synthesize browser = _browser;

- (NSTouchBar*)makeTouchBar {
  // When in tab or extension fullscreen, we should show a touch bar containing
  // only items associated with that mode. Since the toolbar is hidden, only
  // the option to exit fullscreen should show up.
  if (fullscreen_utils::IsInContentFullscreen(_browser)) {
    return [self createTabFullscreenTouchBar];
  }

  return [self createTabTouchBar];
}

- (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar
      makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
  if (!touchBar) {
    return nil;
  }

  if ([identifier hasSuffix:kBackForwardTouchId]) {
    auto* items = @[
      [touchBar itemForIdentifier:ui::GetTouchBarItemId(
                                      kBrowserWindowTouchBarId, kBackTouchId)],
      [touchBar
          itemForIdentifier:ui::GetTouchBarItemId(kBrowserWindowTouchBarId,
                                                  kForwardTouchId)],
    ];
    auto groupItem = [NSGroupTouchBarItem groupItemWithIdentifier:identifier
                                                            items:items];
    [groupItem setCustomizationLabel:
                   l10n_util::GetNSString(
                       IDS_TOUCH_BAR_BACK_FORWARD_CUSTOMIZATION_LABEL)];
    return groupItem;
  }

  NSCustomTouchBarItem* touchBarItem =
      [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
  if ([identifier hasSuffix:kBackTouchId]) {
    auto* button = CreateTouchBarButton(vector_icons::kBackArrowIcon, self,
                                        IDC_BACK, IDS_ACCNAME_BACK);
    [button bind:@"enabled" toObject:self withKeyPath:@"canGoBack" options:nil];
    [touchBarItem setView:button];
    [touchBarItem
        setCustomizationLabel:l10n_util::GetNSString(IDS_ACCNAME_BACK)];
  } else if ([identifier hasSuffix:kForwardTouchId]) {
    auto* button = CreateTouchBarButton(vector_icons::kForwardArrowIcon, self,
                                        IDC_FORWARD, IDS_ACCNAME_FORWARD);
    [button bind:@"enabled"
           toObject:self
        withKeyPath:@"canGoForward"
            options:nil];
    [touchBarItem setView:button];
    [touchBarItem
        setCustomizationLabel:l10n_util::GetNSString(IDS_ACCNAME_FORWARD)];
  } else if ([identifier hasSuffix:kReloadOrStopTouchId]) {
    [self updateReloadStopButton];
    [touchBarItem setView:_reloadStopButton];
    [touchBarItem setCustomizationLabel:
                      l10n_util::GetNSString(
                          IDS_TOUCH_BAR_STOP_RELOAD_CUSTOMIZATION_LABEL)];
  } else if ([identifier hasSuffix:kHomeTouchId]) {
    [touchBarItem setView:CreateTouchBarButton(kNavigateHomeIcon, self,
                                               IDC_HOME, IDS_ACCNAME_HOME)];
    [touchBarItem
        setCustomizationLabel:l10n_util::GetNSString(
                                  IDS_TOUCH_BAR_HOME_CUSTOMIZATION_LABEL)];
  } else if ([identifier hasSuffix:kNewTabTouchId]) {
    [touchBarItem
        setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, IDC_NEW_TAB,
                                     IDS_TOOLTIP_NEW_TAB)];
    [touchBarItem
        setCustomizationLabel:l10n_util::GetNSString(
                                  IDS_TOUCH_BAR_NEW_TAB_CUSTOMIZATION_LABEL)];
  } else if ([identifier hasSuffix:kStarTouchId]) {
    [self updateStarredButton];
    [touchBarItem setView:_starredButton];
    [touchBarItem
        setCustomizationLabel:l10n_util::GetNSString(
                                  IDS_TOUCH_BAR_BOOKMARK_CUSTOMIZATION_LABEL)];
  } else if ([identifier hasSuffix:kSearchTouchId]) {
    [self updateSearchTouchBarButton];
    [touchBarItem setView:_searchButton];
    [touchBarItem setCustomizationLabel:l10n_util::GetNSString(
                                            IDS_TOUCH_BAR_GOOGLE_SEARCH)];
  } else if ([identifier hasSuffix:kFullscreenOriginLabelTouchId]) {
    content::WebContents* contents =
        _browser->tab_strip_model()->GetActiveWebContents();

    if (!contents) {
      return nil;
    }

    // Strip the trailing slash.
    url::Parsed parsed;
    std::u16string displayText = url_formatter::FormatUrl(
        contents->GetLastCommittedURL(),
        url_formatter::kFormatUrlOmitTrailingSlashOnBareHostname,
        base::UnescapeRule::SPACES, &parsed, nullptr, nullptr);

    NSMutableAttributedString* attributedString =
        [[NSMutableAttributedString alloc]
            initWithString:base::SysUTF16ToNSString(displayText)];

    if (parsed.path.is_nonempty()) {
      size_t pathIndex = parsed.path.begin;
      [attributedString
          addAttribute:NSForegroundColorAttributeName
                 value:skia::SkColorToSRGBNSColor(kTouchBarUrlPathColor)
                 range:NSMakeRange(pathIndex,
                                   attributedString.length - pathIndex)];
    }

    [touchBarItem
        setView:[NSTextField labelWithAttributedString:attributedString]];
    [touchBarItem
        setCustomizationLabel:l10n_util::GetNSString(
                                  IDS_TOUCH_BAR_URL_CUSTOMIZATION_LABEL)];
  } else {
    return nil;
  }

  return touchBarItem;
}

- (NSTouchBar*)createTabTouchBar {
  [self updateSearchTouchBarButton];
  bool showHomeButton = _notificationBridge->show_home_button();

  if (!_touchBar || _touchBarHasHomeButton != showHomeButton) {
    _touchBar = [[NSTouchBar alloc] init];
    [_touchBar
        setCustomizationIdentifier:ui::GetTouchBarId(kBrowserWindowTouchBarId)];
    [_touchBar setDelegate:self];

    NSMutableArray<NSString*>* customizationIdentifiers =
        [NSMutableArray array];
    NSMutableArray<NSString*>* defaultIdentifiers = [NSMutableArray array];

    NSArray<NSString*>* touchBarItemIdentifiers = @[
      kBackTouchId, kForwardTouchId, kReloadOrStopTouchId, kHomeTouchId,
      kSearchTouchId, kStarTouchId, kNewTabTouchId
    ];

    for (NSString* itemIdentifier in touchBarItemIdentifiers) {
      NSString* fullIdentifier =
          ui::GetTouchBarItemId(kBrowserWindowTouchBarId, itemIdentifier);
      [customizationIdentifiers addObject:fullIdentifier];

      // Don't add the home button if it's not shown in the toolbar.
      if (itemIdentifier == kHomeTouchId && !showHomeButton) {
        continue;
      }

      [defaultIdentifiers addObject:fullIdentifier];
    }

    [customizationIdentifiers addObject:NSTouchBarItemIdentifierFlexibleSpace];

    [_touchBar setDefaultItemIdentifiers:defaultIdentifiers];
    [_touchBar setCustomizationAllowedItemIdentifiers:customizationIdentifiers];

    _touchBarHasHomeButton = showHomeButton;
  }

  return _touchBar;
}

- (NSTouchBar*)createTabFullscreenTouchBar {
  NSTouchBar* touchBar = [[NSTouchBar alloc] init];
  [touchBar
      setCustomizationIdentifier:ui::GetTouchBarId(kTabFullscreenTouchBarId)];
  [touchBar setDelegate:self];

  NSArray<NSString*>* touchBarItems = @[ ui::GetTouchBarItemId(
      kTabFullscreenTouchBarId, kFullscreenOriginLabelTouchId) ];

  [touchBar setDefaultItemIdentifiers:touchBarItems];
  [touchBar setCustomizationAllowedItemIdentifiers:touchBarItems];

  return touchBar;
}

- (void)setBrowser:(Browser*)browser {
  if (_browser == browser) {
    return;
  }
  _browser = browser;
  _notificationBridge.reset(
      _browser ? new TouchBarNotificationBridge(self, _browser) : nullptr);
}

- (void)updateStarredButton {
  NSImage* image = _isStarred ? [BrowserWindowDefaultTouchBar starActiveIcon]
                              : [BrowserWindowDefaultTouchBar starDefaultIcon];
  int tooltipId = _isStarred ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR;

  if (!_starredButton) {
    _starredButton = CreateTouchBarButtonWithImage(
        image, self, IDC_BOOKMARK_THIS_TAB, tooltipId);
    return;
  }

  if ([_starredButton image] == image) {
    return;
  }

  [_starredButton setImage:image];
  [_starredButton setAccessibilityLabel:l10n_util::GetNSString(tooltipId)];
}

- (void)updateReloadStopButton {
  NSImage* image = _isPageLoading
                       ? [BrowserWindowDefaultTouchBar navigateStopIcon]
                       : [BrowserWindowDefaultTouchBar reloadIcon];
  int commandId = _isPageLoading ? IDC_STOP : IDC_RELOAD;
  int tooltipId = _isPageLoading ? IDS_TOOLTIP_STOP : IDS_TOOLTIP_RELOAD;

  if (!_reloadStopButton) {
    _reloadStopButton =
        CreateTouchBarButtonWithImage(image, self, commandId, tooltipId);
    return;
  }

  if ([_reloadStopButton tag] == commandId) {
    return;
  }

  [_reloadStopButton setImage:image];
  [_reloadStopButton setTag:commandId];
  [_reloadStopButton setAccessibilityLabel:l10n_util::GetNSString(tooltipId)];
}

- (void)updateSearchTouchBarButton {
  TemplateURLService* templateUrlService =
      TemplateURLServiceFactory::GetForProfile(_browser->profile());
  const TemplateURL* defaultProvider =
      templateUrlService->GetDefaultSearchProvider();
  BOOL isGoogle = NO;
  std::u16string title;
  if (defaultProvider) {
    isGoogle =
        defaultProvider->GetEngineType(
            templateUrlService->search_terms_data()) == SEARCH_ENGINE_GOOGLE;

    title = isGoogle ? l10n_util::GetStringUTF16(IDS_TOUCH_BAR_GOOGLE_SEARCH)
                     : l10n_util::GetStringFUTF16(
                           IDS_TOUCH_BAR_SEARCH, defaultProvider->short_name());
  } else {
    title = l10n_util::GetStringUTF16(IDS_TOUCH_BAR_NO_DEFAULT_SEARCH);
  }

  NSString* buttonTitle = base::SysUTF16ToNSString(title);

  if ([buttonTitle isEqualToString:[_searchButton title]]) {
    return;
  }

  NSImage* image = nil;
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  if (isGoogle) {
    image = NSImageFromImageSkia(
        gfx::CreateVectorIcon(vector_icons::kGoogleGLogoIcon, kTouchBarIconSize,
                              gfx::kPlaceholderColor));
  } else {
    image = CreateNSImageFromIcon(vector_icons::kSearchIcon);
  }
#endif
  if (!image) {
    image = CreateNSImageFromIcon(vector_icons::kSearchIcon);
  }

  if (!_searchButton) {
    _searchButton = [NSButton buttonWithTitle:buttonTitle
                                        image:image
                                       target:self
                                       action:@selector(executeCommand:)];
    _searchButton.imageHugsTitle = YES;
    _searchButton.tag = IDC_FOCUS_LOCATION;
    [_searchButton.widthAnchor
        constraintGreaterThanOrEqualToConstant:kSearchBtnMinWidth]
        .active = YES;
    [_searchButton
        setContentHuggingPriority:1.0
                   forOrientation:NSLayoutConstraintOrientationHorizontal];
  } else {
    [_searchButton setTitle:buttonTitle];
    [_searchButton setImage:image];
  }
}

- (void)executeCommand:(id)sender {
  int command = [sender tag];
  _browser->command_controller()->ExecuteCommand(command);
}

- (void)setIsPageLoading:(BOOL)isPageLoading {
  _isPageLoading = isPageLoading;
  [self updateReloadStopButton];
}

- (void)setIsStarred:(BOOL)isStarred {
  _isStarred = isStarred;
  [self updateStarredButton];
}

@end

// Private methods exposed for testing.
@implementation BrowserWindowDefaultTouchBar (ExposedForTesting)

+ (NSString*)reloadOrStopItemIdentifier {
  return ui::GetTouchBarItemId(kBrowserWindowTouchBarId, kReloadOrStopTouchId);
}

+ (NSString*)bookmarkStarItemIdentifier {
  return ui::GetTouchBarItemId(kBrowserWindowTouchBarId, kStarTouchId);
}

+ (NSString*)backItemIdentifier {
  return ui::GetTouchBarItemId(kBrowserWindowTouchBarId, kBackTouchId);
}

+ (NSString*)forwardItemIdentifier {
  return ui::GetTouchBarItemId(kBrowserWindowTouchBarId, kForwardTouchId);
}

+ (NSString*)fullscreenOriginItemIdentifier {
  return ui::GetTouchBarItemId(kTabFullscreenTouchBarId,
                               kFullscreenOriginLabelTouchId);
}

+ (NSImage*)starDefaultIcon {
  static __strong NSImage* starDefaultIcon =
      CreateNSImageFromIcon(omnibox::kStarIcon, kTouchBarDefaultIconColor);
  return starDefaultIcon;
}

+ (NSString*)homeItemIdentifier {
  return ui::GetTouchBarItemId(kBrowserWindowTouchBarId, kHomeTouchId);
}

+ (NSImage*)starActiveIcon {
  static __strong NSImage* starActiveIcon = []() {
    return CreateNSImageFromIcon(omnibox::kStarActiveIcon,
                                 kTouchBarStarActiveColor);
  }();
  return starActiveIcon;
}

+ (NSImage*)navigateStopIcon {
  static __strong NSImage* navigateStopIcon =
      CreateNSImageFromIcon(kNavigateStopIcon);
  return navigateStopIcon;
}

+ (NSImage*)reloadIcon {
  static __strong NSImage* reloadIcon =
      CreateNSImageFromIcon(vector_icons::kReloadIcon);
  return reloadIcon;
}

- (NSButton*)searchButton {
  return _searchButton;
}

- (BookmarkTabHelperObserver*)bookmarkTabObserver {
  return _notificationBridge.get();
}

@end