Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_browsertest.cc
blob1fb178503a44c19585fd81ac8abe29fb76f4ccc0
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
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/path_service.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/content_settings/cookie_settings.h"
11 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
12 #include "chrome/browser/net/url_request_mock_util.h"
13 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_commands.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/render_messages.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/test_switches.h"
22 #include "chrome/test/base/ui_test_utils.h"
23 #include "components/content_settings/core/browser/host_content_settings_map.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/notification_observer.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/plugin_service.h"
28 #include "content/public/browser/render_frame_host.h"
29 #include "content/public/browser/render_process_host.h"
30 #include "content/public/browser/render_view_host.h"
31 #include "content/public/browser/web_contents.h"
32 #include "content/public/common/content_switches.h"
33 #include "content/public/test/browser_test_utils.h"
34 #include "content/public/test/test_utils.h"
35 #include "net/test/spawned_test_server/spawned_test_server.h"
36 #include "net/test/url_request/url_request_mock_http_job.h"
38 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
40 #if defined(OS_MACOSX)
41 #include "base/mac/scoped_nsautorelease_pool.h"
42 #endif
44 using content::BrowserThread;
45 using net::URLRequestMockHTTPJob;
47 class ContentSettingsTest : public InProcessBrowserTest {
48 public:
49 ContentSettingsTest()
50 : https_server_(net::SpawnedTestServer::TYPE_HTTPS,
51 net::SpawnedTestServer::SSLOptions(
52 net::SpawnedTestServer::SSLOptions::CERT_OK),
53 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))) {
56 void SetUpOnMainThread() override {
57 BrowserThread::PostTask(
58 BrowserThread::IO, FROM_HERE,
59 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
62 // Check the cookie for the given URL in an incognito window.
63 void CookieCheckIncognitoWindow(const GURL& url, bool cookies_enabled) {
64 ASSERT_TRUE(content::GetCookies(browser()->profile(), url).empty());
66 Browser* incognito = CreateIncognitoBrowser();
67 ASSERT_TRUE(content::GetCookies(incognito->profile(), url).empty());
68 ui_test_utils::NavigateToURL(incognito, url);
69 ASSERT_EQ(cookies_enabled,
70 !content::GetCookies(incognito->profile(), url).empty());
72 // Ensure incognito cookies don't leak to regular profile.
73 ASSERT_TRUE(content::GetCookies(browser()->profile(), url).empty());
75 // Ensure cookies get wiped after last incognito window closes.
76 content::WindowedNotificationObserver signal(
77 chrome::NOTIFICATION_BROWSER_CLOSED,
78 content::Source<Browser>(incognito));
80 chrome::CloseWindow(incognito);
82 #if defined(OS_MACOSX)
83 // BrowserWindowController depends on the auto release pool being recycled
84 // in the message loop to delete itself, which frees the Browser object
85 // which fires this event.
86 AutoreleasePool()->Recycle();
87 #endif
89 signal.Wait();
91 incognito = CreateIncognitoBrowser();
92 ASSERT_TRUE(content::GetCookies(incognito->profile(), url).empty());
93 chrome::CloseWindow(incognito);
96 void PreBasic(const GURL& url) {
97 ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
99 CookieCheckIncognitoWindow(url, true);
101 ui_test_utils::NavigateToURL(browser(), url);
102 ASSERT_FALSE(GetCookies(browser()->profile(), url).empty());
105 void Basic(const GURL& url) {
106 ASSERT_FALSE(GetCookies(browser()->profile(), url).empty());
109 net::SpawnedTestServer https_server_;
112 // Sanity check on cookies before we do other tests. While these can be written
113 // in content_browsertests, we want to verify Chrome's cookie storage and how it
114 // handles incognito windows.
115 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, PRE_BasicCookies) {
116 ASSERT_TRUE(test_server()->Start());
117 GURL http_url = test_server()->GetURL("files/setcookie.html");
118 PreBasic(http_url);
121 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, BasicCookies) {
122 ASSERT_TRUE(test_server()->Start());
123 GURL http_url = test_server()->GetURL("files/setcookie.html");
124 Basic(http_url);
127 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, PRE_BasicCookiesHttps) {
128 ASSERT_TRUE(https_server_.Start());
129 GURL https_url = https_server_.GetURL("files/setcookie.html");
130 PreBasic(https_url);
133 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, BasicCookiesHttps) {
134 ASSERT_TRUE(https_server_.Start());
135 GURL https_url = https_server_.GetURL("files/setcookie.html");
136 Basic(https_url);
139 // Verify that cookies are being blocked.
140 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, PRE_BlockCookies) {
141 ASSERT_TRUE(test_server()->Start());
142 CookieSettings::Factory::GetForProfile(browser()->profile())->
143 SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
144 GURL url = test_server()->GetURL("files/setcookie.html");
145 ui_test_utils::NavigateToURL(browser(), url);
146 ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
147 CookieCheckIncognitoWindow(url, false);
150 // Ensure that the setting persists.
151 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, BlockCookies) {
152 ASSERT_EQ(
153 CONTENT_SETTING_BLOCK,
154 CookieSettings::Factory::GetForProfile(browser()->profile())->
155 GetDefaultCookieSetting(NULL));
158 // Verify that cookies can be allowed and set using exceptions for particular
159 // website(s) when all others are blocked.
160 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, AllowCookiesUsingExceptions) {
161 ASSERT_TRUE(test_server()->Start());
162 GURL url = test_server()->GetURL("files/setcookie.html");
163 CookieSettings* settings =
164 CookieSettings::Factory::GetForProfile(browser()->profile()).get();
165 settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
167 ui_test_utils::NavigateToURL(browser(), url);
168 ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
170 settings->SetCookieSetting(
171 ContentSettingsPattern::FromURL(url),
172 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW);
174 ui_test_utils::NavigateToURL(browser(), url);
175 ASSERT_FALSE(GetCookies(browser()->profile(), url).empty());
178 // Verify that cookies can be blocked for a specific website using exceptions.
179 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, BlockCookiesUsingExceptions) {
180 ASSERT_TRUE(test_server()->Start());
181 GURL url = test_server()->GetURL("files/setcookie.html");
182 CookieSettings* settings =
183 CookieSettings::Factory::GetForProfile(browser()->profile()).get();
184 settings->SetCookieSetting(ContentSettingsPattern::FromURL(url),
185 ContentSettingsPattern::Wildcard(),
186 CONTENT_SETTING_BLOCK);
188 ui_test_utils::NavigateToURL(browser(), url);
189 ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
191 ASSERT_TRUE(https_server_.Start());
192 GURL unblocked_url = https_server_.GetURL("files/cookie1.html");
194 ui_test_utils::NavigateToURL(browser(), unblocked_url);
195 ASSERT_FALSE(GetCookies(browser()->profile(), unblocked_url).empty());
198 // This fails on ChromeOS because kRestoreOnStartup is ignored and the startup
199 // preference is always "continue where I left off.
200 #if !defined(OS_CHROMEOS)
202 // Verify that cookies can be allowed and set using exceptions for particular
203 // website(s) only for a session when all others are blocked.
204 IN_PROC_BROWSER_TEST_F(ContentSettingsTest,
205 PRE_AllowCookiesForASessionUsingExceptions) {
206 // NOTE: don't use test_server here, since we need the port to be the same
207 // across the restart.
208 GURL url = URLRequestMockHTTPJob::GetMockUrl(
209 base::FilePath(FILE_PATH_LITERAL("setcookie.html")));
210 CookieSettings* settings =
211 CookieSettings::Factory::GetForProfile(browser()->profile()).get();
212 settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
214 ui_test_utils::NavigateToURL(browser(), url);
215 ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
217 settings->SetCookieSetting(
218 ContentSettingsPattern::FromURL(url),
219 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_SESSION_ONLY);
220 ui_test_utils::NavigateToURL(browser(), url);
221 ASSERT_FALSE(GetCookies(browser()->profile(), url).empty());
224 IN_PROC_BROWSER_TEST_F(ContentSettingsTest,
225 AllowCookiesForASessionUsingExceptions) {
226 GURL url = URLRequestMockHTTPJob::GetMockUrl(
227 base::FilePath(FILE_PATH_LITERAL("setcookie.html")));
228 ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
231 #endif // !CHROME_OS
233 // Regression test for http://crbug.com/63649.
234 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, RedirectLoopCookies) {
235 ASSERT_TRUE(test_server()->Start());
237 GURL test_url = test_server()->GetURL("files/redirect-loop.html");
239 CookieSettings::Factory::GetForProfile(browser()->profile())->
240 SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
242 ui_test_utils::NavigateToURL(browser(), test_url);
244 content::WebContents* web_contents =
245 browser()->tab_strip_model()->GetActiveWebContents();
246 ASSERT_EQ(base::UTF8ToUTF16(test_url.spec() + " failed to load"),
247 web_contents->GetTitle());
249 EXPECT_TRUE(TabSpecificContentSettings::FromWebContents(web_contents)->
250 IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
253 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, ContentSettingsBlockDataURLs) {
254 GURL url("data:text/html,<title>Data URL</title><script>alert(1)</script>");
256 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
257 CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
259 ui_test_utils::NavigateToURL(browser(), url);
261 content::WebContents* web_contents =
262 browser()->tab_strip_model()->GetActiveWebContents();
263 ASSERT_EQ(base::UTF8ToUTF16("Data URL"), web_contents->GetTitle());
265 EXPECT_TRUE(TabSpecificContentSettings::FromWebContents(web_contents)->
266 IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
269 // Tests that if redirect across origins occurs, the new process still gets the
270 // content settings before the resource headers.
271 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, RedirectCrossOrigin) {
272 ASSERT_TRUE(test_server()->Start());
274 net::HostPortPair host_port = test_server()->host_port_pair();
275 DCHECK_EQ(host_port.host(), std::string("127.0.0.1"));
277 std::string redirect(base::StringPrintf(
278 "http://localhost:%d/files/redirect-cross-origin.html",
279 host_port.port()));
280 GURL test_url = test_server()->GetURL("server-redirect?" + redirect);
282 CookieSettings::Factory::GetForProfile(browser()->profile())->
283 SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
285 ui_test_utils::NavigateToURL(browser(), test_url);
287 content::WebContents* web_contents =
288 browser()->tab_strip_model()->GetActiveWebContents();
290 EXPECT_TRUE(TabSpecificContentSettings::FromWebContents(web_contents)->
291 IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
294 // On Aura NPAPI only works on Windows.
295 #if !defined(USE_AURA) || defined(OS_WIN)
297 class LoadPluginTest : public ContentSettingsTest {
298 protected:
299 void PerformTest(bool expect_loaded) {
300 GURL url = ui_test_utils::GetTestUrl(
301 base::FilePath(),
302 base::FilePath().AppendASCII("load_npapi_plugin.html"));
303 ui_test_utils::NavigateToURL(browser(), url);
305 const char* expected_result = expect_loaded ? "Loaded" : "Not Loaded";
306 const char* unexpected_result = expect_loaded ? "Not Loaded" : "Loaded";
308 base::string16 expected_title(base::ASCIIToUTF16(expected_result));
309 base::string16 unexpected_title(base::ASCIIToUTF16(unexpected_result));
311 content::TitleWatcher title_watcher(
312 browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
313 title_watcher.AlsoWaitForTitle(unexpected_title);
315 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
318 void SetUpCommandLineInternal(base::CommandLine* command_line,
319 bool expect_loaded) {
320 #if defined(OS_MACOSX)
321 base::FilePath plugin_dir;
322 PathService::Get(base::DIR_MODULE, &plugin_dir);
323 plugin_dir = plugin_dir.AppendASCII("plugins");
324 // The plugins directory isn't read by default on the Mac, so it needs to be
325 // explicitly registered.
326 command_line->AppendSwitchPath(switches::kExtraPluginDir, plugin_dir);
327 #endif
328 command_line->AppendSwitch(switches::kAlwaysAuthorizePlugins);
329 if (expect_loaded)
330 command_line->AppendSwitch(switches::kEnableNpapi);
334 class DisabledPluginTest : public LoadPluginTest {
335 public:
336 DisabledPluginTest() {}
338 void SetUpCommandLine(base::CommandLine* command_line) override {
339 SetUpCommandLineInternal(command_line, false);
343 IN_PROC_BROWSER_TEST_F(DisabledPluginTest, Load) {
344 PerformTest(false);
347 class EnabledPluginTest : public LoadPluginTest {
348 public:
349 EnabledPluginTest() {}
351 void SetUpCommandLine(base::CommandLine* command_line) override {
352 SetUpCommandLineInternal(command_line, true);
356 IN_PROC_BROWSER_TEST_F(EnabledPluginTest, Load) {
357 PerformTest(true);
360 class ClickToPlayPluginTest : public ContentSettingsTest {
361 public:
362 ClickToPlayPluginTest() {}
364 void SetUpCommandLine(base::CommandLine* command_line) override {
365 #if defined(OS_MACOSX)
366 base::FilePath plugin_dir;
367 PathService::Get(base::DIR_MODULE, &plugin_dir);
368 plugin_dir = plugin_dir.AppendASCII("plugins");
369 // The plugins directory isn't read by default on the Mac, so it needs to be
370 // explicitly registered.
371 command_line->AppendSwitchPath(switches::kExtraPluginDir, plugin_dir);
372 #endif
373 command_line->AppendSwitch(switches::kEnableNpapi);
377 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, Basic) {
378 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
379 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
381 GURL url = ui_test_utils::GetTestUrl(
382 base::FilePath(), base::FilePath().AppendASCII("clicktoplay.html"));
383 ui_test_utils::NavigateToURL(browser(), url);
385 base::string16 expected_title(base::ASCIIToUTF16("OK"));
386 content::TitleWatcher title_watcher(
387 browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
389 content::WebContents* web_contents =
390 browser()->tab_strip_model()->GetActiveWebContents();
391 ChromePluginServiceFilter* filter = ChromePluginServiceFilter::GetInstance();
392 int process_id = web_contents->GetMainFrame()->GetProcess()->GetID();
393 base::FilePath path(FILE_PATH_LITERAL("blah"));
394 EXPECT_FALSE(filter->CanLoadPlugin(process_id, path));
395 filter->AuthorizeAllPlugins(web_contents, true, std::string());
396 EXPECT_TRUE(filter->CanLoadPlugin(process_id, path));
398 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
401 // Verify that plugins can be allowed on a domain by adding an exception
402 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, AllowException) {
403 GURL url = ui_test_utils::GetTestUrl(
404 base::FilePath(), base::FilePath().AppendASCII("clicktoplay.html"));
406 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
407 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
408 browser()->profile()->GetHostContentSettingsMap()
409 ->SetContentSetting(ContentSettingsPattern::FromURL(url),
410 ContentSettingsPattern::Wildcard(),
411 CONTENT_SETTINGS_TYPE_PLUGINS,
412 std::string(),
413 CONTENT_SETTING_ALLOW);
415 base::string16 expected_title(base::ASCIIToUTF16("OK"));
416 content::TitleWatcher title_watcher(
417 browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
418 ui_test_utils::NavigateToURL(browser(), url);
419 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
422 // Verify that plugins can be blocked on a domain by adding an exception.
423 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, BlockException) {
424 GURL url = ui_test_utils::GetTestUrl(
425 base::FilePath(), base::FilePath().AppendASCII("clicktoplay.html"));
427 browser()->profile()->GetHostContentSettingsMap()
428 ->SetContentSetting(ContentSettingsPattern::FromURL(url),
429 ContentSettingsPattern::Wildcard(),
430 CONTENT_SETTINGS_TYPE_PLUGINS,
431 std::string(),
432 CONTENT_SETTING_BLOCK);
434 base::string16 expected_title(base::ASCIIToUTF16("Click To Play"));
435 content::TitleWatcher title_watcher(
436 browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
437 ui_test_utils::NavigateToURL(browser(), url);
438 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
441 // Crashes on Mac Asan. http://crbug.com/239169
442 #if defined(OS_MACOSX)
443 #define MAYBE_LoadAllBlockedPlugins DISABLED_LoadAllBlockedPlugins
444 // TODO(jschuh): Flaky plugin tests. crbug.com/244653
445 #elif defined(OS_WIN) && defined(ARCH_CPU_X86_64)
446 #define MAYBE_LoadAllBlockedPlugins DISABLED_LoadAllBlockedPlugins
447 #else
448 #define MAYBE_LoadAllBlockedPlugins LoadAllBlockedPlugins
449 #endif
450 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, MAYBE_LoadAllBlockedPlugins) {
451 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
452 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
454 GURL url = ui_test_utils::GetTestUrl(
455 base::FilePath(),
456 base::FilePath().AppendASCII("load_all_blocked_plugins.html"));
457 ui_test_utils::NavigateToURL(browser(), url);
459 base::string16 expected_title1(base::ASCIIToUTF16("1"));
460 content::TitleWatcher title_watcher1(
461 browser()->tab_strip_model()->GetActiveWebContents(), expected_title1);
463 ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
464 browser()->tab_strip_model()->GetActiveWebContents(), true,
465 std::string());
466 EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle());
468 base::string16 expected_title2(base::ASCIIToUTF16("2"));
469 content::TitleWatcher title_watcher2(
470 browser()->tab_strip_model()->GetActiveWebContents(), expected_title2);
472 ASSERT_TRUE(content::ExecuteScript(
473 browser()->tab_strip_model()->GetActiveWebContents(), "window.inject()"));
475 EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle());
478 // If this flakes, use http://crbug.com/113057.
479 // TODO(jschuh): Hanging plugin tests. crbug.com/244653
480 #if !defined(OS_WIN) && !defined(ARCH_CPU_X86_64)
481 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, NoCallbackAtLoad) {
482 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
483 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
485 GURL url("data:application/vnd.npapi-test,CallOnStartup();");
486 ui_test_utils::NavigateToURL(browser(), url);
488 std::string script("CallOnStartup = function() { ");
489 script.append("document.documentElement.appendChild");
490 script.append("(document.createElement(\"head\")); ");
491 script.append("document.title = \"OK\"; }");
493 // Inject the callback function into the HTML page generated by the browser.
494 ASSERT_TRUE(content::ExecuteScript(
495 browser()->tab_strip_model()->GetActiveWebContents(), script));
497 base::string16 expected_title(base::ASCIIToUTF16("OK"));
498 content::TitleWatcher title_watcher(
499 browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
501 ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
502 browser()->tab_strip_model()->GetActiveWebContents(), true,
503 std::string());
505 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
507 #endif
509 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, DeleteSelfAtLoad) {
510 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
511 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
513 GURL url = ui_test_utils::GetTestUrl(
514 base::FilePath(),
515 base::FilePath().AppendASCII("plugin_delete_self_at_load.html"));
516 ui_test_utils::NavigateToURL(browser(), url);
518 base::string16 expected_title(base::ASCIIToUTF16("OK"));
519 content::TitleWatcher title_watcher(
520 browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
522 ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
523 browser()->tab_strip_model()->GetActiveWebContents(), true,
524 std::string());
526 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
529 #endif // !defined(USE_AURA) || defined(OS_WIN)
531 #if defined(ENABLE_PLUGINS)
532 class PepperContentSettingsSpecialCasesTest : public ContentSettingsTest {
533 protected:
534 static const char* const kExternalClearKeyMimeType;
536 // Registers any CDM plugins not registered by default.
537 void SetUpCommandLine(base::CommandLine* command_line) override {
538 #if defined(ENABLE_PEPPER_CDMS)
539 // Platform-specific filename relative to the chrome executable.
540 #if defined(OS_WIN)
541 const char kLibraryName[] = "clearkeycdmadapter.dll";
542 #else // !defined(OS_WIN)
543 #if defined(OS_MACOSX)
544 const char kLibraryName[] = "clearkeycdmadapter.plugin";
545 #elif defined(OS_POSIX)
546 const char kLibraryName[] = "libclearkeycdmadapter.so";
547 #endif // defined(OS_MACOSX)
548 #endif // defined(OS_WIN)
550 // Append the switch to register the External Clear Key CDM.
551 base::FilePath::StringType pepper_plugins = BuildPepperPluginRegistration(
552 kLibraryName, "Clear Key CDM", kExternalClearKeyMimeType);
553 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
554 // The CDM must be registered when it is a component.
555 pepper_plugins.append(FILE_PATH_LITERAL(","));
556 pepper_plugins.append(
557 BuildPepperPluginRegistration(kWidevineCdmAdapterFileName,
558 kWidevineCdmDisplayName,
559 kWidevineCdmPluginMimeType));
560 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
561 command_line->AppendSwitchNative(switches::kRegisterPepperPlugins,
562 pepper_plugins);
563 #endif // defined(ENABLE_PEPPER_CDMS)
565 #if !defined(DISABLE_NACL)
566 // Ensure NaCl can run.
567 command_line->AppendSwitch(switches::kEnableNaCl);
568 #endif
571 void RunLoadPepperPluginTest(const char* mime_type, bool expect_loaded) {
572 const char* expected_result = expect_loaded ? "Loaded" : "Not Loaded";
573 content::WebContents* web_contents =
574 browser()->tab_strip_model()->GetActiveWebContents();
576 base::string16 expected_title(base::ASCIIToUTF16(expected_result));
577 content::TitleWatcher title_watcher(web_contents, expected_title);
579 // GetTestUrl assumes paths, so we must append query parameters to result.
580 GURL file_url = ui_test_utils::GetTestUrl(
581 base::FilePath(),
582 base::FilePath().AppendASCII("load_pepper_plugin.html"));
583 GURL url(file_url.spec() +
584 base::StringPrintf("?mimetype=%s", mime_type));
585 ui_test_utils::NavigateToURL(browser(), url);
587 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
588 EXPECT_EQ(!expect_loaded,
589 TabSpecificContentSettings::FromWebContents(web_contents)->
590 IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
593 void RunJavaScriptBlockedTest(const char* html_file,
594 bool expect_is_javascript_content_blocked) {
595 // Because JavaScript is blocked, <title> will be the only title set.
596 // Checking for it ensures that the page loaded, though that is not always
597 // sufficient - see below.
598 const char* const kExpectedTitle = "Initial Title";
599 content::WebContents* web_contents =
600 browser()->tab_strip_model()->GetActiveWebContents();
601 TabSpecificContentSettings* tab_settings =
602 TabSpecificContentSettings::FromWebContents(web_contents);
603 base::string16 expected_title(base::ASCIIToUTF16(kExpectedTitle));
604 content::TitleWatcher title_watcher(web_contents, expected_title);
606 // Because JavaScript is blocked, we cannot rely on JavaScript to set a
607 // title, telling us the test is complete.
608 // As a result, it is possible to reach the IsContentBlocked() checks below
609 // before the blocked content can be reported to the browser process.
610 // See http://crbug.com/306702.
611 // Therefore, when expecting blocked content, we must wait until it has been
612 // reported by checking IsContentBlocked() when notified that
613 // NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED. (It is not sufficient to wait
614 // for just the notification because the same notification is reported for
615 // other reasons and the notification contains no indication of what
616 // caused it.)
617 content::WindowedNotificationObserver javascript_content_blocked_observer(
618 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
619 base::Bind(&TabSpecificContentSettings::IsContentBlocked,
620 base::Unretained(tab_settings),
621 CONTENT_SETTINGS_TYPE_JAVASCRIPT));
623 GURL url = ui_test_utils::GetTestUrl(
624 base::FilePath(), base::FilePath().AppendASCII(html_file));
625 ui_test_utils::NavigateToURL(browser(), url);
627 // Always wait for the page to load.
628 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
630 if (expect_is_javascript_content_blocked) {
631 javascript_content_blocked_observer.Wait();
632 } else {
633 // Since there is no notification that content is not blocked and no
634 // content is blocked when |expect_is_javascript_content_blocked| is
635 // false, javascript_content_blocked_observer would never succeed.
636 // There is no way to ensure blocked content would not have been reported
637 // after the check below. For coverage of this scenario, we must rely on
638 // the TitleWatcher adding sufficient delay most of the time.
641 EXPECT_EQ(expect_is_javascript_content_blocked,
642 tab_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
643 EXPECT_FALSE(tab_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
646 private:
647 // Builds the string to pass to kRegisterPepperPlugins for a single
648 // plugin using the provided parameters and a dummy version.
649 // Multiple results may be passed to kRegisterPepperPlugins, separated by ",".
650 base::FilePath::StringType BuildPepperPluginRegistration(
651 const char* library_name,
652 const char* display_name,
653 const char* mime_type) {
654 base::FilePath plugin_dir;
655 EXPECT_TRUE(PathService::Get(base::DIR_MODULE, &plugin_dir));
657 base::FilePath plugin_lib = plugin_dir.AppendASCII(library_name);
658 EXPECT_TRUE(base::PathExists(plugin_lib));
660 base::FilePath::StringType pepper_plugin = plugin_lib.value();
661 std::string string_to_append = "#";
662 string_to_append.append(display_name);
663 string_to_append.append("#A CDM#0.1.0.0;");
664 string_to_append.append(mime_type);
666 #if defined(OS_WIN)
667 pepper_plugin.append(base::ASCIIToUTF16(string_to_append));
668 #else
669 pepper_plugin.append(string_to_append);
670 #endif
672 return pepper_plugin;
676 const char* const
677 PepperContentSettingsSpecialCasesTest::kExternalClearKeyMimeType =
678 "application/x-ppapi-clearkey-cdm";
680 class PepperContentSettingsSpecialCasesPluginsBlockedTest
681 : public PepperContentSettingsSpecialCasesTest {
682 public:
683 void SetUpOnMainThread() override {
684 PepperContentSettingsSpecialCasesTest::SetUpOnMainThread();
685 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
686 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
690 class PepperContentSettingsSpecialCasesJavaScriptBlockedTest
691 : public PepperContentSettingsSpecialCasesTest {
692 public:
693 void SetUpOnMainThread() override {
694 PepperContentSettingsSpecialCasesTest::SetUpOnMainThread();
695 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
696 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ALLOW);
697 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
698 CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
702 #if defined(ENABLE_PEPPER_CDMS)
703 // A sanity check to verify that the plugin that is used as a baseline below
704 // can be loaded.
705 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesTest, Baseline) {
706 #if defined(OS_WIN) && defined(USE_ASH)
707 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
708 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
709 switches::kAshBrowserTests))
710 return;
711 #endif
712 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
713 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ALLOW);
715 RunLoadPepperPluginTest(kExternalClearKeyMimeType, true);
717 #endif // defined(ENABLE_PEPPER_CDMS)
719 // The following tests verify that Pepper plugins that use JavaScript settings
720 // instead of Plugins settings still work when Plugins are blocked.
722 #if defined(ENABLE_PEPPER_CDMS)
723 // The plugin successfully loaded above is blocked.
724 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesPluginsBlockedTest,
725 Normal) {
726 #if defined(OS_WIN) && defined(USE_ASH)
727 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
728 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
729 switches::kAshBrowserTests))
730 return;
731 #endif
732 RunLoadPepperPluginTest(kExternalClearKeyMimeType, false);
735 #if defined(WIDEVINE_CDM_AVAILABLE)
736 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesPluginsBlockedTest,
737 WidevineCdm) {
738 #if defined(OS_WIN) && defined(USE_ASH)
739 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
740 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
741 switches::kAshBrowserTests))
742 return;
743 #endif
744 RunLoadPepperPluginTest(kWidevineCdmPluginMimeType, true);
746 #endif // defined(WIDEVINE_CDM_AVAILABLE)
747 #endif // defined(ENABLE_PEPPER_CDMS)
749 #if !defined(DISABLE_NACL)
750 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesPluginsBlockedTest,
751 NaCl) {
752 #if defined(OS_WIN) && defined(USE_ASH)
753 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
754 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
755 switches::kAshBrowserTests))
756 return;
757 #endif
758 RunLoadPepperPluginTest("application/x-nacl", true);
760 #endif // !defined(DISABLE_NACL)
762 // The following tests verify that those same Pepper plugins do not work when
763 // JavaScript is blocked.
765 #if defined(ENABLE_PEPPER_CDMS)
766 // A plugin with no special behavior is not blocked when JavaScript is blocked.
767 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesJavaScriptBlockedTest,
768 Normal) {
769 #if defined(OS_WIN) && defined(USE_ASH)
770 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
771 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
772 switches::kAshBrowserTests))
773 return;
774 #endif
775 RunJavaScriptBlockedTest("load_clearkey_no_js.html", false);
778 #if defined(WIDEVINE_CDM_AVAILABLE)
779 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesJavaScriptBlockedTest,
780 WidevineCdm) {
781 #if defined(OS_WIN) && defined(USE_ASH)
782 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
783 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
784 switches::kAshBrowserTests))
785 return;
786 #endif
787 RunJavaScriptBlockedTest("load_widevine_no_js.html", true);
789 #endif // defined(WIDEVINE_CDM_AVAILABLE)
790 #endif // defined(ENABLE_PEPPER_CDMS)
792 #if !defined(DISABLE_NACL)
793 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesJavaScriptBlockedTest,
794 NaCl) {
795 #if defined(OS_WIN) && defined(USE_ASH)
796 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
797 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
798 switches::kAshBrowserTests))
799 return;
800 #endif
801 RunJavaScriptBlockedTest("load_nacl_no_js.html", true);
803 #endif // !defined(DISABLE_NACL)
805 #endif // defined(ENABLE_PLUGINS)