| OLD | NEW | 
|    1 // Copyright 2014 The Chromium Authors. All rights reserved. |    1 // Copyright 2014 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 "chrome/browser/supervised_user/supervised_user_resource_throttle.h" |  | 
|    6  |  | 
|    7 #include <memory> |    5 #include <memory> | 
|    8  |    6  | 
|    9 #include "base/command_line.h" |    7 #include "base/command_line.h" | 
|   10 #include "base/files/file_path.h" |    8 #include "base/files/file_path.h" | 
|   11 #include "base/memory/ptr_util.h" |    9 #include "base/memory/ptr_util.h" | 
|   12 #include "base/path_service.h" |   10 #include "base/path_service.h" | 
|   13 #include "base/values.h" |   11 #include "base/values.h" | 
|   14 #include "chrome/browser/profiles/profile.h" |   12 #include "chrome/browser/profiles/profile.h" | 
|   15 #include "chrome/browser/supervised_user/supervised_user_constants.h" |   13 #include "chrome/browser/supervised_user/supervised_user_constants.h" | 
|   16 #include "chrome/browser/supervised_user/supervised_user_service.h" |   14 #include "chrome/browser/supervised_user/supervised_user_service.h" | 
| (...skipping 19 matching lines...) Expand all  Loading... | 
|   36  |   34  | 
|   37 namespace { |   35 namespace { | 
|   38  |   36  | 
|   39 static const char* kExampleHost = "www.example.com"; |   37 static const char* kExampleHost = "www.example.com"; | 
|   40 static const char* kExampleHost2 = "www.example2.com"; |   38 static const char* kExampleHost2 = "www.example2.com"; | 
|   41  |   39  | 
|   42 static const char* kIframeHost2 = "www.iframe2.com"; |   40 static const char* kIframeHost2 = "www.iframe2.com"; | 
|   43  |   41  | 
|   44 }  // namespace |   42 }  // namespace | 
|   45  |   43  | 
|   46 class SupervisedUserResourceThrottleTest : public InProcessBrowserTest { |   44 class SupervisedUserNavigationThrottleTest : public InProcessBrowserTest { | 
|   47  protected: |   45  protected: | 
|   48   SupervisedUserResourceThrottleTest() {} |   46   SupervisedUserNavigationThrottleTest() {} | 
|   49   ~SupervisedUserResourceThrottleTest() override {} |   47   ~SupervisedUserNavigationThrottleTest() override {} | 
|   50  |   48  | 
|   51   void BlockHost(const std::string& host) { |   49   void BlockHost(const std::string& host) { | 
|   52     Profile* profile = browser()->profile(); |   50     Profile* profile = browser()->profile(); | 
|   53     SupervisedUserSettingsService* settings_service = |   51     SupervisedUserSettingsService* settings_service = | 
|   54         SupervisedUserSettingsServiceFactory::GetForProfile(profile); |   52         SupervisedUserSettingsServiceFactory::GetForProfile(profile); | 
|   55     auto dict = base::MakeUnique<base::DictionaryValue>(); |   53     auto dict = base::MakeUnique<base::DictionaryValue>(); | 
|   56     dict->SetBooleanWithoutPathExpansion(host, false); |   54     dict->SetBooleanWithoutPathExpansion(host, false); | 
|   57     settings_service->SetLocalSetting( |   55     settings_service->SetLocalSetting( | 
|   58         supervised_users::kContentPackManualBehaviorHosts, std::move(dict)); |   56         supervised_users::kContentPackManualBehaviorHosts, std::move(dict)); | 
|   59   } |   57   } | 
|   60  |   58  | 
|   61  private: |   59  private: | 
|   62   void SetUpOnMainThread() override; |   60   void SetUpOnMainThread() override; | 
|   63   void SetUpCommandLine(base::CommandLine* command_line) override; |   61   void SetUpCommandLine(base::CommandLine* command_line) override; | 
|   64 }; |   62 }; | 
|   65  |   63  | 
|   66 void SupervisedUserResourceThrottleTest::SetUpOnMainThread() { |   64 void SupervisedUserNavigationThrottleTest::SetUpOnMainThread() { | 
|   67   // Resolve everything to localhost. |   65   // Resolve everything to localhost. | 
|   68   host_resolver()->AddIPLiteralRule("*", "127.0.0.1", "localhost"); |   66   host_resolver()->AddIPLiteralRule("*", "127.0.0.1", "localhost"); | 
|   69  |   67  | 
|   70   ASSERT_TRUE(embedded_test_server()->Start()); |   68   ASSERT_TRUE(embedded_test_server()->Start()); | 
|   71 } |   69 } | 
|   72  |   70  | 
|   73 void SupervisedUserResourceThrottleTest::SetUpCommandLine( |   71 void SupervisedUserNavigationThrottleTest::SetUpCommandLine( | 
|   74     base::CommandLine* command_line) { |   72     base::CommandLine* command_line) { | 
|   75   command_line->AppendSwitchASCII(switches::kSupervisedUserId, "asdf"); |   73   command_line->AppendSwitchASCII(switches::kSupervisedUserId, "asdf"); | 
|   76 } |   74 } | 
|   77  |   75  | 
|   78 // Tests that showing the blocking interstitial for a WebContents without a |   76 // Tests that showing the blocking interstitial for a WebContents without a | 
|   79 // SupervisedUserNavigationObserver doesn't crash. |   77 // SupervisedUserNavigationObserver doesn't crash. | 
|   80 IN_PROC_BROWSER_TEST_F(SupervisedUserResourceThrottleTest, |   78 IN_PROC_BROWSER_TEST_F(SupervisedUserNavigationThrottleTest, | 
|   81                        NoNavigationObserverBlock) { |   79                        NoNavigationObserverBlock) { | 
|   82   Profile* profile = browser()->profile(); |   80   Profile* profile = browser()->profile(); | 
|   83   SupervisedUserSettingsService* supervised_user_settings_service = |   81   SupervisedUserSettingsService* supervised_user_settings_service = | 
|   84       SupervisedUserSettingsServiceFactory::GetForProfile(profile); |   82       SupervisedUserSettingsServiceFactory::GetForProfile(profile); | 
|   85   supervised_user_settings_service->SetLocalSetting( |   83   supervised_user_settings_service->SetLocalSetting( | 
|   86       supervised_users::kContentPackDefaultFilteringBehavior, |   84       supervised_users::kContentPackDefaultFilteringBehavior, | 
|   87       std::unique_ptr<base::Value>( |   85       std::unique_ptr<base::Value>( | 
|   88           new base::Value(SupervisedUserURLFilter::BLOCK))); |   86           new base::Value(SupervisedUserURLFilter::BLOCK))); | 
|   89  |   87  | 
|   90   std::unique_ptr<WebContents> web_contents( |   88   std::unique_ptr<WebContents> web_contents( | 
|   91       WebContents::Create(WebContents::CreateParams(profile))); |   89       WebContents::Create(WebContents::CreateParams(profile))); | 
|   92   NavigationController& controller = web_contents->GetController(); |   90   NavigationController& controller = web_contents->GetController(); | 
|   93   content::TestNavigationObserver observer(web_contents.get()); |   91   content::TestNavigationObserver observer(web_contents.get()); | 
|   94   controller.LoadURL(GURL("http://www.example.com"), content::Referrer(), |   92   controller.LoadURL(GURL("http://www.example.com"), content::Referrer(), | 
|   95                      ui::PAGE_TRANSITION_TYPED, std::string()); |   93                      ui::PAGE_TRANSITION_TYPED, std::string()); | 
|   96   observer.Wait(); |   94   observer.Wait(); | 
|   97   content::NavigationEntry* entry = controller.GetActiveEntry(); |   95   content::NavigationEntry* entry = controller.GetActiveEntry(); | 
|   98   ASSERT_TRUE(entry); |   96   ASSERT_TRUE(entry); | 
|   99   EXPECT_EQ(content::PAGE_TYPE_INTERSTITIAL, entry->GetPageType()); |   97   EXPECT_EQ(content::PAGE_TYPE_INTERSTITIAL, entry->GetPageType()); | 
|  100 } |   98 } | 
|  101  |   99  | 
|  102 IN_PROC_BROWSER_TEST_F(SupervisedUserResourceThrottleTest, |  100 IN_PROC_BROWSER_TEST_F(SupervisedUserNavigationThrottleTest, | 
|  103                        BlockMainFrameWithInterstitial) { |  101                        BlockMainFrameWithInterstitial) { | 
|  104   BlockHost(kExampleHost2); |  102   BlockHost(kExampleHost2); | 
|  105  |  103  | 
|  106   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); |  104   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); | 
|  107  |  105  | 
|  108   GURL allowed_url = embedded_test_server()->GetURL( |  106   GURL allowed_url = embedded_test_server()->GetURL( | 
|  109       kExampleHost, "/supervised_user/simple.html"); |  107       kExampleHost, "/supervised_user/simple.html"); | 
|  110   ui_test_utils::NavigateToURL(browser(), allowed_url); |  108   ui_test_utils::NavigateToURL(browser(), allowed_url); | 
|  111   EXPECT_FALSE(tab->ShowingInterstitialPage()); |  109   EXPECT_FALSE(tab->ShowingInterstitialPage()); | 
|  112  |  110  | 
|  113   GURL blocked_url = embedded_test_server()->GetURL( |  111   GURL blocked_url = embedded_test_server()->GetURL( | 
|  114       kExampleHost2, "/supervised_user/simple.html"); |  112       kExampleHost2, "/supervised_user/simple.html"); | 
|  115   ui_test_utils::NavigateToURL(browser(), blocked_url); |  113   ui_test_utils::NavigateToURL(browser(), blocked_url); | 
|  116   EXPECT_TRUE(tab->ShowingInterstitialPage()); |  114   EXPECT_TRUE(tab->ShowingInterstitialPage()); | 
|  117 } |  115 } | 
|  118  |  116  | 
|  119 IN_PROC_BROWSER_TEST_F(SupervisedUserResourceThrottleTest, DontBlockSubFrame) { |  117 IN_PROC_BROWSER_TEST_F(SupervisedUserNavigationThrottleTest, | 
 |  118                        DontBlockSubFrame) { | 
|  120   BlockHost(kExampleHost2); |  119   BlockHost(kExampleHost2); | 
|  121   BlockHost(kIframeHost2); |  120   BlockHost(kIframeHost2); | 
|  122  |  121  | 
|  123   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); |  122   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); | 
|  124  |  123  | 
|  125   GURL allowed_url_with_iframes = embedded_test_server()->GetURL( |  124   GURL allowed_url_with_iframes = embedded_test_server()->GetURL( | 
|  126       kExampleHost, "/supervised_user/with_iframes.html"); |  125       kExampleHost, "/supervised_user/with_iframes.html"); | 
|  127   ui_test_utils::NavigateToURL(browser(), allowed_url_with_iframes); |  126   ui_test_utils::NavigateToURL(browser(), allowed_url_with_iframes); | 
|  128   EXPECT_FALSE(tab->ShowingInterstitialPage()); |  127   EXPECT_FALSE(tab->ShowingInterstitialPage()); | 
|  129  |  128  | 
|  130   // Both iframes (from allowed host iframe1.com as well as from blocked host |  129   // Both iframes (from allowed host iframe1.com as well as from blocked host | 
|  131   // iframe2.com) should be loaded normally, since we don't filter iframes |  130   // iframe2.com) should be loaded normally, since we don't filter iframes | 
|  132   // (yet) - see crbug.com/651115. |  131   // (yet) - see crbug.com/651115. | 
|  133   bool loaded1 = false; |  132   bool loaded1 = false; | 
|  134   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(tab, "loaded1()", &loaded1)); |  133   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(tab, "loaded1()", &loaded1)); | 
|  135   EXPECT_TRUE(loaded1); |  134   EXPECT_TRUE(loaded1); | 
|  136   bool loaded2 = false; |  135   bool loaded2 = false; | 
|  137   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(tab, "loaded2()", &loaded2)); |  136   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(tab, "loaded2()", &loaded2)); | 
|  138   EXPECT_TRUE(loaded2); |  137   EXPECT_TRUE(loaded2); | 
|  139 } |  138 } | 
| OLD | NEW |