1a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// found in the LICENSE file.
4a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
5a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "chrome/browser/extensions/extension_notification_observer.h"
6a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
7a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include <vector>
8a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
9a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/logging.h"
10a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/strings/stringprintf.h"
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension.h"
12a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
13a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)namespace extensions {
14a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
15a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)namespace {
16a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
17a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)std::string Str(const std::vector<chrome::NotificationType>& types) {
18a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  std::string str = "[";
19a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  bool needs_comma = false;
20a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  for (std::vector<chrome::NotificationType>::const_iterator it =
21a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)       types.begin(); it != types.end(); ++it) {
22a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    if (needs_comma)
23a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      str += ",";
24a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    needs_comma = true;
25a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    str += base::StringPrintf("%d", *it);
26a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
27a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  str += "]";
28a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return str;
29a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
30a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
31a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}  // namespace
32a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
33a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)ExtensionNotificationObserver::ExtensionNotificationObserver(
34a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    content::NotificationSource source,
35a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    const std::set<std::string>& extension_ids)
36a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    : extension_ids_(extension_ids) {
370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  registrar_.Add(
380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      this, chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, source);
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  registrar_.Add(
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      this, chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, source);
4123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  registrar_.Add(
4223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, source);
43a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
44a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
45a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)ExtensionNotificationObserver::~ExtensionNotificationObserver() {}
46a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
47a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)testing::AssertionResult ExtensionNotificationObserver::CheckNotifications() {
48a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return CheckNotifications(std::vector<chrome::NotificationType>());
49a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
50a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
51a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)testing::AssertionResult ExtensionNotificationObserver::CheckNotifications(
52a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    chrome::NotificationType type) {
53a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return CheckNotifications(std::vector<chrome::NotificationType>(1, type));
54a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
55a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
56a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)testing::AssertionResult ExtensionNotificationObserver::CheckNotifications(
57a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    chrome::NotificationType t1,
58a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    chrome::NotificationType t2) {
59a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  std::vector<chrome::NotificationType> types;
60a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  types.push_back(t1);
61a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  types.push_back(t2);
62a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return CheckNotifications(types);
63a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
64a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
65a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)testing::AssertionResult ExtensionNotificationObserver::CheckNotifications(
66a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    chrome::NotificationType t1,
67a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    chrome::NotificationType t2,
68a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    chrome::NotificationType t3) {
69a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  std::vector<chrome::NotificationType> types;
70a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  types.push_back(t1);
71a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  types.push_back(t2);
72a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  types.push_back(t3);
73a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return CheckNotifications(types);
74a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
75a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
76a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)testing::AssertionResult ExtensionNotificationObserver::CheckNotifications(
77a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    chrome::NotificationType t1,
78a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    chrome::NotificationType t2,
79a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    chrome::NotificationType t3,
80a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    chrome::NotificationType t4,
81a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    chrome::NotificationType t5,
82a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    chrome::NotificationType t6) {
83a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  std::vector<chrome::NotificationType> types;
84a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  types.push_back(t1);
85a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  types.push_back(t2);
86a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  types.push_back(t3);
87a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  types.push_back(t4);
88a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  types.push_back(t5);
89a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  types.push_back(t6);
90a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return CheckNotifications(types);
91a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
92a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
93a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// content::NotificationObserver implementation.
94a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)void ExtensionNotificationObserver::Observe(
95a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    int type,
96a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    const content::NotificationSource& source,
97a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    const content::NotificationDetails& details) {
98a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  switch (type) {
99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED: {
100a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      const Extension* extension =
101a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)          content::Details<const InstalledExtensionInfo>(details)->extension;
102a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      if (extension_ids_.count(extension->id()))
103a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        notifications_.push_back(static_cast<chrome::NotificationType>(type));
104a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      break;
105a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    }
106a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
1070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: {
108a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      const Extension* extension =
109a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)          content::Details<const Extension>(details).ptr();
110a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      if (extension_ids_.count(extension->id()))
111a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        notifications_.push_back(static_cast<chrome::NotificationType>(type));
112a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      break;
113a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    }
114a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
11523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
116a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      UnloadedExtensionInfo* reason =
117a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)          content::Details<UnloadedExtensionInfo>(details).ptr();
118a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      if (extension_ids_.count(reason->extension->id())) {
119a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        notifications_.push_back(static_cast<chrome::NotificationType>(type));
120a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        // The only way that extensions are unloaded in these tests is
121a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        // by blacklisting.
1221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        EXPECT_EQ(UnloadedExtensionInfo::REASON_BLACKLIST,
123a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                  reason->reason);
124a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      }
125a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      break;
126a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    }
127a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
128a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    default:
129a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      NOTREACHED();
130a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      break;
131a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
132a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
133a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
134a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)testing::AssertionResult ExtensionNotificationObserver::CheckNotifications(
135a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    const std::vector<chrome::NotificationType>& types) {
136a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  testing::AssertionResult result = (notifications_ == types) ?
137a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      testing::AssertionSuccess() :
138a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      testing::AssertionFailure() << "Expected " << Str(types) << ", " <<
139a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                                     "Got " << Str(notifications_);
140a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  notifications_.clear();
141a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return result;
142a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
143a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
144a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}  // namespace extensions
145