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