blob: 9ff5921a46450184c678dd3b85a7c045880a2a1c [file] [log] [blame]
Devlin Cronin135ce0c2023-11-28 17:53:591// Copyright 2023 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_UI_EXTENSIONS_CONTROLLED_HOME_BUBBLE_DELEGATE_H_
6#define CHROME_BROWSER_UI_EXTENSIONS_CONTROLLED_HOME_BUBBLE_DELEGATE_H_
7
8#include <optional>
9
10#include "base/auto_reset.h"
11#include "base/scoped_observation.h"
12#include "chrome/browser/ui/toolbar/toolbar_actions_bar_bubble_delegate.h"
13#include "extensions/browser/extension_registry.h"
14#include "extensions/browser/extension_registry_observer.h"
15
16class Browser;
17class Profile;
18
19namespace extensions {
20class Extension;
21}
22
23// A bubble shown for an extension overriding the user's home page (different
24// than the NTP).
Alison Gale91301922024-04-15 19:35:2725// TODO(crbug.com/40946250): Have this class use the new dialog builders
Devlin Cronin135ce0c2023-11-28 17:53:5926// and remove ToolbarActionsBarBubbleDelegate.
27class ControlledHomeBubbleDelegate
28 : public ToolbarActionsBarBubbleDelegate,
29 public extensions::ExtensionRegistryObserver {
30 public:
31 explicit ControlledHomeBubbleDelegate(Browser* browser);
32
33 ControlledHomeBubbleDelegate(const ControlledHomeBubbleDelegate&) = delete;
34 ControlledHomeBubbleDelegate& operator=(const ControlledHomeBubbleDelegate&) =
35 delete;
36
37 ~ControlledHomeBubbleDelegate() override;
38
39 // The key in the extensions preferences to indicate if an extension has been
40 // acknowledged.
41 static constexpr char kAcknowledgedPreference[] = "ack_settings_bubble";
42
43 // Called when the bubble is set to show (but hasn't quite shown yet).
44 void PendingShow();
45
46 // ToolbarActionsBarBubbleDelegate:
47 bool ShouldShow() override;
Devlin Cronin135ce0c2023-11-28 17:53:5948 std::u16string GetHeadingText() override;
49 std::u16string GetBodyText(bool anchored_to_action) override;
Devlin Cronin135ce0c2023-11-28 17:53:5950 std::u16string GetActionButtonText() override;
51 std::u16string GetDismissButtonText() override;
52 ui::DialogButton GetDefaultDialogButton() override;
53 std::unique_ptr<ExtraViewInfo> GetExtraViewInfo() override;
54 std::string GetAnchorActionId() override;
55 void OnBubbleShown(base::OnceClosure close_bubble_callback) override;
56 void OnBubbleClosed(CloseAction action) override;
57
58 const extensions::Extension* extension_for_testing() {
59 return extension_.get();
60 }
61 // Don't try to navigate when "learn more" is clicked.
62 static base::AutoReset<bool> IgnoreLearnMoreForTesting();
63 // Clear the set of shown profiles.
64 static void ClearProfileSetForTesting();
65
66 private:
67 // Returns true if we should add the policy indicator to the bubble.
68 bool IsPolicyIndicationNeeded() const;
69
70 // ExtensionRegistryObserver:
71 void OnShutdown(extensions::ExtensionRegistry* registry) override;
72 void OnExtensionUnloaded(content::BrowserContext* browser_context,
73 const extensions::Extension* extension,
74 extensions::UnloadedExtensionReason reason) override;
75 void OnExtensionUninstalled(content::BrowserContext* browser_context,
76 const extensions::Extension* extension,
77 extensions::UninstallReason reason) override;
78
79 // Checks whether `extension` corresponds to this bubble's extension and,
80 // if so, closes the bubble.
81 void HandleExtensionUnloadOrUninstall(const extensions::Extension* extension);
82
83 // The corresponding `Browser`.
84 raw_ptr<Browser> const browser_;
85 // The corresponding `Profile`.
86 raw_ptr<Profile> const profile_;
87 // The action taken when the bubble closed, if any.
88 std::optional<CloseAction> close_action_;
89 // The extension controlling the home page, if any. This is null'd out when
90 // the extension is uninstalled.
91 scoped_refptr<const extensions::Extension> extension_;
92 // A closure to close the native view for the bubble. Populated in
93 // `OnBubbleShown()`.
94 base::OnceClosure close_bubble_callback_;
95
96 base::ScopedObservation<extensions::ExtensionRegistry,
97 extensions::ExtensionRegistryObserver>
98 extension_registry_observation_{this};
99};
100
101#endif // CHROME_BROWSER_UI_EXTENSIONS_CONTROLLED_HOME_BUBBLE_DELEGATE_H_