OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/json/json_file_value_serializer.h" | 6 #include "base/json/json_file_value_serializer.h" |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/extension_service_unittest.h" | 11 #include "chrome/browser/extensions/extension_service_unittest.h" |
12 #include "chrome/browser/extensions/permissions_updater.h" | 12 #include "chrome/browser/extensions/permissions_updater.h" |
13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
14 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/common/chrome_paths.h" |
15 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
16 #include "chrome/common/extensions/extension_permission_set.h" | 16 #include "chrome/common/extensions/permissions/permission_set.h" |
17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
18 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
19 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
20 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
21 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 namespace extensions { | 24 namespace extensions { |
25 | 25 |
26 namespace { | 26 namespace { |
(...skipping 18 matching lines...) Expand all Loading... |
45 void Wait() { | 45 void Wait() { |
46 if (received_notification_) | 46 if (received_notification_) |
47 return; | 47 return; |
48 | 48 |
49 waiting_ = true; | 49 waiting_ = true; |
50 ui_test_utils::RunMessageLoop(); | 50 ui_test_utils::RunMessageLoop(); |
51 } | 51 } |
52 | 52 |
53 bool received_notification() const { return received_notification_; } | 53 bool received_notification() const { return received_notification_; } |
54 const Extension* extension() const { return extension_; } | 54 const Extension* extension() const { return extension_; } |
55 const ExtensionPermissionSet* permissions() const { return permissions_; } | 55 const PermissionSet* permissions() const { return permissions_; } |
56 UpdatedExtensionPermissionsInfo::Reason reason() const { | 56 UpdatedExtensionPermissionsInfo::Reason reason() const { |
57 return reason_; | 57 return reason_; |
58 } | 58 } |
59 | 59 |
60 private: | 60 private: |
61 virtual void Observe(int type, | 61 virtual void Observe(int type, |
62 const content::NotificationSource& source, | 62 const content::NotificationSource& source, |
63 const content::NotificationDetails& details) OVERRIDE { | 63 const content::NotificationDetails& details) OVERRIDE { |
64 received_notification_ = true; | 64 received_notification_ = true; |
65 UpdatedExtensionPermissionsInfo* info = | 65 UpdatedExtensionPermissionsInfo* info = |
66 content::Details<UpdatedExtensionPermissionsInfo>(details).ptr(); | 66 content::Details<UpdatedExtensionPermissionsInfo>(details).ptr(); |
67 | 67 |
68 extension_ = info->extension; | 68 extension_ = info->extension; |
69 permissions_ = info->permissions; | 69 permissions_ = info->permissions; |
70 reason_ = info->reason; | 70 reason_ = info->reason; |
71 | 71 |
72 if (waiting_) { | 72 if (waiting_) { |
73 waiting_ = false; | 73 waiting_ = false; |
74 MessageLoopForUI::current()->Quit(); | 74 MessageLoopForUI::current()->Quit(); |
75 } | 75 } |
76 } | 76 } |
77 | 77 |
78 bool received_notification_; | 78 bool received_notification_; |
79 bool waiting_; | 79 bool waiting_; |
80 content::NotificationRegistrar registrar_; | 80 content::NotificationRegistrar registrar_; |
81 scoped_refptr<const Extension> extension_; | 81 scoped_refptr<const Extension> extension_; |
82 scoped_refptr<const ExtensionPermissionSet> permissions_; | 82 scoped_refptr<const PermissionSet> permissions_; |
83 UpdatedExtensionPermissionsInfo::Reason reason_; | 83 UpdatedExtensionPermissionsInfo::Reason reason_; |
84 }; | 84 }; |
85 | 85 |
86 class PermissionsUpdaterTest : public ExtensionServiceTestBase { | 86 class PermissionsUpdaterTest : public ExtensionServiceTestBase { |
87 }; | 87 }; |
88 | 88 |
89 scoped_refptr<Extension> LoadManifest(std::string* error) { | 89 scoped_refptr<Extension> LoadManifest(std::string* error) { |
90 FilePath path; | 90 FilePath path; |
91 PathService::Get(chrome::DIR_TEST_DATA, &path); | 91 PathService::Get(chrome::DIR_TEST_DATA, &path); |
92 path = path.AppendASCII("extensions") | 92 path = path.AppendASCII("extensions") |
(...skipping 25 matching lines...) Expand all Loading... |
118 // GrantActivePermissions and UpdateActivePermissions are used by | 118 // GrantActivePermissions and UpdateActivePermissions are used by |
119 // AddPermissions. | 119 // AddPermissions. |
120 TEST_F(PermissionsUpdaterTest, AddAndRemovePermissions) { | 120 TEST_F(PermissionsUpdaterTest, AddAndRemovePermissions) { |
121 InitializeEmptyExtensionService(); | 121 InitializeEmptyExtensionService(); |
122 | 122 |
123 // Load the test extension. | 123 // Load the test extension. |
124 std::string error; | 124 std::string error; |
125 scoped_refptr<Extension> extension = LoadManifest(&error); | 125 scoped_refptr<Extension> extension = LoadManifest(&error); |
126 ASSERT_TRUE(error.empty()) << error; | 126 ASSERT_TRUE(error.empty()) << error; |
127 | 127 |
128 ExtensionAPIPermissionSet default_apis; | 128 APIPermissionSet default_apis; |
129 default_apis.insert(ExtensionAPIPermission::kManagement); | 129 default_apis.insert(APIPermission::kManagement); |
130 URLPatternSet default_hosts; | 130 URLPatternSet default_hosts; |
131 AddPattern(&default_hosts, "http://a.com/*"); | 131 AddPattern(&default_hosts, "http://a.com/*"); |
132 scoped_refptr<ExtensionPermissionSet> default_permissions = | 132 scoped_refptr<PermissionSet> default_permissions = |
133 new ExtensionPermissionSet(default_apis, default_hosts, URLPatternSet()); | 133 new PermissionSet(default_apis, default_hosts, URLPatternSet()); |
134 | 134 |
135 // Make sure it loaded properly. | 135 // Make sure it loaded properly. |
136 scoped_refptr<const ExtensionPermissionSet> permissions = | 136 scoped_refptr<const PermissionSet> permissions = |
137 extension->GetActivePermissions(); | 137 extension->GetActivePermissions(); |
138 ASSERT_EQ(*default_permissions, *extension->GetActivePermissions()); | 138 ASSERT_EQ(*default_permissions, *extension->GetActivePermissions()); |
139 | 139 |
140 // Add a few permissions. | 140 // Add a few permissions. |
141 ExtensionAPIPermissionSet apis; | 141 APIPermissionSet apis; |
142 apis.insert(ExtensionAPIPermission::kTab); | 142 apis.insert(APIPermission::kTab); |
143 apis.insert(ExtensionAPIPermission::kNotification); | 143 apis.insert(APIPermission::kNotification); |
144 URLPatternSet hosts; | 144 URLPatternSet hosts; |
145 AddPattern(&hosts, "http://*.c.com/*"); | 145 AddPattern(&hosts, "http://*.c.com/*"); |
146 | 146 |
147 scoped_refptr<ExtensionPermissionSet> delta = | 147 scoped_refptr<PermissionSet> delta = |
148 new ExtensionPermissionSet(apis, hosts, URLPatternSet()); | 148 new PermissionSet(apis, hosts, URLPatternSet()); |
149 | 149 |
150 PermissionsUpdaterListener listener; | 150 PermissionsUpdaterListener listener; |
151 PermissionsUpdater updater(profile_.get()); | 151 PermissionsUpdater updater(profile_.get()); |
152 updater.AddPermissions(extension.get(), delta.get()); | 152 updater.AddPermissions(extension.get(), delta.get()); |
153 | 153 |
154 listener.Wait(); | 154 listener.Wait(); |
155 | 155 |
156 // Verify that the permission notification was sent correctly. | 156 // Verify that the permission notification was sent correctly. |
157 ASSERT_TRUE(listener.received_notification()); | 157 ASSERT_TRUE(listener.received_notification()); |
158 ASSERT_EQ(extension, listener.extension()); | 158 ASSERT_EQ(extension, listener.extension()); |
159 ASSERT_EQ(UpdatedExtensionPermissionsInfo::ADDED, listener.reason()); | 159 ASSERT_EQ(UpdatedExtensionPermissionsInfo::ADDED, listener.reason()); |
160 ASSERT_EQ(*delta, *listener.permissions()); | 160 ASSERT_EQ(*delta, *listener.permissions()); |
161 | 161 |
162 // Make sure the extension's active permissions reflect the change. | 162 // Make sure the extension's active permissions reflect the change. |
163 scoped_refptr<ExtensionPermissionSet> active_permissions = | 163 scoped_refptr<PermissionSet> active_permissions = |
164 ExtensionPermissionSet::CreateUnion(default_permissions, delta); | 164 PermissionSet::CreateUnion(default_permissions, delta); |
165 ASSERT_EQ(*active_permissions, *extension->GetActivePermissions()); | 165 ASSERT_EQ(*active_permissions, *extension->GetActivePermissions()); |
166 | 166 |
167 // Verify that the new granted and active permissions were also stored | 167 // Verify that the new granted and active permissions were also stored |
168 // in the extension preferences. In this case, the granted permissions should | 168 // in the extension preferences. In this case, the granted permissions should |
169 // be equal to the active permissions. | 169 // be equal to the active permissions. |
170 ExtensionPrefs* prefs = service_->extension_prefs(); | 170 ExtensionPrefs* prefs = service_->extension_prefs(); |
171 scoped_refptr<ExtensionPermissionSet> granted_permissions = | 171 scoped_refptr<PermissionSet> granted_permissions = |
172 active_permissions; | 172 active_permissions; |
173 | 173 |
174 scoped_refptr<ExtensionPermissionSet> from_prefs = | 174 scoped_refptr<PermissionSet> from_prefs = |
175 prefs->GetActivePermissions(extension->id()); | 175 prefs->GetActivePermissions(extension->id()); |
176 ASSERT_EQ(*active_permissions, *from_prefs); | 176 ASSERT_EQ(*active_permissions, *from_prefs); |
177 | 177 |
178 from_prefs = prefs->GetGrantedPermissions(extension->id()); | 178 from_prefs = prefs->GetGrantedPermissions(extension->id()); |
179 ASSERT_EQ(*active_permissions, *from_prefs); | 179 ASSERT_EQ(*active_permissions, *from_prefs); |
180 | 180 |
181 // In the second part of the test, we'll remove the permissions that we | 181 // In the second part of the test, we'll remove the permissions that we |
182 // just added except for 'notification'. | 182 // just added except for 'notification'. |
183 apis.erase(ExtensionAPIPermission::kNotification); | 183 apis.erase(APIPermission::kNotification); |
184 delta = new ExtensionPermissionSet(apis, hosts, URLPatternSet()); | 184 delta = new PermissionSet(apis, hosts, URLPatternSet()); |
185 | 185 |
186 listener.Reset(); | 186 listener.Reset(); |
187 updater.RemovePermissions(extension, delta); | 187 updater.RemovePermissions(extension, delta); |
188 listener.Wait(); | 188 listener.Wait(); |
189 | 189 |
190 // Verify that the notification was correct. | 190 // Verify that the notification was correct. |
191 ASSERT_TRUE(listener.received_notification()); | 191 ASSERT_TRUE(listener.received_notification()); |
192 ASSERT_EQ(extension, listener.extension()); | 192 ASSERT_EQ(extension, listener.extension()); |
193 ASSERT_EQ(UpdatedExtensionPermissionsInfo::REMOVED, listener.reason()); | 193 ASSERT_EQ(UpdatedExtensionPermissionsInfo::REMOVED, listener.reason()); |
194 ASSERT_EQ(*delta, *listener.permissions()); | 194 ASSERT_EQ(*delta, *listener.permissions()); |
195 | 195 |
196 // Make sure the extension's active permissions reflect the change. | 196 // Make sure the extension's active permissions reflect the change. |
197 active_permissions = | 197 active_permissions = |
198 ExtensionPermissionSet::CreateDifference(active_permissions, delta); | 198 PermissionSet::CreateDifference(active_permissions, delta); |
199 ASSERT_EQ(*active_permissions, *extension->GetActivePermissions()); | 199 ASSERT_EQ(*active_permissions, *extension->GetActivePermissions()); |
200 | 200 |
201 // Verify that the extension prefs hold the new active permissions and the | 201 // Verify that the extension prefs hold the new active permissions and the |
202 // same granted permissions. | 202 // same granted permissions. |
203 from_prefs = prefs->GetActivePermissions(extension->id()); | 203 from_prefs = prefs->GetActivePermissions(extension->id()); |
204 ASSERT_EQ(*active_permissions, *from_prefs); | 204 ASSERT_EQ(*active_permissions, *from_prefs); |
205 | 205 |
206 from_prefs = prefs->GetGrantedPermissions(extension->id()); | 206 from_prefs = prefs->GetGrantedPermissions(extension->id()); |
207 ASSERT_EQ(*granted_permissions, *from_prefs); | 207 ASSERT_EQ(*granted_permissions, *from_prefs); |
208 } | 208 } |
209 | 209 |
210 } // namespace extensions | 210 } // namespace extensions |
OLD | NEW |