Avi Drissman | 60039d4 | 2022-09-13 21:49:05 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
steel | f65a72a | 2016-10-19 22:16:45 | [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 EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_ |
| 6 | #define EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_ |
| 7 | |
John Delaney | 9f33f220 | 2021-02-22 22:03:44 | [diff] [blame] | 8 | #include <map> |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <string> |
| 11 | |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 12 | #include "base/callback_list.h" |
Avi Drissman | bb9cd02 | 2023-01-11 01:03:29 | [diff] [blame] | 13 | #include "base/functional/callback.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 14 | #include "base/memory/raw_ptr.h" |
Sigurdur Asgeirsson | 834f057 | 2021-03-24 13:24:58 | [diff] [blame] | 15 | #include "base/scoped_observation.h" |
Kevin McNee | df0c85ec | 2023-08-16 19:44:23 | [diff] [blame] | 16 | #include "content/public/test/browser_test_utils.h" |
Evan Stade | 922f3f1f | 2019-09-04 21:05:13 | [diff] [blame] | 17 | #include "extensions/browser/process_manager.h" |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 18 | #include "extensions/browser/process_manager_observer.h" |
Anton Bershanskyi | bcb039d | 2024-02-05 20:03:23 | [diff] [blame] | 19 | #include "extensions/common/extension_id.h" |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 20 | |
| 21 | namespace content { |
| 22 | class BrowserContext; |
John Delaney | 9f33f220 | 2021-02-22 22:03:44 | [diff] [blame] | 23 | class WebContents; |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | namespace extensions { |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 27 | |
| 28 | // Test helper class for observing extension-related events. |
David Bertoni | ae4886abc | 2023-02-14 04:16:34 | [diff] [blame] | 29 | class ExtensionTestNotificationObserver { |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 30 | public: |
| 31 | explicit ExtensionTestNotificationObserver(content::BrowserContext* context); |
Peter Boström | 951cf77e | 2021-09-22 00:02:59 | [diff] [blame] | 32 | |
| 33 | ExtensionTestNotificationObserver(const ExtensionTestNotificationObserver&) = |
| 34 | delete; |
| 35 | ExtensionTestNotificationObserver& operator=( |
| 36 | const ExtensionTestNotificationObserver&) = delete; |
| 37 | |
David Bertoni | ae4886abc | 2023-02-14 04:16:34 | [diff] [blame] | 38 | ~ExtensionTestNotificationObserver(); |
limasdf | 184283c | 2017-04-14 08:22:10 | [diff] [blame] | 39 | |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 40 | protected: |
Kevin McNee | df0c85ec | 2023-08-16 19:44:23 | [diff] [blame] | 41 | class NotificationSet : public extensions::ProcessManagerObserver { |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 42 | public: |
Kevin McNee | df0c85ec | 2023-08-16 19:44:23 | [diff] [blame] | 43 | explicit NotificationSet(ProcessManager* manager); |
Peter Boström | 951cf77e | 2021-09-22 00:02:59 | [diff] [blame] | 44 | |
| 45 | NotificationSet(const NotificationSet&) = delete; |
| 46 | NotificationSet& operator=(const NotificationSet&) = delete; |
| 47 | |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 48 | ~NotificationSet() override; |
| 49 | |
Kevin McNee | df0c85ec | 2023-08-16 19:44:23 | [diff] [blame] | 50 | // Notified any time a notification is received. |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 51 | // The details of the notification are dropped. |
Fabrice de Gans-Riberi | 05dfdeb | 2021-03-22 22:46:31 | [diff] [blame] | 52 | base::RepeatingClosureList& closure_list() { return closure_list_; } |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 53 | |
| 54 | private: |
John Delaney | 9f33f220 | 2021-02-22 22:03:44 | [diff] [blame] | 55 | class ForwardingWebContentsObserver; |
| 56 | |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 57 | // extensions::ProcessManagerObserver: |
| 58 | void OnExtensionFrameUnregistered( |
Anton Bershanskyi | bcb039d | 2024-02-05 20:03:23 | [diff] [blame] | 59 | const ExtensionId& extension_id, |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 60 | content::RenderFrameHost* render_frame_host) override; |
| 61 | |
Kevin McNee | df0c85ec | 2023-08-16 19:44:23 | [diff] [blame] | 62 | void OnWebContentsCreated(content::WebContents* web_contents); |
| 63 | void StartObservingWebContents(content::WebContents* web_contents); |
| 64 | |
| 65 | void DidStopLoading(content::WebContents* web_contents); |
John Delaney | 9f33f220 | 2021-02-22 22:03:44 | [diff] [blame] | 66 | void WebContentsDestroyed(content::WebContents* web_contents); |
| 67 | |
Fabrice de Gans-Riberi | 05dfdeb | 2021-03-22 22:46:31 | [diff] [blame] | 68 | base::RepeatingClosureList closure_list_; |
Kevin McNee | df0c85ec | 2023-08-16 19:44:23 | [diff] [blame] | 69 | |
Sigurdur Asgeirsson | 834f057 | 2021-03-24 13:24:58 | [diff] [blame] | 70 | base::ScopedObservation<extensions::ProcessManager, |
| 71 | extensions::ProcessManagerObserver> |
| 72 | process_manager_observation_{this}; |
John Delaney | 9f33f220 | 2021-02-22 22:03:44 | [diff] [blame] | 73 | |
Kevin McNee | df0c85ec | 2023-08-16 19:44:23 | [diff] [blame] | 74 | base::CallbackListSubscription web_contents_creation_subscription_ = |
| 75 | content::RegisterWebContentsCreationCallback( |
| 76 | base::BindRepeating(&NotificationSet::OnWebContentsCreated, |
| 77 | base::Unretained(this))); |
| 78 | |
John Delaney | 9f33f220 | 2021-02-22 22:03:44 | [diff] [blame] | 79 | std::map<content::WebContents*, |
| 80 | std::unique_ptr<ForwardingWebContentsObserver>> |
| 81 | web_contents_observers_; |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 82 | }; |
| 83 | |
Kevin McNee | df0c85ec | 2023-08-16 19:44:23 | [diff] [blame] | 84 | // A callback that returns true if the condition has been met and takes no |
| 85 | // arguments. |
| 86 | using ConditionCallback = base::RepeatingCallback<bool(void)>; |
| 87 | |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 88 | // Wait for |condition_| to be met. |notification_set| is the set of |
| 89 | // notifications to wait for and to check |condition| when observing. This |
| 90 | // can be NULL if we are instead waiting for a different observer method, like |
| 91 | // OnPageActionsUpdated(). |
Kevin McNee | df0c85ec | 2023-08-16 19:44:23 | [diff] [blame] | 92 | void WaitForCondition(const ConditionCallback& condition, |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 93 | NotificationSet* notification_set); |
| 94 | |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 95 | // Quits the message loop if |condition_| is met. |
| 96 | void MaybeQuit(); |
| 97 | |
Pâris | e6361d0 | 2023-07-19 09:00:43 | [diff] [blame] | 98 | raw_ptr<content::BrowserContext, AcrossTasksDanglingUntriaged> context_; |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 99 | |
| 100 | private: |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 101 | // The condition for which we are waiting. This should be checked in any |
| 102 | // observing methods that could trigger it. |
Kevin McNee | df0c85ec | 2023-08-16 19:44:23 | [diff] [blame] | 103 | ConditionCallback condition_; |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 104 | |
| 105 | // The closure to quit the currently-running message loop. |
Alan Cutter | 9b670bf | 2020-03-04 05:45:46 | [diff] [blame] | 106 | base::OnceClosure quit_closure_; |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 107 | }; |
| 108 | |
rdevlin.cronin | 2ce645a | 2016-12-01 02:54:23 | [diff] [blame] | 109 | } // namespace extensions |
| 110 | |
steel | f65a72a | 2016-10-19 22:16:45 | [diff] [blame] | 111 | #endif // EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_ |