| OLD | NEW |
| 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" | |
| 13 #include "content/public/browser/resource_context.h" | 12 #include "content/public/browser/resource_context.h" |
| 14 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
| 16 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
| 17 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
| 18 #include "content/shell/shell.h" | 17 #include "content/shell/shell.h" |
| 19 #include "content/test/content_browser_test.h" | 18 #include "content/test/content_browser_test.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "webkit/plugins/npapi/plugin_list.h" | 20 #include "webkit/plugins/npapi/plugin_list.h" |
| 22 #include "webkit/plugins/npapi/plugin_utils.h" | 21 #include "webkit/plugins/npapi/plugin_utils.h" |
| 23 | 22 |
| 24 namespace content { | 23 namespace content { |
| 25 | 24 |
| 26 const char kNPAPITestPluginMimeType[] = "application/vnd.npapi-test"; | 25 const char kNPAPITestPluginMimeType[] = "application/vnd.npapi-test"; |
| 27 | 26 |
| 28 void OpenChannel(PluginProcessHost::Client* client) { | 27 void OpenChannel(PluginProcessHost::Client* client) { |
| 29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 30 // Start opening the channel | 29 // Start opening the channel |
| 31 PluginServiceImpl::GetInstance()->OpenChannelToNpapiPlugin( | 30 PluginServiceImpl::GetInstance()->OpenChannelToNpapiPlugin( |
| 32 0, 0, GURL(), GURL(), kNPAPITestPluginMimeType, client); | 31 0, 0, GURL(), GURL(), kNPAPITestPluginMimeType, client); |
| 33 } | 32 } |
| 34 | 33 |
| 35 // Mock up of the Client and the Listener classes that would supply the | 34 // Mock up of the Client and the Listener classes that would supply the |
| 36 // communication channel with the plugin. | 35 // communication channel with the plugin. |
| 37 class MockPluginProcessHostClient : public PluginProcessHost::Client, | 36 class MockPluginProcessHostClient : public PluginProcessHost::Client, |
| 38 public IPC::Listener { | 37 public IPC::Listener { |
| 39 public: | 38 public: |
| 40 MockPluginProcessHostClient(ResourceContext* context, bool expect_fail) | 39 MockPluginProcessHostClient(ResourceContext* context) |
| 41 : context_(context), | 40 : context_(context), |
| 42 channel_(NULL), | 41 channel_(NULL), |
| 43 set_plugin_info_called_(false), | 42 set_plugin_info_called_(false) { |
| 44 expect_fail_(expect_fail) { | |
| 45 } | 43 } |
| 46 | 44 |
| 47 virtual ~MockPluginProcessHostClient() { | 45 virtual ~MockPluginProcessHostClient() { |
| 48 if (channel_) | 46 if (channel_) |
| 49 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, channel_); | 47 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, channel_); |
| 50 } | 48 } |
| 51 | 49 |
| 52 // PluginProcessHost::Client implementation. | 50 // PluginProcessHost::Client implementation. |
| 53 virtual int ID() OVERRIDE { return 42; } | 51 virtual int ID() OVERRIDE { return 42; } |
| 54 virtual bool OffTheRecord() OVERRIDE { return false; } | 52 virtual bool OffTheRecord() OVERRIDE { return false; } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 75 virtual void OnError() OVERRIDE { | 73 virtual void OnError() OVERRIDE { |
| 76 Fail(); | 74 Fail(); |
| 77 } | 75 } |
| 78 | 76 |
| 79 // IPC::Listener implementation. | 77 // IPC::Listener implementation. |
| 80 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 78 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| 81 Fail(); | 79 Fail(); |
| 82 return false; | 80 return false; |
| 83 } | 81 } |
| 84 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE { | 82 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE { |
| 85 if (expect_fail_) | |
| 86 FAIL(); | |
| 87 QuitMessageLoop(); | 83 QuitMessageLoop(); |
| 88 } | 84 } |
| 89 virtual void OnChannelError() OVERRIDE { | 85 virtual void OnChannelError() OVERRIDE { |
| 90 Fail(); | 86 Fail(); |
| 91 } | 87 } |
| 92 #if defined(OS_POSIX) | 88 #if defined(OS_POSIX) |
| 93 virtual void OnChannelDenied() OVERRIDE { | 89 virtual void OnChannelDenied() OVERRIDE { |
| 94 Fail(); | 90 Fail(); |
| 95 } | 91 } |
| 96 virtual void OnChannelListenError() OVERRIDE { | 92 virtual void OnChannelListenError() OVERRIDE { |
| 97 Fail(); | 93 Fail(); |
| 98 } | 94 } |
| 99 #endif | 95 #endif |
| 100 | 96 |
| 101 private: | 97 private: |
| 102 void Fail() { | 98 void Fail() { |
| 103 if (!expect_fail_) | 99 FAIL(); |
| 104 FAIL(); | |
| 105 QuitMessageLoop(); | 100 QuitMessageLoop(); |
| 106 } | 101 } |
| 107 | 102 |
| 108 void QuitMessageLoop() { | 103 void QuitMessageLoop() { |
| 109 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 104 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 110 MessageLoop::QuitClosure()); | 105 MessageLoop::QuitClosure()); |
| 111 } | 106 } |
| 112 | 107 |
| 113 ResourceContext* context_; | 108 ResourceContext* context_; |
| 114 IPC::Channel* channel_; | 109 IPC::Channel* channel_; |
| 115 bool set_plugin_info_called_; | 110 bool set_plugin_info_called_; |
| 116 bool expect_fail_; | |
| 117 DISALLOW_COPY_AND_ASSIGN(MockPluginProcessHostClient); | 111 DISALLOW_COPY_AND_ASSIGN(MockPluginProcessHostClient); |
| 118 }; | 112 }; |
| 119 | 113 |
| 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 | |
| 137 class PluginServiceTest : public ContentBrowserTest { | 114 class PluginServiceTest : public ContentBrowserTest { |
| 138 public: | 115 public: |
| 139 PluginServiceTest() {} | 116 PluginServiceTest() {} |
| 140 | 117 |
| 141 ResourceContext* GetResourceContext() { | 118 ResourceContext* GetResourceContext() { |
| 142 return shell()->web_contents()->GetBrowserContext()->GetResourceContext(); | 119 return shell()->web_contents()->GetBrowserContext()->GetResourceContext(); |
| 143 } | 120 } |
| 144 | 121 |
| 145 virtual void SetUpCommandLine(CommandLine* command_line) { | 122 virtual void SetUpCommandLine(CommandLine* command_line) { |
| 146 #ifdef OS_MACOSX | 123 #ifdef OS_MACOSX |
| 147 FilePath browser_directory; | 124 FilePath browser_directory; |
| 148 PathService::Get(base::DIR_MODULE, &browser_directory); | 125 PathService::Get(base::DIR_MODULE, &browser_directory); |
| 149 command_line->AppendSwitchPath(switches::kExtraPluginDir, | 126 command_line->AppendSwitchPath(switches::kExtraPluginDir, |
| 150 browser_directory.AppendASCII("plugins")); | 127 browser_directory.AppendASCII("plugins")); |
| 151 #endif | 128 #endif |
| 152 // TODO(jam): since these plugin tests are running under Chrome, we need to | 129 // TODO(jam): since these plugin tests are running under Chrome, we need to |
| 153 // tell it to disable its security features for old plugins. Once this is | 130 // tell it to disable its security features for old plugins. Once this is |
| 154 // running under content_browsertests, these flags won't be needed. | 131 // running under content_browsertests, these flags won't be needed. |
| 155 // http://crbug.com/90448 | 132 // http://crbug.com/90448 |
| 156 // switches::kAlwaysAuthorizePlugins | 133 // switches::kAlwaysAuthorizePlugins |
| 157 command_line->AppendSwitch("always-authorize-plugins"); | 134 command_line->AppendSwitch("always-authorize-plugins"); |
| 158 } | 135 } |
| 159 }; | 136 }; |
| 160 | 137 |
| 161 // Try to open a channel to the test plugin. Minimal plugin process spawning | 138 // Try to open a channel to the test plugin. Minimal plugin process spawning |
| 162 // test for the PluginService interface. | 139 // test for the PluginService interface. |
| 163 IN_PROC_BROWSER_TEST_F(PluginServiceTest, OpenChannelToPlugin) { | 140 IN_PROC_BROWSER_TEST_F(PluginServiceTest, OpenChannelToPlugin) { |
| 164 if (!webkit::npapi::NPAPIPluginsSupported()) | 141 if (!webkit::npapi::NPAPIPluginsSupported()) |
| 165 return; | 142 return; |
| 166 MockPluginProcessHostClient mock_client(GetResourceContext(), false); | 143 MockPluginProcessHostClient mock_client(GetResourceContext()); |
| 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); | |
| 179 BrowserThread::PostTask( | 144 BrowserThread::PostTask( |
| 180 BrowserThread::IO, FROM_HERE, | 145 BrowserThread::IO, FROM_HERE, |
| 181 base::Bind(&OpenChannel, &mock_client)); | 146 base::Bind(&OpenChannel, &mock_client)); |
| 182 RunMessageLoop(); | 147 RunMessageLoop(); |
| 183 } | 148 } |
| 184 | 149 |
| 185 // A strict mock that fails if any of the methods are called. They shouldn't be | 150 // A strict mock that fails if any of the methods are called. They shouldn't be |
| 186 // called since the request should get canceled before then. | 151 // called since the request should get canceled before then. |
| 187 class MockCanceledPluginServiceClient : public PluginProcessHost::Client { | 152 class MockCanceledPluginServiceClient : public PluginProcessHost::Client { |
| 188 public: | 153 public: |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 BrowserThread::IO, FROM_HERE, | 335 BrowserThread::IO, FROM_HERE, |
| 371 base::Bind(&OpenChannel, &mock_client)); | 336 base::Bind(&OpenChannel, &mock_client)); |
| 372 RunMessageLoop(); | 337 RunMessageLoop(); |
| 373 EXPECT_TRUE(mock_client.get_resource_context_called()); | 338 EXPECT_TRUE(mock_client.get_resource_context_called()); |
| 374 EXPECT_TRUE(mock_client.set_plugin_info_called()); | 339 EXPECT_TRUE(mock_client.set_plugin_info_called()); |
| 375 EXPECT_TRUE(mock_client.on_found_plugin_process_host_called()); | 340 EXPECT_TRUE(mock_client.on_found_plugin_process_host_called()); |
| 376 EXPECT_TRUE(mock_client.on_sent_plugin_channel_request_called()); | 341 EXPECT_TRUE(mock_client.on_sent_plugin_channel_request_called()); |
| 377 } | 342 } |
| 378 | 343 |
| 379 } // namespace content | 344 } // namespace content |
| OLD | NEW |