Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 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_DEVTOOLS_GLOBAL_CONFIRM_INFO_BAR_H_ |
| 6 | #define CHROME_BROWSER_DEVTOOLS_GLOBAL_CONFIRM_INFO_BAR_H_ |
| 7 | |
| 8 | #include <map> |
Patrick Monette | 592bf1a | 2017-08-22 19:42:18 | [diff] [blame] | 9 | #include <memory> |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 10 | |
Ali Hijazi | 60a72b0a | 2024-09-30 17:58:53 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 13 | #include "chrome/browser/ui/browser_tab_strip_tracker.h" |
| 14 | #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" |
Colin Blundell | e70a6d8 | 2021-05-04 12:03:19 | [diff] [blame] | 15 | #include "components/infobars/content/content_infobar_manager.h" |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 16 | #include "components/infobars/core/confirm_infobar_delegate.h" |
| 17 | |
| 18 | namespace content { |
| 19 | class WebContents; |
| 20 | } |
| 21 | |
| 22 | // GlobalConfirmInfoBar is shown for every tab in every browser until it |
| 23 | // is dismissed or the close method is called. |
| 24 | // It listens to all tabs in all browsers and adds/removes confirm infobar |
| 25 | // to each of the tabs. |
Peter Kasting | 0cdd929e | 2020-05-08 04:26:32 | [diff] [blame] | 26 | // TODO(pkasting): This is a hack, driven by the original design of infobars |
| 27 | // being tab-scoped. Either this should be replaced by a different UI for |
| 28 | // whole-browser notifications, or the core infobar APIs should better |
| 29 | // accommodate these sorts of infobars (e.g. with a separate "global infobar |
| 30 | // manager" object or the like). |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 31 | class GlobalConfirmInfoBar : public TabStripModelObserver, |
| 32 | public infobars::InfoBarManager::Observer { |
| 33 | public: |
Peter Kasting | 0cdd929e | 2020-05-08 04:26:32 | [diff] [blame] | 34 | // Attempts to show a global infobar for |delegate|. If infobar addition |
| 35 | // fails (e.g. because infobars are disabled), the global infobar will not |
| 36 | // appear, and it (and |delegate|) will be deleted asynchronously. Otherwise, |
| 37 | // the delegate will be deleted synchronously when any of the tabs' infobars |
| 38 | // is closed via user action. Note that both of these aspects of lifetime |
| 39 | // management differ from how typical infobars work. |
Ghazale Hosseinabadi | 301cdb4 | 2020-10-02 20:16:57 | [diff] [blame] | 40 | static GlobalConfirmInfoBar* Show( |
| 41 | std::unique_ptr<ConfirmInfoBarDelegate> delegate); |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 42 | |
Peter Boström | fadb175 | 2021-09-30 19:17:01 | [diff] [blame] | 43 | GlobalConfirmInfoBar(const GlobalConfirmInfoBar&) = delete; |
| 44 | GlobalConfirmInfoBar& operator=(const GlobalConfirmInfoBar&) = delete; |
| 45 | |
johnchen | 4c5a6de6 | 2017-04-14 04:25:34 | [diff] [blame] | 46 | // infobars::InfoBarManager::Observer: |
| 47 | void OnInfoBarRemoved(infobars::InfoBar* info_bar, bool animate) override; |
| 48 | void OnManagerShuttingDown(infobars::InfoBarManager* manager) override; |
| 49 | |
Ghazale Hosseinabadi | 301cdb4 | 2020-10-02 20:16:57 | [diff] [blame] | 50 | // Closes all the infobars. |
| 51 | void Close(); |
| 52 | |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 53 | private: |
Peter Kasting | 0cdd929e | 2020-05-08 04:26:32 | [diff] [blame] | 54 | class DelegateProxy; |
| 55 | |
dcheng | b8eac3a | 2016-04-13 02:03:23 | [diff] [blame] | 56 | explicit GlobalConfirmInfoBar( |
| 57 | std::unique_ptr<ConfirmInfoBarDelegate> delegate); |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 58 | ~GlobalConfirmInfoBar() override; |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 59 | |
| 60 | // TabStripModelObserver: |
sangwoo.ko | b4ce470b | 2018-08-09 07:47:02 | [diff] [blame] | 61 | void OnTabStripModelChanged( |
sangwoo.ko | a60a2e6 | 2018-08-14 05:39:47 | [diff] [blame] | 62 | TabStripModel* tab_strip_model, |
sangwoo.ko | b4ce470b | 2018-08-09 07:47:02 | [diff] [blame] | 63 | const TabStripModelChange& change, |
| 64 | const TabStripSelectionChange& selection) override; |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 65 | void TabChangedAt(content::WebContents* web_contents, |
| 66 | int index, |
| 67 | TabChangeType change_type) override; |
| 68 | |
pmonette | 9119e49 | 2016-09-20 22:14:55 | [diff] [blame] | 69 | // Adds the info bar to the tab if it is missing. |
| 70 | void MaybeAddInfoBar(content::WebContents* web_contents); |
| 71 | |
dcheng | b8eac3a | 2016-04-13 02:03:23 | [diff] [blame] | 72 | std::unique_ptr<ConfirmInfoBarDelegate> delegate_; |
Ali Hijazi | 60a72b0a | 2024-09-30 17:58:53 | [diff] [blame] | 73 | std::map<infobars::InfoBarManager*, raw_ptr<DelegateProxy, CtnExperimental>> |
| 74 | proxies_; |
Francois Doray | c39105b | 2020-03-12 17:44:56 | [diff] [blame] | 75 | BrowserTabStripTracker browser_tab_strip_tracker_{this, nullptr}; |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 76 | |
Patrick Monette | 592bf1a | 2017-08-22 19:42:18 | [diff] [blame] | 77 | // Indicates if the global infobar is currently in the process of shutting |
| 78 | // down. |
Peter Kasting | 182d71d | 2020-01-07 05:28:18 | [diff] [blame] | 79 | bool is_closing_ = false; |
Patrick Monette | 592bf1a | 2017-08-22 19:42:18 | [diff] [blame] | 80 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 81 | base::WeakPtrFactory<GlobalConfirmInfoBar> weak_factory_{this}; |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | #endif // CHROME_BROWSER_DEVTOOLS_GLOBAL_CONFIRM_INFO_BAR_H_ |