1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/base64.h"
#include "base/files/file_path.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "base/values.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/messaging/incognito_connectability.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/test_extension_dir.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/api/runtime.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/value_builder.h"
#include "net/cert/asn1_util.h"
#include "net/cert/jwk_serializer.h"
#include "net/dns/mock_host_resolver.h"
#include "net/ssl/channel_id_service.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "url/gurl.h"
namespace extensions {
namespace {
class MessageSender : public content::NotificationObserver {
public:
MessageSender() {
registrar_.Add(this,
extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
content::NotificationService::AllSources());
}
private:
static scoped_ptr<base::ListValue> BuildEventArguments(
const bool last_message,
const std::string& data) {
base::DictionaryValue* event = new base::DictionaryValue();
event->SetBoolean("lastMessage", last_message);
event->SetString("data", data);
scoped_ptr<base::ListValue> arguments(new base::ListValue());
arguments->Append(event);
return arguments.Pass();
}
static scoped_ptr<Event> BuildEvent(scoped_ptr<base::ListValue> event_args,
Profile* profile,
GURL event_url) {
scoped_ptr<Event> event(new Event("test.onMessage", event_args.Pass()));
event->restrict_to_browser_context = profile;
event->event_url = event_url;
return event.Pass();
}
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override {
EventRouter* event_router =
EventRouter::Get(content::Source<Profile>(source).ptr());
// Sends four messages to the extension. All but the third message sent
// from the origin http://b.com/ are supposed to arrive.
event_router->BroadcastEvent(BuildEvent(
BuildEventArguments(false, "no restriction"),
content::Source<Profile>(source).ptr(),
GURL()));
event_router->BroadcastEvent(BuildEvent(
BuildEventArguments(false, "http://a.com/"),
content::Source<Profile>(source).ptr(),
GURL("http://a.com/")));
event_router->BroadcastEvent(BuildEvent(
BuildEventArguments(false, "http://b.com/"),
content::Source<Profile>(source).ptr(),
GURL("http://b.com/")));
event_router->BroadcastEvent(BuildEvent(
BuildEventArguments(true, "last message"),
content::Source<Profile>(source).ptr(),
GURL()));
}
content::NotificationRegistrar registrar_;
};
// Tests that message passing between extensions and content scripts works.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Messaging) {
ASSERT_TRUE(StartEmbeddedTestServer());
ASSERT_TRUE(RunExtensionTest("messaging/connect")) << message_;
}
// Tests that message passing from one extension to another works.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MessagingExternal) {
ASSERT_TRUE(LoadExtension(
test_data_dir_.AppendASCII("..").AppendASCII("good")
.AppendASCII("Extensions")
.AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa")
.AppendASCII("1.0")));
ASSERT_TRUE(RunExtensionTest("messaging/connect_external")) << message_;
}
// Tests that messages with event_urls are only passed to extensions with
// appropriate permissions.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MessagingEventURL) {
MessageSender sender;
ASSERT_TRUE(RunExtensionTest("messaging/event_url")) << message_;
}
// Tests connecting from a panel to its extension.
class PanelMessagingTest : public ExtensionApiTest {
void SetUpCommandLine(base::CommandLine* command_line) override {
ExtensionApiTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kEnablePanels);
}
};
IN_PROC_BROWSER_TEST_F(PanelMessagingTest, MessagingPanel) {
ASSERT_TRUE(RunExtensionTest("messaging/connect_panel")) << message_;
}
// XXX(kalman): All web messaging tests disabled on windows due to extreme
// flakiness. See http://crbug.com/350517.
#if !defined(OS_WIN)
// Tests externally_connectable between a web page and an extension.
//
// TODO(kalman): Test between extensions. This is already tested in this file,
// but not with externally_connectable set in the manifest.
//
// TODO(kalman): Test with host permissions.
class ExternallyConnectableMessagingTest : public ExtensionApiTest {
protected:
// Result codes from the test. These must match up with |results| in
// c/t/d/extensions/api_test/externally_connectable/assertions.json.
enum Result {
OK = 0,
NAMESPACE_NOT_DEFINED = 1,
FUNCTION_NOT_DEFINED = 2,
COULD_NOT_ESTABLISH_CONNECTION_ERROR = 3,
OTHER_ERROR = 4,
INCORRECT_RESPONSE_SENDER = 5,
INCORRECT_RESPONSE_MESSAGE = 6,
};
bool AppendIframe(const GURL& src) {
bool result;
CHECK(content::ExecuteScriptAndExtractBool(
browser()->tab_strip_model()->GetActiveWebContents(),
"actions.appendIframe('" + src.spec() + "');", &result));
return result;
}
Result CanConnectAndSendMessagesToMainFrame(const Extension* extension,
const char* message = NULL) {
return CanConnectAndSendMessagesToFrame(
browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
extension,
message);
}
Result CanConnectAndSendMessagesToIFrame(const Extension* extension,
const char* message = NULL) {
content::RenderFrameHost* frame = content::FrameMatchingPredicate(
browser()->tab_strip_model()->GetActiveWebContents(),
base::Bind(&content::FrameIsChildOfMainFrame));
return CanConnectAndSendMessagesToFrame(frame, extension, message);
}
Result CanConnectAndSendMessagesToFrame(content::RenderFrameHost* frame,
const Extension* extension,
const char* message) {
int result;
std::string command = base::StringPrintf(
"assertions.canConnectAndSendMessages('%s', %s, %s)",
extension->id().c_str(),
extension->is_platform_app() ? "true" : "false",
message ? base::StringPrintf("'%s'", message).c_str() : "undefined");
CHECK(content::ExecuteScriptAndExtractInt(frame, command, &result));
return static_cast<Result>(result);
}
testing::AssertionResult AreAnyNonWebApisDefinedForMainFrame() {
return AreAnyNonWebApisDefinedForFrame(
browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame());
}
testing::AssertionResult AreAnyNonWebApisDefinedForIFrame() {
content::RenderFrameHost* frame = content::FrameMatchingPredicate(
browser()->tab_strip_model()->GetActiveWebContents(),
base::Bind(&content::FrameIsChildOfMainFrame));
return AreAnyNonWebApisDefinedForFrame(frame);
}
testing::AssertionResult AreAnyNonWebApisDefinedForFrame(
content::RenderFrameHost* frame) {
// All runtime API methods are non-web except for sendRequest and connect.
const char* const non_messaging_apis[] = {
"getBackgroundPage",
"getManifest",
"getURL",
"reload",
"requestUpdateCheck",
"restart",
"connectNative",
"sendNativeMessage",
"onStartup",
"onInstalled",
"onSuspend",
"onSuspendCanceled",
"onUpdateAvailable",
"onBrowserUpdateAvailable",
"onConnect",
"onConnectExternal",
"onMessage",
"onMessageExternal",
"onRestartRequired",
// Note: no "id" here because this test method is used for hosted apps,
// which do have access to runtime.id.
};
// Turn the array into a JS array, which effectively gets eval()ed.
std::string as_js_array;
for (size_t i = 0; i < arraysize(non_messaging_apis); ++i) {
as_js_array += as_js_array.empty() ? "[" : ",";
as_js_array += base::StringPrintf("'%s'", non_messaging_apis[i]);
}
as_js_array += "]";
bool any_defined;
CHECK(content::ExecuteScriptAndExtractBool(
frame,
"assertions.areAnyRuntimePropertiesDefined(" + as_js_array + ")",
&any_defined));
return any_defined ?
testing::AssertionSuccess() : testing::AssertionFailure();
}
std::string GetTlsChannelIdFromPortConnect(const Extension* extension,
bool include_tls_channel_id,
const char* message = NULL) {
return GetTlsChannelIdFromAssertion("getTlsChannelIdFromPortConnect",
extension,
include_tls_channel_id,
message);
}
std::string GetTlsChannelIdFromSendMessage(const Extension* extension,
bool include_tls_channel_id,
const char* message = NULL) {
return GetTlsChannelIdFromAssertion("getTlsChannelIdFromSendMessage",
extension,
include_tls_channel_id,
message);
}
GURL GetURLForPath(const std::string& host, const std::string& path) {
std::string port = base::IntToString(embedded_test_server()->port());
GURL::Replacements replacements;
replacements.SetHostStr(host);
replacements.SetPortStr(port);
return embedded_test_server()->GetURL(path).ReplaceComponents(replacements);
}
GURL chromium_org_url() {
return GetURLForPath("www.chromium.org", "/chromium.org.html");
}
GURL google_com_url() {
return GetURLForPath("www.google.com", "/google.com.html");
}
scoped_refptr<const Extension> LoadChromiumConnectableExtension() {
scoped_refptr<const Extension> extension =
LoadExtensionIntoDir(&web_connectable_dir_,
base::StringPrintf(
"{"
" \"name\": \"chromium_connectable\","
" %s,"
" \"externally_connectable\": {"
" \"matches\": [\"*://*.chromium.org:*/*\"]"
" }"
"}",
common_manifest()));
CHECK(extension.get());
return extension;
}
scoped_refptr<const Extension> LoadChromiumConnectableApp(
bool with_event_handlers = true) {
scoped_refptr<const Extension> extension =
LoadExtensionIntoDir(&web_connectable_dir_,
"{"
" \"app\": {"
" \"background\": {"
" \"scripts\": [\"background.js\"]"
" }"
" },"
" \"externally_connectable\": {"
" \"matches\": [\"*://*.chromium.org:*/*\"]"
" },"
" \"manifest_version\": 2,"
" \"name\": \"app_connectable\","
" \"version\": \"1.0\""
"}",
with_event_handlers);
CHECK(extension.get());
return extension;
}
scoped_refptr<const Extension> LoadNotConnectableExtension() {
scoped_refptr<const Extension> extension =
LoadExtensionIntoDir(¬_connectable_dir_,
base::StringPrintf(
"{"
" \"name\": \"not_connectable\","
" %s"
"}",
common_manifest()));
CHECK(extension.get());
return extension;
}
scoped_refptr<const Extension>
LoadChromiumConnectableExtensionWithTlsChannelId() {
return LoadExtensionIntoDir(&tls_channel_id_connectable_dir_,
connectable_with_tls_channel_id_manifest());
}
scoped_refptr<const Extension> LoadChromiumHostedApp() {
scoped_refptr<const Extension> hosted_app =
LoadExtensionIntoDir(&hosted_app_dir_,
base::StringPrintf(
"{"
" \"name\": \"chromium_hosted_app\","
" \"version\": \"1.0\","
" \"manifest_version\": 2,"
" \"app\": {"
" \"urls\": [\"%s\"],"
" \"launch\": {"
" \"web_url\": \"%s\""
" }\n"
" }\n"
"}",
chromium_org_url().spec().c_str(),
chromium_org_url().spec().c_str()));
CHECK(hosted_app.get());
return hosted_app;
}
void InitializeTestServer() {
base::FilePath test_data;
EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data));
embedded_test_server()->ServeFilesFromDirectory(test_data.AppendASCII(
"extensions/api_test/messaging/externally_connectable/sites"));
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
host_resolver()->AddRule("*", embedded_test_server()->base_url().host());
}
const char* close_background_message() {
return "closeBackgroundPage";
}
private:
scoped_refptr<const Extension> LoadExtensionIntoDir(
TestExtensionDir* dir,
const std::string& manifest,
bool with_event_handlers = true) {
dir->WriteManifest(manifest);
if (with_event_handlers) {
dir->WriteFile(
FILE_PATH_LITERAL("background.js"),
base::StringPrintf(
"function maybeClose(message) {\n"
" if (message.indexOf('%s') >= 0)\n"
" window.setTimeout(function() { window.close() }, 0);\n"
"}\n"
"chrome.runtime.onMessageExternal.addListener(\n"
" function(message, sender, reply) {\n"
" reply({ message: message, sender: sender });\n"
" maybeClose(message);\n"
"});\n"
"chrome.runtime.onConnectExternal.addListener(function(port) {\n"
" port.onMessage.addListener(function(message) {\n"
" port.postMessage({ message: message, sender: port.sender "
"});\n"
" maybeClose(message);\n"
" });\n"
"});\n",
close_background_message()));
} else {
dir->WriteFile(FILE_PATH_LITERAL("background.js"), "");
}
return LoadExtension(dir->unpacked_path());
}
const char* common_manifest() {
return "\"version\": \"1.0\","
"\"background\": {"
" \"scripts\": [\"background.js\"],"
" \"persistent\": false"
"},"
"\"manifest_version\": 2";
}
std::string connectable_with_tls_channel_id_manifest() {
return base::StringPrintf(
"{"
" \"name\": \"chromium_connectable_with_tls_channel_id\","
" %s,"
" \"externally_connectable\": {"
" \"matches\": [\"*://*.chromium.org:*/*\"],"
" \"accepts_tls_channel_id\": true"
" }"
"}",
common_manifest());
}
std::string GetTlsChannelIdFromAssertion(const char* method,
const Extension* extension,
bool include_tls_channel_id,
const char* message) {
std::string result;
std::string args = "'" + extension->id() + "', ";
args += include_tls_channel_id ? "true" : "false";
if (message)
args += std::string(", '") + message + "'";
CHECK(content::ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetActiveWebContents(),
base::StringPrintf("assertions.%s(%s)", method, args.c_str()),
&result));
return result;
}
TestExtensionDir web_connectable_dir_;
TestExtensionDir not_connectable_dir_;
TestExtensionDir tls_channel_id_connectable_dir_;
TestExtensionDir hosted_app_dir_;
};
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, NotInstalled) {
InitializeTestServer();
scoped_refptr<const Extension> extension =
ExtensionBuilder()
.SetID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
.SetManifest(DictionaryBuilder()
.Set("name", "Fake extension")
.Set("version", "1")
.Set("manifest_version", 2))
.Build();
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
EXPECT_EQ(NAMESPACE_NOT_DEFINED,
CanConnectAndSendMessagesToMainFrame(extension.get()));
EXPECT_FALSE(AreAnyNonWebApisDefinedForMainFrame());
ui_test_utils::NavigateToURL(browser(), google_com_url());
EXPECT_EQ(NAMESPACE_NOT_DEFINED,
CanConnectAndSendMessagesToMainFrame(extension.get()));
EXPECT_FALSE(AreAnyNonWebApisDefinedForMainFrame());
}
// Tests two extensions on the same sites: one web connectable, one not.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
WebConnectableAndNotConnectable) {
InitializeTestServer();
// Install the web connectable extension. chromium.org can connect to it,
// google.com can't.
scoped_refptr<const Extension> chromium_connectable =
LoadChromiumConnectableExtension();
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
EXPECT_EQ(OK,
CanConnectAndSendMessagesToMainFrame(chromium_connectable.get()));
EXPECT_FALSE(AreAnyNonWebApisDefinedForMainFrame());
ui_test_utils::NavigateToURL(browser(), google_com_url());
EXPECT_EQ(NAMESPACE_NOT_DEFINED,
CanConnectAndSendMessagesToMainFrame(chromium_connectable.get()));
EXPECT_FALSE(AreAnyNonWebApisDefinedForMainFrame());
// Install the non-connectable extension. Nothing can connect to it.
scoped_refptr<const Extension> not_connectable =
LoadNotConnectableExtension();
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
// Namespace will be defined here because |chromium_connectable| can connect
// to it - so this will be the "cannot establish connection" error.
EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
CanConnectAndSendMessagesToMainFrame(not_connectable.get()));
EXPECT_FALSE(AreAnyNonWebApisDefinedForMainFrame());
ui_test_utils::NavigateToURL(browser(), google_com_url());
EXPECT_EQ(NAMESPACE_NOT_DEFINED,
CanConnectAndSendMessagesToMainFrame(not_connectable.get()));
EXPECT_FALSE(AreAnyNonWebApisDefinedForMainFrame());
}
// See http://crbug.com/297866
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
DISABLED_BackgroundPageClosesOnMessageReceipt) {
InitializeTestServer();
// Install the web connectable extension.
scoped_refptr<const Extension> chromium_connectable =
LoadChromiumConnectableExtension();
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
// If the background page closes after receipt of the message, it will still
// reply to this message...
EXPECT_EQ(OK,
CanConnectAndSendMessagesToMainFrame(chromium_connectable.get(),
close_background_message()));
// and be re-opened by receipt of a subsequent message.
EXPECT_EQ(OK,
CanConnectAndSendMessagesToMainFrame(chromium_connectable.get()));
}
// Tests a web connectable extension that doesn't receive TLS channel id.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
WebConnectableWithoutTlsChannelId) {
InitializeTestServer();
// Install the web connectable extension. chromium.org can connect to it,
// google.com can't.
scoped_refptr<const Extension> chromium_connectable =
LoadChromiumConnectableExtension();
ASSERT_TRUE(chromium_connectable.get());
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
// The web connectable extension doesn't request the TLS channel ID, so it
// doesn't get it, whether or not the page asks for it.
EXPECT_EQ(std::string(),
GetTlsChannelIdFromPortConnect(chromium_connectable.get(), false));
EXPECT_EQ(std::string(),
GetTlsChannelIdFromSendMessage(chromium_connectable.get(), true));
EXPECT_EQ(std::string(),
GetTlsChannelIdFromPortConnect(chromium_connectable.get(), false));
EXPECT_EQ(std::string(),
GetTlsChannelIdFromSendMessage(chromium_connectable.get(), true));
}
// Tests a web connectable extension that receives TLS channel id with a site
// that can't connect to it.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
WebConnectableWithTlsChannelIdWithNonMatchingSite) {
InitializeTestServer();
scoped_refptr<const Extension> chromium_connectable =
LoadChromiumConnectableExtensionWithTlsChannelId();
ASSERT_TRUE(chromium_connectable.get());
ui_test_utils::NavigateToURL(browser(), google_com_url());
// The extension requests the TLS channel ID, but it doesn't get it for a
// site that can't connect to it, regardless of whether the page asks for it.
EXPECT_EQ(base::StringPrintf("%d", NAMESPACE_NOT_DEFINED),
GetTlsChannelIdFromPortConnect(chromium_connectable.get(), false));
EXPECT_EQ(base::StringPrintf("%d", NAMESPACE_NOT_DEFINED),
GetTlsChannelIdFromSendMessage(chromium_connectable.get(), true));
EXPECT_EQ(base::StringPrintf("%d", NAMESPACE_NOT_DEFINED),
GetTlsChannelIdFromPortConnect(chromium_connectable.get(), false));
EXPECT_EQ(base::StringPrintf("%d", NAMESPACE_NOT_DEFINED),
GetTlsChannelIdFromSendMessage(chromium_connectable.get(), true));
}
// Tests a web connectable extension that receives TLS channel id on a site
// that can connect to it, but with no TLS channel ID having been generated.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
WebConnectableWithTlsChannelIdWithEmptyTlsChannelId) {
InitializeTestServer();
scoped_refptr<const Extension> chromium_connectable =
LoadChromiumConnectableExtensionWithTlsChannelId();
ASSERT_TRUE(chromium_connectable.get());
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
// Since the extension requests the TLS channel ID, it gets it for a site that
// can connect to it, but only if the page also asks to include it.
EXPECT_EQ(std::string(),
GetTlsChannelIdFromPortConnect(chromium_connectable.get(), false));
EXPECT_EQ(std::string(),
GetTlsChannelIdFromSendMessage(chromium_connectable.get(), false));
// If the page does ask for it, it isn't empty.
std::string tls_channel_id =
GetTlsChannelIdFromPortConnect(chromium_connectable.get(), true);
// Because the TLS channel ID has never been generated for this domain,
// no TLS channel ID is reported.
EXPECT_EQ(std::string(), tls_channel_id);
}
// Flaky on Linux and Windows. http://crbug.com/315264
// Tests a web connectable extension that receives TLS channel id, but
// immediately closes its background page upon receipt of a message.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
DISABLED_WebConnectableWithEmptyTlsChannelIdAndClosedBackgroundPage) {
InitializeTestServer();
scoped_refptr<const Extension> chromium_connectable =
LoadChromiumConnectableExtensionWithTlsChannelId();
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
// If the page does ask for it, it isn't empty, even if the background page
// closes upon receipt of the connect.
std::string tls_channel_id = GetTlsChannelIdFromPortConnect(
chromium_connectable.get(), true, close_background_message());
// Because the TLS channel ID has never been generated for this domain,
// no TLS channel ID is reported.
EXPECT_EQ(std::string(), tls_channel_id);
// A subsequent connect will still succeed, even if the background page was
// previously closed.
tls_channel_id =
GetTlsChannelIdFromPortConnect(chromium_connectable.get(), true);
// And the empty value is still retrieved.
EXPECT_EQ(std::string(), tls_channel_id);
}
// Tests that enabling and disabling an extension makes the runtime bindings
// appear and disappear.
//
// TODO(kalman): Test with multiple extensions that can be accessed by the same
// host.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
EnablingAndDisabling) {
InitializeTestServer();
scoped_refptr<const Extension> chromium_connectable =
LoadChromiumConnectableExtension();
scoped_refptr<const Extension> not_connectable =
LoadNotConnectableExtension();
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
EXPECT_EQ(OK,
CanConnectAndSendMessagesToMainFrame(chromium_connectable.get()));
EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
CanConnectAndSendMessagesToMainFrame(not_connectable.get()));
DisableExtension(chromium_connectable->id());
EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
CanConnectAndSendMessagesToMainFrame(chromium_connectable.get()));
EnableExtension(chromium_connectable->id());
EXPECT_EQ(OK,
CanConnectAndSendMessagesToMainFrame(chromium_connectable.get()));
EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
CanConnectAndSendMessagesToMainFrame(not_connectable.get()));
}
// Tests connection from incognito tabs when the user denies the connection
// request. Spanning mode only. A separate test for apps and extensions.
//
// TODO(kalman): ensure that we exercise split vs spanning incognito logic
// somewhere. This is a test that should be shared with the content script logic
// so it's not really our specific concern for web connectable.
//
// TODO(kalman): test messages from incognito extensions too.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
FromIncognitoDenyApp) {
InitializeTestServer();
scoped_refptr<const Extension> app = LoadChromiumConnectableApp();
ASSERT_TRUE(app->is_platform_app());
Browser* incognito_browser = ui_test_utils::OpenURLOffTheRecord(
profile()->GetOffTheRecordProfile(),
chromium_org_url());
content::RenderFrameHost* incognito_frame = incognito_browser->
tab_strip_model()->GetActiveWebContents()->GetMainFrame();
{
IncognitoConnectability::ScopedAlertTracker alert_tracker(
IncognitoConnectability::ScopedAlertTracker::ALWAYS_DENY);
// No connection because incognito-enabled hasn't been set for the app, and
// the user denied our interactive request.
EXPECT_EQ(
COULD_NOT_ESTABLISH_CONNECTION_ERROR,
CanConnectAndSendMessagesToFrame(incognito_frame, app.get(), NULL));
EXPECT_EQ(1, alert_tracker.GetAndResetAlertCount());
// Try again. User has already denied so alert not shown.
EXPECT_EQ(
COULD_NOT_ESTABLISH_CONNECTION_ERROR,
CanConnectAndSendMessagesToFrame(incognito_frame, app.get(), NULL));
EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount());
}
// It's not possible to allow an app in incognito.
ExtensionPrefs::Get(profile())->SetIsIncognitoEnabled(app->id(), true);
EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
CanConnectAndSendMessagesToFrame(incognito_frame, app.get(), NULL));
}
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
FromIncognitoDenyExtension) {
InitializeTestServer();
scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension();
Browser* incognito_browser = ui_test_utils::OpenURLOffTheRecord(
profile()->GetOffTheRecordProfile(), chromium_org_url());
content::RenderFrameHost* incognito_frame =
incognito_browser->tab_strip_model()
->GetActiveWebContents()
->GetMainFrame();
{
IncognitoConnectability::ScopedAlertTracker alert_tracker(
IncognitoConnectability::ScopedAlertTracker::ALWAYS_DENY);
// The alert doesn't show for extensions.
EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
CanConnectAndSendMessagesToFrame(
incognito_frame, extension.get(), NULL));
EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount());
}
// Allowing the extension in incognito mode will bypass the deny.
ExtensionPrefs::Get(profile())->SetIsIncognitoEnabled(extension->id(), true);
EXPECT_EQ(
OK,
CanConnectAndSendMessagesToFrame(incognito_frame, extension.get(), NULL));
}
// Tests connection from incognito tabs when the extension doesn't have an event
// handler for the connection event.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
FromIncognitoNoEventHandlerInApp) {
InitializeTestServer();
scoped_refptr<const Extension> app = LoadChromiumConnectableApp(false);
ASSERT_TRUE(app->is_platform_app());
Browser* incognito_browser = ui_test_utils::OpenURLOffTheRecord(
profile()->GetOffTheRecordProfile(), chromium_org_url());
content::RenderFrameHost* incognito_frame =
incognito_browser->tab_strip_model()
->GetActiveWebContents()
->GetMainFrame();
{
IncognitoConnectability::ScopedAlertTracker alert_tracker(
IncognitoConnectability::ScopedAlertTracker::ALWAYS_ALLOW);
// No connection because incognito-enabled hasn't been set for the app, and
// the app hasn't installed event handlers.
EXPECT_EQ(
COULD_NOT_ESTABLISH_CONNECTION_ERROR,
CanConnectAndSendMessagesToFrame(incognito_frame, app.get(), NULL));
// No dialog should have been shown.
EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount());
}
}
// Tests connection from incognito tabs when the user accepts the connection
// request. Spanning mode only. Separate tests for apps and extensions.
//
// TODO(kalman): see comment above about split mode.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
FromIncognitoAllowApp) {
InitializeTestServer();
scoped_refptr<const Extension> app = LoadChromiumConnectableApp();
ASSERT_TRUE(app->is_platform_app());
Browser* incognito_browser = ui_test_utils::OpenURLOffTheRecord(
profile()->GetOffTheRecordProfile(),
chromium_org_url());
content::RenderFrameHost* incognito_frame = incognito_browser->
tab_strip_model()->GetActiveWebContents()->GetMainFrame();
{
IncognitoConnectability::ScopedAlertTracker alert_tracker(
IncognitoConnectability::ScopedAlertTracker::ALWAYS_ALLOW);
// Connection allowed even with incognito disabled, because the user
// accepted the interactive request.
EXPECT_EQ(
OK, CanConnectAndSendMessagesToFrame(incognito_frame, app.get(), NULL));
EXPECT_EQ(1, alert_tracker.GetAndResetAlertCount());
// Try again. User has already allowed.
EXPECT_EQ(
OK, CanConnectAndSendMessagesToFrame(incognito_frame, app.get(), NULL));
EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount());
}
// Apps can't be allowed in incognito mode, but it's moot because it's
// already allowed.
ExtensionPrefs::Get(profile())->SetIsIncognitoEnabled(app->id(), true);
EXPECT_EQ(OK,
CanConnectAndSendMessagesToFrame(incognito_frame, app.get(), NULL));
}
// Tests connection from incognito tabs when there are multiple tabs open to the
// same origin. The user should only need to accept the connection request once.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
FromIncognitoPromptApp) {
InitializeTestServer();
scoped_refptr<const Extension> app = LoadChromiumConnectableApp();
ASSERT_TRUE(app->is_platform_app());
// Open an incognito browser with two tabs displaying "chromium.org".
Browser* incognito_browser = ui_test_utils::OpenURLOffTheRecord(
profile()->GetOffTheRecordProfile(), chromium_org_url());
content::RenderFrameHost* incognito_frame1 =
incognito_browser->tab_strip_model()
->GetActiveWebContents()
->GetMainFrame();
InfoBarService* infobar_service1 = InfoBarService::FromWebContents(
incognito_browser->tab_strip_model()->GetActiveWebContents());
CHECK(ui_test_utils::OpenURLOffTheRecord(profile()->GetOffTheRecordProfile(),
chromium_org_url()) ==
incognito_browser);
content::RenderFrameHost* incognito_frame2 =
incognito_browser->tab_strip_model()
->GetActiveWebContents()
->GetMainFrame();
InfoBarService* infobar_service2 = InfoBarService::FromWebContents(
incognito_browser->tab_strip_model()->GetActiveWebContents());
EXPECT_EQ(2, incognito_browser->tab_strip_model()->count());
EXPECT_NE(incognito_frame1, incognito_frame2);
// Trigger a infobars in both tabs by trying to send messages.
std::string script =
base::StringPrintf("assertions.trySendMessage('%s')", app->id().c_str());
CHECK(content::ExecuteScript(incognito_frame1, script));
CHECK(content::ExecuteScript(incognito_frame2, script));
EXPECT_EQ(1U, infobar_service1->infobar_count());
EXPECT_EQ(1U, infobar_service2->infobar_count());
// Navigating away will dismiss the infobar on the active tab only.
ui_test_utils::NavigateToURL(incognito_browser, google_com_url());
EXPECT_EQ(1U, infobar_service1->infobar_count());
EXPECT_EQ(0U, infobar_service2->infobar_count());
// Navigate back and accept the infobar this time. Both should be dismissed.
{
IncognitoConnectability::ScopedAlertTracker alert_tracker(
IncognitoConnectability::ScopedAlertTracker::ALWAYS_ALLOW);
ui_test_utils::NavigateToURL(incognito_browser, chromium_org_url());
incognito_frame2 = incognito_browser->tab_strip_model()
->GetActiveWebContents()
->GetMainFrame();
EXPECT_NE(incognito_frame1, incognito_frame2);
EXPECT_EQ(1U, infobar_service1->infobar_count());
EXPECT_EQ(OK, CanConnectAndSendMessagesToFrame(incognito_frame2, app.get(),
NULL));
EXPECT_EQ(1, alert_tracker.GetAndResetAlertCount());
EXPECT_EQ(0U, infobar_service1->infobar_count());
}
}
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
FromIncognitoAllowExtension) {
InitializeTestServer();
scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension();
Browser* incognito_browser = ui_test_utils::OpenURLOffTheRecord(
profile()->GetOffTheRecordProfile(), chromium_org_url());
content::RenderFrameHost* incognito_frame =
incognito_browser->tab_strip_model()
->GetActiveWebContents()
->GetMainFrame();
{
IncognitoConnectability::ScopedAlertTracker alert_tracker(
IncognitoConnectability::ScopedAlertTracker::ALWAYS_ALLOW);
// No alert is shown.
EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
CanConnectAndSendMessagesToFrame(
incognito_frame, extension.get(), NULL));
EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount());
}
// Allowing the extension in incognito mode is what allows connections.
ExtensionPrefs::Get(profile())->SetIsIncognitoEnabled(extension->id(), true);
EXPECT_EQ(
OK,
CanConnectAndSendMessagesToFrame(incognito_frame, extension.get(), NULL));
}
// Tests a connection from an iframe within a tab which doesn't have
// permission. Iframe should work.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
FromIframeWithPermission) {
InitializeTestServer();
scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension();
ui_test_utils::NavigateToURL(browser(), google_com_url());
EXPECT_EQ(NAMESPACE_NOT_DEFINED,
CanConnectAndSendMessagesToMainFrame(extension.get()));
EXPECT_FALSE(AreAnyNonWebApisDefinedForMainFrame());
ASSERT_TRUE(AppendIframe(chromium_org_url()));
EXPECT_EQ(OK, CanConnectAndSendMessagesToIFrame(extension.get()));
EXPECT_FALSE(AreAnyNonWebApisDefinedForIFrame());
}
// Tests connection from an iframe without permission within a tab that does.
// Iframe shouldn't work.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
FromIframeWithoutPermission) {
InitializeTestServer();
scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension();
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
EXPECT_EQ(OK, CanConnectAndSendMessagesToMainFrame(extension.get()));
EXPECT_FALSE(AreAnyNonWebApisDefinedForMainFrame());
ASSERT_TRUE(AppendIframe(google_com_url()));
EXPECT_EQ(NAMESPACE_NOT_DEFINED,
CanConnectAndSendMessagesToIFrame(extension.get()));
EXPECT_FALSE(AreAnyNonWebApisDefinedForIFrame());
}
// Tests externally_connectable between a web page and an extension with a
// TLS channel ID created for the origin.
class ExternallyConnectableMessagingWithTlsChannelIdTest :
public ExternallyConnectableMessagingTest {
public:
ExternallyConnectableMessagingWithTlsChannelIdTest()
: tls_channel_id_created_(false, false) {
}
std::string CreateTlsChannelId() {
scoped_refptr<net::URLRequestContextGetter> request_context_getter(
profile()->GetRequestContext());
std::string channel_id_private_key;
std::string channel_id_cert;
net::ChannelIDService::RequestHandle request_handle;
content::BrowserThread::PostTask(
content::BrowserThread::IO,
FROM_HERE,
base::Bind(
&ExternallyConnectableMessagingWithTlsChannelIdTest::
CreateDomainBoundCertOnIOThread,
base::Unretained(this),
base::Unretained(&channel_id_private_key),
base::Unretained(&channel_id_cert),
base::Unretained(&request_handle),
request_context_getter));
tls_channel_id_created_.Wait();
// Create the expected value.
base::StringPiece spki;
net::asn1::ExtractSPKIFromDERCert(channel_id_cert, &spki);
base::DictionaryValue jwk_value;
net::JwkSerializer::ConvertSpkiFromDerToJwk(spki, &jwk_value);
std::string tls_channel_id_value;
base::JSONWriter::Write(&jwk_value, &tls_channel_id_value);
return tls_channel_id_value;
}
private:
void CreateDomainBoundCertOnIOThread(
std::string* channel_id_private_key,
std::string* channel_id_cert,
net::ChannelIDService::RequestHandle* request_handle,
scoped_refptr<net::URLRequestContextGetter> request_context_getter) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
net::ChannelIDService* channel_id_service =
request_context_getter->GetURLRequestContext()->
channel_id_service();
int status = channel_id_service->GetOrCreateChannelID(
chromium_org_url().host(),
channel_id_private_key,
channel_id_cert,
base::Bind(&ExternallyConnectableMessagingWithTlsChannelIdTest::
GotDomainBoundCert,
base::Unretained(this)),
request_handle);
if (status == net::ERR_IO_PENDING)
return;
GotDomainBoundCert(status);
}
void GotDomainBoundCert(int status) {
ASSERT_TRUE(status == net::OK);
tls_channel_id_created_.Signal();
}
base::WaitableEvent tls_channel_id_created_;
};
// Tests a web connectable extension that receives TLS channel id on a site
// that can connect to it, with a TLS channel ID having been generated.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingWithTlsChannelIdTest,
WebConnectableWithNonEmptyTlsChannelId) {
InitializeTestServer();
std::string expected_tls_channel_id_value = CreateTlsChannelId();
scoped_refptr<const Extension> chromium_connectable =
LoadChromiumConnectableExtensionWithTlsChannelId();
ASSERT_TRUE(chromium_connectable.get());
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
// Since the extension requests the TLS channel ID, it gets it for a site that
// can connect to it, but only if the page also asks to send it.
EXPECT_EQ(std::string(),
GetTlsChannelIdFromPortConnect(chromium_connectable.get(), false));
EXPECT_EQ(std::string(),
GetTlsChannelIdFromSendMessage(chromium_connectable.get(), false));
// If the page does ask to send the TLS channel ID, it's sent and non-empty.
std::string tls_channel_id_from_port_connect =
GetTlsChannelIdFromPortConnect(chromium_connectable.get(), true);
EXPECT_NE(0u, tls_channel_id_from_port_connect.size());
// The same value is received by both connect and sendMessage.
std::string tls_channel_id_from_send_message =
GetTlsChannelIdFromSendMessage(chromium_connectable.get(), true);
EXPECT_EQ(tls_channel_id_from_port_connect, tls_channel_id_from_send_message);
// And since a TLS channel ID exists for the domain, the value received is
// parseable as a JWK. (In particular, it has the same value we created by
// converting the public key to JWK with net::ConvertSpkiFromDerToJwk.)
std::string tls_channel_id(tls_channel_id_from_port_connect);
EXPECT_EQ(expected_tls_channel_id_value, tls_channel_id);
// The TLS channel ID shouldn't change from one connection to the next...
std::string tls_channel_id2 =
GetTlsChannelIdFromPortConnect(chromium_connectable.get(), true);
EXPECT_EQ(tls_channel_id, tls_channel_id2);
tls_channel_id2 =
GetTlsChannelIdFromSendMessage(chromium_connectable.get(), true);
EXPECT_EQ(tls_channel_id, tls_channel_id2);
// nor should it change when navigating away, revisiting the page and
// requesting it again.
ui_test_utils::NavigateToURL(browser(), google_com_url());
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
tls_channel_id2 =
GetTlsChannelIdFromPortConnect(chromium_connectable.get(), true);
EXPECT_EQ(tls_channel_id, tls_channel_id2);
tls_channel_id2 =
GetTlsChannelIdFromSendMessage(chromium_connectable.get(), true);
EXPECT_EQ(tls_channel_id, tls_channel_id2);
}
// Tests a web connectable extension that receives TLS channel id, but
// immediately closes its background page upon receipt of a message.
// Same flakiness seen in http://crbug.com/297866
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingWithTlsChannelIdTest,
DISABLED_WebConnectableWithNonEmptyTlsChannelIdAndClosedBackgroundPage) {
InitializeTestServer();
std::string expected_tls_channel_id_value = CreateTlsChannelId();
scoped_refptr<const Extension> chromium_connectable =
LoadChromiumConnectableExtensionWithTlsChannelId();
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
// If the page does ask for it, it isn't empty, even if the background page
// closes upon receipt of the connect.
std::string tls_channel_id = GetTlsChannelIdFromPortConnect(
chromium_connectable.get(), true, close_background_message());
EXPECT_EQ(expected_tls_channel_id_value, tls_channel_id);
// A subsequent connect will still succeed, even if the background page was
// previously closed.
tls_channel_id =
GetTlsChannelIdFromPortConnect(chromium_connectable.get(), true);
// And the expected value is still retrieved.
EXPECT_EQ(expected_tls_channel_id_value, tls_channel_id);
}
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MessagingUserGesture) {
const char kManifest[] = "{"
" \"name\": \"user_gesture\","
" \"version\": \"1.0\","
" \"background\": {"
" \"scripts\": [\"background.js\"]"
" },"
" \"manifest_version\": 2"
"}";
TestExtensionDir receiver_dir;
receiver_dir.WriteManifest(kManifest);
receiver_dir.WriteFile(FILE_PATH_LITERAL("background.js"),
"chrome.runtime.onMessageExternal.addListener(\n"
" function(msg, sender, reply) {\n"
" reply({result:chrome.test.isProcessingUserGesture()});\n"
" });");
const Extension* receiver = LoadExtension(receiver_dir.unpacked_path());
ASSERT_TRUE(receiver);
TestExtensionDir sender_dir;
sender_dir.WriteManifest(kManifest);
sender_dir.WriteFile(FILE_PATH_LITERAL("background.js"), "");
const Extension* sender = LoadExtension(sender_dir.unpacked_path());
ASSERT_TRUE(sender);
EXPECT_EQ("false",
ExecuteScriptInBackgroundPage(sender->id(),
base::StringPrintf(
"chrome.test.runWithoutUserGesture(function() {\n"
" chrome.runtime.sendMessage('%s', {}, function(response) {\n"
" window.domAutomationController.send('' + response.result);\n"
" });\n"
"});", receiver->id().c_str())));
EXPECT_EQ("true",
ExecuteScriptInBackgroundPage(sender->id(),
base::StringPrintf(
"chrome.test.runWithUserGesture(function() {\n"
" chrome.runtime.sendMessage('%s', {}, function(response) {\n"
" window.domAutomationController.send('' + response.result);\n"
" });\n"
"});", receiver->id().c_str())));
}
// Tests that a hosted app on a connectable site doesn't interfere with the
// connectability of that site.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, HostedAppOnWebsite) {
InitializeTestServer();
scoped_refptr<const Extension> app = LoadChromiumHostedApp();
// The presence of the hosted app shouldn't give the ability to send messages.
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
EXPECT_EQ(NAMESPACE_NOT_DEFINED,
CanConnectAndSendMessagesToMainFrame(app.get()));
EXPECT_FALSE(AreAnyNonWebApisDefinedForMainFrame());
// Once a connectable extension is installed, it should.
scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension();
EXPECT_EQ(OK, CanConnectAndSendMessagesToMainFrame(extension.get()));
EXPECT_FALSE(AreAnyNonWebApisDefinedForMainFrame());
}
// Tests that an invalid extension ID specified in a hosted app does not crash
// the hosted app's renderer.
//
// This is a regression test for http://crbug.com/326250#c12.
IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
InvalidExtensionIDFromHostedApp) {
InitializeTestServer();
// The presence of the chromium hosted app triggers this bug. The chromium
// connectable extension needs to be installed to set up the runtime bindings.
LoadChromiumHostedApp();
LoadChromiumConnectableExtension();
scoped_refptr<const Extension> invalid =
ExtensionBuilder()
// A bit scary that this works...
.SetID("invalid")
.SetManifest(DictionaryBuilder()
.Set("name", "Fake extension")
.Set("version", "1")
.Set("manifest_version", 2))
.Build();
ui_test_utils::NavigateToURL(browser(), chromium_org_url());
EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
CanConnectAndSendMessagesToMainFrame(invalid.get()));
}
#endif // !defined(OS_WIN) - http://crbug.com/350517.
} // namespace
}; // namespace extensions
|