| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/at_exit.h" | 8 #include "base/at_exit.h" |
| 9 #include "base/bind.h" | |
| 10 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 11 #include "chrome/browser/chrome_plugin_service_filter.h" | 10 #include "chrome/browser/chrome_plugin_service_filter.h" |
| 12 #include "chrome/browser/chromeos/gview_request_interceptor.h" | 11 #include "chrome/browser/chromeos/gview_request_interceptor.h" |
| 13 #include "chrome/browser/plugin_prefs.h" | 12 #include "chrome/browser/plugin_prefs.h" |
| 14 #include "chrome/browser/plugin_prefs_factory.h" | 13 #include "chrome/browser/plugin_prefs_factory.h" |
| 15 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/common/chrome_paths.h" |
| 16 #include "chrome/test/base/testing_pref_service.h" | 15 #include "chrome/test/base/testing_pref_service.h" |
| 17 #include "content/browser/mock_resource_context.h" | 16 #include "content/browser/mock_resource_context.h" |
| 18 #include "content/browser/plugin_service.h" | 17 #include "content/browser/plugin_service.h" |
| 19 #include "content/browser/renderer_host/dummy_resource_handler.h" | 18 #include "content/browser/renderer_host/dummy_resource_handler.h" |
| 20 #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" | 19 #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" |
| 21 #include "content/test/test_browser_thread.h" | 20 #include "content/test/test_browser_thread.h" |
| 22 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
| 23 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 24 #include "net/url_request/url_request_job.h" | 23 #include "net/url_request/url_request_job.h" |
| 25 #include "net/url_request/url_request_job_factory.h" | 24 #include "net/url_request/url_request_job_factory.h" |
| 26 #include "net/url_request/url_request_test_job.h" | 25 #include "net/url_request/url_request_test_job.h" |
| 27 #include "net/url_request/url_request_test_util.h" | 26 #include "net/url_request/url_request_test_util.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 #include "webkit/plugins/npapi/mock_plugin_list.h" |
| 29 | 29 |
| 30 using content::BrowserThread; | 30 using content::BrowserThread; |
| 31 | 31 |
| 32 namespace chromeos { | 32 namespace chromeos { |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 const char kPdfUrl[] = "http://foo.com/file.pdf"; | 36 const char kPdfUrl[] = "http://foo.com/file.pdf"; |
| 37 const char kPptUrl[] = "http://foo.com/file.ppt"; | 37 const char kPptUrl[] = "http://foo.com/file.ppt"; |
| 38 const char kHtmlUrl[] = "http://foo.com/index.html"; | 38 const char kHtmlUrl[] = "http://foo.com/index.html"; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 : public net::URLRequestJobFactory::ProtocolHandler { | 77 : public net::URLRequestJobFactory::ProtocolHandler { |
| 78 public: | 78 public: |
| 79 GViewRequestProtocolFactory() {} | 79 GViewRequestProtocolFactory() {} |
| 80 virtual ~GViewRequestProtocolFactory() {} | 80 virtual ~GViewRequestProtocolFactory() {} |
| 81 | 81 |
| 82 virtual net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const { | 82 virtual net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const { |
| 83 return new GViewURLRequestTestJob(request); | 83 return new GViewURLRequestTestJob(request); |
| 84 } | 84 } |
| 85 }; | 85 }; |
| 86 | 86 |
| 87 void QuitMessageLoop(const std::vector<webkit::WebPluginInfo>&) { | |
| 88 MessageLoop::current()->Quit(); | |
| 89 } | |
| 90 | |
| 91 class GViewRequestInterceptorTest : public testing::Test { | 87 class GViewRequestInterceptorTest : public testing::Test { |
| 92 public: | 88 public: |
| 93 GViewRequestInterceptorTest() | 89 GViewRequestInterceptorTest() |
| 94 : ui_thread_(BrowserThread::UI, &message_loop_), | 90 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 95 file_thread_(BrowserThread::FILE, &message_loop_), | 91 file_thread_(BrowserThread::FILE, &message_loop_), |
| 96 io_thread_(BrowserThread::IO, &message_loop_) {} | 92 io_thread_(BrowserThread::IO, &message_loop_), |
| 93 plugin_list_(NULL, 0) {} |
| 97 | 94 |
| 98 virtual void SetUp() { | 95 virtual void SetUp() { |
| 99 content::ResourceContext* resource_context = | 96 content::ResourceContext* resource_context = |
| 100 content::MockResourceContext::GetInstance(); | 97 content::MockResourceContext::GetInstance(); |
| 101 net::URLRequestContext* request_context = | 98 net::URLRequestContext* request_context = |
| 102 resource_context->request_context(); | 99 resource_context->request_context(); |
| 103 old_factory_ = request_context->job_factory(); | 100 old_factory_ = request_context->job_factory(); |
| 104 job_factory_.SetProtocolHandler("http", new GViewRequestProtocolFactory); | 101 job_factory_.SetProtocolHandler("http", new GViewRequestProtocolFactory); |
| 105 job_factory_.AddInterceptor(new GViewRequestInterceptor); | 102 job_factory_.AddInterceptor(new GViewRequestInterceptor); |
| 106 request_context->set_job_factory(&job_factory_); | 103 request_context->set_job_factory(&job_factory_); |
| 107 PluginPrefsFactory::GetInstance()->ForceRegisterPrefsForTest(&prefs_); | 104 PluginPrefsFactory::GetInstance()->ForceRegisterPrefsForTest(&prefs_); |
| 108 plugin_prefs_ = new PluginPrefs(); | 105 plugin_prefs_ = new PluginPrefs(); |
| 109 plugin_prefs_->SetPrefs(&prefs_); | 106 plugin_prefs_->SetPrefs(&prefs_); |
| 110 ChromePluginServiceFilter* filter = | 107 ChromePluginServiceFilter* filter = |
| 111 ChromePluginServiceFilter::GetInstance(); | 108 ChromePluginServiceFilter::GetInstance(); |
| 112 filter->RegisterResourceContext(plugin_prefs_, resource_context); | 109 filter->RegisterResourceContext(plugin_prefs_, resource_context); |
| 113 PluginService::GetInstance()->set_filter(filter); | 110 PluginService::GetInstance()->set_filter(filter); |
| 114 | 111 |
| 115 ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path_)); | 112 ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path_)); |
| 116 | 113 |
| 117 handler_ = new content::DummyResourceHandler(); | 114 handler_ = new content::DummyResourceHandler(); |
| 118 | 115 |
| 116 PluginService::GetInstance()->SetPluginListForTesting(&plugin_list_); |
| 119 PluginService::GetInstance()->Init(); | 117 PluginService::GetInstance()->Init(); |
| 120 PluginService::GetInstance()->RefreshPlugins(); | |
| 121 PluginService::GetInstance()->GetPlugins(base::Bind(&QuitMessageLoop)); | |
| 122 MessageLoop::current()->RunAllPending(); | |
| 123 } | 118 } |
| 124 | 119 |
| 125 virtual void TearDown() { | 120 virtual void TearDown() { |
| 126 plugin_prefs_->ShutdownOnUIThread(); | 121 plugin_prefs_->ShutdownOnUIThread(); |
| 127 content::ResourceContext* resource_context = | 122 content::ResourceContext* resource_context = |
| 128 content::MockResourceContext::GetInstance(); | 123 content::MockResourceContext::GetInstance(); |
| 129 net::URLRequestContext* request_context = | 124 net::URLRequestContext* request_context = |
| 130 resource_context->request_context(); | 125 resource_context->request_context(); |
| 131 request_context->set_job_factory(old_factory_); | 126 request_context->set_job_factory(old_factory_); |
| 132 ChromePluginServiceFilter* filter = | 127 ChromePluginServiceFilter* filter = |
| 133 ChromePluginServiceFilter::GetInstance(); | 128 ChromePluginServiceFilter::GetInstance(); |
| 134 filter->UnregisterResourceContext(resource_context); | 129 filter->UnregisterResourceContext(resource_context); |
| 135 PluginService::GetInstance()->set_filter(NULL); | 130 PluginService::GetInstance()->set_filter(NULL); |
| 136 } | 131 } |
| 137 | 132 |
| 138 // GetPluginInfoByPath() will only use stale information. Because plugin | |
| 139 // refresh is asynchronous, spin a MessageLoop until the callback is run, | |
| 140 // after which, the test will continue. | |
| 141 void RegisterPDFPlugin() { | 133 void RegisterPDFPlugin() { |
| 142 webkit::WebPluginInfo info; | 134 webkit::WebPluginInfo info; |
| 143 info.path = pdf_path_; | 135 info.path = pdf_path_; |
| 144 PluginService::GetInstance()->RegisterInternalPlugin(info); | 136 plugin_list_.AddPluginToLoad(info); |
| 145 | |
| 146 PluginService::GetInstance()->RefreshPlugins(); | |
| 147 PluginService::GetInstance()->GetPlugins(base::Bind(&QuitMessageLoop)); | |
| 148 MessageLoop::current()->RunAllPending(); | |
| 149 } | 137 } |
| 150 | 138 |
| 151 void UnregisterPDFPlugin() { | 139 void UnregisterPDFPlugin() { |
| 152 PluginService::GetInstance()->UnregisterInternalPlugin(pdf_path_); | 140 plugin_list_.ClearPluginsToLoad(); |
| 153 | |
| 154 PluginService::GetInstance()->RefreshPlugins(); | |
| 155 PluginService::GetInstance()->GetPlugins(base::Bind(&QuitMessageLoop)); | |
| 156 MessageLoop::current()->RunAllPending(); | |
| 157 } | 141 } |
| 158 | 142 |
| 159 void SetPDFPluginLoadedState(bool want_loaded) { | 143 void SetPDFPluginLoadedState(bool want_loaded) { |
| 160 webkit::WebPluginInfo info; | 144 webkit::WebPluginInfo info; |
| 161 bool is_loaded = PluginService::GetInstance()->GetPluginInfoByPath( | 145 bool is_loaded = PluginService::GetInstance()->GetPluginInfoByPath( |
| 162 pdf_path_, &info); | 146 pdf_path_, &info); |
| 163 if (is_loaded && !want_loaded) { | 147 if (is_loaded && !want_loaded) { |
| 164 UnregisterPDFPlugin(); | 148 UnregisterPDFPlugin(); |
| 165 is_loaded = PluginService::GetInstance()->GetPluginInfoByPath( | 149 is_loaded = PluginService::GetInstance()->GetPluginInfoByPath( |
| 166 pdf_path_, &info); | 150 pdf_path_, &info); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 request->SetUserData(NULL, info); | 184 request->SetUserData(NULL, info); |
| 201 request->set_context(context->request_context()); | 185 request->set_context(context->request_context()); |
| 202 } | 186 } |
| 203 | 187 |
| 204 protected: | 188 protected: |
| 205 base::ShadowingAtExitManager at_exit_manager_; // Deletes PluginService. | 189 base::ShadowingAtExitManager at_exit_manager_; // Deletes PluginService. |
| 206 MessageLoopForIO message_loop_; | 190 MessageLoopForIO message_loop_; |
| 207 content::TestBrowserThread ui_thread_; | 191 content::TestBrowserThread ui_thread_; |
| 208 content::TestBrowserThread file_thread_; | 192 content::TestBrowserThread file_thread_; |
| 209 content::TestBrowserThread io_thread_; | 193 content::TestBrowserThread io_thread_; |
| 194 webkit::npapi::MockPluginList plugin_list_; |
| 210 TestingPrefService prefs_; | 195 TestingPrefService prefs_; |
| 211 scoped_refptr<PluginPrefs> plugin_prefs_; | 196 scoped_refptr<PluginPrefs> plugin_prefs_; |
| 212 net::URLRequestJobFactory job_factory_; | 197 net::URLRequestJobFactory job_factory_; |
| 213 const net::URLRequestJobFactory* old_factory_; | 198 const net::URLRequestJobFactory* old_factory_; |
| 214 scoped_refptr<ResourceHandler> handler_; | 199 scoped_refptr<ResourceHandler> handler_; |
| 215 TestDelegate test_delegate_; | 200 TestDelegate test_delegate_; |
| 216 FilePath pdf_path_; | 201 FilePath pdf_path_; |
| 217 }; | 202 }; |
| 218 | 203 |
| 219 TEST_F(GViewRequestInterceptorTest, DoNotInterceptHtml) { | 204 TEST_F(GViewRequestInterceptorTest, DoNotInterceptHtml) { |
| 220 net::URLRequest request(GURL(kHtmlUrl), &test_delegate_); | 205 net::URLRequest request(GURL(kHtmlUrl), &test_delegate_); |
| 221 SetupRequest(&request); | 206 SetupRequest(&request); |
| 222 request.Start(); | 207 request.Start(); |
| 223 MessageLoop::current()->Run(); | 208 MessageLoop::current()->Run(); |
| 224 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 209 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 225 EXPECT_EQ(GURL(kHtmlUrl), request.url()); | 210 EXPECT_EQ(GURL(kHtmlUrl), request.url()); |
| 226 } | 211 } |
| 227 | 212 |
| 228 TEST_F(GViewRequestInterceptorTest, DoNotInterceptDownload) { | 213 TEST_F(GViewRequestInterceptorTest, DoNotInterceptDownload) { |
| 229 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); | 214 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
| 230 SetupRequest(&request); | 215 SetupRequest(&request); |
| 231 request.set_load_flags(net::LOAD_IS_DOWNLOAD); | 216 request.set_load_flags(net::LOAD_IS_DOWNLOAD); |
| 232 request.Start(); | 217 request.Start(); |
| 233 MessageLoop::current()->Run(); | 218 MessageLoop::current()->Run(); |
| 234 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 219 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 235 EXPECT_EQ(GURL(kPdfUrl), request.url()); | 220 EXPECT_EQ(GURL(kPdfUrl), request.url()); |
| 236 } | 221 } |
| 237 | 222 |
| 238 TEST_F(GViewRequestInterceptorTest, DISABLED_DoNotInterceptPdfWhenEnabled) { | 223 TEST_F(GViewRequestInterceptorTest, DoNotInterceptPdfWhenEnabled) { |
| 239 SetPDFPluginLoadedState(true); | 224 SetPDFPluginLoadedState(true); |
| 240 plugin_prefs_->EnablePlugin(true, pdf_path_); | 225 plugin_prefs_->EnablePlugin(true, pdf_path_); |
| 241 | 226 |
| 242 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); | 227 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
| 243 SetupRequest(&request); | 228 SetupRequest(&request); |
| 244 request.Start(); | 229 request.Start(); |
| 245 MessageLoop::current()->Run(); | 230 MessageLoop::current()->Run(); |
| 246 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 231 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 247 EXPECT_EQ(GURL(kPdfUrl), request.url()); | 232 EXPECT_EQ(GURL(kPdfUrl), request.url()); |
| 248 } | 233 } |
| 249 | 234 |
| 250 TEST_F(GViewRequestInterceptorTest, DISABLED_InterceptPdfWhenDisabled) { | 235 TEST_F(GViewRequestInterceptorTest, InterceptPdfWhenDisabled) { |
| 251 SetPDFPluginLoadedState(true); | 236 SetPDFPluginLoadedState(true); |
| 252 plugin_prefs_->EnablePlugin(false, pdf_path_); | 237 plugin_prefs_->EnablePlugin(false, pdf_path_); |
| 253 | 238 |
| 254 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); | 239 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
| 255 SetupRequest(&request); | 240 SetupRequest(&request); |
| 256 request.Start(); | 241 request.Start(); |
| 257 MessageLoop::current()->Run(); | 242 MessageLoop::current()->Run(); |
| 258 EXPECT_EQ(1, test_delegate_.received_redirect_count()); | 243 EXPECT_EQ(1, test_delegate_.received_redirect_count()); |
| 259 EXPECT_EQ(GURL(kPdfUrlIntercepted), request.url()); | 244 EXPECT_EQ(GURL(kPdfUrlIntercepted), request.url()); |
| 260 } | 245 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 SetupRequest(&request); | 283 SetupRequest(&request); |
| 299 request.Start(); | 284 request.Start(); |
| 300 MessageLoop::current()->Run(); | 285 MessageLoop::current()->Run(); |
| 301 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 286 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 302 EXPECT_EQ(GURL(kPdfBlob), request.url()); | 287 EXPECT_EQ(GURL(kPdfBlob), request.url()); |
| 303 } | 288 } |
| 304 | 289 |
| 305 } // namespace | 290 } // namespace |
| 306 | 291 |
| 307 } // namespace chromeos | 292 } // namespace chromeos |
| OLD | NEW |