File: theme_installed_infobar_delegate.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (146 lines) | stat: -rw-r--r-- 5,409 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
// 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/extensions/theme_installed_infobar_delegate.h"

#include <stddef.h>

#include <string>
#include <utility>

#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/infobars/confirm_infobar_creator.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/grit/generated_resources.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/infobar.h"
#include "extensions/browser/extension_system.h"
#include "ui/base/l10n/l10n_util.h"

// static
void ThemeInstalledInfoBarDelegate::CreateForLastActiveTab(
    Profile* profile,
    const std::string& theme_name,
    const std::string& theme_id,
    std::unique_ptr<ThemeService::ThemeReinstaller> prev_theme_reinstaller) {
  // FindTabbedBrowser() is called with |match_original_profiles| true because
  // a theme install in either a normal or incognito window for a profile
  // affects all normal and incognito windows for that profile.
  Browser* browser =
      chrome::FindTabbedBrowser(profile, /*match_original_profiles=*/true);
  if (browser) {
    content::WebContents* web_contents =
        browser->tab_strip_model()->GetActiveWebContents();
    if (web_contents) {
      ThemeInstalledInfoBarDelegate::Create(
          infobars::ContentInfoBarManager::FromWebContents(web_contents),
          ThemeServiceFactory::GetForProfile(profile), theme_name, theme_id,
          std::move(prev_theme_reinstaller));
    }
  }
}

// static
void ThemeInstalledInfoBarDelegate::Create(
    infobars::ContentInfoBarManager* infobar_manager,
    ThemeService* theme_service,
    const std::string& theme_name,
    const std::string& theme_id,
    std::unique_ptr<ThemeService::ThemeReinstaller> prev_theme_reinstaller) {
  // Create the new infobar.
  std::unique_ptr<infobars::InfoBar> new_infobar(
      CreateConfirmInfoBar(base::WrapUnique<ConfirmInfoBarDelegate>(
          new ThemeInstalledInfoBarDelegate(
              theme_service, theme_name, theme_id,
              std::move(prev_theme_reinstaller)))));

  // If there's a previous theme infobar, just replace that instead of adding a
  // new one.
  for (infobars::InfoBar* old_infobar : infobar_manager->infobars()) {
    ThemeInstalledInfoBarDelegate* theme_infobar =
        old_infobar->delegate()->AsThemePreviewInfobarDelegate();
    if (theme_infobar) {
      // If the user installed the same theme twice, ignore the second install
      // and keep the first install info bar, so that they can easily undo to
      // get back the previous theme.
      // |theme_id| is not defined for autogenerated themes, but since those
      // don't show an infobar, it's valid in this case.
      if (theme_infobar->theme_id_ != theme_id) {
        infobar_manager->ReplaceInfoBar(old_infobar, std::move(new_infobar));
      }
      return;
    }
  }
  // No previous theme infobar, so add this.
  infobar_manager->AddInfoBar(std::move(new_infobar));
}

ThemeInstalledInfoBarDelegate::ThemeInstalledInfoBarDelegate(
    ThemeService* theme_service,
    const std::string& theme_name,
    const std::string& theme_id,
    std::unique_ptr<ThemeService::ThemeReinstaller> prev_theme_reinstaller)
    : ConfirmInfoBarDelegate(),
      theme_service_(theme_service),
      theme_name_(theme_name),
      theme_id_(theme_id),
      prev_theme_reinstaller_(std::move(prev_theme_reinstaller)) {
  theme_service_->AddObserver(this);
}

ThemeInstalledInfoBarDelegate::~ThemeInstalledInfoBarDelegate() {
  // We don't want any notifications while we're running our destructor.
  theme_service_->RemoveObserver(this);
}

infobars::InfoBarDelegate::InfoBarIdentifier
ThemeInstalledInfoBarDelegate::GetIdentifier() const {
  return THEME_INSTALLED_INFOBAR_DELEGATE;
}

const gfx::VectorIcon& ThemeInstalledInfoBarDelegate::GetVectorIcon() const {
  return kPaintbrushIcon;
}

ThemeInstalledInfoBarDelegate*
    ThemeInstalledInfoBarDelegate::AsThemePreviewInfobarDelegate() {
  return this;
}

std::u16string ThemeInstalledInfoBarDelegate::GetMessageText() const {
  return l10n_util::GetStringFUTF16(IDS_THEME_INSTALL_INFOBAR_LABEL,
                                    base::UTF8ToUTF16(theme_name_));
}

int ThemeInstalledInfoBarDelegate::GetButtons() const {
  return BUTTON_CANCEL;
}

std::u16string ThemeInstalledInfoBarDelegate::GetButtonLabel(
    InfoBarButton button) const {
  DCHECK_EQ(BUTTON_CANCEL, button);
  return l10n_util::GetStringUTF16(IDS_THEME_INSTALL_INFOBAR_UNDO_BUTTON);
}

bool ThemeInstalledInfoBarDelegate::Cancel() {
  if (prev_theme_reinstaller_) {
    prev_theme_reinstaller_->Reinstall();
  }
  return false;  // The theme change will close us.
}

void ThemeInstalledInfoBarDelegate::OnThemeChanged() {
  // If the new theme is different from what this info bar is associated with,
  // close this info bar since it is no longer relevant.
  if (theme_id_ != theme_service_->GetThemeID()) {
    infobar()->RemoveSelf();
  }
}