• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/plugin_service.h"
26 #include "content/public/browser/render_process_host.h"
27 #include "content/public/browser/render_view_host.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/common/content_switches.h"
30 #include "content/public/test/browser_test_utils.h"
31 #include "content/public/test/test_utils.h"
32 #include "content/test/net/url_request_mock_http_job.h"
33 #include "net/test/spawned_test_server/spawned_test_server.h"
34 
35 #include "widevine_cdm_version.h"  // In SHARED_INTERMEDIATE_DIR.
36 
37 #if defined(OS_MACOSX)
38 #include "base/mac/scoped_nsautorelease_pool.h"
39 #endif
40 
41 using content::BrowserThread;
42 using content::URLRequestMockHTTPJob;
43 
44 class ContentSettingsTest : public InProcessBrowserTest {
45  public:
ContentSettingsTest()46   ContentSettingsTest()
47       : https_server_(net::SpawnedTestServer::TYPE_HTTPS,
48                       net::SpawnedTestServer::SSLOptions(
49                           net::SpawnedTestServer::SSLOptions::CERT_OK),
50                       base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))) {
51   }
52 
SetUpOnMainThread()53   virtual void SetUpOnMainThread() OVERRIDE {
54     BrowserThread::PostTask(
55         BrowserThread::IO, FROM_HERE,
56         base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
57   }
58 
59   // Check the cookie for the given URL in an incognito window.
CookieCheckIncognitoWindow(const GURL & url,bool cookies_enabled)60   void CookieCheckIncognitoWindow(const GURL& url, bool cookies_enabled) {
61     ASSERT_TRUE(content::GetCookies(browser()->profile(), url).empty());
62 
63     Browser* incognito = CreateIncognitoBrowser();
64     ASSERT_TRUE(content::GetCookies(incognito->profile(), url).empty());
65     ui_test_utils::NavigateToURL(incognito, url);
66     ASSERT_EQ(cookies_enabled,
67               !content::GetCookies(incognito->profile(), url).empty());
68 
69     // Ensure incognito cookies don't leak to regular profile.
70     ASSERT_TRUE(content::GetCookies(browser()->profile(), url).empty());
71 
72     // Ensure cookies get wiped after last incognito window closes.
73     content::WindowedNotificationObserver signal(
74         chrome::NOTIFICATION_BROWSER_CLOSED,
75         content::Source<Browser>(incognito));
76 
77     chrome::CloseWindow(incognito);
78 
79 #if defined(OS_MACOSX)
80     // BrowserWindowController depends on the auto release pool being recycled
81     // in the message loop to delete itself, which frees the Browser object
82     // which fires this event.
83     AutoreleasePool()->Recycle();
84 #endif
85 
86     signal.Wait();
87 
88     incognito = CreateIncognitoBrowser();
89     ASSERT_TRUE(content::GetCookies(incognito->profile(), url).empty());
90     chrome::CloseWindow(incognito);
91   }
92 
PreBasic(const GURL & url)93   void PreBasic(const GURL& url) {
94     ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
95 
96     CookieCheckIncognitoWindow(url, true);
97 
98     ui_test_utils::NavigateToURL(browser(), url);
99     ASSERT_FALSE(GetCookies(browser()->profile(), url).empty());
100   }
101 
Basic(const GURL & url)102   void Basic(const GURL& url) {
103     ASSERT_FALSE(GetCookies(browser()->profile(), url).empty());
104   }
105 
106   net::SpawnedTestServer https_server_;
107 };
108 
109 // Sanity check on cookies before we do other tests. While these can be written
110 // in content_browsertests, we want to verify Chrome's cookie storage and how it
111 // handles incognito windows.
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,PRE_BasicCookies)112 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, PRE_BasicCookies) {
113   ASSERT_TRUE(test_server()->Start());
114   GURL http_url = test_server()->GetURL("files/setcookie.html");
115   PreBasic(http_url);
116 }
117 
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,BasicCookies)118 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, BasicCookies) {
119   ASSERT_TRUE(test_server()->Start());
120   GURL http_url = test_server()->GetURL("files/setcookie.html");
121   Basic(http_url);
122 }
123 
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,PRE_BasicCookiesHttps)124 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, PRE_BasicCookiesHttps) {
125   ASSERT_TRUE(https_server_.Start());
126   GURL https_url = https_server_.GetURL("files/setcookie.html");
127   PreBasic(https_url);
128 }
129 
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,BasicCookiesHttps)130 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, BasicCookiesHttps) {
131   ASSERT_TRUE(https_server_.Start());
132   GURL https_url = https_server_.GetURL("files/setcookie.html");
133   Basic(https_url);
134 }
135 
136 // Verify that cookies are being blocked.
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,PRE_BlockCookies)137 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, PRE_BlockCookies) {
138   ASSERT_TRUE(test_server()->Start());
139   CookieSettings::Factory::GetForProfile(browser()->profile())->
140       SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
141   GURL url = test_server()->GetURL("files/setcookie.html");
142   ui_test_utils::NavigateToURL(browser(), url);
143   ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
144   CookieCheckIncognitoWindow(url, false);
145 }
146 
147 // Ensure that the setting persists.
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,BlockCookies)148 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, BlockCookies) {
149   ASSERT_EQ(
150       CONTENT_SETTING_BLOCK,
151       CookieSettings::Factory::GetForProfile(browser()->profile())->
152           GetDefaultCookieSetting(NULL));
153 }
154 
155 // Verify that cookies can be allowed and set using exceptions for particular
156 // website(s) when all others are blocked.
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,AllowCookiesUsingExceptions)157 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, AllowCookiesUsingExceptions) {
158   ASSERT_TRUE(test_server()->Start());
159   GURL url = test_server()->GetURL("files/setcookie.html");
160   CookieSettings* settings =
161       CookieSettings::Factory::GetForProfile(browser()->profile()).get();
162   settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
163 
164   ui_test_utils::NavigateToURL(browser(), url);
165   ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
166 
167   settings->SetCookieSetting(
168       ContentSettingsPattern::FromURL(url),
169       ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW);
170 
171   ui_test_utils::NavigateToURL(browser(), url);
172   ASSERT_FALSE(GetCookies(browser()->profile(), url).empty());
173 }
174 
175 // Verify that cookies can be blocked for a specific website using exceptions.
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,BlockCookiesUsingExceptions)176 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, BlockCookiesUsingExceptions) {
177   ASSERT_TRUE(test_server()->Start());
178   GURL url = test_server()->GetURL("files/setcookie.html");
179   CookieSettings* settings =
180       CookieSettings::Factory::GetForProfile(browser()->profile()).get();
181   settings->SetCookieSetting(ContentSettingsPattern::FromURL(url),
182                              ContentSettingsPattern::Wildcard(),
183                              CONTENT_SETTING_BLOCK);
184 
185   ui_test_utils::NavigateToURL(browser(), url);
186   ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
187 
188   ASSERT_TRUE(https_server_.Start());
189   GURL unblocked_url = https_server_.GetURL("files/cookie1.html");
190 
191   ui_test_utils::NavigateToURL(browser(), unblocked_url);
192   ASSERT_FALSE(GetCookies(browser()->profile(), unblocked_url).empty());
193 }
194 
195 // This fails on ChromeOS because kRestoreOnStartup is ignored and the startup
196 // preference is always "continue where I left off.
197 #if !defined(OS_CHROMEOS)
198 
199 // Verify that cookies can be allowed and set using exceptions for particular
200 // website(s) only for a session when all others are blocked.
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,PRE_AllowCookiesForASessionUsingExceptions)201 IN_PROC_BROWSER_TEST_F(ContentSettingsTest,
202                        PRE_AllowCookiesForASessionUsingExceptions) {
203   // NOTE: don't use test_server here, since we need the port to be the same
204   // across the restart.
205   GURL url = URLRequestMockHTTPJob::GetMockUrl(
206       base::FilePath(FILE_PATH_LITERAL("setcookie.html")));
207   CookieSettings* settings =
208       CookieSettings::Factory::GetForProfile(browser()->profile()).get();
209   settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
210 
211   ui_test_utils::NavigateToURL(browser(), url);
212   ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
213 
214   settings->SetCookieSetting(
215       ContentSettingsPattern::FromURL(url),
216       ContentSettingsPattern::Wildcard(), CONTENT_SETTING_SESSION_ONLY);
217   ui_test_utils::NavigateToURL(browser(), url);
218   ASSERT_FALSE(GetCookies(browser()->profile(), url).empty());
219 }
220 
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,AllowCookiesForASessionUsingExceptions)221 IN_PROC_BROWSER_TEST_F(ContentSettingsTest,
222                        AllowCookiesForASessionUsingExceptions) {
223   GURL url = URLRequestMockHTTPJob::GetMockUrl(
224       base::FilePath(FILE_PATH_LITERAL("setcookie.html")));
225   ASSERT_TRUE(GetCookies(browser()->profile(), url).empty());
226 }
227 
228 #endif // !CHROME_OS
229 
230 // Regression test for http://crbug.com/63649.
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,RedirectLoopCookies)231 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, RedirectLoopCookies) {
232   ASSERT_TRUE(test_server()->Start());
233 
234   GURL test_url = test_server()->GetURL("files/redirect-loop.html");
235 
236   CookieSettings::Factory::GetForProfile(browser()->profile())->
237       SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
238 
239   ui_test_utils::NavigateToURL(browser(), test_url);
240 
241   content::WebContents* web_contents =
242       browser()->tab_strip_model()->GetActiveWebContents();
243   ASSERT_EQ(UTF8ToUTF16(test_url.spec() + " failed to load"),
244             web_contents->GetTitle());
245 
246   EXPECT_TRUE(TabSpecificContentSettings::FromWebContents(web_contents)->
247       IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
248 }
249 
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,ContentSettingsBlockDataURLs)250 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, ContentSettingsBlockDataURLs) {
251   GURL url("data:text/html,<title>Data URL</title><script>alert(1)</script>");
252 
253   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
254       CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
255 
256   ui_test_utils::NavigateToURL(browser(), url);
257 
258   content::WebContents* web_contents =
259       browser()->tab_strip_model()->GetActiveWebContents();
260   ASSERT_EQ(UTF8ToUTF16("Data URL"), web_contents->GetTitle());
261 
262   EXPECT_TRUE(TabSpecificContentSettings::FromWebContents(web_contents)->
263       IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
264 }
265 
266 // Tests that if redirect across origins occurs, the new process still gets the
267 // content settings before the resource headers.
IN_PROC_BROWSER_TEST_F(ContentSettingsTest,RedirectCrossOrigin)268 IN_PROC_BROWSER_TEST_F(ContentSettingsTest, RedirectCrossOrigin) {
269   ASSERT_TRUE(test_server()->Start());
270 
271   net::HostPortPair host_port = test_server()->host_port_pair();
272   DCHECK_EQ(host_port.host(), std::string("127.0.0.1"));
273 
274   std::string redirect(base::StringPrintf(
275       "http://localhost:%d/files/redirect-cross-origin.html",
276       host_port.port()));
277   GURL test_url = test_server()->GetURL("server-redirect?" + redirect);
278 
279   CookieSettings::Factory::GetForProfile(browser()->profile())->
280       SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
281 
282   ui_test_utils::NavigateToURL(browser(), test_url);
283 
284   content::WebContents* web_contents =
285       browser()->tab_strip_model()->GetActiveWebContents();
286 
287   EXPECT_TRUE(TabSpecificContentSettings::FromWebContents(web_contents)->
288       IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
289 }
290 
291 // On Aura NPAPI only works on Windows.
292 #if !defined(USE_AURA) || defined(OS_WIN)
293 
294 class ClickToPlayPluginTest : public ContentSettingsTest {
295  public:
ClickToPlayPluginTest()296   ClickToPlayPluginTest() {}
297 
298 #if defined(OS_MACOSX)
SetUpCommandLine(CommandLine * command_line)299   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
300     base::FilePath plugin_dir;
301     PathService::Get(base::DIR_MODULE, &plugin_dir);
302     plugin_dir = plugin_dir.AppendASCII("plugins");
303     // The plugins directory isn't read by default on the Mac, so it needs to be
304     // explicitly registered.
305     command_line->AppendSwitchPath(switches::kExtraPluginDir, plugin_dir);
306   }
307 #endif
308 };
309 
IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest,Basic)310 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, Basic) {
311   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
312       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
313 
314   GURL url = ui_test_utils::GetTestUrl(
315       base::FilePath(), base::FilePath().AppendASCII("clicktoplay.html"));
316   ui_test_utils::NavigateToURL(browser(), url);
317 
318   base::string16 expected_title(ASCIIToUTF16("OK"));
319   content::TitleWatcher title_watcher(
320       browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
321 
322   content::RenderViewHost* host =
323       browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
324   ChromePluginServiceFilter* filter = ChromePluginServiceFilter::GetInstance();
325   int process_id = host->GetProcess()->GetID();
326   base::FilePath path(FILE_PATH_LITERAL("blah"));
327   EXPECT_FALSE(filter->CanLoadPlugin(process_id, path));
328   filter->AuthorizeAllPlugins(process_id);
329   EXPECT_TRUE(filter->CanLoadPlugin(process_id, path));
330   host->Send(new ChromeViewMsg_LoadBlockedPlugins(
331       host->GetRoutingID(), std::string()));
332 
333   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
334 }
335 
336 // Verify that plugins can be allowed on a domain by adding an exception
IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest,AllowException)337 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, AllowException) {
338   GURL url = ui_test_utils::GetTestUrl(
339       base::FilePath(), base::FilePath().AppendASCII("clicktoplay.html"));
340 
341   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
342       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
343   browser()->profile()->GetHostContentSettingsMap()
344       ->SetContentSetting(ContentSettingsPattern::FromURL(url),
345                           ContentSettingsPattern::Wildcard(),
346                           CONTENT_SETTINGS_TYPE_PLUGINS,
347                           std::string(),
348                           CONTENT_SETTING_ALLOW);
349 
350   base::string16 expected_title(ASCIIToUTF16("OK"));
351   content::TitleWatcher title_watcher(
352       browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
353   ui_test_utils::NavigateToURL(browser(), url);
354   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
355 }
356 
357 // Verify that plugins can be blocked on a domain by adding an exception.
IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest,BlockException)358 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, BlockException) {
359   GURL url = ui_test_utils::GetTestUrl(
360       base::FilePath(), base::FilePath().AppendASCII("clicktoplay.html"));
361 
362   browser()->profile()->GetHostContentSettingsMap()
363       ->SetContentSetting(ContentSettingsPattern::FromURL(url),
364                           ContentSettingsPattern::Wildcard(),
365                           CONTENT_SETTINGS_TYPE_PLUGINS,
366                           std::string(),
367                           CONTENT_SETTING_BLOCK);
368 
369   base::string16 expected_title(ASCIIToUTF16("Click To Play"));
370   content::TitleWatcher title_watcher(
371       browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
372   ui_test_utils::NavigateToURL(browser(), url);
373   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
374 }
375 
376 // Crashes on Mac Asan.  http://crbug.com/239169
377 #if defined(OS_MACOSX)
378 #define MAYBE_LoadAllBlockedPlugins DISABLED_LoadAllBlockedPlugins
379 // TODO(jschuh): Flaky plugin tests. crbug.com/244653
380 #elif defined(OS_WIN) && defined(ARCH_CPU_X86_64)
381 #define MAYBE_LoadAllBlockedPlugins DISABLED_LoadAllBlockedPlugins
382 #else
383 #define MAYBE_LoadAllBlockedPlugins LoadAllBlockedPlugins
384 #endif
IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest,MAYBE_LoadAllBlockedPlugins)385 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, MAYBE_LoadAllBlockedPlugins) {
386   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
387       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
388 
389   GURL url = ui_test_utils::GetTestUrl(
390       base::FilePath(),
391       base::FilePath().AppendASCII("load_all_blocked_plugins.html"));
392   ui_test_utils::NavigateToURL(browser(), url);
393 
394   base::string16 expected_title1(ASCIIToUTF16("1"));
395   content::TitleWatcher title_watcher1(
396       browser()->tab_strip_model()->GetActiveWebContents(), expected_title1);
397 
398   content::RenderViewHost* host =
399       browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
400   ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
401       host->GetProcess()->GetID());
402   host->Send(new ChromeViewMsg_LoadBlockedPlugins(
403       host->GetRoutingID(), std::string()));
404   EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle());
405 
406   base::string16 expected_title2(ASCIIToUTF16("2"));
407   content::TitleWatcher title_watcher2(
408       browser()->tab_strip_model()->GetActiveWebContents(), expected_title2);
409 
410   ASSERT_TRUE(content::ExecuteScript(
411       browser()->tab_strip_model()->GetActiveWebContents(), "window.inject()"));
412 
413   EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle());
414 }
415 
416 // If this flakes, use http://crbug.com/113057.
417 // TODO(jschuh): Hanging plugin tests. crbug.com/244653
418 #if !defined(OS_WIN) && !defined(ARCH_CPU_X86_64)
IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest,NoCallbackAtLoad)419 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, NoCallbackAtLoad) {
420   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
421       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
422 
423   GURL url("data:application/vnd.npapi-test,CallOnStartup();");
424   ui_test_utils::NavigateToURL(browser(), url);
425 
426   // Inject the callback function into the HTML page generated by the browser.
427   ASSERT_TRUE(content::ExecuteScript(
428       browser()->tab_strip_model()->GetActiveWebContents(),
429       "CallOnStartup = function() { document.title = \"OK\"; }"));
430 
431   base::string16 expected_title(ASCIIToUTF16("OK"));
432   content::TitleWatcher title_watcher(
433       browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
434 
435   content::RenderViewHost* host =
436       browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
437   ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
438       host->GetProcess()->GetID());
439   host->Send(new ChromeViewMsg_LoadBlockedPlugins(
440       host->GetRoutingID(), std::string()));
441 
442   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
443 }
444 #endif
445 
IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest,DeleteSelfAtLoad)446 IN_PROC_BROWSER_TEST_F(ClickToPlayPluginTest, DeleteSelfAtLoad) {
447   browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
448       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
449 
450   GURL url = ui_test_utils::GetTestUrl(
451       base::FilePath(),
452       base::FilePath().AppendASCII("plugin_delete_self_at_load.html"));
453   ui_test_utils::NavigateToURL(browser(), url);
454 
455   base::string16 expected_title(ASCIIToUTF16("OK"));
456   content::TitleWatcher title_watcher(
457       browser()->tab_strip_model()->GetActiveWebContents(), expected_title);
458 
459   content::RenderViewHost* host =
460       browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
461   ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
462       host->GetProcess()->GetID());
463   host->Send(new ChromeViewMsg_LoadBlockedPlugins(
464       host->GetRoutingID(), std::string()));
465 
466   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
467 }
468 
469 #endif  // !defined(USE_AURA) || defined(OS_WIN)
470 
471 #if defined(ENABLE_PLUGINS)
472 
473 class PepperContentSettingsTest : public ContentSettingsTest {
474  public:
PepperContentSettingsTest()475   PepperContentSettingsTest() {}
476 
477  protected:
478   static const char* const kExternalClearKeyMimeType;
479 
480   // Registers any CDM plugins not registered by default.
SetUpCommandLine(CommandLine * command_line)481   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
482 #if defined(ENABLE_PEPPER_CDMS)
483     // Platform-specific filename relative to the chrome executable.
484 #if defined(OS_WIN)
485     const std::wstring external_clear_key_mime_type =
486         ASCIIToWide(kExternalClearKeyMimeType);
487     const char kLibraryName[] = "clearkeycdmadapter.dll";
488 #else  // !defined(OS_WIN)
489     const char* external_clear_key_mime_type = kExternalClearKeyMimeType;
490 #if defined(OS_MACOSX)
491     const char kLibraryName[] = "clearkeycdmadapter.plugin";
492 #elif defined(OS_POSIX)
493     const char kLibraryName[] = "libclearkeycdmadapter.so";
494 #endif  // defined(OS_MACOSX)
495 #endif  // defined(OS_WIN)
496 
497     // Append the switch to register the External Clear Key CDM.
498     base::FilePath plugin_dir;
499     EXPECT_TRUE(PathService::Get(base::DIR_MODULE, &plugin_dir));
500     base::FilePath plugin_lib = plugin_dir.AppendASCII(kLibraryName);
501     EXPECT_TRUE(base::PathExists(plugin_lib));
502     base::FilePath::StringType pepper_plugin = plugin_lib.value();
503     pepper_plugin.append(FILE_PATH_LITERAL(
504         "#Clear Key CDM#Clear Key CDM 0.1.0.0#0.1.0.0;"));
505     pepper_plugin.append(external_clear_key_mime_type);
506     command_line->AppendSwitchNative(switches::kRegisterPepperPlugins,
507                                      pepper_plugin);
508 #endif  // defined(ENABLE_PEPPER_CDMS)
509 
510 #if !defined(DISABLE_NACL)
511     // Ensure NaCl can run.
512     command_line->AppendSwitch(switches::kEnableNaCl);
513 #endif
514   }
515 
RunLoadPepperPluginTest(const char * mime_type,bool expect_loaded)516   void RunLoadPepperPluginTest(const char* mime_type, bool expect_loaded) {
517     const char* expected_result = expect_loaded ? "Loaded" : "Not Loaded";
518     content::WebContents* web_contents =
519         browser()->tab_strip_model()->GetActiveWebContents();
520 
521     base::string16 expected_title(ASCIIToUTF16(expected_result));
522     content::TitleWatcher title_watcher(web_contents, expected_title);
523 
524     // GetTestUrl assumes paths, so we must append query parameters to result.
525     GURL file_url = ui_test_utils::GetTestUrl(
526         base::FilePath(),
527         base::FilePath().AppendASCII("load_pepper_plugin.html"));
528     GURL url(file_url.spec() +
529              base::StringPrintf("?mimetype=%s", mime_type));
530     ui_test_utils::NavigateToURL(browser(), url);
531 
532     EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
533     EXPECT_EQ(!expect_loaded,
534               TabSpecificContentSettings::FromWebContents(web_contents)->
535                   IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
536   }
537 
RunJavaScriptBlockedTest(const char * html_file,bool expect_is_javascript_content_blocked)538   void RunJavaScriptBlockedTest(const char* html_file,
539                                 bool expect_is_javascript_content_blocked) {
540     // Because JavaScript is disabled, <title> will be the only title set.
541     // Checking for it ensures that the page loaded.
542     const char* const kExpectedTitle = "Initial Title";
543     content::WebContents* web_contents =
544         browser()->tab_strip_model()->GetActiveWebContents();
545     TabSpecificContentSettings* tab_settings =
546         TabSpecificContentSettings::FromWebContents(web_contents);
547     base::string16 expected_title(ASCIIToUTF16(kExpectedTitle));
548     content::TitleWatcher title_watcher(web_contents, expected_title);
549 
550     GURL url = ui_test_utils::GetTestUrl(
551         base::FilePath(), base::FilePath().AppendASCII(html_file));
552     ui_test_utils::NavigateToURL(browser(), url);
553 
554     EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
555 
556     EXPECT_EQ(expect_is_javascript_content_blocked,
557               tab_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
558     EXPECT_FALSE(tab_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
559   }
560 };
561 
562 const char* const PepperContentSettingsTest::kExternalClearKeyMimeType =
563     "application/x-ppapi-clearkey-cdm";
564 
565 // Tests Pepper plugins that use JavaScript instead of Plug-ins settings.
IN_PROC_BROWSER_TEST_F(PepperContentSettingsTest,DISABLED_PluginSpecialCases)566 IN_PROC_BROWSER_TEST_F(PepperContentSettingsTest, DISABLED_PluginSpecialCases) {
567 #if defined(OS_WIN) && defined(USE_ASH)
568   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
569   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
570     return;
571 #endif
572 
573   HostContentSettingsMap* content_settings =
574       browser()->profile()->GetHostContentSettingsMap();
575 
576   // First, verify that this plugin can be loaded.
577   content_settings->SetDefaultContentSetting(
578       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ALLOW);
579 
580 #if defined(ENABLE_PEPPER_CDMS)
581   RunLoadPepperPluginTest(kExternalClearKeyMimeType, true);
582 #endif  // defined(ENABLE_PEPPER_CDMS)
583 
584   // Next, test behavior when plug-ins are blocked.
585   content_settings->SetDefaultContentSetting(
586       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
587 
588 #if defined(ENABLE_PEPPER_CDMS)
589   // The plugin we loaded above does not load now.
590   RunLoadPepperPluginTest(kExternalClearKeyMimeType, false);
591 
592 #if defined(WIDEVINE_CDM_AVAILABLE)
593   RunLoadPepperPluginTest(kWidevineCdmPluginMimeType, true);
594 #endif  // defined(WIDEVINE_CDM_AVAILABLE)
595 #endif  // defined(ENABLE_PEPPER_CDMS)
596 
597 #if !defined(DISABLE_NACL)
598   RunLoadPepperPluginTest("application/x-nacl", true);
599 #endif  // !defined(DISABLE_NACL)
600 
601   // Finally, test behavior when (just) JavaScript is blocked.
602   content_settings->SetDefaultContentSetting(
603       CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ALLOW);
604   content_settings->SetDefaultContentSetting(
605       CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
606 
607 #if defined(ENABLE_PEPPER_CDMS)
608   // This plugin has no special behavior and does not require JavaScript.
609   RunJavaScriptBlockedTest("load_clearkey_no_js.html", false);
610 
611 #if defined(WIDEVINE_CDM_AVAILABLE)
612   RunJavaScriptBlockedTest("load_widevine_no_js.html", true);
613 #endif  // defined(WIDEVINE_CDM_AVAILABLE)
614 #endif  // defined(ENABLE_PEPPER_CDMS)
615 
616 #if !defined(DISABLE_NACL)
617   RunJavaScriptBlockedTest("load_nacl_no_js.html", true);
618 #endif  // !defined(DISABLE_NACL)
619 }
620 
621 #endif  // defined(ENABLE_PLUGINS)
622