blob: adc83ca8db0500432e227953db0ad3966fe39f45 [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"
Lei Zhang7790c0302025-06-17 17:23:3011#include "base/memory/raw_ptr.h"
Devlin Cronin135ce0c2023-11-28 17:53:5912#include "base/scoped_observation.h"
13#include "chrome/browser/ui/toolbar/toolbar_actions_bar_bubble_delegate.h"
14#include "extensions/browser/extension_registry.h"
15#include "extensions/browser/extension_registry_observer.h"
Md Hasibul Hasane914ad72024-08-20 12:38:5816#include "ui/base/mojom/dialog_button.mojom.h"
Devlin Cronin135ce0c2023-11-28 17:53:5917
18class Browser;
19class Profile;
20
21namespace extensions {
22class Extension;
23}
24
25// A bubble shown for an extension overriding the user's home page (different
26// than the NTP).
Alison Gale91301922024-04-15 19:35:2727// TODO(crbug.com/40946250): Have this class use the new dialog builders
Devlin Cronin135ce0c2023-11-28 17:53:5928// and remove ToolbarActionsBarBubbleDelegate.
29class ControlledHomeBubbleDelegate
30 : public ToolbarActionsBarBubbleDelegate,
31 public extensions::ExtensionRegistryObserver {
32 public:
33 explicit ControlledHomeBubbleDelegate(Browser* browser);
34
35 ControlledHomeBubbleDelegate(const ControlledHomeBubbleDelegate&) = delete;
36 ControlledHomeBubbleDelegate& operator=(const ControlledHomeBubbleDelegate&) =
37 delete;
38
39 ~ControlledHomeBubbleDelegate() override;
40
41 // The key in the extensions preferences to indicate if an extension has been
42 // acknowledged.
43 static constexpr char kAcknowledgedPreference[] = "ack_settings_bubble";
44
45 // Called when the bubble is set to show (but hasn't quite shown yet).
46 void PendingShow();
47
48 // ToolbarActionsBarBubbleDelegate:
49 bool ShouldShow() override;
Devlin Cronin135ce0c2023-11-28 17:53:5950 std::u16string GetHeadingText() override;
51 std::u16string GetBodyText(bool anchored_to_action) override;
Devlin Cronin135ce0c2023-11-28 17:53:5952 std::u16string GetActionButtonText() override;
53 std::u16string GetDismissButtonText() override;
Md Hasibul Hasane914ad72024-08-20 12:38:5854 ui::mojom::DialogButton GetDefaultDialogButton() override;
Devlin Cronin135ce0c2023-11-28 17:53:5955 std::unique_ptr<ExtraViewInfo> GetExtraViewInfo() override;
56 std::string GetAnchorActionId() override;
57 void OnBubbleShown(base::OnceClosure close_bubble_callback) override;
58 void OnBubbleClosed(CloseAction action) override;
59
60 const extensions::Extension* extension_for_testing() {
Devlin Cronin135ce0c2023-11-28 17:53:5961 return extension_.get();
62 }
63 // Don't try to navigate when "learn more" is clicked.
64 static base::AutoReset<bool> IgnoreLearnMoreForTesting();
65 // Clear the set of shown profiles.
66 static void ClearProfileSetForTesting();
67
68 private:
69 // Returns true if we should add the policy indicator to the bubble.
70 bool IsPolicyIndicationNeeded() const;
71
72 // ExtensionRegistryObserver:
73 void OnShutdown(extensions::ExtensionRegistry* registry) override;
74 void OnExtensionUnloaded(content::BrowserContext* browser_context,
75 const extensions::Extension* extension,
76 extensions::UnloadedExtensionReason reason) override;
77 void OnExtensionUninstalled(content::BrowserContext* browser_context,
78 const extensions::Extension* extension,
79 extensions::UninstallReason reason) override;
80
81 // Checks whether `extension` corresponds to this bubble's extension and,
82 // if so, closes the bubble.
83 void HandleExtensionUnloadOrUninstall(const extensions::Extension* extension);
84
85 // The corresponding `Browser`.
86 raw_ptr<Browser> const browser_;
87 // The corresponding `Profile`.
88 raw_ptr<Profile> const profile_;
89 // The action taken when the bubble closed, if any.
90 std::optional<CloseAction> close_action_;
91 // The extension controlling the home page, if any. This is null'd out when
92 // the extension is uninstalled.
93 scoped_refptr<const extensions::Extension> extension_;
94 // A closure to close the native view for the bubble. Populated in
95 // `OnBubbleShown()`.
96 base::OnceClosure close_bubble_callback_;
97
98 base::ScopedObservation<extensions::ExtensionRegistry,
99 extensions::ExtensionRegistryObserver>
100 extension_registry_observation_{this};
101};
102
103#endif // CHROME_BROWSER_UI_EXTENSIONS_CONTROLLED_HOME_BUBBLE_DELEGATE_H_