[go: nahoru, domu]

blob: 91cfc54893a8d19ebf82a3b198a33d67f6b39551 [file] [log] [blame]
Avi Drissman60039d42022-09-13 21:49:051// Copyright 2016 The Chromium Authors
steelf65a72a2016-10-19 22:16:452// 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 Delaney9f33f2202021-02-22 22:03:448#include <map>
steelf65a72a2016-10-19 22:16:459#include <memory>
10#include <string>
11
steelf65a72a2016-10-19 22:16:4512#include "base/callback_list.h"
Avi Drissmanbb9cd022023-01-11 01:03:2913#include "base/functional/callback.h"
Keishi Hattori0e45c022021-11-27 09:25:5214#include "base/memory/raw_ptr.h"
Sigurdur Asgeirsson834f0572021-03-24 13:24:5815#include "base/scoped_observation.h"
Kevin McNeedf0c85ec2023-08-16 19:44:2316#include "content/public/test/browser_test_utils.h"
Evan Stade922f3f1f2019-09-04 21:05:1317#include "extensions/browser/process_manager.h"
steelf65a72a2016-10-19 22:16:4518#include "extensions/browser/process_manager_observer.h"
Anton Bershanskyibcb039d2024-02-05 20:03:2319#include "extensions/common/extension_id.h"
steelf65a72a2016-10-19 22:16:4520
21namespace content {
22class BrowserContext;
John Delaney9f33f2202021-02-22 22:03:4423class WebContents;
steelf65a72a2016-10-19 22:16:4524}
25
26namespace extensions {
steelf65a72a2016-10-19 22:16:4527
28// Test helper class for observing extension-related events.
David Bertoniae4886abc2023-02-14 04:16:3429class ExtensionTestNotificationObserver {
steelf65a72a2016-10-19 22:16:4530 public:
31 explicit ExtensionTestNotificationObserver(content::BrowserContext* context);
Peter Boström951cf77e2021-09-22 00:02:5932
33 ExtensionTestNotificationObserver(const ExtensionTestNotificationObserver&) =
34 delete;
35 ExtensionTestNotificationObserver& operator=(
36 const ExtensionTestNotificationObserver&) = delete;
37
David Bertoniae4886abc2023-02-14 04:16:3438 ~ExtensionTestNotificationObserver();
limasdf184283c2017-04-14 08:22:1039
steelf65a72a2016-10-19 22:16:4540 protected:
Kevin McNeedf0c85ec2023-08-16 19:44:2341 class NotificationSet : public extensions::ProcessManagerObserver {
steelf65a72a2016-10-19 22:16:4542 public:
Kevin McNeedf0c85ec2023-08-16 19:44:2343 explicit NotificationSet(ProcessManager* manager);
Peter Boström951cf77e2021-09-22 00:02:5944
45 NotificationSet(const NotificationSet&) = delete;
46 NotificationSet& operator=(const NotificationSet&) = delete;
47
steelf65a72a2016-10-19 22:16:4548 ~NotificationSet() override;
49
Kevin McNeedf0c85ec2023-08-16 19:44:2350 // Notified any time a notification is received.
steelf65a72a2016-10-19 22:16:4551 // The details of the notification are dropped.
Fabrice de Gans-Riberi05dfdeb2021-03-22 22:46:3152 base::RepeatingClosureList& closure_list() { return closure_list_; }
steelf65a72a2016-10-19 22:16:4553
54 private:
John Delaney9f33f2202021-02-22 22:03:4455 class ForwardingWebContentsObserver;
56
steelf65a72a2016-10-19 22:16:4557 // extensions::ProcessManagerObserver:
58 void OnExtensionFrameUnregistered(
Anton Bershanskyibcb039d2024-02-05 20:03:2359 const ExtensionId& extension_id,
steelf65a72a2016-10-19 22:16:4560 content::RenderFrameHost* render_frame_host) override;
61
Kevin McNeedf0c85ec2023-08-16 19:44:2362 void OnWebContentsCreated(content::WebContents* web_contents);
63 void StartObservingWebContents(content::WebContents* web_contents);
64
65 void DidStopLoading(content::WebContents* web_contents);
John Delaney9f33f2202021-02-22 22:03:4466 void WebContentsDestroyed(content::WebContents* web_contents);
67
Fabrice de Gans-Riberi05dfdeb2021-03-22 22:46:3168 base::RepeatingClosureList closure_list_;
Kevin McNeedf0c85ec2023-08-16 19:44:2369
Sigurdur Asgeirsson834f0572021-03-24 13:24:5870 base::ScopedObservation<extensions::ProcessManager,
71 extensions::ProcessManagerObserver>
72 process_manager_observation_{this};
John Delaney9f33f2202021-02-22 22:03:4473
Kevin McNeedf0c85ec2023-08-16 19:44:2374 base::CallbackListSubscription web_contents_creation_subscription_ =
75 content::RegisterWebContentsCreationCallback(
76 base::BindRepeating(&NotificationSet::OnWebContentsCreated,
77 base::Unretained(this)));
78
John Delaney9f33f2202021-02-22 22:03:4479 std::map<content::WebContents*,
80 std::unique_ptr<ForwardingWebContentsObserver>>
81 web_contents_observers_;
steelf65a72a2016-10-19 22:16:4582 };
83
Kevin McNeedf0c85ec2023-08-16 19:44:2384 // A callback that returns true if the condition has been met and takes no
85 // arguments.
86 using ConditionCallback = base::RepeatingCallback<bool(void)>;
87
steelf65a72a2016-10-19 22:16:4588 // 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 McNeedf0c85ec2023-08-16 19:44:2392 void WaitForCondition(const ConditionCallback& condition,
steelf65a72a2016-10-19 22:16:4593 NotificationSet* notification_set);
94
steelf65a72a2016-10-19 22:16:4595 // Quits the message loop if |condition_| is met.
96 void MaybeQuit();
97
Pârise6361d02023-07-19 09:00:4398 raw_ptr<content::BrowserContext, AcrossTasksDanglingUntriaged> context_;
steelf65a72a2016-10-19 22:16:4599
100 private:
steelf65a72a2016-10-19 22:16:45101 // The condition for which we are waiting. This should be checked in any
102 // observing methods that could trigger it.
Kevin McNeedf0c85ec2023-08-16 19:44:23103 ConditionCallback condition_;
steelf65a72a2016-10-19 22:16:45104
105 // The closure to quit the currently-running message loop.
Alan Cutter9b670bf2020-03-04 05:45:46106 base::OnceClosure quit_closure_;
steelf65a72a2016-10-19 22:16:45107};
108
rdevlin.cronin2ce645a2016-12-01 02:54:23109} // namespace extensions
110
steelf65a72a2016-10-19 22:16:45111#endif // EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_