Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: content/browser/plugin_service_impl_browsertest.cc

Issue 12086077: Only permit plug-in loads in the browser if the plug-in isn't blocked or the (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/plugin_service_impl.h" 5 #include "content/browser/plugin_service_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "content/public/browser/browser_context.h" 11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/plugin_service_filter.h"
12 #include "content/public/browser/resource_context.h" 13 #include "content/public/browser/resource_context.h"
13 #include "content/public/browser/web_contents.h" 14 #include "content/public/browser/web_contents.h"
14 #include "content/public/common/content_switches.h" 15 #include "content/public/common/content_switches.h"
15 #include "content/public/test/test_browser_thread.h" 16 #include "content/public/test/test_browser_thread.h"
16 #include "content/public/test/test_utils.h" 17 #include "content/public/test/test_utils.h"
17 #include "content/shell/shell.h" 18 #include "content/shell/shell.h"
18 #include "content/test/content_browser_test.h" 19 #include "content/test/content_browser_test.h"
19 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
20 #include "webkit/plugins/npapi/plugin_list.h" 21 #include "webkit/plugins/npapi/plugin_list.h"
21 #include "webkit/plugins/npapi/plugin_utils.h" 22 #include "webkit/plugins/npapi/plugin_utils.h"
22 23
23 namespace content { 24 namespace content {
24 25
25 const char kNPAPITestPluginMimeType[] = "application/vnd.npapi-test"; 26 const char kNPAPITestPluginMimeType[] = "application/vnd.npapi-test";
26 27
27 void OpenChannel(PluginProcessHost::Client* client) { 28 void OpenChannel(PluginProcessHost::Client* client) {
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
29 // Start opening the channel 30 // Start opening the channel
30 PluginServiceImpl::GetInstance()->OpenChannelToNpapiPlugin( 31 PluginServiceImpl::GetInstance()->OpenChannelToNpapiPlugin(
31 0, 0, GURL(), GURL(), kNPAPITestPluginMimeType, client); 32 0, 0, GURL(), GURL(), kNPAPITestPluginMimeType, client);
32 } 33 }
33 34
34 // Mock up of the Client and the Listener classes that would supply the 35 // Mock up of the Client and the Listener classes that would supply the
35 // communication channel with the plugin. 36 // communication channel with the plugin.
36 class MockPluginProcessHostClient : public PluginProcessHost::Client, 37 class MockPluginProcessHostClient : public PluginProcessHost::Client,
37 public IPC::Listener { 38 public IPC::Listener {
38 public: 39 public:
39 MockPluginProcessHostClient(ResourceContext* context) 40 MockPluginProcessHostClient(ResourceContext* context, bool expect_fail)
40 : context_(context), 41 : context_(context),
41 channel_(NULL), 42 channel_(NULL),
42 set_plugin_info_called_(false) { 43 set_plugin_info_called_(false),
44 expect_fail_(expect_fail) {
43 } 45 }
44 46
45 virtual ~MockPluginProcessHostClient() { 47 virtual ~MockPluginProcessHostClient() {
46 if (channel_) 48 if (channel_)
47 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, channel_); 49 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, channel_);
48 } 50 }
49 51
50 // PluginProcessHost::Client implementation. 52 // PluginProcessHost::Client implementation.
51 virtual int ID() OVERRIDE { return 42; } 53 virtual int ID() OVERRIDE { return 42; }
52 virtual bool OffTheRecord() OVERRIDE { return false; } 54 virtual bool OffTheRecord() OVERRIDE { return false; }
(...skipping 20 matching lines...) Expand all
73 virtual void OnError() OVERRIDE { 75 virtual void OnError() OVERRIDE {
74 Fail(); 76 Fail();
75 } 77 }
76 78
77 // IPC::Listener implementation. 79 // IPC::Listener implementation.
78 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { 80 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
79 Fail(); 81 Fail();
80 return false; 82 return false;
81 } 83 }
82 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE { 84 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE {
85 if (expect_fail_)
86 FAIL();
83 QuitMessageLoop(); 87 QuitMessageLoop();
84 } 88 }
85 virtual void OnChannelError() OVERRIDE { 89 virtual void OnChannelError() OVERRIDE {
86 Fail(); 90 Fail();
87 } 91 }
88 #if defined(OS_POSIX) 92 #if defined(OS_POSIX)
89 virtual void OnChannelDenied() OVERRIDE { 93 virtual void OnChannelDenied() OVERRIDE {
90 Fail(); 94 Fail();
91 } 95 }
92 virtual void OnChannelListenError() OVERRIDE { 96 virtual void OnChannelListenError() OVERRIDE {
93 Fail(); 97 Fail();
94 } 98 }
95 #endif 99 #endif
96 100
97 private: 101 private:
98 void Fail() { 102 void Fail() {
99 FAIL(); 103 if (!expect_fail_)
104 FAIL();
100 QuitMessageLoop(); 105 QuitMessageLoop();
101 } 106 }
102 107
103 void QuitMessageLoop() { 108 void QuitMessageLoop() {
104 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 109 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
105 MessageLoop::QuitClosure()); 110 MessageLoop::QuitClosure());
106 } 111 }
107 112
108 ResourceContext* context_; 113 ResourceContext* context_;
109 IPC::Channel* channel_; 114 IPC::Channel* channel_;
110 bool set_plugin_info_called_; 115 bool set_plugin_info_called_;
116 bool expect_fail_;
111 DISALLOW_COPY_AND_ASSIGN(MockPluginProcessHostClient); 117 DISALLOW_COPY_AND_ASSIGN(MockPluginProcessHostClient);
112 }; 118 };
113 119
120 class MockPluginServiceFilter : public content::PluginServiceFilter {
121 public:
122 MockPluginServiceFilter() {}
123
124 virtual bool IsPluginEnabled(
125 int render_process_id,
126 int render_view_id,
127 const void* context,
128 const GURL& url,
129 const GURL& policy_url,
130 webkit::WebPluginInfo* plugin) OVERRIDE { return true; }
131
132 virtual bool CanLoadPlugin(
133 int render_process_id,
134 const FilePath& path) OVERRIDE { return false; }
135 };
136
114 class PluginServiceTest : public ContentBrowserTest { 137 class PluginServiceTest : public ContentBrowserTest {
115 public: 138 public:
116 PluginServiceTest() {} 139 PluginServiceTest() {}
117 140
118 ResourceContext* GetResourceContext() { 141 ResourceContext* GetResourceContext() {
119 return shell()->web_contents()->GetBrowserContext()->GetResourceContext(); 142 return shell()->web_contents()->GetBrowserContext()->GetResourceContext();
120 } 143 }
121 144
122 virtual void SetUpCommandLine(CommandLine* command_line) { 145 virtual void SetUpCommandLine(CommandLine* command_line) {
123 #ifdef OS_MACOSX 146 #ifdef OS_MACOSX
124 FilePath browser_directory; 147 FilePath browser_directory;
125 PathService::Get(base::DIR_MODULE, &browser_directory); 148 PathService::Get(base::DIR_MODULE, &browser_directory);
126 command_line->AppendSwitchPath(switches::kExtraPluginDir, 149 command_line->AppendSwitchPath(switches::kExtraPluginDir,
127 browser_directory.AppendASCII("plugins")); 150 browser_directory.AppendASCII("plugins"));
128 #endif 151 #endif
129 // TODO(jam): since these plugin tests are running under Chrome, we need to 152 // TODO(jam): since these plugin tests are running under Chrome, we need to
130 // tell it to disable its security features for old plugins. Once this is 153 // tell it to disable its security features for old plugins. Once this is
131 // running under content_browsertests, these flags won't be needed. 154 // running under content_browsertests, these flags won't be needed.
132 // http://crbug.com/90448 155 // http://crbug.com/90448
133 // switches::kAlwaysAuthorizePlugins 156 // switches::kAlwaysAuthorizePlugins
134 command_line->AppendSwitch("always-authorize-plugins"); 157 command_line->AppendSwitch("always-authorize-plugins");
135 } 158 }
136 }; 159 };
137 160
138 // Try to open a channel to the test plugin. Minimal plugin process spawning 161 // Try to open a channel to the test plugin. Minimal plugin process spawning
139 // test for the PluginService interface. 162 // test for the PluginService interface.
140 IN_PROC_BROWSER_TEST_F(PluginServiceTest, OpenChannelToPlugin) { 163 IN_PROC_BROWSER_TEST_F(PluginServiceTest, OpenChannelToPlugin) {
141 if (!webkit::npapi::NPAPIPluginsSupported()) 164 if (!webkit::npapi::NPAPIPluginsSupported())
142 return; 165 return;
143 MockPluginProcessHostClient mock_client(GetResourceContext()); 166 MockPluginProcessHostClient mock_client(GetResourceContext(), false);
167 BrowserThread::PostTask(
168 BrowserThread::IO, FROM_HERE,
169 base::Bind(&OpenChannel, &mock_client));
170 RunMessageLoop();
171 }
172
173 IN_PROC_BROWSER_TEST_F(PluginServiceTest, OpenChannelToDeniedPlugin) {
174 if (!webkit::npapi::NPAPIPluginsSupported())
175 return;
176 MockPluginServiceFilter filter;
177 PluginServiceImpl::GetInstance()->SetFilter(&filter);
178 MockPluginProcessHostClient mock_client(GetResourceContext(), true);
144 BrowserThread::PostTask( 179 BrowserThread::PostTask(
145 BrowserThread::IO, FROM_HERE, 180 BrowserThread::IO, FROM_HERE,
146 base::Bind(&OpenChannel, &mock_client)); 181 base::Bind(&OpenChannel, &mock_client));
147 RunMessageLoop(); 182 RunMessageLoop();
148 } 183 }
149 184
150 // A strict mock that fails if any of the methods are called. They shouldn't be 185 // A strict mock that fails if any of the methods are called. They shouldn't be
151 // called since the request should get canceled before then. 186 // called since the request should get canceled before then.
152 class MockCanceledPluginServiceClient : public PluginProcessHost::Client { 187 class MockCanceledPluginServiceClient : public PluginProcessHost::Client {
153 public: 188 public:
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 BrowserThread::IO, FROM_HERE, 370 BrowserThread::IO, FROM_HERE,
336 base::Bind(&OpenChannel, &mock_client)); 371 base::Bind(&OpenChannel, &mock_client));
337 RunMessageLoop(); 372 RunMessageLoop();
338 EXPECT_TRUE(mock_client.get_resource_context_called()); 373 EXPECT_TRUE(mock_client.get_resource_context_called());
339 EXPECT_TRUE(mock_client.set_plugin_info_called()); 374 EXPECT_TRUE(mock_client.set_plugin_info_called());
340 EXPECT_TRUE(mock_client.on_found_plugin_process_host_called()); 375 EXPECT_TRUE(mock_client.on_found_plugin_process_host_called());
341 EXPECT_TRUE(mock_client.on_sent_plugin_channel_request_called()); 376 EXPECT_TRUE(mock_client.on_sent_plugin_channel_request_called());
342 } 377 }
343 378
344 } // namespace content 379 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698