File: extension_error_ui_desktop.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (251 lines) | stat: -rw-r--r-- 8,843 bytes parent folder | download | duplicates (5)
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
// 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/extension_error_ui_desktop.h"

#include "base/check.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/global_error/global_error_bubble_view_base.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/strings/grit/components_strings.h"
#include "extensions/browser/blocklist_extension_prefs.h"
#include "extensions/browser/disable_reason.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/management_policy.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
#include "ui/base/l10n/l10n_util.h"

namespace extensions {

namespace {

std::u16string GenerateTitle(const ExtensionSet& forbidden,
                             bool item_blocked_by_policy_exists,
                             int extension_count,
                             int app_count) {
  // If |item_blocked_by_policy_exists| is true, this ignores the case that
  // there may be a mixture of enterprise and blocklisted items in |forbidden|.
  // The case does happen but is rare. In addition, this assumes that all policy
  // blocked items are extensions only.
  if (item_blocked_by_policy_exists) {
    return l10n_util::GetPluralStringFUTF16(
        IDS_POLICY_BLOCKED_EXTENSION_ALERT_TITLE, extension_count + app_count);
  }

  // Otherwise, the extensions/apps are marked as malware because all other
  // blocklist reasons are not included in alerts yet.
  if ((app_count > 1) && (extension_count > 1)) {
    return l10n_util::GetStringUTF16(IDS_EXTENSION_AND_APP_ALERT_TITLE);
  }
  if (app_count > 0) {
    return l10n_util::GetPluralStringFUTF16(IDS_APP_ALERT_TITLE, app_count);
  }
  return l10n_util::GetPluralStringFUTF16(IDS_EXTENSION_ALERT_TITLE,
                                          extension_count);
}

std::vector<std::u16string> GenerateEnterpriseMessage(
    const ExtensionSet& forbidden) {
  std::vector<std::u16string> message;
  message.reserve(forbidden.size() + 1);
  // This assumes that all policy blocked items are extensions only.
  if (forbidden.size() > 1) {
    message.push_back(l10n_util::GetStringUTF16(
        IDS_POLICY_BLOCKED_EXTENSIONS_ALERT_ITEM_TITLE));
    for (const auto& extension : forbidden) {
      message.push_back(l10n_util::GetStringFUTF16(
          IDS_BLOCKLISTED_EXTENSIONS_ALERT_ITEM,
          util::GetFixupExtensionNameForUIDisplay(extension->name())));
    }
  } else {
    message.push_back(l10n_util::GetStringFUTF16(
        IDS_POLICY_BLOCKED_EXTENSION_ALERT_ITEM_DETAIL,
        util::GetFixupExtensionNameForUIDisplay(
            forbidden.begin()->get()->name())));
  }
  return message;
}

std::vector<std::u16string> GenerateMessage(
    const ExtensionSet& forbidden,
    bool item_blocked_by_policy_exists) {
  std::vector<std::u16string> message;
  message.reserve(forbidden.size());

  // Currently, this ignores the case where there may be an extension that is
  // blocklisted by enterprise and another extension blocklisted by Safe
  // Browsing.
  if (item_blocked_by_policy_exists) {
    return GenerateEnterpriseMessage(forbidden);
  }

  if (forbidden.size() == 1) {
    message.push_back(
        l10n_util::GetStringFUTF16(IDS_EXTENSION_ALERT_ITEM_BLOCKLISTED_MALWARE,
                                   util::GetFixupExtensionNameForUIDisplay(
                                       forbidden.begin()->get()->name())));
    return message;
  }
  message.push_back(l10n_util::GetStringUTF16(
      IDS_EXTENSIONS_ALERT_ITEM_BLOCKLISTED_MALWARE_TITLE));
  for (const auto& extension : forbidden) {
    message.push_back(l10n_util::GetStringFUTF16(
        IDS_BLOCKLISTED_EXTENSIONS_ALERT_ITEM,
        util::GetFixupExtensionNameForUIDisplay(extension->name())));
  }
  return message;
}

}  // namespace

class ExtensionGlobalError : public GlobalErrorWithStandardBubble {
 public:
  explicit ExtensionGlobalError(ExtensionErrorUI::Delegate* delegate)
      : delegate_(delegate),
        management_policy_(
            ExtensionSystem::Get(delegate->GetContext())->management_policy()) {
    for (const auto& extension : delegate_->GetBlocklistedExtensions()) {
      if (extension->is_app()) {
        app_count_++;
      } else {
        extension_count_++;
      }
      if (management_policy_ &&
          !management_policy_->UserMayLoad(extension.get())) {
        item_blocked_by_policy_exists_ = true;
      }
    }
  }

  void SetManagementPolicy(ManagementPolicy* management_policy) {
    management_policy_ = management_policy;

    // Since the |management_policy_| may be set to something new,
    // |item_blocked_by_policy_exists_| may also need to be updated.
    if (management_policy_) {
      for (const auto& extension : delegate_->GetBlocklistedExtensions()) {
        if (!management_policy_->UserMayLoad(extension.get())) {
          item_blocked_by_policy_exists_ = true;
          break;
        }
      }
    }
  }

 private:
  // GlobalError overrides:
  bool HasMenuItem() override { return false; }

  int MenuItemCommandID() override { NOTREACHED(); }

  std::u16string MenuItemLabel() override { NOTREACHED(); }

  void ExecuteMenuItem(Browser* browser) override { NOTREACHED(); }

  std::u16string GetBubbleViewTitle() override {
    return GenerateTitle(delegate_->GetBlocklistedExtensions(),
                         item_blocked_by_policy_exists_, extension_count_,
                         app_count_);
  }

  std::vector<std::u16string> GetBubbleViewMessages() override {
    return GenerateMessage(delegate_->GetBlocklistedExtensions(),
                           item_blocked_by_policy_exists_);
  }

  std::u16string GetBubbleViewAcceptButtonLabel() override {
    return l10n_util::GetStringUTF16(IDS_EXTENSION_ALERT_ITEM_OK);
  }

  std::u16string GetBubbleViewCancelButtonLabel() override { return {}; }

  std::u16string GetBubbleViewDetailsButtonLabel() override {
    return l10n_util::GetStringUTF16(IDS_EXTENSION_ALERT_ITEM_DETAILS);
  }

  void OnBubbleViewDidClose(Browser* browser) override {
    delegate_->OnAlertClosed();
  }

  void BubbleViewAcceptButtonPressed(Browser* browser) override {
    delegate_->OnAlertAccept();
  }

  void BubbleViewCancelButtonPressed(Browser* browser) override {
    // Even though there is no cancel button, users can still cancel the dialog
    // by pressing escape.
    delegate_->OnAlertClosed();
  }

  base::WeakPtr<GlobalErrorWithStandardBubble> AsWeakPtr() override {
    return weak_ptr_factory_.GetWeakPtr();
  }

  void BubbleViewDetailsButtonPressed(Browser* browser) override {
    delegate_->OnAlertDetails();
  }

  raw_ptr<ExtensionErrorUI::Delegate> delegate_;
  raw_ptr<ManagementPolicy, DanglingUntriaged> management_policy_;
  int app_count_ = 0;
  int extension_count_ = 0;
  bool item_blocked_by_policy_exists_ = false;
  base::WeakPtrFactory<ExtensionGlobalError> weak_ptr_factory_{this};

  ExtensionGlobalError(const ExtensionGlobalError&) = delete;
  ExtensionGlobalError& operator=(const ExtensionGlobalError&) = delete;
};

ExtensionErrorUIDesktop::ExtensionErrorUIDesktop(
    ExtensionErrorUI::Delegate* delegate)
    : profile_(Profile::FromBrowserContext(delegate->GetContext())),
      global_error_(std::make_unique<ExtensionGlobalError>(delegate)) {}

ExtensionErrorUIDesktop::~ExtensionErrorUIDesktop() = default;

bool ExtensionErrorUIDesktop::ShowErrorInBubbleView() {
  Browser* browser = chrome::FindLastActiveWithProfile(profile_);
  if (!browser)
    return false;

  browser_ = browser;
  global_error_->ShowBubbleView(browser);
  return true;
}

void ExtensionErrorUIDesktop::ShowExtensions() {
  DCHECK(browser_);
  chrome::ShowExtensions(browser_);
}

void ExtensionErrorUIDesktop::Close() {
  if (global_error_->HasShownBubbleView()) {
    // Will end up calling into |global_error_|->OnBubbleViewDidClose,
    // possibly synchronously.
    global_error_->GetBubbleView()->CloseBubbleView();
  }
}

GlobalErrorWithStandardBubble* ExtensionErrorUIDesktop::GetErrorForTesting() {
  return global_error_.get();
}

void ExtensionErrorUIDesktop::SetManagementPolicyForTesting(
    ManagementPolicy* management_policy) {
  global_error_->SetManagementPolicy(management_policy);
}

}  // namespace extensions