app_list: Re-enable people search.
[chromium-blink-merge.git] / chrome / browser / extensions / active_tab_unittest.cc
blobd86665aae0771eb5c52918d4a8a1620bd7e55ccd
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_tab_helper.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/test/test_browser_thread.h"
26 #include "extensions/browser/extension_registry.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 void SetUp() override {
81 ChromeRenderViewHostTestHarness::SetUp();
82 TabHelper::CreateForWebContents(web_contents());
85 int tab_id() {
86 return SessionTabHelper::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 const PermissionsData* permissions_data = extension->permissions_data();
110 bool script = permissions_data->CanAccessPage(
111 extension.get(), url, url, tab_id, -1, NULL);
112 bool capture = HasTabsPermission(extension, tab_id) &&
113 permissions_data->CanCaptureVisiblePage(tab_id, NULL);
114 switch (feature) {
115 case PERMITTED_SCRIPT_ONLY:
116 return script && !capture;
117 case PERMITTED_CAPTURE_ONLY:
118 return capture && !script;
119 case PERMITTED_BOTH:
120 return script && capture;
121 case PERMITTED_NONE:
122 return !script && !capture;
124 NOTREACHED();
125 return false;
128 bool IsBlocked(const scoped_refptr<const Extension>& extension,
129 const GURL& url) {
130 return IsBlocked(extension, url, tab_id());
133 bool IsBlocked(const scoped_refptr<const Extension>& extension,
134 const GURL& url,
135 int tab_id) {
136 return IsAllowed(extension, url, PERMITTED_NONE, tab_id);
139 bool HasTabsPermission(const scoped_refptr<const Extension>& extension) {
140 return HasTabsPermission(extension, tab_id());
143 bool HasTabsPermission(const scoped_refptr<const Extension>& extension,
144 int tab_id) {
145 return extension->permissions_data()->HasAPIPermissionForTab(
146 tab_id, APIPermission::kTab);
149 bool IsGrantedForTab(const Extension* extension,
150 const content::WebContents* web_contents) {
151 return extension->permissions_data()->HasAPIPermissionForTab(
152 SessionTabHelper::IdForTab(web_contents), APIPermission::kTab);
155 // TODO(justinlin): Remove when tabCapture is moved to stable.
156 ScopedCurrentChannel current_channel;
158 // An extension with the activeTab permission.
159 scoped_refptr<const Extension> extension;
161 // Another extension with activeTab (for good measure).
162 scoped_refptr<const Extension> another_extension;
164 // An extension without the activeTab permission.
165 scoped_refptr<const Extension> extension_without_active_tab;
167 // An extension with both the activeTab and tabCapture permission.
168 scoped_refptr<const Extension> extension_with_tab_capture;
171 TEST_F(ActiveTabTest, GrantToSinglePage) {
172 GURL google("http://www.google.com");
173 NavigateAndCommit(google);
175 // No access unless it's been granted.
176 EXPECT_TRUE(IsBlocked(extension, google));
177 EXPECT_TRUE(IsBlocked(another_extension, google));
178 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
180 EXPECT_FALSE(HasTabsPermission(extension));
181 EXPECT_FALSE(HasTabsPermission(another_extension));
182 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab));
184 active_tab_permission_granter()->GrantIfRequested(extension.get());
185 active_tab_permission_granter()->GrantIfRequested(
186 extension_without_active_tab.get());
188 // Granted to extension and extension_without_active_tab, but the latter
189 // doesn't have the activeTab permission so not granted.
190 EXPECT_TRUE(IsAllowed(extension, google));
191 EXPECT_TRUE(IsBlocked(another_extension, google));
192 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
194 // Other subdomains shouldn't be given access.
195 GURL mail_google("http://mail.google.com");
196 EXPECT_TRUE(IsAllowed(extension, mail_google, PERMITTED_CAPTURE_ONLY));
197 EXPECT_TRUE(IsBlocked(another_extension, mail_google));
198 EXPECT_TRUE(IsBlocked(extension_without_active_tab, mail_google));
200 // Reloading the page should clear the active permissions.
201 Reload();
203 EXPECT_TRUE(IsBlocked(extension, google));
204 EXPECT_TRUE(IsBlocked(another_extension, google));
205 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
207 EXPECT_FALSE(HasTabsPermission(extension));
208 EXPECT_FALSE(HasTabsPermission(another_extension));
209 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab));
211 // But they should still be able to be granted again.
212 active_tab_permission_granter()->GrantIfRequested(extension.get());
214 EXPECT_TRUE(IsAllowed(extension, google));
215 EXPECT_TRUE(IsBlocked(another_extension, google));
216 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
218 // And grant a few more times redundantly for good measure.
219 active_tab_permission_granter()->GrantIfRequested(extension.get());
220 active_tab_permission_granter()->GrantIfRequested(extension.get());
221 active_tab_permission_granter()->GrantIfRequested(another_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(extension.get());
225 active_tab_permission_granter()->GrantIfRequested(extension.get());
226 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
227 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
229 EXPECT_TRUE(IsAllowed(extension, google));
230 EXPECT_TRUE(IsAllowed(another_extension, google));
231 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
233 // Navigating to a new URL should clear the active permissions.
234 GURL chromium("http://www.chromium.org");
235 NavigateAndCommit(chromium);
237 EXPECT_TRUE(IsBlocked(extension, google));
238 EXPECT_TRUE(IsBlocked(another_extension, google));
239 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
241 EXPECT_TRUE(IsBlocked(extension, chromium));
242 EXPECT_TRUE(IsBlocked(another_extension, chromium));
243 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium));
245 EXPECT_FALSE(HasTabsPermission(extension));
246 EXPECT_FALSE(HasTabsPermission(another_extension));
247 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab));
249 // Should be able to grant to multiple extensions at the same time (if they
250 // have the activeTab permission, of course).
251 active_tab_permission_granter()->GrantIfRequested(extension.get());
252 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
253 active_tab_permission_granter()->GrantIfRequested(
254 extension_without_active_tab.get());
256 EXPECT_TRUE(IsAllowed(extension, google, PERMITTED_CAPTURE_ONLY));
257 EXPECT_TRUE(IsAllowed(another_extension, google, PERMITTED_CAPTURE_ONLY));
258 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
260 EXPECT_TRUE(IsAllowed(extension, chromium));
261 EXPECT_TRUE(IsAllowed(another_extension, chromium));
262 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium));
264 // Should be able to go back to URLs that were previously cleared.
265 NavigateAndCommit(google);
267 active_tab_permission_granter()->GrantIfRequested(extension.get());
268 active_tab_permission_granter()->GrantIfRequested(another_extension.get());
269 active_tab_permission_granter()->GrantIfRequested(
270 extension_without_active_tab.get());
272 EXPECT_TRUE(IsAllowed(extension, google));
273 EXPECT_TRUE(IsAllowed(another_extension, google));
274 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
276 EXPECT_TRUE(IsAllowed(extension, chromium, PERMITTED_CAPTURE_ONLY));
277 EXPECT_TRUE(IsAllowed(another_extension, chromium, PERMITTED_CAPTURE_ONLY));
278 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium));
281 TEST_F(ActiveTabTest, Uninstalling) {
282 // Some semi-arbitrary setup.
283 GURL google("http://www.google.com");
284 NavigateAndCommit(google);
286 active_tab_permission_granter()->GrantIfRequested(extension.get());
288 EXPECT_TRUE(IsGrantedForTab(extension.get(), web_contents()));
289 EXPECT_TRUE(IsAllowed(extension, google));
291 // Uninstalling the extension should clear its tab permissions.
292 ExtensionRegistry* registry =
293 ExtensionRegistry::Get(web_contents()->GetBrowserContext());
294 registry->TriggerOnUnloaded(extension.get(),
295 UnloadedExtensionInfo::REASON_DISABLE);
297 // Note: can't EXPECT_FALSE(IsAllowed) here because uninstalled extensions
298 // are just that... considered to be uninstalled, and the manager might
299 // just ignore them from here on.
301 // Granting the extension again should give them back.
302 active_tab_permission_granter()->GrantIfRequested(extension.get());
304 EXPECT_TRUE(IsGrantedForTab(extension.get(), web_contents()));
305 EXPECT_TRUE(IsAllowed(extension, google));
308 TEST_F(ActiveTabTest, OnlyActiveTab) {
309 GURL google("http://www.google.com");
310 NavigateAndCommit(google);
312 active_tab_permission_granter()->GrantIfRequested(extension.get());
314 EXPECT_TRUE(IsAllowed(extension, google, PERMITTED_BOTH, tab_id()));
315 EXPECT_TRUE(IsBlocked(extension, google, tab_id() + 1));
316 EXPECT_FALSE(HasTabsPermission(extension, tab_id() + 1));
319 TEST_F(ActiveTabTest, NavigateInPage) {
320 GURL google("http://www.google.com");
321 NavigateAndCommit(google);
323 active_tab_permission_granter()->GrantIfRequested(extension.get());
325 // Perform an in-page navigation. The extension should not lose the temporary
326 // permission.
327 GURL google_h1("http://www.google.com#h1");
328 NavigateAndCommit(google_h1);
330 EXPECT_TRUE(IsAllowed(extension, google));
331 EXPECT_TRUE(IsAllowed(extension, google_h1));
333 GURL chromium("http://www.chromium.org");
334 NavigateAndCommit(chromium);
336 EXPECT_FALSE(IsAllowed(extension, google));
337 EXPECT_FALSE(IsAllowed(extension, google_h1));
338 EXPECT_FALSE(IsAllowed(extension, chromium));
340 active_tab_permission_granter()->GrantIfRequested(extension.get());
342 EXPECT_FALSE(IsAllowed(extension, google));
343 EXPECT_FALSE(IsAllowed(extension, google_h1));
344 EXPECT_TRUE(IsAllowed(extension, chromium));
346 GURL chromium_h1("http://www.chromium.org#h1");
347 NavigateAndCommit(chromium_h1);
349 EXPECT_FALSE(IsAllowed(extension, google));
350 EXPECT_FALSE(IsAllowed(extension, google_h1));
351 EXPECT_TRUE(IsAllowed(extension, chromium));
352 EXPECT_TRUE(IsAllowed(extension, chromium_h1));
354 Reload();
356 EXPECT_FALSE(IsAllowed(extension, google));
357 EXPECT_FALSE(IsAllowed(extension, google_h1));
358 EXPECT_FALSE(IsAllowed(extension, chromium));
359 EXPECT_FALSE(IsAllowed(extension, chromium_h1));
362 TEST_F(ActiveTabTest, ChromeUrlGrants) {
363 GURL internal("chrome://version");
364 NavigateAndCommit(internal);
365 active_tab_permission_granter()->GrantIfRequested(
366 extension_with_tab_capture.get());
367 // Do not grant tabs/hosts permissions for tab.
368 EXPECT_TRUE(IsAllowed(extension_with_tab_capture, internal,
369 PERMITTED_CAPTURE_ONLY));
370 const PermissionsData* permissions_data =
371 extension_with_tab_capture->permissions_data();
372 EXPECT_TRUE(permissions_data->HasAPIPermissionForTab(
373 tab_id(), APIPermission::kTabCaptureForTab));
375 EXPECT_TRUE(IsBlocked(extension_with_tab_capture, internal, tab_id() + 1));
376 EXPECT_FALSE(permissions_data->HasAPIPermissionForTab(
377 tab_id() + 1, APIPermission::kTabCaptureForTab));
380 } // namespace
381 } // namespace extensions