blob: 8396b3b8f48f422e752d849db158a14deb0e25e8 [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).
25// TODO(https://crbug.com/1505612): Have this class use the new dialog builders
26// 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;
48 bool ShouldCloseOnDeactivate() override;
49 std::u16string GetHeadingText() override;
50 std::u16string GetBodyText(bool anchored_to_action) override;
51 std::u16string GetItemListText() override;
52 std::u16string GetActionButtonText() override;
53 std::u16string GetDismissButtonText() override;
54 ui::DialogButton GetDefaultDialogButton() override;
55 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() {
61 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_