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/files/file_path.h" | 5 #include "base/files/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/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 void Wait() { | 48 void Wait() { |
49 if (received_notification_) | 49 if (received_notification_) |
50 return; | 50 return; |
51 | 51 |
52 waiting_ = true; | 52 waiting_ = true; |
53 base::RunLoop run_loop; | 53 base::RunLoop run_loop; |
54 run_loop.Run(); | 54 run_loop.Run(); |
55 } | 55 } |
56 | 56 |
57 bool received_notification() const { return received_notification_; } | 57 bool received_notification() const { return received_notification_; } |
58 const Extension* extension() const { return extension_; } | 58 const Extension* extension() const { return extension_.get(); } |
59 const PermissionSet* permissions() const { return permissions_; } | 59 const PermissionSet* permissions() const { return permissions_.get(); } |
60 UpdatedExtensionPermissionsInfo::Reason reason() const { | 60 UpdatedExtensionPermissionsInfo::Reason reason() const { return reason_; } |
61 return reason_; | |
62 } | |
63 | 61 |
64 private: | 62 private: |
65 virtual void Observe(int type, | 63 virtual void Observe(int type, |
66 const content::NotificationSource& source, | 64 const content::NotificationSource& source, |
67 const content::NotificationDetails& details) OVERRIDE { | 65 const content::NotificationDetails& details) OVERRIDE { |
68 received_notification_ = true; | 66 received_notification_ = true; |
69 UpdatedExtensionPermissionsInfo* info = | 67 UpdatedExtensionPermissionsInfo* info = |
70 content::Details<UpdatedExtensionPermissionsInfo>(details).ptr(); | 68 content::Details<UpdatedExtensionPermissionsInfo>(details).ptr(); |
71 | 69 |
72 extension_ = info->extension; | 70 extension_ = info->extension; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 APIPermissionSet default_apis; | 120 APIPermissionSet default_apis; |
123 default_apis.insert(APIPermission::kManagement); | 121 default_apis.insert(APIPermission::kManagement); |
124 URLPatternSet default_hosts; | 122 URLPatternSet default_hosts; |
125 AddPattern(&default_hosts, "http://a.com/*"); | 123 AddPattern(&default_hosts, "http://a.com/*"); |
126 scoped_refptr<PermissionSet> default_permissions = | 124 scoped_refptr<PermissionSet> default_permissions = |
127 new PermissionSet(default_apis, default_hosts, URLPatternSet()); | 125 new PermissionSet(default_apis, default_hosts, URLPatternSet()); |
128 | 126 |
129 // Make sure it loaded properly. | 127 // Make sure it loaded properly. |
130 scoped_refptr<const PermissionSet> permissions = | 128 scoped_refptr<const PermissionSet> permissions = |
131 extension->GetActivePermissions(); | 129 extension->GetActivePermissions(); |
132 ASSERT_EQ(*default_permissions, *extension->GetActivePermissions()); | 130 ASSERT_EQ(*default_permissions.get(), *extension->GetActivePermissions()); |
133 | 131 |
134 // Add a few permissions. | 132 // Add a few permissions. |
135 APIPermissionSet apis; | 133 APIPermissionSet apis; |
136 apis.insert(APIPermission::kTab); | 134 apis.insert(APIPermission::kTab); |
137 apis.insert(APIPermission::kNotification); | 135 apis.insert(APIPermission::kNotification); |
138 URLPatternSet hosts; | 136 URLPatternSet hosts; |
139 AddPattern(&hosts, "http://*.c.com/*"); | 137 AddPattern(&hosts, "http://*.c.com/*"); |
140 | 138 |
141 scoped_refptr<PermissionSet> delta = | 139 scoped_refptr<PermissionSet> delta = |
142 new PermissionSet(apis, hosts, URLPatternSet()); | 140 new PermissionSet(apis, hosts, URLPatternSet()); |
143 | 141 |
144 PermissionsUpdaterListener listener; | 142 PermissionsUpdaterListener listener; |
145 PermissionsUpdater updater(profile_.get()); | 143 PermissionsUpdater updater(profile_.get()); |
146 updater.AddPermissions(extension.get(), delta.get()); | 144 updater.AddPermissions(extension.get(), delta.get()); |
147 | 145 |
148 listener.Wait(); | 146 listener.Wait(); |
149 | 147 |
150 // Verify that the permission notification was sent correctly. | 148 // Verify that the permission notification was sent correctly. |
151 ASSERT_TRUE(listener.received_notification()); | 149 ASSERT_TRUE(listener.received_notification()); |
152 ASSERT_EQ(extension, listener.extension()); | 150 ASSERT_EQ(extension, listener.extension()); |
153 ASSERT_EQ(UpdatedExtensionPermissionsInfo::ADDED, listener.reason()); | 151 ASSERT_EQ(UpdatedExtensionPermissionsInfo::ADDED, listener.reason()); |
154 ASSERT_EQ(*delta, *listener.permissions()); | 152 ASSERT_EQ(*delta.get(), *listener.permissions()); |
155 | 153 |
156 // Make sure the extension's active permissions reflect the change. | 154 // Make sure the extension's active permissions reflect the change. |
157 scoped_refptr<PermissionSet> active_permissions = | 155 scoped_refptr<PermissionSet> active_permissions = |
158 PermissionSet::CreateUnion(default_permissions, delta); | 156 PermissionSet::CreateUnion(default_permissions.get(), delta.get()); |
159 ASSERT_EQ(*active_permissions, *extension->GetActivePermissions()); | 157 ASSERT_EQ(*active_permissions.get(), *extension->GetActivePermissions()); |
160 | 158 |
161 // Verify that the new granted and active permissions were also stored | 159 // Verify that the new granted and active permissions were also stored |
162 // in the extension preferences. In this case, the granted permissions should | 160 // in the extension preferences. In this case, the granted permissions should |
163 // be equal to the active permissions. | 161 // be equal to the active permissions. |
164 ExtensionPrefs* prefs = service_->extension_prefs(); | 162 ExtensionPrefs* prefs = service_->extension_prefs(); |
165 scoped_refptr<PermissionSet> granted_permissions = | 163 scoped_refptr<PermissionSet> granted_permissions = |
166 active_permissions; | 164 active_permissions; |
167 | 165 |
168 scoped_refptr<PermissionSet> from_prefs = | 166 scoped_refptr<PermissionSet> from_prefs = |
169 prefs->GetActivePermissions(extension->id()); | 167 prefs->GetActivePermissions(extension->id()); |
170 ASSERT_EQ(*active_permissions, *from_prefs); | 168 ASSERT_EQ(*active_permissions.get(), *from_prefs.get()); |
171 | 169 |
172 from_prefs = prefs->GetGrantedPermissions(extension->id()); | 170 from_prefs = prefs->GetGrantedPermissions(extension->id()); |
173 ASSERT_EQ(*active_permissions, *from_prefs); | 171 ASSERT_EQ(*active_permissions.get(), *from_prefs.get()); |
174 | 172 |
175 // In the second part of the test, we'll remove the permissions that we | 173 // In the second part of the test, we'll remove the permissions that we |
176 // just added except for 'notification'. | 174 // just added except for 'notification'. |
177 apis.erase(APIPermission::kNotification); | 175 apis.erase(APIPermission::kNotification); |
178 delta = new PermissionSet(apis, hosts, URLPatternSet()); | 176 delta = new PermissionSet(apis, hosts, URLPatternSet()); |
179 | 177 |
180 listener.Reset(); | 178 listener.Reset(); |
181 updater.RemovePermissions(extension, delta); | 179 updater.RemovePermissions(extension.get(), delta.get()); |
182 listener.Wait(); | 180 listener.Wait(); |
183 | 181 |
184 // Verify that the notification was correct. | 182 // Verify that the notification was correct. |
185 ASSERT_TRUE(listener.received_notification()); | 183 ASSERT_TRUE(listener.received_notification()); |
186 ASSERT_EQ(extension, listener.extension()); | 184 ASSERT_EQ(extension, listener.extension()); |
187 ASSERT_EQ(UpdatedExtensionPermissionsInfo::REMOVED, listener.reason()); | 185 ASSERT_EQ(UpdatedExtensionPermissionsInfo::REMOVED, listener.reason()); |
188 ASSERT_EQ(*delta, *listener.permissions()); | 186 ASSERT_EQ(*delta.get(), *listener.permissions()); |
189 | 187 |
190 // Make sure the extension's active permissions reflect the change. | 188 // Make sure the extension's active permissions reflect the change. |
191 active_permissions = | 189 active_permissions = |
192 PermissionSet::CreateDifference(active_permissions, delta); | 190 PermissionSet::CreateDifference(active_permissions.get(), delta.get()); |
193 ASSERT_EQ(*active_permissions, *extension->GetActivePermissions()); | 191 ASSERT_EQ(*active_permissions.get(), *extension->GetActivePermissions()); |
194 | 192 |
195 // Verify that the extension prefs hold the new active permissions and the | 193 // Verify that the extension prefs hold the new active permissions and the |
196 // same granted permissions. | 194 // same granted permissions. |
197 from_prefs = prefs->GetActivePermissions(extension->id()); | 195 from_prefs = prefs->GetActivePermissions(extension->id()); |
198 ASSERT_EQ(*active_permissions, *from_prefs); | 196 ASSERT_EQ(*active_permissions.get(), *from_prefs.get()); |
199 | 197 |
200 from_prefs = prefs->GetGrantedPermissions(extension->id()); | 198 from_prefs = prefs->GetGrantedPermissions(extension->id()); |
201 ASSERT_EQ(*granted_permissions, *from_prefs); | 199 ASSERT_EQ(*granted_permissions.get(), *from_prefs.get()); |
202 } | 200 } |
203 | 201 |
204 } // namespace extensions | 202 } // namespace extensions |
OLD | NEW |