| 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 <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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 } | 77 } |
| 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 (extension->CanExecuteScriptOnPage(url, tab_id, NULL, NULL) && | 87 return extension->CanExecuteScriptOnPage(url, tab_id, NULL, NULL) && |
| 88 extension->CanCaptureVisiblePage(url, tab_id, NULL)); | 88 extension->CanCaptureVisiblePage(url, tab_id, NULL) && |
| 89 HasTabsPermission(extension, tab_id); |
| 89 } | 90 } |
| 90 | 91 |
| 91 bool IsBlocked(const scoped_refptr<const Extension>& extension, | 92 bool IsBlocked(const scoped_refptr<const Extension>& extension, |
| 92 const GURL& url) { | 93 const GURL& url) { |
| 93 return IsBlocked(extension, url, tab_id()); | 94 return IsBlocked(extension, url, tab_id()); |
| 94 } | 95 } |
| 95 | 96 |
| 96 bool IsBlocked(const scoped_refptr<const Extension>& extension, | 97 bool IsBlocked(const scoped_refptr<const Extension>& extension, |
| 97 const GURL& url, | 98 const GURL& url, |
| 98 int tab_id) { | 99 int tab_id) { |
| 99 return (!extension->CanExecuteScriptOnPage(url, tab_id, NULL, NULL) && | 100 // Note: can't check HasTabsPermission because it isn't URL specific. |
| 100 !extension->CanCaptureVisiblePage(url, tab_id, NULL)); | 101 return !extension->CanExecuteScriptOnPage(url, tab_id, NULL, NULL) && |
| 102 !extension->CanCaptureVisiblePage(url, tab_id, NULL); |
| 103 } |
| 104 |
| 105 bool HasTabsPermission(const scoped_refptr<const Extension>& extension) { |
| 106 return HasTabsPermission(extension, tab_id()); |
| 107 } |
| 108 |
| 109 bool HasTabsPermission(const scoped_refptr<const Extension>& extension, |
| 110 int tab_id) { |
| 111 return extension->HasAPIPermissionForTab(tab_id, APIPermission::kTab); |
| 101 } | 112 } |
| 102 | 113 |
| 103 // An extension with the activeTab permission. | 114 // An extension with the activeTab permission. |
| 104 scoped_refptr<const Extension> extension; | 115 scoped_refptr<const Extension> extension; |
| 105 | 116 |
| 106 // Another extension with activeTab (for good measure). | 117 // Another extension with activeTab (for good measure). |
| 107 scoped_refptr<const Extension> another_extension; | 118 scoped_refptr<const Extension> another_extension; |
| 108 | 119 |
| 109 // An extension without the activeTab permission. | 120 // An extension without the activeTab permission. |
| 110 scoped_refptr<const Extension> extension_without_active_tab; | 121 scoped_refptr<const Extension> extension_without_active_tab; |
| 111 | 122 |
| 112 private: | 123 private: |
| 113 content::TestBrowserThread ui_thread_; | 124 content::TestBrowserThread ui_thread_; |
| 114 }; | 125 }; |
| 115 | 126 |
| 116 TEST_F(ActiveTabTest, GrantToSinglePage) { | 127 TEST_F(ActiveTabTest, GrantToSinglePage) { |
| 117 GURL google("http://www.google.com"); | 128 GURL google("http://www.google.com"); |
| 118 NavigateAndCommit(google); | 129 NavigateAndCommit(google); |
| 119 | 130 |
| 120 // No access unless it's been granted. | 131 // No access unless it's been granted. |
| 121 EXPECT_TRUE(IsBlocked(extension, google)); | 132 EXPECT_TRUE(IsBlocked(extension, google)); |
| 122 EXPECT_TRUE(IsBlocked(another_extension, google)); | 133 EXPECT_TRUE(IsBlocked(another_extension, google)); |
| 123 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); | 134 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); |
| 124 | 135 |
| 136 EXPECT_FALSE(HasTabsPermission(extension)); |
| 137 EXPECT_FALSE(HasTabsPermission(another_extension)); |
| 138 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab)); |
| 139 |
| 125 active_tab_permission_manager()->GrantIfRequested(extension); | 140 active_tab_permission_manager()->GrantIfRequested(extension); |
| 126 active_tab_permission_manager()->GrantIfRequested( | 141 active_tab_permission_manager()->GrantIfRequested( |
| 127 extension_without_active_tab); | 142 extension_without_active_tab); |
| 128 | 143 |
| 129 // Granted to extension and extension_without_active_tab, but the latter | 144 // Granted to extension and extension_without_active_tab, but the latter |
| 130 // doesn't have the activeTab permission so not granted. | 145 // doesn't have the activeTab permission so not granted. |
| 131 EXPECT_TRUE(IsAllowed(extension, google)); | 146 EXPECT_TRUE(IsAllowed(extension, google)); |
| 132 EXPECT_TRUE(IsBlocked(another_extension, google)); | 147 EXPECT_TRUE(IsBlocked(another_extension, google)); |
| 133 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); | 148 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); |
| 134 | 149 |
| 135 // Other subdomains shouldn't be given access. | 150 // Other subdomains shouldn't be given access. |
| 136 GURL mail_google("http://mail.google.com"); | 151 GURL mail_google("http://mail.google.com"); |
| 137 EXPECT_TRUE(IsBlocked(extension, mail_google)); | 152 EXPECT_TRUE(IsBlocked(extension, mail_google)); |
| 138 EXPECT_TRUE(IsBlocked(another_extension, google)); | 153 EXPECT_TRUE(IsBlocked(another_extension, mail_google)); |
| 139 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); | 154 EXPECT_TRUE(IsBlocked(extension_without_active_tab, mail_google)); |
| 140 | 155 |
| 141 // Reloading the page should clear the active permissions. | 156 // Reloading the page should clear the active permissions. |
| 142 Reload(); | 157 Reload(); |
| 143 | 158 |
| 144 EXPECT_TRUE(IsBlocked(extension, google)); | 159 EXPECT_TRUE(IsBlocked(extension, google)); |
| 145 EXPECT_TRUE(IsBlocked(another_extension, google)); | 160 EXPECT_TRUE(IsBlocked(another_extension, google)); |
| 146 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); | 161 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); |
| 147 | 162 |
| 163 EXPECT_FALSE(HasTabsPermission(extension)); |
| 164 EXPECT_FALSE(HasTabsPermission(another_extension)); |
| 165 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab)); |
| 166 |
| 148 // But they should still be able to be granted again. | 167 // But they should still be able to be granted again. |
| 149 active_tab_permission_manager()->GrantIfRequested(extension); | 168 active_tab_permission_manager()->GrantIfRequested(extension); |
| 150 | 169 |
| 151 EXPECT_TRUE(IsAllowed(extension, google)); | 170 EXPECT_TRUE(IsAllowed(extension, google)); |
| 152 EXPECT_TRUE(IsBlocked(another_extension, google)); | 171 EXPECT_TRUE(IsBlocked(another_extension, google)); |
| 153 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); | 172 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); |
| 154 | 173 |
| 155 // And grant a few more times redundantly for good measure. | 174 // And grant a few more times redundantly for good measure. |
| 156 active_tab_permission_manager()->GrantIfRequested(extension); | 175 active_tab_permission_manager()->GrantIfRequested(extension); |
| 157 active_tab_permission_manager()->GrantIfRequested(extension); | 176 active_tab_permission_manager()->GrantIfRequested(extension); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 172 NavigateAndCommit(chromium); | 191 NavigateAndCommit(chromium); |
| 173 | 192 |
| 174 EXPECT_TRUE(IsBlocked(extension, google)); | 193 EXPECT_TRUE(IsBlocked(extension, google)); |
| 175 EXPECT_TRUE(IsBlocked(another_extension, google)); | 194 EXPECT_TRUE(IsBlocked(another_extension, google)); |
| 176 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); | 195 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); |
| 177 | 196 |
| 178 EXPECT_TRUE(IsBlocked(extension, chromium)); | 197 EXPECT_TRUE(IsBlocked(extension, chromium)); |
| 179 EXPECT_TRUE(IsBlocked(another_extension, chromium)); | 198 EXPECT_TRUE(IsBlocked(another_extension, chromium)); |
| 180 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium)); | 199 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium)); |
| 181 | 200 |
| 201 EXPECT_FALSE(HasTabsPermission(extension)); |
| 202 EXPECT_FALSE(HasTabsPermission(another_extension)); |
| 203 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab)); |
| 204 |
| 182 // Should be able to grant to multiple extensions at the same time (if they | 205 // Should be able to grant to multiple extensions at the same time (if they |
| 183 // have the activeTab permission, of course). | 206 // have the activeTab permission, of course). |
| 184 active_tab_permission_manager()->GrantIfRequested(extension); | 207 active_tab_permission_manager()->GrantIfRequested(extension); |
| 185 active_tab_permission_manager()->GrantIfRequested(another_extension); | 208 active_tab_permission_manager()->GrantIfRequested(another_extension); |
| 186 active_tab_permission_manager()->GrantIfRequested( | 209 active_tab_permission_manager()->GrantIfRequested( |
| 187 extension_without_active_tab); | 210 extension_without_active_tab); |
| 188 | 211 |
| 189 EXPECT_TRUE(IsBlocked(extension, google)); | 212 EXPECT_TRUE(IsBlocked(extension, google)); |
| 190 EXPECT_TRUE(IsBlocked(another_extension, google)); | 213 EXPECT_TRUE(IsBlocked(another_extension, google)); |
| 191 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); | 214 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 } | 266 } |
| 244 | 267 |
| 245 TEST_F(ActiveTabTest, OnlyActiveTab) { | 268 TEST_F(ActiveTabTest, OnlyActiveTab) { |
| 246 GURL google("http://www.google.com"); | 269 GURL google("http://www.google.com"); |
| 247 NavigateAndCommit(google); | 270 NavigateAndCommit(google); |
| 248 | 271 |
| 249 active_tab_permission_manager()->GrantIfRequested(extension); | 272 active_tab_permission_manager()->GrantIfRequested(extension); |
| 250 | 273 |
| 251 EXPECT_TRUE(IsAllowed(extension, google, tab_id())); | 274 EXPECT_TRUE(IsAllowed(extension, google, tab_id())); |
| 252 EXPECT_TRUE(IsBlocked(extension, google, tab_id() + 1)); | 275 EXPECT_TRUE(IsBlocked(extension, google, tab_id() + 1)); |
| 276 EXPECT_FALSE(HasTabsPermission(extension, tab_id() + 1)); |
| 253 } | 277 } |
| 254 | 278 |
| 255 TEST_F(ActiveTabTest, NavigateInPage) { | 279 TEST_F(ActiveTabTest, NavigateInPage) { |
| 256 GURL google("http://www.google.com"); | 280 GURL google("http://www.google.com"); |
| 257 NavigateAndCommit(google); | 281 NavigateAndCommit(google); |
| 258 | 282 |
| 259 active_tab_permission_manager()->GrantIfRequested(extension); | 283 active_tab_permission_manager()->GrantIfRequested(extension); |
| 260 | 284 |
| 261 // Perform an in-page navigation. The extension should not lose the temporary | 285 // Perform an in-page navigation. The extension should not lose the temporary |
| 262 // permission. | 286 // permission. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 290 Reload(); | 314 Reload(); |
| 291 | 315 |
| 292 EXPECT_FALSE(IsAllowed(extension, google, tab_id())); | 316 EXPECT_FALSE(IsAllowed(extension, google, tab_id())); |
| 293 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id())); | 317 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id())); |
| 294 EXPECT_FALSE(IsAllowed(extension, chromium, tab_id())); | 318 EXPECT_FALSE(IsAllowed(extension, chromium, tab_id())); |
| 295 EXPECT_FALSE(IsAllowed(extension, chromium_h1, tab_id())); | 319 EXPECT_FALSE(IsAllowed(extension, chromium_h1, tab_id())); |
| 296 } | 320 } |
| 297 | 321 |
| 298 } // namespace | 322 } // namespace |
| 299 } // namespace extensions | 323 } // namespace extensions |
| OLD | NEW |