| 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<extensions::NotificationType>& 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<extensions::NotificationType>::const_iterator it = |
| 21 types.begin(); it != types.end(); ++it) { | 21 types.begin(); |
| 22 it != types.end(); |
| 23 ++it) { |
| 22 if (needs_comma) | 24 if (needs_comma) |
| 23 str += ","; | 25 str += ","; |
| 24 needs_comma = true; | 26 needs_comma = true; |
| 25 str += base::StringPrintf("%d", *it); | 27 str += base::StringPrintf("%d", *it); |
| 26 } | 28 } |
| 27 str += "]"; | 29 str += "]"; |
| 28 return str; | 30 return str; |
| 29 } | 31 } |
| 30 | 32 |
| 31 } // namespace | 33 } // namespace |
| 32 | 34 |
| 33 ExtensionNotificationObserver::ExtensionNotificationObserver( | 35 ExtensionNotificationObserver::ExtensionNotificationObserver( |
| 34 content::NotificationSource source, | 36 content::NotificationSource source, |
| 35 const std::set<std::string>& extension_ids) | 37 const std::set<std::string>& extension_ids) |
| 36 : extension_ids_(extension_ids) { | 38 : extension_ids_(extension_ids) { |
| 37 registrar_.Add( | 39 registrar_.Add( |
| 38 this, chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, source); | 40 this, extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, source); |
| 39 registrar_.Add(this, | |
| 40 chrome::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED, | |
| 41 source); | |
| 42 registrar_.Add( | 41 registrar_.Add( |
| 43 this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, source); | 42 this, |
| 43 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED, |
| 44 source); |
| 45 registrar_.Add( |
| 46 this, extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, source); |
| 44 } | 47 } |
| 45 | 48 |
| 46 ExtensionNotificationObserver::~ExtensionNotificationObserver() {} | 49 ExtensionNotificationObserver::~ExtensionNotificationObserver() {} |
| 47 | 50 |
| 48 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications() { | 51 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications() { |
| 49 return CheckNotifications(std::vector<chrome::NotificationType>()); | 52 return CheckNotifications(std::vector<extensions::NotificationType>()); |
| 50 } | 53 } |
| 51 | 54 |
| 52 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( | 55 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( |
| 53 chrome::NotificationType type) { | 56 extensions::NotificationType type) { |
| 54 return CheckNotifications(std::vector<chrome::NotificationType>(1, type)); | 57 return CheckNotifications(std::vector<extensions::NotificationType>(1, type)); |
| 55 } | 58 } |
| 56 | 59 |
| 57 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( | 60 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( |
| 58 chrome::NotificationType t1, | 61 extensions::NotificationType t1, |
| 59 chrome::NotificationType t2) { | 62 extensions::NotificationType t2) { |
| 60 std::vector<chrome::NotificationType> types; | 63 std::vector<extensions::NotificationType> types; |
| 61 types.push_back(t1); | 64 types.push_back(t1); |
| 62 types.push_back(t2); | 65 types.push_back(t2); |
| 63 return CheckNotifications(types); | 66 return CheckNotifications(types); |
| 64 } | 67 } |
| 65 | 68 |
| 66 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( | 69 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( |
| 67 chrome::NotificationType t1, | 70 extensions::NotificationType t1, |
| 68 chrome::NotificationType t2, | 71 extensions::NotificationType t2, |
| 69 chrome::NotificationType t3) { | 72 extensions::NotificationType t3) { |
| 70 std::vector<chrome::NotificationType> types; | 73 std::vector<extensions::NotificationType> types; |
| 71 types.push_back(t1); | 74 types.push_back(t1); |
| 72 types.push_back(t2); | 75 types.push_back(t2); |
| 73 types.push_back(t3); | 76 types.push_back(t3); |
| 74 return CheckNotifications(types); | 77 return CheckNotifications(types); |
| 75 } | 78 } |
| 76 | 79 |
| 77 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( | 80 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( |
| 78 chrome::NotificationType t1, | 81 extensions::NotificationType t1, |
| 79 chrome::NotificationType t2, | 82 extensions::NotificationType t2, |
| 80 chrome::NotificationType t3, | 83 extensions::NotificationType t3, |
| 81 chrome::NotificationType t4, | 84 extensions::NotificationType t4, |
| 82 chrome::NotificationType t5, | 85 extensions::NotificationType t5, |
| 83 chrome::NotificationType t6) { | 86 extensions::NotificationType t6) { |
| 84 std::vector<chrome::NotificationType> types; | 87 std::vector<extensions::NotificationType> types; |
| 85 types.push_back(t1); | 88 types.push_back(t1); |
| 86 types.push_back(t2); | 89 types.push_back(t2); |
| 87 types.push_back(t3); | 90 types.push_back(t3); |
| 88 types.push_back(t4); | 91 types.push_back(t4); |
| 89 types.push_back(t5); | 92 types.push_back(t5); |
| 90 types.push_back(t6); | 93 types.push_back(t6); |
| 91 return CheckNotifications(types); | 94 return CheckNotifications(types); |
| 92 } | 95 } |
| 93 | 96 |
| 94 // content::NotificationObserver implementation. | 97 // content::NotificationObserver implementation. |
| 95 void ExtensionNotificationObserver::Observe( | 98 void ExtensionNotificationObserver::Observe( |
| 96 int type, | 99 int type, |
| 97 const content::NotificationSource& source, | 100 const content::NotificationSource& source, |
| 98 const content::NotificationDetails& details) { | 101 const content::NotificationDetails& details) { |
| 99 switch (type) { | 102 switch (type) { |
| 100 case chrome::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED: { | 103 case extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED: { |
| 101 const Extension* extension = | 104 const Extension* extension = |
| 102 content::Details<const InstalledExtensionInfo>(details)->extension; | 105 content::Details<const InstalledExtensionInfo>(details)->extension; |
| 103 if (extension_ids_.count(extension->id())) | 106 if (extension_ids_.count(extension->id())) |
| 104 notifications_.push_back(static_cast<chrome::NotificationType>(type)); | 107 notifications_.push_back( |
| 108 static_cast<extensions::NotificationType>(type)); |
| 105 break; | 109 break; |
| 106 } | 110 } |
| 107 | 111 |
| 108 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { | 112 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { |
| 109 const Extension* extension = | 113 const Extension* extension = |
| 110 content::Details<const Extension>(details).ptr(); | 114 content::Details<const Extension>(details).ptr(); |
| 111 if (extension_ids_.count(extension->id())) | 115 if (extension_ids_.count(extension->id())) |
| 112 notifications_.push_back(static_cast<chrome::NotificationType>(type)); | 116 notifications_.push_back( |
| 117 static_cast<extensions::NotificationType>(type)); |
| 113 break; | 118 break; |
| 114 } | 119 } |
| 115 | 120 |
| 116 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | 121 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { |
| 117 UnloadedExtensionInfo* reason = | 122 UnloadedExtensionInfo* reason = |
| 118 content::Details<UnloadedExtensionInfo>(details).ptr(); | 123 content::Details<UnloadedExtensionInfo>(details).ptr(); |
| 119 if (extension_ids_.count(reason->extension->id())) { | 124 if (extension_ids_.count(reason->extension->id())) { |
| 120 notifications_.push_back(static_cast<chrome::NotificationType>(type)); | 125 notifications_.push_back( |
| 126 static_cast<extensions::NotificationType>(type)); |
| 121 // The only way that extensions are unloaded in these tests is | 127 // The only way that extensions are unloaded in these tests is |
| 122 // by blacklisting. | 128 // by blacklisting. |
| 123 EXPECT_EQ(UnloadedExtensionInfo::REASON_BLACKLIST, | 129 EXPECT_EQ(UnloadedExtensionInfo::REASON_BLACKLIST, |
| 124 reason->reason); | 130 reason->reason); |
| 125 } | 131 } |
| 126 break; | 132 break; |
| 127 } | 133 } |
| 128 | 134 |
| 129 default: | 135 default: |
| 130 NOTREACHED(); | 136 NOTREACHED(); |
| 131 break; | 137 break; |
| 132 } | 138 } |
| 133 } | 139 } |
| 134 | 140 |
| 135 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( | 141 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( |
| 136 const std::vector<chrome::NotificationType>& types) { | 142 const std::vector<extensions::NotificationType>& types) { |
| 137 testing::AssertionResult result = (notifications_ == types) ? | 143 testing::AssertionResult result = (notifications_ == types) ? |
| 138 testing::AssertionSuccess() : | 144 testing::AssertionSuccess() : |
| 139 testing::AssertionFailure() << "Expected " << Str(types) << ", " << | 145 testing::AssertionFailure() << "Expected " << Str(types) << ", " << |
| 140 "Got " << Str(notifications_); | 146 "Got " << Str(notifications_); |
| 141 notifications_.clear(); | 147 notifications_.clear(); |
| 142 return result; | 148 return result; |
| 143 } | 149 } |
| 144 | 150 |
| 145 } // namespace extensions | 151 } // namespace extensions |
| OLD | NEW |