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