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 "base/base64.h" | 5 #include "base/base64.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 " %s," | 299 " %s," |
300 " \"externally_connectable\": {" | 300 " \"externally_connectable\": {" |
301 " \"matches\": [\"*://*.chromium.org:*/*\"]" | 301 " \"matches\": [\"*://*.chromium.org:*/*\"]" |
302 " }" | 302 " }" |
303 "}", | 303 "}", |
304 common_manifest())); | 304 common_manifest())); |
305 CHECK(extension.get()); | 305 CHECK(extension.get()); |
306 return extension; | 306 return extension; |
307 } | 307 } |
308 | 308 |
309 scoped_refptr<const Extension> LoadChromiumConnectableApp() { | 309 scoped_refptr<const Extension> LoadChromiumConnectableApp( |
| 310 bool with_event_handlers = true) { |
310 scoped_refptr<const Extension> extension = | 311 scoped_refptr<const Extension> extension = |
311 LoadExtensionIntoDir(&web_connectable_dir_, | 312 LoadExtensionIntoDir(&web_connectable_dir_, |
312 "{" | 313 "{" |
313 " \"app\": {" | 314 " \"app\": {" |
314 " \"background\": {" | 315 " \"background\": {" |
315 " \"scripts\": [\"background.js\"]" | 316 " \"scripts\": [\"background.js\"]" |
316 " }" | 317 " }" |
317 " }," | 318 " }," |
318 " \"externally_connectable\": {" | 319 " \"externally_connectable\": {" |
319 " \"matches\": [\"*://*.chromium.org:*/*\"]" | 320 " \"matches\": [\"*://*.chromium.org:*/*\"]" |
320 " }," | 321 " }," |
321 " \"manifest_version\": 2," | 322 " \"manifest_version\": 2," |
322 " \"name\": \"app_connectable\"," | 323 " \"name\": \"app_connectable\"," |
323 " \"version\": \"1.0\"" | 324 " \"version\": \"1.0\"" |
324 "}"); | 325 "}", |
| 326 with_event_handlers); |
325 CHECK(extension.get()); | 327 CHECK(extension.get()); |
326 return extension; | 328 return extension; |
327 } | 329 } |
328 | 330 |
329 scoped_refptr<const Extension> LoadNotConnectableExtension() { | 331 scoped_refptr<const Extension> LoadNotConnectableExtension() { |
330 scoped_refptr<const Extension> extension = | 332 scoped_refptr<const Extension> extension = |
331 LoadExtensionIntoDir(¬_connectable_dir_, | 333 LoadExtensionIntoDir(¬_connectable_dir_, |
332 base::StringPrintf( | 334 base::StringPrintf( |
333 "{" | 335 "{" |
334 " \"name\": \"not_connectable\"," | 336 " \"name\": \"not_connectable\"," |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 host_resolver()->AddRule("*", embedded_test_server()->base_url().host()); | 377 host_resolver()->AddRule("*", embedded_test_server()->base_url().host()); |
376 } | 378 } |
377 | 379 |
378 const char* close_background_message() { | 380 const char* close_background_message() { |
379 return "closeBackgroundPage"; | 381 return "closeBackgroundPage"; |
380 } | 382 } |
381 | 383 |
382 private: | 384 private: |
383 scoped_refptr<const Extension> LoadExtensionIntoDir( | 385 scoped_refptr<const Extension> LoadExtensionIntoDir( |
384 TestExtensionDir* dir, | 386 TestExtensionDir* dir, |
385 const std::string& manifest) { | 387 const std::string& manifest, |
| 388 bool with_event_handlers = true) { |
386 dir->WriteManifest(manifest); | 389 dir->WriteManifest(manifest); |
387 dir->WriteFile(FILE_PATH_LITERAL("background.js"), | 390 if (with_event_handlers) { |
388 base::StringPrintf( | 391 dir->WriteFile( |
389 "function maybeClose(message) {\n" | 392 FILE_PATH_LITERAL("background.js"), |
390 " if (message.indexOf('%s') >= 0)\n" | 393 base::StringPrintf( |
391 " window.setTimeout(function() { window.close() }, 0);\n" | 394 "function maybeClose(message) {\n" |
392 "}\n" | 395 " if (message.indexOf('%s') >= 0)\n" |
393 "chrome.runtime.onMessageExternal.addListener(\n" | 396 " window.setTimeout(function() { window.close() }, 0);\n" |
394 " function(message, sender, reply) {\n" | 397 "}\n" |
395 " reply({ message: message, sender: sender });\n" | 398 "chrome.runtime.onMessageExternal.addListener(\n" |
396 " maybeClose(message);\n" | 399 " function(message, sender, reply) {\n" |
397 "});\n" | 400 " reply({ message: message, sender: sender });\n" |
398 "chrome.runtime.onConnectExternal.addListener(function(port) {\n" | 401 " maybeClose(message);\n" |
399 " port.onMessage.addListener(function(message) {\n" | 402 "});\n" |
400 " port.postMessage({ message: message, sender: port.sender });\n" | 403 "chrome.runtime.onConnectExternal.addListener(function(port) {\n" |
401 " maybeClose(message);\n" | 404 " port.onMessage.addListener(function(message) {\n" |
402 " });\n" | 405 " port.postMessage({ message: message, sender: port.sender " |
403 "});\n", | 406 "});\n" |
404 close_background_message())); | 407 " maybeClose(message);\n" |
| 408 " });\n" |
| 409 "});\n", |
| 410 close_background_message())); |
| 411 } else { |
| 412 dir->WriteFile(FILE_PATH_LITERAL("background.js"), ""); |
| 413 } |
405 return LoadExtension(dir->unpacked_path()); | 414 return LoadExtension(dir->unpacked_path()); |
406 } | 415 } |
407 | 416 |
408 const char* common_manifest() { | 417 const char* common_manifest() { |
409 return "\"version\": \"1.0\"," | 418 return "\"version\": \"1.0\"," |
410 "\"background\": {" | 419 "\"background\": {" |
411 " \"scripts\": [\"background.js\"]," | 420 " \"scripts\": [\"background.js\"]," |
412 " \"persistent\": false" | 421 " \"persistent\": false" |
413 "}," | 422 "}," |
414 "\"manifest_version\": 2"; | 423 "\"manifest_version\": 2"; |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
728 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount()); | 737 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount()); |
729 } | 738 } |
730 | 739 |
731 // Allowing the extension in incognito mode will bypass the deny. | 740 // Allowing the extension in incognito mode will bypass the deny. |
732 ExtensionPrefs::Get(profile())->SetIsIncognitoEnabled(extension->id(), true); | 741 ExtensionPrefs::Get(profile())->SetIsIncognitoEnabled(extension->id(), true); |
733 EXPECT_EQ( | 742 EXPECT_EQ( |
734 OK, | 743 OK, |
735 CanConnectAndSendMessagesToFrame(incognito_frame, extension.get(), NULL)); | 744 CanConnectAndSendMessagesToFrame(incognito_frame, extension.get(), NULL)); |
736 } | 745 } |
737 | 746 |
| 747 // Tests connection from incognito tabs when the extension doesn't have an event |
| 748 // handler for the connection event. |
| 749 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, |
| 750 FromIncognitoNoEventHandlerInApp) { |
| 751 InitializeTestServer(); |
| 752 |
| 753 scoped_refptr<const Extension> app = LoadChromiumConnectableApp(false); |
| 754 ASSERT_TRUE(app->is_platform_app()); |
| 755 |
| 756 Browser* incognito_browser = ui_test_utils::OpenURLOffTheRecord( |
| 757 profile()->GetOffTheRecordProfile(), chromium_org_url()); |
| 758 content::RenderFrameHost* incognito_frame = |
| 759 incognito_browser->tab_strip_model() |
| 760 ->GetActiveWebContents() |
| 761 ->GetMainFrame(); |
| 762 |
| 763 { |
| 764 IncognitoConnectability::ScopedAlertTracker alert_tracker( |
| 765 IncognitoConnectability::ScopedAlertTracker::ALWAYS_ALLOW); |
| 766 |
| 767 // No connection because incognito-enabled hasn't been set for the app, and |
| 768 // the app hasn't installed event handlers. |
| 769 EXPECT_EQ( |
| 770 COULD_NOT_ESTABLISH_CONNECTION_ERROR, |
| 771 CanConnectAndSendMessagesToFrame(incognito_frame, app.get(), NULL)); |
| 772 // No dialog should have been shown. |
| 773 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount()); |
| 774 } |
| 775 } |
| 776 |
738 // Tests connection from incognito tabs when the user accepts the connection | 777 // Tests connection from incognito tabs when the user accepts the connection |
739 // request. Spanning mode only. Separate tests for apps and extensions. | 778 // request. Spanning mode only. Separate tests for apps and extensions. |
740 // | 779 // |
741 // TODO(kalman): see comment above about split mode. | 780 // TODO(kalman): see comment above about split mode. |
742 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, | 781 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, |
743 FromIncognitoAllowApp) { | 782 FromIncognitoAllowApp) { |
744 InitializeTestServer(); | 783 InitializeTestServer(); |
745 | 784 |
746 scoped_refptr<const Extension> app = LoadChromiumConnectableApp(); | 785 scoped_refptr<const Extension> app = LoadChromiumConnectableApp(); |
747 ASSERT_TRUE(app->is_platform_app()); | 786 ASSERT_TRUE(app->is_platform_app()); |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1146 ui_test_utils::NavigateToURL(browser(), chromium_org_url()); | 1185 ui_test_utils::NavigateToURL(browser(), chromium_org_url()); |
1147 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, | 1186 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, |
1148 CanConnectAndSendMessagesToMainFrame(invalid.get())); | 1187 CanConnectAndSendMessagesToMainFrame(invalid.get())); |
1149 } | 1188 } |
1150 | 1189 |
1151 #endif // !defined(OS_WIN) - http://crbug.com/350517. | 1190 #endif // !defined(OS_WIN) - http://crbug.com/350517. |
1152 | 1191 |
1153 } // namespace | 1192 } // namespace |
1154 | 1193 |
1155 }; // namespace extensions | 1194 }; // namespace extensions |
OLD | NEW |