Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Side by Side Diff: chrome/browser/extensions/extension_messages_apitest.cc

Issue 2766263003: Extensions: Only load incognito-enabled extensions in an incognito renderer. (Closed)
Patch Set: -- Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/base64.h" 11 #include "base/base64.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
14 #include "base/json/json_writer.h" 14 #include "base/json/json_writer.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/path_service.h" 16 #include "base/path_service.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_piece.h" 18 #include "base/strings/string_piece.h"
19 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h" 20 #include "base/strings/utf_string_conversions.h"
21 #include "base/synchronization/waitable_event.h" 21 #include "base/synchronization/waitable_event.h"
22 #include "base/values.h" 22 #include "base/values.h"
23 #include "build/build_config.h" 23 #include "build/build_config.h"
24 #include "chrome/browser/chrome_notification_types.h" 24 #include "chrome/browser/chrome_notification_types.h"
25 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h" 25 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h"
26 #include "chrome/browser/extensions/chrome_extension_test_notification_observer. h"
26 #include "chrome/browser/extensions/extension_apitest.h" 27 #include "chrome/browser/extensions/extension_apitest.h"
28 #include "chrome/browser/extensions/extension_util.h"
27 #include "chrome/browser/extensions/test_extension_dir.h" 29 #include "chrome/browser/extensions/test_extension_dir.h"
28 #include "chrome/browser/infobars/infobar_service.h" 30 #include "chrome/browser/infobars/infobar_service.h"
29 #include "chrome/browser/profiles/profile.h" 31 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/ui/browser.h" 32 #include "chrome/browser/ui/browser.h"
31 #include "chrome/browser/ui/browser_commands.h" 33 #include "chrome/browser/ui/browser_commands.h"
32 #include "chrome/browser/ui/tabs/tab_strip_model.h" 34 #include "chrome/browser/ui/tabs/tab_strip_model.h"
33 #include "chrome/common/chrome_paths.h" 35 #include "chrome/common/chrome_paths.h"
34 #include "chrome/common/chrome_switches.h" 36 #include "chrome/common/chrome_switches.h"
35 #include "chrome/test/base/ui_test_utils.h" 37 #include "chrome/test/base/ui_test_utils.h"
36 #include "content/public/browser/notification_registrar.h" 38 #include "content/public/browser/notification_registrar.h"
(...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 778
777 scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension(); 779 scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension();
778 780
779 Browser* incognito_browser = OpenURLOffTheRecord( 781 Browser* incognito_browser = OpenURLOffTheRecord(
780 profile()->GetOffTheRecordProfile(), chromium_org_url()); 782 profile()->GetOffTheRecordProfile(), chromium_org_url());
781 content::RenderFrameHost* incognito_frame = 783 content::RenderFrameHost* incognito_frame =
782 incognito_browser->tab_strip_model() 784 incognito_browser->tab_strip_model()
783 ->GetActiveWebContents() 785 ->GetActiveWebContents()
784 ->GetMainFrame(); 786 ->GetMainFrame();
785 787
786 { 788 IncognitoConnectability::ScopedAlertTracker alert_tracker(
787 IncognitoConnectability::ScopedAlertTracker alert_tracker( 789 IncognitoConnectability::ScopedAlertTracker::ALWAYS_DENY);
788 IncognitoConnectability::ScopedAlertTracker::ALWAYS_DENY);
789 790
790 // The alert doesn't show for extensions. 791 // Extensions disabled in incognito mode are not loaded in an incognito
791 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, 792 // renderer.
792 CanConnectAndSendMessagesToFrame( 793 EXPECT_EQ(NAMESPACE_NOT_DEFINED, CanConnectAndSendMessagesToFrame(
793 incognito_frame, extension.get(), NULL)); 794 incognito_frame, extension.get(), NULL));
794 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount());
795 }
796 795
797 // Allowing the extension in incognito mode will bypass the deny. 796 // Allowing the extension in incognito mode loads the extension in the
798 ExtensionPrefs::Get(profile())->SetIsIncognitoEnabled(extension->id(), true); 797 // incognito renderer, allowing it to receive connections.
799 EXPECT_EQ( 798 ChromeExtensionTestNotificationObserver observer(incognito_browser);
800 OK, 799 util::SetIsIncognitoEnabled(extension->id(),
801 CanConnectAndSendMessagesToFrame(incognito_frame, extension.get(), NULL)); 800 profile()->GetOffTheRecordProfile(), true);
801 observer.WaitForExtensionLoad();
802 EXPECT_EQ(OK, CanConnectAndSendMessagesToFrame(incognito_frame,
803 extension.get(), NULL));
Devlin 2017/03/23 22:08:36 Subtle: when we reload the extension, the Extensio
Devlin 2017/03/23 22:08:37 prefer nullptr in new code
karandeepb 2017/04/04 03:44:15 Done.
karandeepb 2017/04/04 03:44:15 Done.
804
805 // No alert is shown for extensions which support being enabled in incognito
806 // mode.
807 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount());
802 } 808 }
803 809
804 // Tests connection from incognito tabs when the extension doesn't have an event 810 // Tests connection from incognito tabs when the extension doesn't have an event
805 // handler for the connection event. 811 // handler for the connection event.
806 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, 812 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
807 FromIncognitoNoEventHandlerInApp) { 813 FromIncognitoNoEventHandlerInApp) {
808 InitializeTestServer(); 814 InitializeTestServer();
809 815
810 scoped_refptr<const Extension> app = LoadChromiumConnectableApp(false); 816 scoped_refptr<const Extension> app = LoadChromiumConnectableApp(false);
811 ASSERT_TRUE(app->is_platform_app()); 817 ASSERT_TRUE(app->is_platform_app());
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
951 957
952 scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension(); 958 scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension();
953 959
954 Browser* incognito_browser = OpenURLOffTheRecord( 960 Browser* incognito_browser = OpenURLOffTheRecord(
955 profile()->GetOffTheRecordProfile(), chromium_org_url()); 961 profile()->GetOffTheRecordProfile(), chromium_org_url());
956 content::RenderFrameHost* incognito_frame = 962 content::RenderFrameHost* incognito_frame =
957 incognito_browser->tab_strip_model() 963 incognito_browser->tab_strip_model()
958 ->GetActiveWebContents() 964 ->GetActiveWebContents()
959 ->GetMainFrame(); 965 ->GetMainFrame();
960 966
961 { 967 IncognitoConnectability::ScopedAlertTracker alert_tracker(
962 IncognitoConnectability::ScopedAlertTracker alert_tracker( 968 IncognitoConnectability::ScopedAlertTracker::ALWAYS_ALLOW);
963 IncognitoConnectability::ScopedAlertTracker::ALWAYS_ALLOW);
964 969
965 // No alert is shown. 970 // Extensions disabled in incognito mode are not loaded in an incognito
966 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, 971 // renderer.
967 CanConnectAndSendMessagesToFrame( 972 EXPECT_EQ(NAMESPACE_NOT_DEFINED, CanConnectAndSendMessagesToFrame(
968 incognito_frame, extension.get(), NULL)); 973 incognito_frame, extension.get(), NULL));
969 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount());
970 }
971 974
972 // Allowing the extension in incognito mode is what allows connections. 975 // Allowing the extension in incognito mode loads the extension in the
973 ExtensionPrefs::Get(profile())->SetIsIncognitoEnabled(extension->id(), true); 976 // incognito renderer, allowing it to receive connections.
974 EXPECT_EQ( 977 ChromeExtensionTestNotificationObserver observer(incognito_browser);
975 OK, 978 util::SetIsIncognitoEnabled(extension->id(),
976 CanConnectAndSendMessagesToFrame(incognito_frame, extension.get(), NULL)); 979 profile()->GetOffTheRecordProfile(), true);
980 observer.WaitForExtensionLoad();
981 EXPECT_EQ(OK, CanConnectAndSendMessagesToFrame(incognito_frame,
982 extension.get(), NULL));
Devlin 2017/03/23 22:08:37 (Same comment here)
karandeepb 2017/04/04 03:44:15 Done.
983
984 // No alert is shown for extensions which support being enabled in incognito
985 // mode.
986 EXPECT_EQ(0, alert_tracker.GetAndResetAlertCount());
977 } 987 }
978 988
979 // Tests a connection from an iframe within a tab which doesn't have 989 // Tests a connection from an iframe within a tab which doesn't have
980 // permission. Iframe should work. 990 // permission. Iframe should work.
981 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, 991 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
982 FromIframeWithPermission) { 992 FromIframeWithPermission) {
983 InitializeTestServer(); 993 InitializeTestServer();
984 994
985 scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension(); 995 scoped_refptr<const Extension> extension = LoadChromiumConnectableExtension();
986 996
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
1324 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( 1334 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
1325 background_contents, 1335 background_contents,
1326 "window.domAutomationController.send(window.messageCount);", 1336 "window.domAutomationController.send(window.messageCount);",
1327 &message_count)); 1337 &message_count));
1328 EXPECT_EQ(1, message_count); 1338 EXPECT_EQ(1, message_count);
1329 } 1339 }
1330 1340
1331 } // namespace 1341 } // namespace
1332 1342
1333 }; // namespace extensions 1343 }; // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698