forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_background_page_ready_observer.cc
71 lines (60 loc) · 2.45 KB
/
test_background_page_ready_observer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/test/test_background_page_ready_observer.h"
#include "base/bind.h"
#include "content/public/browser/notification_source.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/notification_types.h"
#include "extensions/browser/runtime_data.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
namespace extensions {
namespace {
bool IsExtensionBackgroundPageReady(
content::BrowserContext* browser_context,
const extensions::ExtensionId& extension_id) {
const auto* const extension_registry =
extensions::ExtensionRegistry::Get(browser_context);
if (!extension_registry)
return false;
const extensions::Extension* const extension =
extension_registry->GetInstalledExtension(extension_id);
if (!extension)
return false;
auto* const extension_system =
extensions::ExtensionSystem::Get(browser_context);
if (!extension_system)
return false;
return extension_system->runtime_data()->IsBackgroundPageReady(extension);
}
} // namespace
ExtensionBackgroundPageReadyObserver::ExtensionBackgroundPageReadyObserver(
content::BrowserContext* browser_context,
const extensions::ExtensionId& extension_id)
: browser_context_(browser_context),
extension_id_(extension_id),
notification_observer_(
extensions::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
base::BindRepeating(
&ExtensionBackgroundPageReadyObserver::IsNotificationRelevant,
base::Unretained(this))) {}
ExtensionBackgroundPageReadyObserver::~ExtensionBackgroundPageReadyObserver() =
default;
void ExtensionBackgroundPageReadyObserver::Wait() {
notification_observer_.Wait();
}
bool ExtensionBackgroundPageReadyObserver::IsNotificationRelevant(
const content::NotificationSource& source,
const content::NotificationDetails& /*details*/) const {
if (content::Source<const extensions::Extension>(source)->id() !=
extension_id_) {
return false;
}
// Double-check via the extension system, since the notification could be
// for a different profile.
return IsExtensionBackgroundPageReady(browser_context_, extension_id_);
}
} // namespace extensions