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.
8 #include "base/callback.h"
9 #include "base/command_line.h"
10 #include "base/files/file_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/path_service.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/process/kill.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/plugins/plugin_prefs.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/ui_test_utils.h"
22 #include "content/public/browser/browser_child_process_host_iterator.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/child_process_data.h"
25 #include "content/public/browser/plugin_service.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/common/content_constants.h"
28 #include "content/public/common/content_paths.h"
29 #include "content/public/common/process_type.h"
30 #include "content/public/common/webplugininfo.h"
31 #include "content/public/test/browser_test_utils.h"
32 #include "content/public/test/test_utils.h"
33 #include "net/base/filename_util.h"
36 #include "ui/aura/window.h"
37 #include "ui/aura/window_tree_host.h"
40 using content::BrowserThread
;
44 class CallbackBarrier
: public base::RefCountedThreadSafe
<CallbackBarrier
> {
46 explicit CallbackBarrier(const base::Closure
& target_callback
)
47 : target_callback_(target_callback
),
48 outstanding_callbacks_(0),
52 base::Callback
<void(bool)> CreateCallback() {
53 outstanding_callbacks_
++;
54 return base::Bind(&CallbackBarrier::MayRunTargetCallback
, this);
58 friend class base::RefCountedThreadSafe
<CallbackBarrier
>;
61 EXPECT_TRUE(target_callback_
.is_null());
64 void MayRunTargetCallback(bool did_enable
) {
65 EXPECT_GT(outstanding_callbacks_
, 0);
66 did_enable_
= did_enable_
&& did_enable
;
67 if (--outstanding_callbacks_
== 0) {
68 EXPECT_TRUE(did_enable_
);
69 target_callback_
.Run();
70 target_callback_
.Reset();
74 base::Closure target_callback_
;
75 int outstanding_callbacks_
;
81 class ChromePluginTest
: public InProcessBrowserTest
{
85 static GURL
GetURL(const char* filename
) {
87 PathService::Get(content::DIR_TEST_DATA
, &path
);
88 path
= path
.AppendASCII("plugin").AppendASCII(filename
);
89 CHECK(base::PathExists(path
));
90 return net::FilePathToFileURL(path
);
93 static void LoadAndWait(Browser
* window
, const GURL
& url
, bool pass
) {
94 content::WebContents
* web_contents
=
95 window
->tab_strip_model()->GetActiveWebContents();
96 base::string16
expected_title(
97 base::ASCIIToUTF16(pass
? "OK" : "plugin_not_found"));
98 content::TitleWatcher
title_watcher(web_contents
, expected_title
);
99 title_watcher
.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
100 title_watcher
.AlsoWaitForTitle(base::ASCIIToUTF16(
101 pass
? "plugin_not_found" : "OK"));
102 ui_test_utils::NavigateToURL(window
, url
);
103 ASSERT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
106 static void CrashFlash() {
107 scoped_refptr
<content::MessageLoopRunner
> runner
=
108 new content::MessageLoopRunner
;
109 BrowserThread::PostTask(
112 base::Bind(&CrashFlashInternal
, runner
->QuitClosure()));
116 static void GetFlashPath(std::vector
<base::FilePath
>* paths
) {
118 std::vector
<content::WebPluginInfo
> plugins
= GetPlugins();
119 for (std::vector
<content::WebPluginInfo
>::const_iterator it
=
120 plugins
.begin(); it
!= plugins
.end(); ++it
) {
121 if (it
->name
== base::ASCIIToUTF16(content::kFlashPluginName
))
122 paths
->push_back(it
->path
);
126 static std::vector
<content::WebPluginInfo
> GetPlugins() {
127 std::vector
<content::WebPluginInfo
> plugins
;
128 scoped_refptr
<content::MessageLoopRunner
> runner
=
129 new content::MessageLoopRunner
;
130 content::PluginService::GetInstance()->GetPlugins(
131 base::Bind(&GetPluginsInfoCallback
, &plugins
, runner
->QuitClosure()));
136 static void EnableFlash(bool enable
, Profile
* profile
) {
137 std::vector
<base::FilePath
> paths
;
138 GetFlashPath(&paths
);
139 ASSERT_FALSE(paths
.empty());
141 PluginPrefs
* plugin_prefs
= PluginPrefs::GetForProfile(profile
).get();
142 scoped_refptr
<content::MessageLoopRunner
> runner
=
143 new content::MessageLoopRunner
;
144 scoped_refptr
<CallbackBarrier
> callback_barrier(
145 new CallbackBarrier(runner
->QuitClosure()));
146 for (std::vector
<base::FilePath
>::iterator iter
= paths
.begin();
147 iter
!= paths
.end(); ++iter
) {
148 plugin_prefs
->EnablePlugin(enable
, *iter
,
149 callback_barrier
->CreateCallback());
154 static void EnsureFlashProcessCount(int expected
) {
156 scoped_refptr
<content::MessageLoopRunner
> runner
=
157 new content::MessageLoopRunner
;
158 BrowserThread::PostTask(
161 base::Bind(&CountPluginProcesses
, &actual
, runner
->QuitClosure()));
163 ASSERT_EQ(expected
, actual
);
167 static void CrashFlashInternal(const base::Closure
& quit_task
) {
169 for (content::BrowserChildProcessHostIterator iter
; !iter
.Done(); ++iter
) {
170 if (iter
.GetData().process_type
!= content::PROCESS_TYPE_PLUGIN
&&
171 iter
.GetData().process_type
!= content::PROCESS_TYPE_PPAPI_PLUGIN
) {
174 base::KillProcess(iter
.GetData().handle
, 0, true);
177 ASSERT_TRUE(found
) << "Didn't find Flash process!";
178 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, quit_task
);
181 static void GetPluginsInfoCallback(
182 std::vector
<content::WebPluginInfo
>* rv
,
183 const base::Closure
& quit_task
,
184 const std::vector
<content::WebPluginInfo
>& plugins
) {
189 static void CountPluginProcesses(int* count
, const base::Closure
& quit_task
) {
190 for (content::BrowserChildProcessHostIterator iter
; !iter
.Done(); ++iter
) {
191 if (iter
.GetData().process_type
== content::PROCESS_TYPE_PLUGIN
||
192 iter
.GetData().process_type
== content::PROCESS_TYPE_PPAPI_PLUGIN
) {
196 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, quit_task
);
200 // Tests a bunch of basic scenarios with Flash.
201 // This test fails under ASan on Mac, see http://crbug.com/147004.
202 // It fails elsewhere, too. See http://crbug.com/152071.
203 IN_PROC_BROWSER_TEST_F(ChromePluginTest
, DISABLED_Flash
) {
204 // Official builds always have bundled Flash.
205 #if !defined(OFFICIAL_BUILD)
206 std::vector
<base::FilePath
> flash_paths
;
207 GetFlashPath(&flash_paths
);
208 if (flash_paths
.empty()) {
209 LOG(INFO
) << "Test not running because couldn't find Flash.";
214 GURL url
= GetURL("flash.html");
215 EnsureFlashProcessCount(0);
218 ASSERT_NO_FATAL_FAILURE(LoadAndWait(browser(), url
, true));
219 EnsureFlashProcessCount(1);
220 Profile
* profile
= browser()->profile();
222 ASSERT_NO_FATAL_FAILURE(LoadAndWait(CreateBrowser(profile
), url
, true));
223 // Try an incognito window.
224 ASSERT_NO_FATAL_FAILURE(LoadAndWait(CreateIncognitoBrowser(), url
, true));
225 EnsureFlashProcessCount(1);
227 // Now kill Flash process and verify it reloads.
229 EnsureFlashProcessCount(0);
231 ASSERT_NO_FATAL_FAILURE(LoadAndWait(browser(), url
, true));
232 EnsureFlashProcessCount(1);
234 // Now try disabling it.
235 EnableFlash(false, profile
);
238 ASSERT_NO_FATAL_FAILURE(LoadAndWait(browser(), url
, false));
239 EnsureFlashProcessCount(0);
241 // Now enable it again.
242 EnableFlash(true, profile
);
243 ASSERT_NO_FATAL_FAILURE(LoadAndWait(browser(), url
, true));
244 EnsureFlashProcessCount(1);
247 #if defined(OFFICIAL_BUILD)
248 // Verify that the official builds have the known set of plugins.
249 IN_PROC_BROWSER_TEST_F(ChromePluginTest
, InstalledPlugins
) {
250 const char* expected
[] = {
254 "Chrome Remote Desktop Viewer",
257 std::vector
<content::WebPluginInfo
> plugins
= GetPlugins();
258 for (size_t i
= 0; i
< arraysize(expected
); ++i
) {
260 for (; j
< plugins
.size(); ++j
) {
261 if (plugins
[j
].name
== base::ASCIIToUTF16(expected
[i
]))
264 ASSERT_TRUE(j
!= plugins
.size()) << "Didn't find " << expected
[i
];