File: controlled_home_bubble_delegate.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 (324 lines) | stat: -rw-r--r-- 11,934 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
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
// Copyright 2023 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/extensions/controlled_home_bubble_delegate.h"

#include <utility>

#include "base/auto_reset.h"
#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/extensions/settings_api_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/common/referrer.h"
#include "extensions/browser/disable_reason.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/manifest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/window_open_disposition.h"

namespace {

// Whether we should ignore learn more clicks.
bool g_should_ignore_learn_more_for_testing = false;

// The set of profiles for which a controlled home bubble has been shown (we
// only show once per profile per session).
std::set<Profile*>& GetShownProfileSet() {
  static base::NoDestructor<std::set<Profile*>> g_profiles;
  return *g_profiles;
}

// The set of profiles for which a bubble is pending (but hasn't yet shown).
std::set<Profile*>& GetPendingProfileSet() {
  static base::NoDestructor<std::set<Profile*>> g_profiles;
  return *g_profiles;
}

// Gets the extension that currently controls the home page and has not yet
// been acknowledged, if any.
const extensions::Extension* GetExtensionToWarnAbout(Profile& profile) {
  const extensions::Extension* controlling_extension =
      extensions::GetExtensionOverridingHomepage(&profile);
  if (!controlling_extension) {
    // No controlling extension; nothing to warn about.
    return nullptr;
  }

  extensions::ExtensionPrefs* extension_prefs =
      extensions::ExtensionPrefs::Get(&profile);
  bool was_acknowledged = false;
  if (extension_prefs->ReadPrefAsBoolean(
          controlling_extension->id(),
          ControlledHomeBubbleDelegate::kAcknowledgedPreference,
          &was_acknowledged) &&
      was_acknowledged) {
    // Extension was already acknowledged.
    return nullptr;
  }

  return controlling_extension;
}

// Acknowledges the extension with the given `extension_id` so that we don't
// prompt the user about it again in the future.
void AcknowledgeExtension(Profile& profile,
                          const extensions::ExtensionId& extension_id) {
  extensions::ExtensionPrefs* extension_prefs =
      extensions::ExtensionPrefs::Get(&profile);
  extension_prefs->UpdateExtensionPref(
      extension_id, ControlledHomeBubbleDelegate::kAcknowledgedPreference,
      base::Value(true));
}

}  // namespace

ControlledHomeBubbleDelegate::ControlledHomeBubbleDelegate(Browser* browser)
    : browser_(browser),
      profile_(browser->profile()),
      extension_(GetExtensionToWarnAbout(*profile_)) {
  extension_registry_observation_.Observe(
      extensions::ExtensionRegistry::Get(profile_));
}

ControlledHomeBubbleDelegate::~ControlledHomeBubbleDelegate() {
  GetPendingProfileSet().erase(profile_);
}

base::AutoReset<bool>
ControlledHomeBubbleDelegate::IgnoreLearnMoreForTesting() {
  return base::AutoReset<bool>(&g_should_ignore_learn_more_for_testing, true);
}

void ControlledHomeBubbleDelegate::ClearProfileSetForTesting() {
  GetShownProfileSet().clear();
}

bool ControlledHomeBubbleDelegate::ShouldShow() {
  // Show if there's a non-acknowledged controlling extension and we haven't
  // shown (and aren't about to show in a pending bubble) for this profile.
  return extension_ && GetShownProfileSet().count(profile_) == 0u &&
         GetPendingProfileSet().count(profile_) == 0u;
}

void ControlledHomeBubbleDelegate::PendingShow() {
  DCHECK_EQ(0u, GetPendingProfileSet().count(profile_));
  // Mark the profile as having a pending bubble. This way, we won't queue up
  // another bubble if one is waiting for animation.
  GetPendingProfileSet().insert(profile_);
}

std::u16string ControlledHomeBubbleDelegate::GetHeadingText() {
  return l10n_util::GetStringUTF16(
      IDS_EXTENSIONS_SETTINGS_API_TITLE_HOME_PAGE_BUBBLE);
}

std::u16string ControlledHomeBubbleDelegate::GetBodyText(
    bool anchored_to_action) {
  const extensions::SettingsOverrides* settings =
      extensions::SettingsOverrides::Get(extension_.get());
  CHECK(settings);

  bool startup_change = !settings->startup_pages.empty();
  bool search_change = settings->search_engine.has_value();

  std::u16string body;
  int first_line_id =
      anchored_to_action
          ? IDS_EXTENSIONS_SETTINGS_API_FIRST_LINE_HOME_PAGE_SPECIFIC
          : IDS_EXTENSIONS_SETTINGS_API_FIRST_LINE_HOME_PAGE;
  int second_line_id = 0;
  if (startup_change && search_change) {
    second_line_id = IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_START_AND_SEARCH;
  } else if (startup_change) {
    second_line_id = IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_START_PAGES;
  } else if (search_change) {
    second_line_id = IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_SEARCH_ENGINE;
  }
  DCHECK_NE(0, first_line_id);
  body = anchored_to_action
             ? l10n_util::GetStringUTF16(first_line_id)
             : l10n_util::GetStringFUTF16(
                   first_line_id,
                   extensions::util::GetFixupExtensionNameForUIDisplay(
                       extension_->name()));
  if (second_line_id) {
    body += l10n_util::GetStringUTF16(second_line_id);
  }

  body += l10n_util::GetStringUTF16(
      IDS_EXTENSIONS_SETTINGS_API_THIRD_LINE_CONFIRMATION);

  return body;
}

std::u16string ControlledHomeBubbleDelegate::GetActionButtonText() {
  // An empty string is returned so that we don't display the button prompting
  // to remove policy-installed extensions.
  if (IsPolicyIndicationNeeded()) {
    return std::u16string();
  }
  return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_RESTORE_SETTINGS);
}

std::u16string ControlledHomeBubbleDelegate::GetDismissButtonText() {
  return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_KEEP_CHANGES);
}

ui::mojom::DialogButton ControlledHomeBubbleDelegate::GetDefaultDialogButton() {
  // TODO(estade): we should set a default where appropriate. See
  // http://crbug.com/751279
  return ui::mojom::DialogButton::kNone;
}

std::string ControlledHomeBubbleDelegate::GetAnchorActionId() {
  return extension_->id();
}

void ControlledHomeBubbleDelegate::OnBubbleShown(
    base::OnceClosure close_bubble_callback) {
  DCHECK_EQ(0u, GetShownProfileSet().count(profile_));
  DCHECK_EQ(1u, GetPendingProfileSet().count(profile_));

  GetShownProfileSet().insert(profile_);
  GetPendingProfileSet().erase(profile_);
  close_bubble_callback_ = std::move(close_bubble_callback);

  // It's possible the extension was removed while the bubble was getting ready
  // to show. If that happens, close the bubble "immediately" (after a post
  // task) when it's shown. We post a task just so we don't enter a CloseWidget
  // cycle in the same series as it being shown.
  if (!extension_) {
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, std::move(close_bubble_callback_));
  }
}

void ControlledHomeBubbleDelegate::OnBubbleClosed(CloseAction action) {
  // OnBubbleClosed() can be called twice when we receive multiple
  // "OnWidgetDestroying" notifications (this can at least happen when we close
  // a window with a notification open). Handle this gracefully.
  if (close_action_) {
    DCHECK(close_action_ == CLOSE_DISMISS_USER_ACTION ||
           close_action_ == CLOSE_DISMISS_DEACTIVATION);
    return;
  }

  close_action_ = action;
  extension_registry_observation_.Reset();

  if (action == CLOSE_DISMISS_DEACTIVATION) {
    return;  // Do nothing if the bubble was dismissed due to focus loss.
  }

  // We clear the profile set because the user chose to either remove, disable,
  // or acknowledge the extension. If they acknowledged it, we won't show the
  // bubble again, and in any other cases, we should re-show the bubble if
  // any extension goes back to overriding the home page (because it's contrary
  // to the user's choice).
  GetShownProfileSet().clear();

  switch (action) {
    case CLOSE_EXECUTE:
      // User clicked to disable the extension.
      extensions::ExtensionRegistrar::Get(profile_)->DisableExtension(
          extension_->id(), {extensions::disable_reason::DISABLE_USER_ACTION});
      break;
    case CLOSE_LEARN_MORE: {
      AcknowledgeExtension(*profile_, extension_->id());
      if (!g_should_ignore_learn_more_for_testing) {
        GURL learn_more_url(chrome::kExtensionControlledSettingLearnMoreURL);
        DCHECK(learn_more_url.is_valid());
        browser_->OpenURL(
            content::OpenURLParams(learn_more_url, content::Referrer(),
                                   WindowOpenDisposition::NEW_FOREGROUND_TAB,
                                   ui::PAGE_TRANSITION_LINK, false),
            /*navigation_handle_callback=*/{});
      }
      break;
    }
    case CLOSE_DISMISS_USER_ACTION:
      AcknowledgeExtension(*profile_, extension_->id());
      break;
    case CLOSE_DISMISS_DEACTIVATION:
      NOTREACHED();  // This was handled above.
  }

  // Warning: |this| may be deleted here!
}

std::unique_ptr<ToolbarActionsBarBubbleDelegate::ExtraViewInfo>
ControlledHomeBubbleDelegate::GetExtraViewInfo() {
  auto extra_view_info = std::make_unique<ExtraViewInfo>();

  if (IsPolicyIndicationNeeded()) {
    extra_view_info->resource = &vector_icons::kBusinessIcon;
    extra_view_info->text =
        l10n_util::GetStringUTF16(IDS_EXTENSIONS_INSTALLED_BY_ADMIN);
    extra_view_info->is_learn_more = false;
  } else {
    extra_view_info->text = l10n_util::GetStringUTF16(IDS_LEARN_MORE);
    extra_view_info->is_learn_more = true;
  }

  return extra_view_info;
}

bool ControlledHomeBubbleDelegate::IsPolicyIndicationNeeded() const {
  return extensions::Manifest::IsPolicyLocation(extension_->location());
}

void ControlledHomeBubbleDelegate::OnExtensionUnloaded(
    content::BrowserContext* browser_context,
    const extensions::Extension* extension,
    extensions::UnloadedExtensionReason reason) {
  HandleExtensionUnloadOrUninstall(extension);
}

void ControlledHomeBubbleDelegate::OnExtensionUninstalled(
    content::BrowserContext* browser_context,
    const extensions::Extension* extension,
    extensions::UninstallReason reason) {
  HandleExtensionUnloadOrUninstall(extension);
}

void ControlledHomeBubbleDelegate::HandleExtensionUnloadOrUninstall(
    const extensions::Extension* extension) {
  if (extension != extension_) {
    return;
  }

  // Null out `extension_` to indicate it was removed.
  extension_ = nullptr;

  // If the callback is set, then that means that OnShown() was called, and the
  // bubble was displayed. Close it, since the extension is gone.
  if (close_bubble_callback_) {
    std::move(close_bubble_callback_).Run();
  }
}

void ControlledHomeBubbleDelegate::OnShutdown(
    extensions::ExtensionRegistry* registry) {
  // It is possible that the extension registry is destroyed before the
  // controller. In such case, the controller should no longer observe the
  // registry.
  DCHECK(extension_registry_observation_.IsObservingSource(registry));
  extension_registry_observation_.Reset();
}