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

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

Issue 10443105: Take 2 at implementing activeTab. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: many more tests Created 8 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <string>
6
7 #include "base/compiler_specific.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/active_tab_permission_manager.h"
12 #include "chrome/browser/extensions/extension_tab_helper.h"
13 #include "chrome/browser/ui/tab_contents/tab_contents.h"
14 #include "chrome/browser/ui/tab_contents/test_tab_contents.h"
15 #include "chrome/common/chrome_notification_types.h"
16 #include "chrome/common/extensions/extension.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_types.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/page_transition_types.h"
22 #include "content/public/test/test_browser_thread.h"
23
24 using base::DictionaryValue;
25 using base::ListValue;
26 using content::BrowserThread;
27 using content::NavigationController;
28
29 namespace extensions {
30 namespace {
31
32 class ActiveTabTest : public TabContentsTestHarness {
33 public:
34 ActiveTabTest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) {
35 {
36 scoped_ptr<ListValue> permissions(new ListValue());
37 permissions->Append(Value::CreateStringValue("activeTab"));
Aaron Boodman 2012/06/08 05:31:30 indent -= 2;
not at google - send to devlin 2012/06/12 20:40:51 Aww I like it like that.
38 DictionaryValue manifest;
39 manifest.SetString("name", "Extension with activeTab.");
40 manifest.SetString("version", "1.0.0");
41 manifest.SetInteger("manifest_version", 2);
42 manifest.Set("permissions", permissions.release());
43 std::string error;
44 extension_ = Extension::Create(
45 FilePath(),
Aaron Boodman 2012/06/08 05:31:30 Nit: these params can be start on the same line as
46 Extension::EXTERNAL_PREF,
Aaron Boodman 2012/06/08 05:31:30 Is there any particular reason for this location?
not at google - send to devlin 2012/06/12 20:40:51 Oh ok. INTERNAL it is.
47 manifest,
48 0,
Aaron Boodman 2012/06/08 05:31:30 Document or constify magic number. Same with strin
not at google - send to devlin 2012/06/12 20:40:51 Done.
49 "extension",
50 &error);
51 CHECK_EQ("", error);
52 }
53 {
Aaron Boodman 2012/06/08 05:31:30 These blocks are a little unusual. Consider moving
not at google - send to devlin 2012/06/12 20:40:51 Done.
54 scoped_ptr<ListValue> permissions(new ListValue());
55 permissions->Append(Value::CreateStringValue("activeTab"));
Aaron Boodman 2012/06/08 05:31:30 indent -= 2;
not at google - send to devlin 2012/06/12 20:40:51 Done.
56 DictionaryValue manifest;
57 manifest.SetString("name", "Another extension with activeTab.");
58 manifest.SetString("version", "1.0.0");
59 manifest.SetInteger("manifest_version", 2);
60 manifest.Set("permissions", permissions.release());
61 std::string error;
62 another_extension_ = Extension::Create(
63 FilePath(),
64 Extension::EXTERNAL_PREF,
65 manifest,
66 0,
67 "another extension",
68 &error);
69 CHECK_EQ("", error);
70 }
71 {
72 scoped_ptr<ListValue> permissions(new ListValue());
73 DictionaryValue manifest;
74 manifest.SetString("name", "Extension without activeTab.");
75 manifest.SetString("version", "1.0.0");
76 manifest.SetInteger("manifest_version", 2);
77 manifest.Set("permissions", permissions.release());
78 std::string error;
79 extension_without_active_tab_ = Extension::Create(
80 FilePath(),
81 Extension::EXTERNAL_PREF,
82 manifest,
83 0,
84 "extension without activeTab",
85 &error);
86 CHECK_EQ("", error);
87 }
88 }
89
90 protected:
91 const Extension* extension() {
92 return extension_.get();
Aaron Boodman 2012/06/08 05:31:30 Can you just make the members protected? The gette
not at google - send to devlin 2012/06/12 20:40:51 It's so I could type () instead of .get() everywhe
93 }
94
95 const Extension* another_extension() {
96 return another_extension_.get();
97 }
98
99 const Extension* extension_without_active_tab() {
100 return extension_without_active_tab_.get();
101 }
102
103 int tab_id() {
Aaron Boodman 2012/06/08 05:31:30 Nit: GetTabId().
not at google - send to devlin 2012/06/12 20:40:51 Done.
104 return tab_contents()->extension_tab_helper()->GetTabId();
105 }
106
107 ActiveTabPermissionManager* manager() {
Aaron Boodman 2012/06/08 05:31:30 'manager' is a little generic. Maybe active_tab_ma
not at google - send to devlin 2012/06/12 20:40:51 Done.
108 return tab_contents()->extension_tab_helper()->
109 active_tab_permission_manager();
110 }
111
112 bool Allowed(const Extension* extension, const GURL& url) {
Aaron Boodman 2012/06/08 05:31:30 IsAllowed
not at google - send to devlin 2012/06/12 20:40:51 Done.
113 return Allowed(extension, url, tab_id());
114 }
115
116 bool Allowed(const Extension* extension, const GURL& url, int tab_id) {
117 return (extension->CanExecuteScriptOnPage(url, tab_id, NULL, NULL) &&
118 extension->CanCaptureVisiblePage(url, tab_id, NULL));
119 }
120
121 bool Blocked(const Extension* extension, const GURL& url) {
Aaron Boodman 2012/06/08 05:31:30 IsBlocked
not at google - send to devlin 2012/06/12 20:40:51 Done.
122 return Blocked(extension, url, tab_id());
123 }
124
125 bool Blocked(const Extension* extension, const GURL& url, int tab_id) {
126 return (!extension->CanExecuteScriptOnPage(url, tab_id, NULL, NULL) &&
127 !extension->CanCaptureVisiblePage(url, tab_id, NULL));
128 }
129
130 // Fakes loading a new frame on the page using the WebContentsObserver
131 // interface.
132 // TODO(kalman): if somebody can tell me a way to do this from the
133 // TabContentsTestHarness (or any other test harness) then pray tell.
134 void AddFrame(const GURL& url) {
135 manager()->DidCommitProvisionalLoadForFrame(
136 0, // frame_id
137 false, // is_main_frame
138 url,
139 content::PAGE_TRANSITION_AUTO_SUBFRAME,
140 NULL); // render_view_host
141 }
142
143 private:
144 // An extension with the activeTab permission.
145 scoped_refptr<const Extension> extension_;
146
147 // Another extension with activeTab (for good measure).
148 scoped_refptr<const Extension> another_extension_;
149
150 // An extension without the activeTab permission.
151 scoped_refptr<const Extension> extension_without_active_tab_;
152
153 content::TestBrowserThread ui_thread_;
154 };
155
156 TEST_F(ActiveTabTest, GrantToSinglePage) {
157 GURL google("http://www.google.com");
158 NavigateAndCommit(google);
159
160 // No access unless it's been granted.
161 EXPECT_TRUE(Blocked(extension(), google));
162 EXPECT_TRUE(Blocked(another_extension(), google));
163 EXPECT_TRUE(Blocked(extension_without_active_tab(), google));
164
165 manager()->MaybeGrant(extension());
166 manager()->MaybeGrant(extension_without_active_tab());
167
168 // Granted to extension() and extension_without_active_tab(), but the latter
169 // doesn't have the activeTab permission so not granted.
170 EXPECT_TRUE(Allowed(extension(), google));
171 EXPECT_TRUE(Blocked(another_extension(), google));
172 EXPECT_TRUE(Blocked(extension_without_active_tab(), google));
173
174 // Other subdomains shouldn't be given access.
175 GURL mail_google("http://mail.google.com");
176 EXPECT_TRUE(Blocked(extension(), mail_google));
177 EXPECT_TRUE(Blocked(another_extension(), google));
178 EXPECT_TRUE(Blocked(extension_without_active_tab(), google));
179
180 // Reloading the page should clear the active permissions.
181 Reload();
182
183 EXPECT_TRUE(Blocked(extension(), google));
184 EXPECT_TRUE(Blocked(another_extension(), google));
185 EXPECT_TRUE(Blocked(extension_without_active_tab(), google));
186
187 // But they should still be able to be granted again.
188 manager()->MaybeGrant(extension());
189
190 EXPECT_TRUE(Allowed(extension(), google));
191 EXPECT_TRUE(Blocked(another_extension(), google));
192 EXPECT_TRUE(Blocked(extension_without_active_tab(), google));
193
194 // And grant a few more times redundantly for good measure.
195 manager()->MaybeGrant(extension());
196 manager()->MaybeGrant(extension());
197 manager()->MaybeGrant(another_extension());
198 manager()->MaybeGrant(another_extension());
199 manager()->MaybeGrant(another_extension());
200 manager()->MaybeGrant(extension());
201 manager()->MaybeGrant(extension());
202 manager()->MaybeGrant(another_extension());
203 manager()->MaybeGrant(another_extension());
204
205 EXPECT_TRUE(Allowed(extension(), google));
206 EXPECT_TRUE(Allowed(another_extension(), google));
207 EXPECT_TRUE(Blocked(extension_without_active_tab(), google));
208
209 // Navigating to a new URL should clear the active permissions.
210 GURL chromium("http://www.chromium.org");
211 NavigateAndCommit(chromium);
212
213 EXPECT_TRUE(Blocked(extension(), google));
214 EXPECT_TRUE(Blocked(another_extension(), google));
215 EXPECT_TRUE(Blocked(extension_without_active_tab(), google));
216
217 EXPECT_TRUE(Blocked(extension(), chromium));
218 EXPECT_TRUE(Blocked(another_extension(), chromium));
219 EXPECT_TRUE(Blocked(extension_without_active_tab(), chromium));
220
221 // Should be able to grant to multiple extensions at the same time (if they
222 // have the activeTab permission, of course).
223 manager()->MaybeGrant(extension());
224 manager()->MaybeGrant(another_extension());
225 manager()->MaybeGrant(extension_without_active_tab());
226
227 EXPECT_TRUE(Blocked(extension(), google));
228 EXPECT_TRUE(Blocked(another_extension(), google));
229 EXPECT_TRUE(Blocked(extension_without_active_tab(), google));
230
231 EXPECT_TRUE(Allowed(extension(), chromium));
232 EXPECT_TRUE(Allowed(another_extension(), chromium));
233 EXPECT_TRUE(Blocked(extension_without_active_tab(), chromium));
234
235 // Should be able to go back to URLs that were previously cleared.
236 NavigateAndCommit(google);
237
238 manager()->MaybeGrant(extension());
239 manager()->MaybeGrant(another_extension());
240 manager()->MaybeGrant(extension_without_active_tab());
241
242 EXPECT_TRUE(Allowed(extension(), google));
243 EXPECT_TRUE(Allowed(another_extension(), google));
244 EXPECT_TRUE(Blocked(extension_without_active_tab(), google));
245
246 EXPECT_TRUE(Blocked(extension(), chromium));
247 EXPECT_TRUE(Blocked(another_extension(), chromium));
248 EXPECT_TRUE(Blocked(extension_without_active_tab(), chromium));
249 };
250
251 TEST_F(ActiveTabTest, GrantToMultiplePages) {
252 GURL google("http://www.google.com");
253 NavigateAndCommit(google);
254
255 manager()->MaybeGrant(extension());
256
257 // Adding a frame after access was granted shouldn't give it access.
258 GURL chromium("http://www.chromium.org");
259 AddFrame(chromium);
260
261 EXPECT_TRUE(Allowed(extension(), google));
262 EXPECT_TRUE(Blocked(extension(), chromium));
263
264 // Granting access to another extension should give it access to both the
265 // main and sub-frames, but still not to the first extension.
266 manager()->MaybeGrant(another_extension());
267
268 EXPECT_TRUE(Allowed(extension(), google));
269 EXPECT_TRUE(Blocked(extension(), chromium));
270 EXPECT_TRUE(Allowed(another_extension(), google));
271 EXPECT_TRUE(Allowed(another_extension(), chromium));
272
273 // Granting access to the first extension should now give it access to the
274 // frame.
275 manager()->MaybeGrant(extension());
276
277 EXPECT_TRUE(Allowed(extension(), google));
278 EXPECT_TRUE(Allowed(extension(), chromium));
279 EXPECT_TRUE(Allowed(another_extension(), google));
280 EXPECT_TRUE(Allowed(another_extension(), chromium));
281
282 // Reloading should clear all access.
283 Reload();
284
285 EXPECT_TRUE(Blocked(extension(), google));
286 EXPECT_TRUE(Blocked(extension(), chromium));
287 EXPECT_TRUE(Blocked(another_extension(), google));
288 EXPECT_TRUE(Blocked(another_extension(), chromium));
289
290 // And after granting, no access to the frames that were there.
291 manager()->MaybeGrant(extension());
292
293 EXPECT_TRUE(Allowed(extension(), google));
294 EXPECT_TRUE(Blocked(extension(), chromium));
295 EXPECT_TRUE(Blocked(another_extension(), google));
296 EXPECT_TRUE(Blocked(another_extension(), chromium));
297
298 // Having lots of frames on the same page should behave as expected.
299 GURL chromium_index("http://www.chromium.org/index.html");
300 GURL chromium_about("http://www.chromium.org/about.html");
301 GURL chromium_blank("http://www.chromium.org/blank.html");
302 GURL gmail("http://www.gmail.com");
303 GURL mail_google("http://mail.google.com");
304 GURL plus_google("http://plus.google.com");
305 GURL codereview_appspot("http://codereview.appspot.com");
306 GURL omahaproxy_appspot("http://omahaproxy.appspot.com");
307
308 AddFrame(chromium_index);
309 AddFrame(chromium_about);
310 AddFrame(gmail);
311 AddFrame(mail_google);
312
313 EXPECT_TRUE(Blocked(extension(), chromium_index));
314 EXPECT_TRUE(Blocked(extension(), chromium_about));
315 EXPECT_TRUE(Blocked(extension(), chromium_blank));
316 EXPECT_TRUE(Blocked(extension(), gmail));
317 EXPECT_TRUE(Blocked(extension(), mail_google));
318 EXPECT_TRUE(Blocked(extension(), plus_google));
319 EXPECT_TRUE(Blocked(extension(), codereview_appspot));
320 EXPECT_TRUE(Blocked(extension(), omahaproxy_appspot));
321
322 EXPECT_TRUE(Blocked(another_extension(), chromium_index));
323 EXPECT_TRUE(Blocked(another_extension(), chromium_about));
324 EXPECT_TRUE(Blocked(another_extension(), chromium_blank));
325 EXPECT_TRUE(Blocked(another_extension(), gmail));
326 EXPECT_TRUE(Blocked(another_extension(), mail_google));
327 EXPECT_TRUE(Blocked(another_extension(), plus_google));
328 EXPECT_TRUE(Blocked(another_extension(), codereview_appspot));
329 EXPECT_TRUE(Blocked(another_extension(), omahaproxy_appspot));
330
331 manager()->MaybeGrant(extension());
332
333 AddFrame(chromium_blank);
334 AddFrame(plus_google);
335 AddFrame(codereview_appspot);
336
337 EXPECT_TRUE(Allowed(extension(), chromium_index));
338 EXPECT_TRUE(Allowed(extension(), chromium_about));
339 EXPECT_TRUE(Allowed(extension(), chromium_blank));
Aaron Boodman 2012/06/08 05:31:30 Maybe comment that this works because the comparis
not at google - send to devlin 2012/06/12 20:40:51 Done.
340 EXPECT_TRUE(Allowed(extension(), gmail));
341 EXPECT_TRUE(Allowed(extension(), mail_google));
342 EXPECT_TRUE(Blocked(extension(), plus_google));
343 EXPECT_TRUE(Blocked(extension(), codereview_appspot));
344 EXPECT_TRUE(Blocked(extension(), omahaproxy_appspot));
345
346 EXPECT_TRUE(Blocked(another_extension(), chromium_index));
347 EXPECT_TRUE(Blocked(another_extension(), chromium_about));
348 EXPECT_TRUE(Blocked(another_extension(), chromium_blank));
349 EXPECT_TRUE(Blocked(another_extension(), gmail));
350 EXPECT_TRUE(Blocked(another_extension(), mail_google));
351 EXPECT_TRUE(Blocked(another_extension(), plus_google));
352 EXPECT_TRUE(Blocked(another_extension(), codereview_appspot));
353 EXPECT_TRUE(Blocked(another_extension(), omahaproxy_appspot));
354
355 manager()->MaybeGrant(another_extension());
356
357 AddFrame(omahaproxy_appspot);
358
359 EXPECT_TRUE(Allowed(extension(), chromium_index));
360 EXPECT_TRUE(Allowed(extension(), chromium_about));
361 EXPECT_TRUE(Allowed(extension(), chromium_blank));
362 EXPECT_TRUE(Allowed(extension(), gmail));
363 EXPECT_TRUE(Allowed(extension(), mail_google));
364 EXPECT_TRUE(Blocked(extension(), plus_google));
365 EXPECT_TRUE(Blocked(extension(), codereview_appspot));
366 EXPECT_TRUE(Blocked(extension(), omahaproxy_appspot));
367
368 EXPECT_TRUE(Allowed(another_extension(), chromium_index));
369 EXPECT_TRUE(Allowed(another_extension(), chromium_about));
370 EXPECT_TRUE(Allowed(another_extension(), chromium_blank));
371 EXPECT_TRUE(Allowed(another_extension(), gmail));
372 EXPECT_TRUE(Allowed(another_extension(), mail_google));
373 EXPECT_TRUE(Allowed(another_extension(), plus_google));
374 EXPECT_TRUE(Allowed(another_extension(), codereview_appspot));
375 EXPECT_TRUE(Blocked(another_extension(), omahaproxy_appspot));
376
377 manager()->MaybeGrant(extension());
378
379 EXPECT_TRUE(Allowed(extension(), chromium_index));
380 EXPECT_TRUE(Allowed(extension(), chromium_about));
381 EXPECT_TRUE(Allowed(extension(), chromium_blank));
382 EXPECT_TRUE(Allowed(extension(), gmail));
383 EXPECT_TRUE(Allowed(extension(), mail_google));
384 EXPECT_TRUE(Allowed(extension(), plus_google));
385 EXPECT_TRUE(Allowed(extension(), codereview_appspot));
386 EXPECT_TRUE(Allowed(extension(), omahaproxy_appspot));
387
388 EXPECT_TRUE(Allowed(another_extension(), chromium_index));
389 EXPECT_TRUE(Allowed(another_extension(), chromium_about));
390 EXPECT_TRUE(Allowed(another_extension(), chromium_blank));
391 EXPECT_TRUE(Allowed(another_extension(), gmail));
392 EXPECT_TRUE(Allowed(another_extension(), mail_google));
393 EXPECT_TRUE(Allowed(another_extension(), plus_google));
394 EXPECT_TRUE(Allowed(another_extension(), codereview_appspot));
395 EXPECT_TRUE(Blocked(another_extension(), omahaproxy_appspot));
396 }
397
398 TEST_F(ActiveTabTest, Uninstalling) {
399 // Some semi-arbitrary setup.
400 GURL google("http://www.google.com");
401 NavigateAndCommit(google);
402
403 GURL chromium("http://www.chromium.org");
404 AddFrame(chromium);
405
406 manager()->MaybeGrant(extension());
407
408 GURL gmail("http://www.gmail.com");
409 AddFrame(gmail);
410
411 EXPECT_TRUE(Allowed(extension(), google));
412 EXPECT_TRUE(Allowed(extension(), chromium));
413 EXPECT_TRUE(Blocked(extension(), gmail));
414
415 // Uninstalling the extension should clear its tab permissions.
416 UnloadedExtensionInfo details(
417 extension(),
418 extension_misc::UNLOAD_REASON_DISABLE);
419 content::NotificationService::current()->Notify(
420 chrome::NOTIFICATION_EXTENSION_UNLOADED,
421 content::Source<Profile>(tab_contents()->profile()),
422 content::Details<UnloadedExtensionInfo>(&details));
423
424 EXPECT_TRUE(Blocked(extension(), google));
425 EXPECT_TRUE(Blocked(extension(), chromium));
426 EXPECT_TRUE(Blocked(extension(), gmail));
427
428 // Granting the extension again should give them back.
429 manager()->MaybeGrant(extension());
430
431 EXPECT_TRUE(Allowed(extension(), google));
432 EXPECT_TRUE(Allowed(extension(), chromium));
433 EXPECT_TRUE(Allowed(extension(), gmail));
434 }
435
436 TEST_F(ActiveTabTest, OnlyActiveTab) {
437 GURL google("http://www.google.com");
438 NavigateAndCommit(google);
439
440 manager()->MaybeGrant(extension());
441
442 EXPECT_TRUE(Allowed(extension(), google, tab_id()));
443 EXPECT_TRUE(Blocked(extension(), google, tab_id() + 1));
444 }
Aaron Boodman 2012/06/08 05:31:30 Very nice tests.
not at google - send to devlin 2012/06/12 20:40:51 Thank you.
445
446 } // namespace
447 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698