Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: chrome/browser/extensions/active_tab_unittest.cc

Issue 16295003: Update chrome/ to use scoped_refptr<T>::get() rather than implicit "operator T*" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <string> 5 #include <string>
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/values.h" 10 #include "base/values.h"
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 78
79 bool IsAllowed(const scoped_refptr<const Extension>& extension, 79 bool IsAllowed(const scoped_refptr<const Extension>& extension,
80 const GURL& url) { 80 const GURL& url) {
81 return IsAllowed(extension, url, tab_id()); 81 return IsAllowed(extension, url, tab_id());
82 } 82 }
83 83
84 bool IsAllowed(const scoped_refptr<const Extension>& extension, 84 bool IsAllowed(const scoped_refptr<const Extension>& extension,
85 const GURL& url, 85 const GURL& url,
86 int tab_id) { 86 int tab_id) {
87 return PermissionsData::CanExecuteScriptOnPage( 87 return PermissionsData::CanExecuteScriptOnPage(
88 extension, url, url, tab_id, NULL, -1, NULL) && 88 extension.get(), url, url, tab_id, NULL, -1, NULL) &&
89 PermissionsData::CanCaptureVisiblePage( 89 PermissionsData::CanCaptureVisiblePage(
90 extension, url, tab_id, NULL) && 90 extension.get(), url, tab_id, NULL) &&
91 HasTabsPermission(extension, tab_id); 91 HasTabsPermission(extension, tab_id);
92 } 92 }
93 93
94 bool IsBlocked(const scoped_refptr<const Extension>& extension, 94 bool IsBlocked(const scoped_refptr<const Extension>& extension,
95 const GURL& url) { 95 const GURL& url) {
96 return IsBlocked(extension, url, tab_id()); 96 return IsBlocked(extension, url, tab_id());
97 } 97 }
98 98
99 bool IsBlocked(const scoped_refptr<const Extension>& extension, 99 bool IsBlocked(const scoped_refptr<const Extension>& extension,
100 const GURL& url, 100 const GURL& url,
101 int tab_id) { 101 int tab_id) {
102 // Note: can't check HasTabsPermission because it isn't URL specific. 102 // Note: can't check HasTabsPermission because it isn't URL specific.
103 return !PermissionsData::CanExecuteScriptOnPage( 103 return !PermissionsData::CanExecuteScriptOnPage(
104 extension, url, url, tab_id, NULL, -1, NULL) && 104 extension.get(), url, url, tab_id, NULL, -1, NULL) &&
105 !PermissionsData::CanCaptureVisiblePage( 105 !PermissionsData::CanCaptureVisiblePage(
106 extension, url, tab_id, NULL); 106 extension.get(), url, tab_id, NULL);
107 } 107 }
108 108
109 bool HasTabsPermission(const scoped_refptr<const Extension>& extension) { 109 bool HasTabsPermission(const scoped_refptr<const Extension>& extension) {
110 return HasTabsPermission(extension, tab_id()); 110 return HasTabsPermission(extension, tab_id());
111 } 111 }
112 112
113 bool HasTabsPermission(const scoped_refptr<const Extension>& extension, 113 bool HasTabsPermission(const scoped_refptr<const Extension>& extension,
114 int tab_id) { 114 int tab_id) {
115 return PermissionsData::HasAPIPermissionForTab( 115 return PermissionsData::HasAPIPermissionForTab(
116 extension.get(), tab_id, APIPermission::kTab); 116 extension.get(), tab_id, APIPermission::kTab);
(...skipping 18 matching lines...) Expand all
135 135
136 // No access unless it's been granted. 136 // No access unless it's been granted.
137 EXPECT_TRUE(IsBlocked(extension, google)); 137 EXPECT_TRUE(IsBlocked(extension, google));
138 EXPECT_TRUE(IsBlocked(another_extension, google)); 138 EXPECT_TRUE(IsBlocked(another_extension, google));
139 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 139 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
140 140
141 EXPECT_FALSE(HasTabsPermission(extension)); 141 EXPECT_FALSE(HasTabsPermission(extension));
142 EXPECT_FALSE(HasTabsPermission(another_extension)); 142 EXPECT_FALSE(HasTabsPermission(another_extension));
143 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab)); 143 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab));
144 144
145 active_tab_permission_granter()->GrantIfRequested(extension); 145 active_tab_permission_granter()->GrantIfRequested(extension.get());
146 active_tab_permission_granter()->GrantIfRequested( 146 active_tab_permission_granter()->GrantIfRequested(
147 extension_without_active_tab); 147 extension_without_active_tab.get());
148 148
149 // Granted to extension and extension_without_active_tab, but the latter 149 // Granted to extension and extension_without_active_tab, but the latter
150 // doesn't have the activeTab permission so not granted. 150 // doesn't have the activeTab permission so not granted.
151 EXPECT_TRUE(IsAllowed(extension, google)); 151 EXPECT_TRUE(IsAllowed(extension, google));
152 EXPECT_TRUE(IsBlocked(another_extension, google)); 152 EXPECT_TRUE(IsBlocked(another_extension, google));
153 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 153 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
154 154
155 // Other subdomains shouldn't be given access. 155 // Other subdomains shouldn't be given access.
156 GURL mail_google("http://mail.google.com"); 156 GURL mail_google("http://mail.google.com");
157 EXPECT_TRUE(IsBlocked(extension, mail_google)); 157 EXPECT_TRUE(IsBlocked(extension, mail_google));
158 EXPECT_TRUE(IsBlocked(another_extension, mail_google)); 158 EXPECT_TRUE(IsBlocked(another_extension, mail_google));
159 EXPECT_TRUE(IsBlocked(extension_without_active_tab, mail_google)); 159 EXPECT_TRUE(IsBlocked(extension_without_active_tab, mail_google));
160 160
161 // Reloading the page should clear the active permissions. 161 // Reloading the page should clear the active permissions.
162 Reload(); 162 Reload();
163 163
164 EXPECT_TRUE(IsBlocked(extension, google)); 164 EXPECT_TRUE(IsBlocked(extension, google));
165 EXPECT_TRUE(IsBlocked(another_extension, google)); 165 EXPECT_TRUE(IsBlocked(another_extension, google));
166 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 166 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
167 167
168 EXPECT_FALSE(HasTabsPermission(extension)); 168 EXPECT_FALSE(HasTabsPermission(extension));
169 EXPECT_FALSE(HasTabsPermission(another_extension)); 169 EXPECT_FALSE(HasTabsPermission(another_extension));
170 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab)); 170 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab));
171 171
172 // But they should still be able to be granted again. 172 // But they should still be able to be granted again.
173 active_tab_permission_granter()->GrantIfRequested(extension); 173 active_tab_permission_granter()->GrantIfRequested(extension.get());
174 174
175 EXPECT_TRUE(IsAllowed(extension, google)); 175 EXPECT_TRUE(IsAllowed(extension, google));
176 EXPECT_TRUE(IsBlocked(another_extension, google)); 176 EXPECT_TRUE(IsBlocked(another_extension, google));
177 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 177 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
178 178
179 // And grant a few more times redundantly for good measure. 179 // And grant a few more times redundantly for good measure.
180 active_tab_permission_granter()->GrantIfRequested(extension); 180 active_tab_permission_granter()->GrantIfRequested(extension.get());
181 active_tab_permission_granter()->GrantIfRequested(extension); 181 active_tab_permission_granter()->GrantIfRequested(extension.get());
182 active_tab_permission_granter()->GrantIfRequested(another_extension); 182 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
183 active_tab_permission_granter()->GrantIfRequested(another_extension); 183 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
184 active_tab_permission_granter()->GrantIfRequested(another_extension); 184 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
185 active_tab_permission_granter()->GrantIfRequested(extension); 185 active_tab_permission_granter()->GrantIfRequested(extension.get());
186 active_tab_permission_granter()->GrantIfRequested(extension); 186 active_tab_permission_granter()->GrantIfRequested(extension.get());
187 active_tab_permission_granter()->GrantIfRequested(another_extension); 187 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
188 active_tab_permission_granter()->GrantIfRequested(another_extension); 188 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
189 189
190 EXPECT_TRUE(IsAllowed(extension, google)); 190 EXPECT_TRUE(IsAllowed(extension, google));
191 EXPECT_TRUE(IsAllowed(another_extension, google)); 191 EXPECT_TRUE(IsAllowed(another_extension, google));
192 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 192 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
193 193
194 // Navigating to a new URL should clear the active permissions. 194 // Navigating to a new URL should clear the active permissions.
195 GURL chromium("http://www.chromium.org"); 195 GURL chromium("http://www.chromium.org");
196 NavigateAndCommit(chromium); 196 NavigateAndCommit(chromium);
197 197
198 EXPECT_TRUE(IsBlocked(extension, google)); 198 EXPECT_TRUE(IsBlocked(extension, google));
199 EXPECT_TRUE(IsBlocked(another_extension, google)); 199 EXPECT_TRUE(IsBlocked(another_extension, google));
200 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 200 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
201 201
202 EXPECT_TRUE(IsBlocked(extension, chromium)); 202 EXPECT_TRUE(IsBlocked(extension, chromium));
203 EXPECT_TRUE(IsBlocked(another_extension, chromium)); 203 EXPECT_TRUE(IsBlocked(another_extension, chromium));
204 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium)); 204 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium));
205 205
206 EXPECT_FALSE(HasTabsPermission(extension)); 206 EXPECT_FALSE(HasTabsPermission(extension));
207 EXPECT_FALSE(HasTabsPermission(another_extension)); 207 EXPECT_FALSE(HasTabsPermission(another_extension));
208 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab)); 208 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab));
209 209
210 // Should be able to grant to multiple extensions at the same time (if they 210 // Should be able to grant to multiple extensions at the same time (if they
211 // have the activeTab permission, of course). 211 // have the activeTab permission, of course).
212 active_tab_permission_granter()->GrantIfRequested(extension); 212 active_tab_permission_granter()->GrantIfRequested(extension.get());
213 active_tab_permission_granter()->GrantIfRequested(another_extension); 213 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
214 active_tab_permission_granter()->GrantIfRequested( 214 active_tab_permission_granter()->GrantIfRequested(
215 extension_without_active_tab); 215 extension_without_active_tab.get());
216 216
217 EXPECT_TRUE(IsBlocked(extension, google)); 217 EXPECT_TRUE(IsBlocked(extension, google));
218 EXPECT_TRUE(IsBlocked(another_extension, google)); 218 EXPECT_TRUE(IsBlocked(another_extension, google));
219 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 219 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
220 220
221 EXPECT_TRUE(IsAllowed(extension, chromium)); 221 EXPECT_TRUE(IsAllowed(extension, chromium));
222 EXPECT_TRUE(IsAllowed(another_extension, chromium)); 222 EXPECT_TRUE(IsAllowed(another_extension, chromium));
223 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium)); 223 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium));
224 224
225 // Should be able to go back to URLs that were previously cleared. 225 // Should be able to go back to URLs that were previously cleared.
226 NavigateAndCommit(google); 226 NavigateAndCommit(google);
227 227
228 active_tab_permission_granter()->GrantIfRequested(extension); 228 active_tab_permission_granter()->GrantIfRequested(extension.get());
229 active_tab_permission_granter()->GrantIfRequested(another_extension); 229 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
230 active_tab_permission_granter()->GrantIfRequested( 230 active_tab_permission_granter()->GrantIfRequested(
231 extension_without_active_tab); 231 extension_without_active_tab.get());
232 232
233 EXPECT_TRUE(IsAllowed(extension, google)); 233 EXPECT_TRUE(IsAllowed(extension, google));
234 EXPECT_TRUE(IsAllowed(another_extension, google)); 234 EXPECT_TRUE(IsAllowed(another_extension, google));
235 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 235 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
236 236
237 EXPECT_TRUE(IsBlocked(extension, chromium)); 237 EXPECT_TRUE(IsBlocked(extension, chromium));
238 EXPECT_TRUE(IsBlocked(another_extension, chromium)); 238 EXPECT_TRUE(IsBlocked(another_extension, chromium));
239 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium)); 239 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium));
240 }; 240 };
241 241
242 TEST_F(ActiveTabTest, Uninstalling) { 242 TEST_F(ActiveTabTest, Uninstalling) {
243 // Some semi-arbitrary setup. 243 // Some semi-arbitrary setup.
244 GURL google("http://www.google.com"); 244 GURL google("http://www.google.com");
245 NavigateAndCommit(google); 245 NavigateAndCommit(google);
246 246
247 active_tab_permission_granter()->GrantIfRequested(extension); 247 active_tab_permission_granter()->GrantIfRequested(extension.get());
248 248
249 EXPECT_TRUE(active_tab_permission_granter()->IsGranted(extension)); 249 EXPECT_TRUE(active_tab_permission_granter()->IsGranted(extension.get()));
250 EXPECT_TRUE(IsAllowed(extension, google)); 250 EXPECT_TRUE(IsAllowed(extension, google));
251 251
252 // Uninstalling the extension should clear its tab permissions. 252 // Uninstalling the extension should clear its tab permissions.
253 UnloadedExtensionInfo details( 253 UnloadedExtensionInfo details(extension.get(),
254 extension, 254 extension_misc::UNLOAD_REASON_DISABLE);
255 extension_misc::UNLOAD_REASON_DISABLE);
256 content::NotificationService::current()->Notify( 255 content::NotificationService::current()->Notify(
257 chrome::NOTIFICATION_EXTENSION_UNLOADED, 256 chrome::NOTIFICATION_EXTENSION_UNLOADED,
258 content::Source<Profile>(Profile::FromBrowserContext( 257 content::Source<Profile>(Profile::FromBrowserContext(
259 web_contents()->GetBrowserContext())), 258 web_contents()->GetBrowserContext())),
260 content::Details<UnloadedExtensionInfo>(&details)); 259 content::Details<UnloadedExtensionInfo>(&details));
261 260
262 EXPECT_FALSE(active_tab_permission_granter()->IsGranted(extension)); 261 EXPECT_FALSE(active_tab_permission_granter()->IsGranted(extension.get()));
263 // Note: can't EXPECT_FALSE(IsAllowed) here because uninstalled extensions 262 // Note: can't EXPECT_FALSE(IsAllowed) here because uninstalled extensions
264 // are just that... considered to be uninstalled, and the manager might 263 // are just that... considered to be uninstalled, and the manager might
265 // just ignore them from here on. 264 // just ignore them from here on.
266 265
267 // Granting the extension again should give them back. 266 // Granting the extension again should give them back.
268 active_tab_permission_granter()->GrantIfRequested(extension); 267 active_tab_permission_granter()->GrantIfRequested(extension.get());
269 268
270 EXPECT_TRUE(active_tab_permission_granter()->IsGranted(extension)); 269 EXPECT_TRUE(active_tab_permission_granter()->IsGranted(extension.get()));
271 EXPECT_TRUE(IsAllowed(extension, google)); 270 EXPECT_TRUE(IsAllowed(extension, google));
272 } 271 }
273 272
274 TEST_F(ActiveTabTest, OnlyActiveTab) { 273 TEST_F(ActiveTabTest, OnlyActiveTab) {
275 GURL google("http://www.google.com"); 274 GURL google("http://www.google.com");
276 NavigateAndCommit(google); 275 NavigateAndCommit(google);
277 276
278 active_tab_permission_granter()->GrantIfRequested(extension); 277 active_tab_permission_granter()->GrantIfRequested(extension.get());
279 278
280 EXPECT_TRUE(IsAllowed(extension, google, tab_id())); 279 EXPECT_TRUE(IsAllowed(extension, google, tab_id()));
281 EXPECT_TRUE(IsBlocked(extension, google, tab_id() + 1)); 280 EXPECT_TRUE(IsBlocked(extension, google, tab_id() + 1));
282 EXPECT_FALSE(HasTabsPermission(extension, tab_id() + 1)); 281 EXPECT_FALSE(HasTabsPermission(extension, tab_id() + 1));
283 } 282 }
284 283
285 TEST_F(ActiveTabTest, NavigateInPage) { 284 TEST_F(ActiveTabTest, NavigateInPage) {
286 GURL google("http://www.google.com"); 285 GURL google("http://www.google.com");
287 NavigateAndCommit(google); 286 NavigateAndCommit(google);
288 287
289 active_tab_permission_granter()->GrantIfRequested(extension); 288 active_tab_permission_granter()->GrantIfRequested(extension.get());
290 289
291 // Perform an in-page navigation. The extension should not lose the temporary 290 // Perform an in-page navigation. The extension should not lose the temporary
292 // permission. 291 // permission.
293 GURL google_h1("http://www.google.com#h1"); 292 GURL google_h1("http://www.google.com#h1");
294 NavigateAndCommit(google_h1); 293 NavigateAndCommit(google_h1);
295 294
296 EXPECT_TRUE(IsAllowed(extension, google, tab_id())); 295 EXPECT_TRUE(IsAllowed(extension, google, tab_id()));
297 EXPECT_TRUE(IsAllowed(extension, google_h1, tab_id())); 296 EXPECT_TRUE(IsAllowed(extension, google_h1, tab_id()));
298 297
299 GURL chromium("http://www.chromium.org"); 298 GURL chromium("http://www.chromium.org");
300 NavigateAndCommit(chromium); 299 NavigateAndCommit(chromium);
301 300
302 EXPECT_FALSE(IsAllowed(extension, google, tab_id())); 301 EXPECT_FALSE(IsAllowed(extension, google, tab_id()));
303 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id())); 302 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id()));
304 EXPECT_FALSE(IsAllowed(extension, chromium, tab_id())); 303 EXPECT_FALSE(IsAllowed(extension, chromium, tab_id()));
305 304
306 active_tab_permission_granter()->GrantIfRequested(extension); 305 active_tab_permission_granter()->GrantIfRequested(extension.get());
307 306
308 EXPECT_FALSE(IsAllowed(extension, google, tab_id())); 307 EXPECT_FALSE(IsAllowed(extension, google, tab_id()));
309 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id())); 308 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id()));
310 EXPECT_TRUE(IsAllowed(extension, chromium, tab_id())); 309 EXPECT_TRUE(IsAllowed(extension, chromium, tab_id()));
311 310
312 GURL chromium_h1("http://www.chromium.org#h1"); 311 GURL chromium_h1("http://www.chromium.org#h1");
313 NavigateAndCommit(chromium_h1); 312 NavigateAndCommit(chromium_h1);
314 313
315 EXPECT_FALSE(IsAllowed(extension, google, tab_id())); 314 EXPECT_FALSE(IsAllowed(extension, google, tab_id()));
316 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id())); 315 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id()));
317 EXPECT_TRUE(IsAllowed(extension, chromium, tab_id())); 316 EXPECT_TRUE(IsAllowed(extension, chromium, tab_id()));
318 EXPECT_TRUE(IsAllowed(extension, chromium_h1, tab_id())); 317 EXPECT_TRUE(IsAllowed(extension, chromium_h1, tab_id()));
319 318
320 Reload(); 319 Reload();
321 320
322 EXPECT_FALSE(IsAllowed(extension, google, tab_id())); 321 EXPECT_FALSE(IsAllowed(extension, google, tab_id()));
323 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id())); 322 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id()));
324 EXPECT_FALSE(IsAllowed(extension, chromium, tab_id())); 323 EXPECT_FALSE(IsAllowed(extension, chromium, tab_id()));
325 EXPECT_FALSE(IsAllowed(extension, chromium_h1, tab_id())); 324 EXPECT_FALSE(IsAllowed(extension, chromium_h1, tab_id()));
326 } 325 }
327 326
328 } // namespace 327 } // namespace
329 } // namespace extensions 328 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/download/save_package_file_picker.cc ('k') | chrome/browser/extensions/api/alarms/alarms_api_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698