Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / content_settings / content_settings_browsertest.cc
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.
4
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/host_content_settings_map.h"
12 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
13 #include "chrome/browser/net/url_request_mock_util.h"
14 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_commands.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/render_messages.h"
21 #include "chrome/test/base/in_process_browser_test.h"
22 #include "chrome/test/base/test_switches.h"
23 #include "chrome/test/base/ui_test_utils.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 "content/test/net/url_request_mock_http_job.h"
36 #include "net/test/spawned_test_server/spawned_test_server.h"
37
38 #include "widevine_cdm_version.h"  // In SHARED_INTERMEDIATE_DIR.
39
40 #if defined(OS_MACOSX)
41 #include "base/mac/scoped_nsautorelease_pool.h"
42 #endif
43
44 using content::BrowserThread;
45 using content::URLRequestMockHTTPJob;
46
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"))) {
54   }
55
56   virtual void SetUpOnMainThread() OVERRIDE {
57     BrowserThread::PostTask(
58         BrowserThread::IO, FROM_HERE,
59         base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
60   }
61
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());
65
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());
71
72     // Ensure incognito cookies don't leak to regular profile.
73     ASSERT_TRUE(content::GetCookies(browser()->profile(), url).empty());
74
75     // Ensure cookies get wiped after last incognito window closes.
76     content::WindowedNotificationObserver signal(
77         chrome::NOTIFICATION_BROWSER_CLOSED,
78         content::Source<Browser>(incognito));
79
80     chrome::CloseWindow(incognito);
81
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
88
89     signal.Wait();
90
91     incognito = CreateIncognitoBrowser();
92     ASSERT_TRUE(content::GetCookies(incognito->profile(), url).empty());
93     chrome::CloseWindow(incognito);
94   }
95
96   void PreBasic(const GURL& url) {
97     ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
98
99     CookieCheckIncognitoWindow(url, true);
100
101     ui_test_utils::NavigateToURL(browser(), url);
102     ASSERT_FALSE(GetCookies(browser()->profile(), url).empty());
103   }
104
105   void Basic(const GURL& url) {
106     ASSERT_FALSE(GetCookies(browser()->profile(), url).empty());
107   }
108
109   net::SpawnedTestServer https_server_;
110 };
111
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);
119 }
120
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);
125 }
126
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);
131 }
132
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);
137 }
138
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);
148 }
149
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));
156 }
157
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);
166
167   ui_test_utils::NavigateToURL(browser(), url);
168   ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
169
170   settings->SetCookieSetting(
171       ContentSettingsPattern::FromURL(url),
172       ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW);
173
174   ui_test_utils::NavigateToURL(browser(), url);
175   ASSERT_FALSE(GetCookies(browser()->profile(), url).empty());
176 }
177
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);
187
188   ui_test_utils::NavigateToURL(browser(), url);
189   ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
190
191   ASSERT_TRUE(https_server_.Start());
192   GURL unblocked_url = https_server_.GetURL("files/cookie1.html");
193
194   ui_test_utils::NavigateToURL(browser(), unblocked_url);
195   ASSERT_FALSE(GetCookies(browser()->profile(), unblocked_url).empty());
196 }
197
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)
201
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);
213
214   ui_test_utils::NavigateToURL(browser(), url);
215   ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
216
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());
222 }
223
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());
229 }
230
231 #endif // !CHROME_OS
232
233 // Regression test for http://crbug.com/63649.
234 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, RedirectLoopCookies) {
235   ASSERT_TRUE(test_server()->Start());
236
237   GURL test_url = test_server()->GetURL("files/redirect-loop.html");
238
239   CookieSettings::Factory::GetForProfile(browser()->profile())->
240       SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
241
242   ui_test_utils::NavigateToURL(browser(), test_url);
243
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());
248
249   EXPECT_TRUE(TabSpecificContentSettings::FromWebContents(web_contents)->
250       IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
251 }
252
253 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, ContentSettingsBlockDataURLs) {
254   GURL url("data:text/html,<title>Data URL</title><script>alert(1)</script>");
255
256   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
257       CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
258
259   ui_test_utils::NavigateToURL(browser(), url);
260
261   content::WebContents* web_contents =
262       browser()->tab_strip_model()->GetActiveWebContents();
263   ASSERT_EQ(base::UTF8ToUTF16("Data URL"), web_contents->GetTitle());
264
265   EXPECT_TRUE(TabSpecificContentSettings::FromWebContents(web_contents)->
266       IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
267 }
268
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());
273
274   net::HostPortPair host_port = test_server()->host_port_pair();
275   DCHECK_EQ(host_port.host(), std::string("127.0.0.1"));
276
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);
281
282   CookieSettings::Factory::GetForProfile(browser()->profile())->
283       SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
284
285   ui_test_utils::NavigateToURL(browser(), test_url);
286
287   content::WebContents* web_contents =
288       browser()->tab_strip_model()->GetActiveWebContents();
289
290   EXPECT_TRUE(TabSpecificContentSettings::FromWebContents(web_contents)->
291       IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
292 }
293
294 // On Aura NPAPI only works on Windows.
295 #if !defined(USE_AURA) || defined(OS_WIN)
296
297 class ClickToPlayPluginTest : public ContentSettingsTest {
298  public:
299   ClickToPlayPluginTest() {}
300
301 #if defined(OS_MACOSX)
302   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
303     base::FilePath plugin_dir;
304     PathService::Get(base::DIR_MODULE, &plugin_dir);
305     plugin_dir = plugin_dir.AppendASCII("plugins");
306     // The plugins directory isn't read by default on the Mac, so it needs to be
307     // explicitly registered.
308     command_line->AppendSwitchPath(switches::kExtraPluginDir, plugin_dir);
309   }
310 #endif
311 };
312
313 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, Basic) {
314   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
315       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
316
317   GURL url = ui_test_utils::GetTestUrl(
318       base::FilePath(), base::FilePath().AppendASCII("clicktoplay.html"));
319   ui_test_utils::NavigateToURL(browser(), url);
320
321   base::string16 expected_title(base::ASCIIToUTF16("OK"));
322   content::TitleWatcher title_watcher(
323       browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
324
325   content::WebContents* web_contents =
326       browser()->tab_strip_model()->GetActiveWebContents();
327   ChromePluginServiceFilter* filter = ChromePluginServiceFilter::GetInstance();
328   int process_id = web_contents->GetMainFrame()->GetProcess()->GetID();
329   base::FilePath path(FILE_PATH_LITERAL("blah"));
330   EXPECT_FALSE(filter->CanLoadPlugin(process_id, path));
331   filter->AuthorizeAllPlugins(web_contents, true, std::string());
332   EXPECT_TRUE(filter->CanLoadPlugin(process_id, path));
333
334   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
335 }
336
337 // Verify that plugins can be allowed on a domain by adding an exception
338 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, AllowException) {
339   GURL url = ui_test_utils::GetTestUrl(
340       base::FilePath(), base::FilePath().AppendASCII("clicktoplay.html"));
341
342   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
343       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
344   browser()->profile()->GetHostContentSettingsMap()
345       ->SetContentSetting(ContentSettingsPattern::FromURL(url),
346                           ContentSettingsPattern::Wildcard(),
347                           CONTENT_SETTINGS_TYPE_PLUGINS,
348                           std::string(),
349                           CONTENT_SETTING_ALLOW);
350
351   base::string16 expected_title(base::ASCIIToUTF16("OK"));
352   content::TitleWatcher title_watcher(
353       browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
354   ui_test_utils::NavigateToURL(browser(), url);
355   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
356 }
357
358 // Verify that plugins can be blocked on a domain by adding an exception.
359 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, BlockException) {
360   GURL url = ui_test_utils::GetTestUrl(
361       base::FilePath(), base::FilePath().AppendASCII("clicktoplay.html"));
362
363   browser()->profile()->GetHostContentSettingsMap()
364       ->SetContentSetting(ContentSettingsPattern::FromURL(url),
365                           ContentSettingsPattern::Wildcard(),
366                           CONTENT_SETTINGS_TYPE_PLUGINS,
367                           std::string(),
368                           CONTENT_SETTING_BLOCK);
369
370   base::string16 expected_title(base::ASCIIToUTF16("Click To Play"));
371   content::TitleWatcher title_watcher(
372       browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
373   ui_test_utils::NavigateToURL(browser(), url);
374   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
375 }
376
377 // Crashes on Mac Asan.  http://crbug.com/239169
378 #if defined(OS_MACOSX)
379 #define MAYBE_LoadAllBlockedPlugins DISABLED_LoadAllBlockedPlugins
380 // TODO(jschuh): Flaky plugin tests. crbug.com/244653
381 #elif defined(OS_WIN) && defined(ARCH_CPU_X86_64)
382 #define MAYBE_LoadAllBlockedPlugins DISABLED_LoadAllBlockedPlugins
383 #else
384 #define MAYBE_LoadAllBlockedPlugins LoadAllBlockedPlugins
385 #endif
386 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, MAYBE_LoadAllBlockedPlugins) {
387   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
388       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
389
390   GURL url = ui_test_utils::GetTestUrl(
391       base::FilePath(),
392       base::FilePath().AppendASCII("load_all_blocked_plugins.html"));
393   ui_test_utils::NavigateToURL(browser(), url);
394
395   base::string16 expected_title1(base::ASCIIToUTF16("1"));
396   content::TitleWatcher title_watcher1(
397       browser()->tab_strip_model()->GetActiveWebContents(), expected_title1);
398
399   ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
400       browser()->tab_strip_model()->GetActiveWebContents(), true,
401       std::string());
402   EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle());
403
404   base::string16 expected_title2(base::ASCIIToUTF16("2"));
405   content::TitleWatcher title_watcher2(
406       browser()->tab_strip_model()->GetActiveWebContents(), expected_title2);
407
408   ASSERT_TRUE(content::ExecuteScript(
409       browser()->tab_strip_model()->GetActiveWebContents(), "window.inject()"));
410
411   EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle());
412 }
413
414 // If this flakes, use http://crbug.com/113057.
415 // TODO(jschuh): Hanging plugin tests. crbug.com/244653
416 #if !defined(OS_WIN) && !defined(ARCH_CPU_X86_64)
417 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, NoCallbackAtLoad) {
418   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
419       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
420
421   GURL url("data:application/vnd.npapi-test,CallOnStartup();");
422   ui_test_utils::NavigateToURL(browser(), url);
423
424   // Inject the callback function into the HTML page generated by the browser.
425   ASSERT_TRUE(content::ExecuteScript(
426       browser()->tab_strip_model()->GetActiveWebContents(),
427       "CallOnStartup = function() { document.title = \"OK\"; }"));
428
429   base::string16 expected_title(base::ASCIIToUTF16("OK"));
430   content::TitleWatcher title_watcher(
431       browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
432
433   ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
434       browser()->tab_strip_model()->GetActiveWebContents(), true,
435       std::string());
436
437   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
438 }
439 #endif
440
441 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, DeleteSelfAtLoad) {
442   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
443       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
444
445   GURL url = ui_test_utils::GetTestUrl(
446       base::FilePath(),
447       base::FilePath().AppendASCII("plugin_delete_self_at_load.html"));
448   ui_test_utils::NavigateToURL(browser(), url);
449
450   base::string16 expected_title(base::ASCIIToUTF16("OK"));
451   content::TitleWatcher title_watcher(
452       browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
453
454   ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
455       browser()->tab_strip_model()->GetActiveWebContents(), true,
456       std::string());
457
458   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
459 }
460
461 #endif  // !defined(USE_AURA) || defined(OS_WIN)
462
463 #if defined(ENABLE_PLUGINS)
464 class PepperContentSettingsSpecialCasesTest : public ContentSettingsTest {
465  protected:
466   static const char* const kExternalClearKeyMimeType;
467
468   // Registers any CDM plugins not registered by default.
469   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
470 #if defined(ENABLE_PEPPER_CDMS)
471     // Platform-specific filename relative to the chrome executable.
472 #if defined(OS_WIN)
473     const char kLibraryName[] = "clearkeycdmadapter.dll";
474 #else  // !defined(OS_WIN)
475 #if defined(OS_MACOSX)
476     const char kLibraryName[] = "clearkeycdmadapter.plugin";
477 #elif defined(OS_POSIX)
478     const char kLibraryName[] = "libclearkeycdmadapter.so";
479 #endif  // defined(OS_MACOSX)
480 #endif  // defined(OS_WIN)
481
482     // Append the switch to register the External Clear Key CDM.
483     base::FilePath::StringType pepper_plugins = BuildPepperPluginRegistration(
484         kLibraryName, "Clear Key CDM", kExternalClearKeyMimeType);
485 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
486     // The CDM must be registered when it is a component.
487     pepper_plugins.append(FILE_PATH_LITERAL(","));
488     pepper_plugins.append(
489         BuildPepperPluginRegistration(kWidevineCdmAdapterFileName,
490                                       kWidevineCdmDisplayName,
491                                       kWidevineCdmPluginMimeType));
492 #endif  // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
493     command_line->AppendSwitchNative(switches::kRegisterPepperPlugins,
494                                      pepper_plugins);
495 #endif  // defined(ENABLE_PEPPER_CDMS)
496
497 #if !defined(DISABLE_NACL)
498     // Ensure NaCl can run.
499     command_line->AppendSwitch(switches::kEnableNaCl);
500 #endif
501   }
502
503   void RunLoadPepperPluginTest(const char* mime_type, bool expect_loaded) {
504     const char* expected_result = expect_loaded ? "Loaded" : "Not Loaded";
505     content::WebContents* web_contents =
506         browser()->tab_strip_model()->GetActiveWebContents();
507
508     base::string16 expected_title(base::ASCIIToUTF16(expected_result));
509     content::TitleWatcher title_watcher(web_contents, expected_title);
510
511     // GetTestUrl assumes paths, so we must append query parameters to result.
512     GURL file_url = ui_test_utils::GetTestUrl(
513         base::FilePath(),
514         base::FilePath().AppendASCII("load_pepper_plugin.html"));
515     GURL url(file_url.spec() +
516              base::StringPrintf("?mimetype=%s", mime_type));
517     ui_test_utils::NavigateToURL(browser(), url);
518
519     EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
520     EXPECT_EQ(!expect_loaded,
521               TabSpecificContentSettings::FromWebContents(web_contents)->
522                   IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
523   }
524
525   void RunJavaScriptBlockedTest(const char* html_file,
526                                 bool expect_is_javascript_content_blocked) {
527     // Because JavaScript is blocked, <title> will be the only title set.
528     // Checking for it ensures that the page loaded, though that is not always
529     // sufficient - see below.
530     const char* const kExpectedTitle = "Initial Title";
531     content::WebContents* web_contents =
532         browser()->tab_strip_model()->GetActiveWebContents();
533     TabSpecificContentSettings* tab_settings =
534         TabSpecificContentSettings::FromWebContents(web_contents);
535     base::string16 expected_title(base::ASCIIToUTF16(kExpectedTitle));
536     content::TitleWatcher title_watcher(web_contents, expected_title);
537
538     // Because JavaScript is blocked, we cannot rely on JavaScript to set a
539     // title, telling us the test is complete.
540     // As a result, it is possible to reach the IsContentBlocked() checks below
541     // before the blocked content can be reported to the browser process.
542     // See http://crbug.com/306702.
543     // Therefore, when expecting blocked content, we must wait until it has been
544     // reported by checking IsContentBlocked() when notified that
545     // NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED. (It is not sufficient to wait
546     // for just the notification because the same notification is reported for
547     // other reasons and the notification contains no indication of what
548     // caused it.)
549     content::WindowedNotificationObserver javascript_content_blocked_observer(
550               chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
551               base::Bind(&TabSpecificContentSettings::IsContentBlocked,
552                                    base::Unretained(tab_settings),
553                                    CONTENT_SETTINGS_TYPE_JAVASCRIPT));
554
555     GURL url = ui_test_utils::GetTestUrl(
556         base::FilePath(), base::FilePath().AppendASCII(html_file));
557     ui_test_utils::NavigateToURL(browser(), url);
558
559     // Always wait for the page to load.
560     EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
561
562     if (expect_is_javascript_content_blocked) {
563       javascript_content_blocked_observer.Wait();
564     } else {
565       // Since there is no notification that content is not blocked and no
566       // content is blocked when |expect_is_javascript_content_blocked| is
567       // false, javascript_content_blocked_observer would never succeed.
568       // There is no way to ensure blocked content would not have been reported
569       // after the check below. For coverage of this scenario, we must rely on
570       // the TitleWatcher adding sufficient delay most of the time.
571     }
572
573     EXPECT_EQ(expect_is_javascript_content_blocked,
574               tab_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
575     EXPECT_FALSE(tab_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
576   }
577
578  private:
579   // Builds the string to pass to kRegisterPepperPlugins for a single
580   // plugin using the provided parameters and a dummy version.
581   // Multiple results may be passed to kRegisterPepperPlugins, separated by ",".
582   base::FilePath::StringType BuildPepperPluginRegistration(
583       const char* library_name,
584       const char* display_name,
585       const char* mime_type) {
586     base::FilePath plugin_dir;
587     EXPECT_TRUE(PathService::Get(base::DIR_MODULE, &plugin_dir));
588
589     base::FilePath plugin_lib = plugin_dir.AppendASCII(library_name);
590     EXPECT_TRUE(base::PathExists(plugin_lib));
591
592     base::FilePath::StringType pepper_plugin = plugin_lib.value();
593     pepper_plugin.append(FILE_PATH_LITERAL("#"));
594 #if defined(OS_WIN)
595     pepper_plugin.append(base::ASCIIToWide(display_name));
596 #else
597     pepper_plugin.append(display_name);
598 #endif
599     pepper_plugin.append(FILE_PATH_LITERAL("#A CDM#0.1.0.0;"));
600 #if defined(OS_WIN)
601     pepper_plugin.append(base::ASCIIToWide(mime_type));
602 #else
603     pepper_plugin.append(mime_type);
604 #endif
605
606     return pepper_plugin;
607   }
608 };
609
610 const char* const
611 PepperContentSettingsSpecialCasesTest::kExternalClearKeyMimeType =
612     "application/x-ppapi-clearkey-cdm";
613
614 class PepperContentSettingsSpecialCasesPluginsBlockedTest
615     : public PepperContentSettingsSpecialCasesTest {
616  public:
617   virtual void SetUpOnMainThread() OVERRIDE {
618     PepperContentSettingsSpecialCasesTest::SetUpOnMainThread();
619     browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
620         CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
621   }
622 };
623
624 class PepperContentSettingsSpecialCasesJavaScriptBlockedTest
625     : public PepperContentSettingsSpecialCasesTest {
626  public:
627   virtual void SetUpOnMainThread() OVERRIDE {
628     PepperContentSettingsSpecialCasesTest::SetUpOnMainThread();
629     browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
630         CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ALLOW);
631     browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
632         CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
633   }
634 };
635
636 #if defined(ENABLE_PEPPER_CDMS)
637 // A sanity check to verify that the plugin that is used as a baseline below
638 // can be loaded.
639 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesTest, Baseline) {
640 #if defined(OS_WIN) && defined(USE_ASH)
641   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
642   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
643     return;
644 #endif
645   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
646       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ALLOW);
647
648   RunLoadPepperPluginTest(kExternalClearKeyMimeType, true);
649 }
650 #endif  // defined(ENABLE_PEPPER_CDMS)
651
652 // The following tests verify that Pepper plugins that use JavaScript settings
653 // instead of Plug-ins settings still work when Plug-ins are blocked.
654
655 #if defined(ENABLE_PEPPER_CDMS)
656 // The plugin successfully loaded above is blocked.
657 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesPluginsBlockedTest,
658                        Normal) {
659 #if defined(OS_WIN) && defined(USE_ASH)
660   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
661   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
662     return;
663 #endif
664   RunLoadPepperPluginTest(kExternalClearKeyMimeType, false);
665 }
666
667 #if defined(WIDEVINE_CDM_AVAILABLE)
668 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesPluginsBlockedTest,
669                        WidevineCdm) {
670 #if defined(OS_WIN) && defined(USE_ASH)
671   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
672   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
673     return;
674 #endif
675   RunLoadPepperPluginTest(kWidevineCdmPluginMimeType, true);
676 }
677 #endif  // defined(WIDEVINE_CDM_AVAILABLE)
678 #endif  // defined(ENABLE_PEPPER_CDMS)
679
680 #if !defined(DISABLE_NACL)
681 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesPluginsBlockedTest,
682                        NaCl) {
683 #if defined(OS_WIN) && defined(USE_ASH)
684   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
685   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
686     return;
687 #endif
688   RunLoadPepperPluginTest("application/x-nacl", true);
689 }
690 #endif  // !defined(DISABLE_NACL)
691
692 // The following tests verify that those same Pepper plugins do not work when
693 // JavaScript is blocked.
694
695 #if defined(ENABLE_PEPPER_CDMS)
696 // A plugin with no special behavior is not blocked when JavaScript is blocked.
697 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesJavaScriptBlockedTest,
698                        Normal) {
699 #if defined(OS_WIN) && defined(USE_ASH)
700   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
701   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
702     return;
703 #endif
704   RunJavaScriptBlockedTest("load_clearkey_no_js.html", false);
705 }
706
707 #if defined(WIDEVINE_CDM_AVAILABLE)
708 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesJavaScriptBlockedTest,
709                        WidevineCdm) {
710 #if defined(OS_WIN) && defined(USE_ASH)
711   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
712   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
713     return;
714 #endif
715   RunJavaScriptBlockedTest("load_widevine_no_js.html", true);
716 }
717 #endif  // defined(WIDEVINE_CDM_AVAILABLE)
718 #endif  // defined(ENABLE_PEPPER_CDMS)
719
720 #if !defined(DISABLE_NACL)
721 IN_PROC_BROWSER_TEST_F(PepperContentSettingsSpecialCasesJavaScriptBlockedTest,
722                        NaCl) {
723 #if defined(OS_WIN) && defined(USE_ASH)
724   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
725   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
726     return;
727 #endif
728   RunJavaScriptBlockedTest("load_nacl_no_js.html", true);
729 }
730 #endif  // !defined(DISABLE_NACL)
731
732 #endif  // defined(ENABLE_PLUGINS)