| 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" |
| 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 | 22 |
| 22 namespace content { | 23 namespace content { |
| 23 | 24 |
| 24 const char kNPAPITestPluginMimeType[] = "application/vnd.npapi-test"; | 25 const char kNPAPITestPluginMimeType[] = "application/vnd.npapi-test"; |
| 25 | 26 |
| 26 void OpenChannel(PluginProcessHost::Client* client) { | 27 void OpenChannel(PluginProcessHost::Client* client) { |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 28 // Start opening the channel | 29 // Start opening the channel |
| 29 PluginServiceImpl::GetInstance()->OpenChannelToNpapiPlugin( | 30 PluginServiceImpl::GetInstance()->OpenChannelToNpapiPlugin( |
| 30 0, 0, GURL(), GURL(), kNPAPITestPluginMimeType, client); | 31 0, 0, GURL(), GURL(), kNPAPITestPluginMimeType, client); |
| 31 } | 32 } |
| 32 | 33 |
| 33 // 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 |
| 34 // communication channel with the plugin. | 35 // communication channel with the plugin. |
| 35 class MockPluginProcessHostClient : public PluginProcessHost::Client, | 36 class MockPluginProcessHostClient : public PluginProcessHost::Client, |
| 36 public IPC::Listener { | 37 public IPC::Listener { |
| 37 public: | 38 public: |
| 38 MockPluginProcessHostClient(ResourceContext* context) | 39 MockPluginProcessHostClient(ResourceContext* context, bool expect_fail) |
| 39 : context_(context), | 40 : context_(context), |
| 40 channel_(NULL), | 41 channel_(NULL), |
| 41 set_plugin_info_called_(false) { | 42 set_plugin_info_called_(false), |
| 43 expect_fail_(expect_fail) { |
| 42 } | 44 } |
| 43 | 45 |
| 44 virtual ~MockPluginProcessHostClient() { | 46 virtual ~MockPluginProcessHostClient() { |
| 45 if (channel_) | 47 if (channel_) |
| 46 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, channel_); | 48 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, channel_); |
| 47 } | 49 } |
| 48 | 50 |
| 49 // PluginProcessHost::Client implementation. | 51 // PluginProcessHost::Client implementation. |
| 50 virtual int ID() OVERRIDE { return 42; } | 52 virtual int ID() OVERRIDE { return 42; } |
| 51 virtual bool OffTheRecord() OVERRIDE { return false; } | 53 virtual bool OffTheRecord() OVERRIDE { return false; } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 72 virtual void OnError() OVERRIDE { | 74 virtual void OnError() OVERRIDE { |
| 73 Fail(); | 75 Fail(); |
| 74 } | 76 } |
| 75 | 77 |
| 76 // IPC::Listener implementation. | 78 // IPC::Listener implementation. |
| 77 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 79 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| 78 Fail(); | 80 Fail(); |
| 79 return false; | 81 return false; |
| 80 } | 82 } |
| 81 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE { | 83 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE { |
| 84 if (expect_fail_) |
| 85 FAIL(); |
| 82 QuitMessageLoop(); | 86 QuitMessageLoop(); |
| 83 } | 87 } |
| 84 virtual void OnChannelError() OVERRIDE { | 88 virtual void OnChannelError() OVERRIDE { |
| 85 Fail(); | 89 Fail(); |
| 86 } | 90 } |
| 87 #if defined(OS_POSIX) | 91 #if defined(OS_POSIX) |
| 88 virtual void OnChannelDenied() OVERRIDE { | 92 virtual void OnChannelDenied() OVERRIDE { |
| 89 Fail(); | 93 Fail(); |
| 90 } | 94 } |
| 91 virtual void OnChannelListenError() OVERRIDE { | 95 virtual void OnChannelListenError() OVERRIDE { |
| 92 Fail(); | 96 Fail(); |
| 93 } | 97 } |
| 94 #endif | 98 #endif |
| 95 | 99 |
| 96 private: | 100 private: |
| 97 void Fail() { | 101 void Fail() { |
| 98 FAIL(); | 102 if (!expect_fail_) |
| 103 FAIL(); |
| 99 QuitMessageLoop(); | 104 QuitMessageLoop(); |
| 100 } | 105 } |
| 101 | 106 |
| 102 void QuitMessageLoop() { | 107 void QuitMessageLoop() { |
| 103 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 108 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 104 MessageLoop::QuitClosure()); | 109 MessageLoop::QuitClosure()); |
| 105 } | 110 } |
| 106 | 111 |
| 107 ResourceContext* context_; | 112 ResourceContext* context_; |
| 108 IPC::Channel* channel_; | 113 IPC::Channel* channel_; |
| 109 bool set_plugin_info_called_; | 114 bool set_plugin_info_called_; |
| 115 bool expect_fail_; |
| 110 DISALLOW_COPY_AND_ASSIGN(MockPluginProcessHostClient); | 116 DISALLOW_COPY_AND_ASSIGN(MockPluginProcessHostClient); |
| 111 }; | 117 }; |
| 112 | 118 |
| 119 class MockPluginServiceFilter : public content::PluginServiceFilter { |
| 120 public: |
| 121 MockPluginServiceFilter() {} |
| 122 |
| 123 virtual bool IsPluginEnabled( |
| 124 int render_process_id, |
| 125 int render_view_id, |
| 126 const void* context, |
| 127 const GURL& url, |
| 128 const GURL& policy_url, |
| 129 webkit::WebPluginInfo* plugin) OVERRIDE { return true; } |
| 130 |
| 131 virtual bool CanLoadPlugin( |
| 132 int render_process_id, |
| 133 const FilePath& path) OVERRIDE { return false; } |
| 134 }; |
| 135 |
| 113 class PluginServiceTest : public ContentBrowserTest { | 136 class PluginServiceTest : public ContentBrowserTest { |
| 114 public: | 137 public: |
| 115 PluginServiceTest() {} | 138 PluginServiceTest() {} |
| 116 | 139 |
| 117 ResourceContext* GetResourceContext() { | 140 ResourceContext* GetResourceContext() { |
| 118 return shell()->web_contents()->GetBrowserContext()->GetResourceContext(); | 141 return shell()->web_contents()->GetBrowserContext()->GetResourceContext(); |
| 119 } | 142 } |
| 120 | 143 |
| 121 virtual void SetUpCommandLine(CommandLine* command_line) { | 144 virtual void SetUpCommandLine(CommandLine* command_line) { |
| 122 #ifdef OS_MACOSX | 145 #ifdef OS_MACOSX |
| 123 FilePath browser_directory; | 146 FilePath browser_directory; |
| 124 PathService::Get(base::DIR_MODULE, &browser_directory); | 147 PathService::Get(base::DIR_MODULE, &browser_directory); |
| 125 command_line->AppendSwitchPath(switches::kExtraPluginDir, | 148 command_line->AppendSwitchPath(switches::kExtraPluginDir, |
| 126 browser_directory.AppendASCII("plugins")); | 149 browser_directory.AppendASCII("plugins")); |
| 127 #endif | 150 #endif |
| 128 // TODO(jam): since these plugin tests are running under Chrome, we need to | 151 // TODO(jam): since these plugin tests are running under Chrome, we need to |
| 129 // tell it to disable its security features for old plugins. Once this is | 152 // tell it to disable its security features for old plugins. Once this is |
| 130 // running under content_browsertests, these flags won't be needed. | 153 // running under content_browsertests, these flags won't be needed. |
| 131 // http://crbug.com/90448 | 154 // http://crbug.com/90448 |
| 132 // switches::kAlwaysAuthorizePlugins | 155 // switches::kAlwaysAuthorizePlugins |
| 133 command_line->AppendSwitch("always-authorize-plugins"); | 156 command_line->AppendSwitch("always-authorize-plugins"); |
| 134 } | 157 } |
| 135 }; | 158 }; |
| 136 | 159 |
| 137 // Try to open a channel to the test plugin. Minimal plugin process spawning | 160 // Try to open a channel to the test plugin. Minimal plugin process spawning |
| 138 // test for the PluginService interface. | 161 // test for the PluginService interface. |
| 139 IN_PROC_BROWSER_TEST_F(PluginServiceTest, OpenChannelToPlugin) { | 162 IN_PROC_BROWSER_TEST_F(PluginServiceTest, OpenChannelToPlugin) { |
| 140 MockPluginProcessHostClient mock_client(GetResourceContext()); | 163 MockPluginProcessHostClient mock_client(GetResourceContext(), false); |
| 164 BrowserThread::PostTask( |
| 165 BrowserThread::IO, FROM_HERE, |
| 166 base::Bind(&OpenChannel, &mock_client)); |
| 167 RunMessageLoop(); |
| 168 } |
| 169 |
| 170 IN_PROC_BROWSER_TEST_F(PluginServiceTest, OpenChannelToDeniedPlugin) { |
| 171 MockPluginServiceFilter filter; |
| 172 PluginServiceImpl::GetInstance()->SetFilter(&filter); |
| 173 MockPluginProcessHostClient mock_client(GetResourceContext(), true); |
| 141 BrowserThread::PostTask( | 174 BrowserThread::PostTask( |
| 142 BrowserThread::IO, FROM_HERE, | 175 BrowserThread::IO, FROM_HERE, |
| 143 base::Bind(&OpenChannel, &mock_client)); | 176 base::Bind(&OpenChannel, &mock_client)); |
| 144 RunMessageLoop(); | 177 RunMessageLoop(); |
| 145 } | 178 } |
| 146 | 179 |
| 147 // A strict mock that fails if any of the methods are called. They shouldn't be | 180 // A strict mock that fails if any of the methods are called. They shouldn't be |
| 148 // called since the request should get canceled before then. | 181 // called since the request should get canceled before then. |
| 149 class MockCanceledPluginServiceClient : public PluginProcessHost::Client { | 182 class MockCanceledPluginServiceClient : public PluginProcessHost::Client { |
| 150 public: | 183 public: |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 BrowserThread::IO, FROM_HERE, | 361 BrowserThread::IO, FROM_HERE, |
| 329 base::Bind(&OpenChannel, &mock_client)); | 362 base::Bind(&OpenChannel, &mock_client)); |
| 330 RunMessageLoop(); | 363 RunMessageLoop(); |
| 331 EXPECT_TRUE(mock_client.get_resource_context_called()); | 364 EXPECT_TRUE(mock_client.get_resource_context_called()); |
| 332 EXPECT_TRUE(mock_client.set_plugin_info_called()); | 365 EXPECT_TRUE(mock_client.set_plugin_info_called()); |
| 333 EXPECT_TRUE(mock_client.on_found_plugin_process_host_called()); | 366 EXPECT_TRUE(mock_client.on_found_plugin_process_host_called()); |
| 334 EXPECT_TRUE(mock_client.on_sent_plugin_channel_request_called()); | 367 EXPECT_TRUE(mock_client.on_sent_plugin_channel_request_called()); |
| 335 } | 368 } |
| 336 | 369 |
| 337 } // namespace content | 370 } // namespace content |
| OLD | NEW |