NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / active_tab_unittest.cc
blob186f0d9a2efa29bb0f53b67c319818f64a6ceba7
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.
5 #include <string>
7 #include "base/compiler_specific.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/values.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/active_tab_permission_granter.h"
13 #include "chrome/browser/extensions/tab_helper.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/sessions/session_id.h"
16 #include "chrome/common/extensions/features/feature_channel.h"
17 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/navigation_details.h"
20 #include "content/public/browser/navigation_entry.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/notification_types.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/common/frame_navigate_params.h"
25 #include "content/public/common/page_transition_types.h"
26 #include "content/public/test/test_browser_thread.h"
27 #include "extensions/common/extension.h"
28 #include "extensions/common/extension_builder.h"
29 #include "extensions/common/features/feature.h"
30 #include "extensions/common/permissions/permissions_data.h"
31 #include "extensions/common/value_builder.h"
33 using base::DictionaryValue;
34 using base::ListValue;
35 using content::BrowserThread;
36 using content::NavigationController;
38 namespace extensions {
39 namespace {
41 scoped_refptr<const Extension> CreateTestExtension(
42 const std::string& id,
43 bool has_active_tab_permission,
44 bool has_tab_capture_permission) {
45 ListBuilder permissions;
46 if (has_active_tab_permission)
47 permissions.Append("activeTab");
48 if (has_tab_capture_permission)
49 permissions.Append("tabCapture");
50 return ExtensionBuilder()
51 .SetManifest(DictionaryBuilder()
52 .Set("name", "Extension with ID " + id)
53 .Set("version", "1.0")
54 .Set("manifest_version", 2)
55 .Set("permissions", permissions))
56 .SetID(id)
57 .Build();
60 enum PermittedFeature {
61 PERMITTED_NONE,
62 PERMITTED_SCRIPT_ONLY,
63 PERMITTED_CAPTURE_ONLY,
64 PERMITTED_BOTH
67 class ActiveTabTest : public ChromeRenderViewHostTestHarness {
68 protected:
69 ActiveTabTest()
70 : current_channel(chrome::VersionInfo::CHANNEL_DEV),
71 extension(CreateTestExtension("deadbeef", true, false)),
72 another_extension(CreateTestExtension("feedbeef", true, false)),
73 extension_without_active_tab(CreateTestExtension("badbeef",
74 false,
75 false)),
76 extension_with_tab_capture(CreateTestExtension("cafebeef",
77 true,
78 true)) {}
80 virtual void SetUp() OVERRIDE {
81 ChromeRenderViewHostTestHarness::SetUp();
82 TabHelper::CreateForWebContents(web_contents());
85 int tab_id() {
86 return SessionID::IdForTab(web_contents());
89 ActiveTabPermissionGranter* active_tab_permission_granter() {
90 return extensions::TabHelper::FromWebContents(web_contents())->
91 active_tab_permission_granter();
94 bool IsAllowed(const scoped_refptr<const Extension>& extension,
95 const GURL& url) {
96 return IsAllowed(extension, url, PERMITTED_BOTH, tab_id());
99 bool IsAllowed(const scoped_refptr<const Extension>& extension,
100 const GURL& url,
101 PermittedFeature feature) {
102 return IsAllowed(extension, url, feature, tab_id());
105 bool IsAllowed(const scoped_refptr<const Extension>& extension,
106 const GURL& url,
107 PermittedFeature feature,
108 int tab_id) {
109 bool script = PermissionsData::CanExecuteScriptOnPage(
110 extension.get(), url, url, tab_id, NULL, -1, NULL);
111 bool capture = HasTabsPermission(extension, tab_id) &&
112 PermissionsData::CanCaptureVisiblePage(extension.get(), tab_id, NULL);
113 switch (feature) {
114 case PERMITTED_SCRIPT_ONLY:
115 return script && !capture;
116 case PERMITTED_CAPTURE_ONLY:
117 return capture && !script;
118 case PERMITTED_BOTH:
119 return script && capture;
120 case PERMITTED_NONE:
121 return !script && !capture;
123 NOTREACHED();
124 return false;
127 bool IsBlocked(const scoped_refptr<const Extension>& extension,
128 const GURL& url) {
129 return IsBlocked(extension, url, tab_id());
132 bool IsBlocked(const scoped_refptr<const Extension>& extension,
133 const GURL& url,
134 int tab_id) {
135 return IsAllowed(extension, url, PERMITTED_NONE, tab_id);
138 bool HasTabsPermission(const scoped_refptr<const Extension>& extension) {
139 return HasTabsPermission(extension, tab_id());
142 bool HasTabsPermission(const scoped_refptr<const Extension>& extension,
143 int tab_id) {
144 return PermissionsData::HasAPIPermissionForTab(
145 extension.get(), tab_id, APIPermission::kTab);
148 bool IsGrantedForTab(const Extension* extension,
149 const content::WebContents* web_contents) {
150 return PermissionsData::HasAPIPermissionForTab(
151 extension,
152 SessionID::IdForTab(web_contents),
153 APIPermission::kTab);
156 // TODO(justinlin): Remove when tabCapture is moved to stable.
157 ScopedCurrentChannel current_channel;
159 // An extension with the activeTab permission.
160 scoped_refptr<const Extension> extension;
162 // Another extension with activeTab (for good measure).
163 scoped_refptr<const Extension> another_extension;
165 // An extension without the activeTab permission.
166 scoped_refptr<const Extension> extension_without_active_tab;
168 // An extension with both the activeTab and tabCapture permission.
169 scoped_refptr<const Extension> extension_with_tab_capture;
172 TEST_F(ActiveTabTest, GrantToSinglePage) {
173 GURL google("http://www.google.com");
174 NavigateAndCommit(google);
176 // No access unless it's been granted.
177 EXPECT_TRUE(IsBlocked(extension, google));
178 EXPECT_TRUE(IsBlocked(another_extension, google));
179 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
181 EXPECT_FALSE(HasTabsPermission(extension));
182 EXPECT_FALSE(HasTabsPermission(another_extension));
183 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab));
185 active_tab_permission_granter()->GrantIfRequested(extension.get());
186 active_tab_permission_granter()->GrantIfRequested(
187 extension_without_active_tab.get());
189 // Granted to extension and extension_without_active_tab, but the latter
190 // doesn't have the activeTab permission so not granted.
191 EXPECT_TRUE(IsAllowed(extension, google));
192 EXPECT_TRUE(IsBlocked(another_extension, google));
193 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
195 // Other subdomains shouldn't be given access.
196 GURL mail_google("http://mail.google.com");
197 EXPECT_TRUE(IsAllowed(extension, mail_google, PERMITTED_CAPTURE_ONLY));
198 EXPECT_TRUE(IsBlocked(another_extension, mail_google));
199 EXPECT_TRUE(IsBlocked(extension_without_active_tab, mail_google));
201 // Reloading the page should clear the active permissions.
202 Reload();
204 EXPECT_TRUE(IsBlocked(extension, google));
205 EXPECT_TRUE(IsBlocked(another_extension, google));
206 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
208 EXPECT_FALSE(HasTabsPermission(extension));
209 EXPECT_FALSE(HasTabsPermission(another_extension));
210 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab));
212 // But they should still be able to be granted again.
213 active_tab_permission_granter()->GrantIfRequested(extension.get());
215 EXPECT_TRUE(IsAllowed(extension, google));
216 EXPECT_TRUE(IsBlocked(another_extension, google));
217 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
219 // And grant a few more times redundantly for good measure.
220 active_tab_permission_granter()->GrantIfRequested(extension.get());
221 active_tab_permission_granter()->GrantIfRequested(extension.get());
222 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
223 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
224 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
225 active_tab_permission_granter()->GrantIfRequested(extension.get());
226 active_tab_permission_granter()->GrantIfRequested(extension.get());
227 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
228 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
230 EXPECT_TRUE(IsAllowed(extension, google));
231 EXPECT_TRUE(IsAllowed(another_extension, google));
232 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
234 // Navigating to a new URL should clear the active permissions.
235 GURL chromium("http://www.chromium.org");
236 NavigateAndCommit(chromium);
238 EXPECT_TRUE(IsBlocked(extension, google));
239 EXPECT_TRUE(IsBlocked(another_extension, google));
240 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
242 EXPECT_TRUE(IsBlocked(extension, chromium));
243 EXPECT_TRUE(IsBlocked(another_extension, chromium));
244 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium));
246 EXPECT_FALSE(HasTabsPermission(extension));
247 EXPECT_FALSE(HasTabsPermission(another_extension));
248 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab));
250 // Should be able to grant to multiple extensions at the same time (if they
251 // have the activeTab permission, of course).
252 active_tab_permission_granter()->GrantIfRequested(extension.get());
253 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
254 active_tab_permission_granter()->GrantIfRequested(
255 extension_without_active_tab.get());
257 EXPECT_TRUE(IsAllowed(extension, google, PERMITTED_CAPTURE_ONLY));
258 EXPECT_TRUE(IsAllowed(another_extension, google, PERMITTED_CAPTURE_ONLY));
259 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
261 EXPECT_TRUE(IsAllowed(extension, chromium));
262 EXPECT_TRUE(IsAllowed(another_extension, chromium));
263 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium));
265 // Should be able to go back to URLs that were previously cleared.
266 NavigateAndCommit(google);
268 active_tab_permission_granter()->GrantIfRequested(extension.get());
269 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
270 active_tab_permission_granter()->GrantIfRequested(
271 extension_without_active_tab.get());
273 EXPECT_TRUE(IsAllowed(extension, google));
274 EXPECT_TRUE(IsAllowed(another_extension, google));
275 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
277 EXPECT_TRUE(IsAllowed(extension, chromium, PERMITTED_CAPTURE_ONLY));
278 EXPECT_TRUE(IsAllowed(another_extension, chromium, PERMITTED_CAPTURE_ONLY));
279 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium));
282 TEST_F(ActiveTabTest, Uninstalling) {
283 // Some semi-arbitrary setup.
284 GURL google("http://www.google.com");
285 NavigateAndCommit(google);
287 active_tab_permission_granter()->GrantIfRequested(extension.get());
289 EXPECT_TRUE(IsGrantedForTab(extension.get(), web_contents()));
290 EXPECT_TRUE(IsAllowed(extension, google));
292 // Uninstalling the extension should clear its tab permissions.
293 UnloadedExtensionInfo details(extension.get(),
294 UnloadedExtensionInfo::REASON_DISABLE);
295 content::NotificationService::current()->Notify(
296 chrome::NOTIFICATION_EXTENSION_UNLOADED,
297 content::Source<Profile>(Profile::FromBrowserContext(
298 web_contents()->GetBrowserContext())),
299 content::Details<UnloadedExtensionInfo>(&details));
301 // Note: can't EXPECT_FALSE(IsAllowed) here because uninstalled extensions
302 // are just that... considered to be uninstalled, and the manager might
303 // just ignore them from here on.
305 // Granting the extension again should give them back.
306 active_tab_permission_granter()->GrantIfRequested(extension.get());
308 EXPECT_TRUE(IsGrantedForTab(extension.get(), web_contents()));
309 EXPECT_TRUE(IsAllowed(extension, google));
312 TEST_F(ActiveTabTest, OnlyActiveTab) {
313 GURL google("http://www.google.com");
314 NavigateAndCommit(google);
316 active_tab_permission_granter()->GrantIfRequested(extension.get());
318 EXPECT_TRUE(IsAllowed(extension, google, PERMITTED_BOTH, tab_id()));
319 EXPECT_TRUE(IsBlocked(extension, google, tab_id() + 1));
320 EXPECT_FALSE(HasTabsPermission(extension, tab_id() + 1));
323 TEST_F(ActiveTabTest, NavigateInPage) {
324 GURL google("http://www.google.com");
325 NavigateAndCommit(google);
327 active_tab_permission_granter()->GrantIfRequested(extension.get());
329 // Perform an in-page navigation. The extension should not lose the temporary
330 // permission.
331 GURL google_h1("http://www.google.com#h1");
332 NavigateAndCommit(google_h1);
334 EXPECT_TRUE(IsAllowed(extension, google));
335 EXPECT_TRUE(IsAllowed(extension, google_h1));
337 GURL chromium("http://www.chromium.org");
338 NavigateAndCommit(chromium);
340 EXPECT_FALSE(IsAllowed(extension, google));
341 EXPECT_FALSE(IsAllowed(extension, google_h1));
342 EXPECT_FALSE(IsAllowed(extension, chromium));
344 active_tab_permission_granter()->GrantIfRequested(extension.get());
346 EXPECT_FALSE(IsAllowed(extension, google));
347 EXPECT_FALSE(IsAllowed(extension, google_h1));
348 EXPECT_TRUE(IsAllowed(extension, chromium));
350 GURL chromium_h1("http://www.chromium.org#h1");
351 NavigateAndCommit(chromium_h1);
353 EXPECT_FALSE(IsAllowed(extension, google));
354 EXPECT_FALSE(IsAllowed(extension, google_h1));
355 EXPECT_TRUE(IsAllowed(extension, chromium));
356 EXPECT_TRUE(IsAllowed(extension, chromium_h1));
358 Reload();
360 EXPECT_FALSE(IsAllowed(extension, google));
361 EXPECT_FALSE(IsAllowed(extension, google_h1));
362 EXPECT_FALSE(IsAllowed(extension, chromium));
363 EXPECT_FALSE(IsAllowed(extension, chromium_h1));
366 TEST_F(ActiveTabTest, ChromeUrlGrants) {
367 GURL internal("chrome://version");
368 NavigateAndCommit(internal);
369 active_tab_permission_granter()->GrantIfRequested(
370 extension_with_tab_capture.get());
371 // Do not grant tabs/hosts permissions for tab.
372 EXPECT_TRUE(IsAllowed(extension_with_tab_capture, internal,
373 PERMITTED_CAPTURE_ONLY));
374 EXPECT_TRUE(PermissionsData::HasAPIPermissionForTab(
375 extension_with_tab_capture.get(),
376 tab_id(),
377 APIPermission::kTabCaptureForTab));
379 EXPECT_TRUE(IsBlocked(extension_with_tab_capture, internal, tab_id() + 1));
380 EXPECT_FALSE(PermissionsData::HasAPIPermissionForTab(
381 extension_with_tab_capture.get(),
382 tab_id() + 1,
383 APIPermission::kTabCaptureForTab));
386 } // namespace
387 } // namespace extensions