Chromium Code Reviews| 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_profile.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 plugin_prefs_ = PluginPrefs::GetForTestingProfile(&profile_); |
| 108 plugin_prefs_ = new PluginPrefs(); | |
| 109 plugin_prefs_->SetPrefs(&prefs_); | |
| 110 ChromePluginServiceFilter* filter = | 105 ChromePluginServiceFilter* filter = |
| 111 ChromePluginServiceFilter::GetInstance(); | 106 ChromePluginServiceFilter::GetInstance(); |
| 112 filter->RegisterResourceContext(plugin_prefs_, resource_context); | 107 filter->RegisterResourceContext(plugin_prefs_, resource_context); |
| 113 PluginService::GetInstance()->set_filter(filter); | 108 PluginService::GetInstance()->set_filter(filter); |
| 114 | 109 |
| 115 ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path_)); | 110 ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path_)); |
| 116 | 111 |
| 117 handler_ = new content::DummyResourceHandler(); | 112 handler_ = new content::DummyResourceHandler(); |
| 118 | 113 |
| 114 PluginService::GetInstance()->SetPluginListForTesting(&plugin_list_); | |
|
Bernhard Bauer
2011/11/15 23:00:20
What is going to happen when |plugin_list_| is des
Robert Sesek
2011/11/15 23:02:02
ShadowingAtExitManager will delete the PluginServi
Bernhard Bauer
2011/11/15 23:14:15
Ah, I overlooked that one.
| |
| 119 PluginService::GetInstance()->Init(); | 115 PluginService::GetInstance()->Init(); |
| 120 PluginService::GetInstance()->RefreshPlugins(); | |
| 121 PluginService::GetInstance()->GetPlugins(base::Bind(&QuitMessageLoop)); | |
| 122 MessageLoop::current()->RunAllPending(); | |
| 123 } | 116 } |
| 124 | 117 |
| 125 virtual void TearDown() { | 118 virtual void TearDown() { |
| 126 plugin_prefs_->ShutdownOnUIThread(); | 119 plugin_prefs_->ShutdownOnUIThread(); |
| 127 content::ResourceContext* resource_context = | 120 content::ResourceContext* resource_context = |
| 128 content::MockResourceContext::GetInstance(); | 121 content::MockResourceContext::GetInstance(); |
| 129 net::URLRequestContext* request_context = | 122 net::URLRequestContext* request_context = |
| 130 resource_context->request_context(); | 123 resource_context->request_context(); |
| 131 request_context->set_job_factory(old_factory_); | 124 request_context->set_job_factory(old_factory_); |
| 132 ChromePluginServiceFilter* filter = | 125 ChromePluginServiceFilter* filter = |
| 133 ChromePluginServiceFilter::GetInstance(); | 126 ChromePluginServiceFilter::GetInstance(); |
| 134 filter->UnregisterResourceContext(resource_context); | 127 filter->UnregisterResourceContext(resource_context); |
| 135 PluginService::GetInstance()->set_filter(NULL); | 128 PluginService::GetInstance()->set_filter(NULL); |
| 136 } | 129 } |
| 137 | 130 |
| 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() { | 131 void RegisterPDFPlugin() { |
| 142 webkit::WebPluginInfo info; | 132 webkit::WebPluginInfo info; |
| 143 info.path = pdf_path_; | 133 info.path = pdf_path_; |
| 144 PluginService::GetInstance()->RegisterInternalPlugin(info); | 134 plugin_list_.AddPluginToLoad(info); |
| 145 | |
| 146 PluginService::GetInstance()->RefreshPlugins(); | |
| 147 PluginService::GetInstance()->GetPlugins(base::Bind(&QuitMessageLoop)); | |
| 148 MessageLoop::current()->RunAllPending(); | |
| 149 } | 135 } |
| 150 | 136 |
| 151 void UnregisterPDFPlugin() { | 137 void UnregisterPDFPlugin() { |
| 152 PluginService::GetInstance()->UnregisterInternalPlugin(pdf_path_); | 138 plugin_list_.ClearPluginsToLoad(); |
| 153 | |
| 154 PluginService::GetInstance()->RefreshPlugins(); | |
| 155 PluginService::GetInstance()->GetPlugins(base::Bind(&QuitMessageLoop)); | |
| 156 MessageLoop::current()->RunAllPending(); | |
| 157 } | 139 } |
| 158 | 140 |
| 159 void SetPDFPluginLoadedState(bool want_loaded) { | 141 void SetPDFPluginLoadedState(bool want_loaded) { |
| 160 webkit::WebPluginInfo info; | 142 webkit::WebPluginInfo info; |
| 161 bool is_loaded = PluginService::GetInstance()->GetPluginInfoByPath( | 143 bool is_loaded = PluginService::GetInstance()->GetPluginInfoByPath( |
| 162 pdf_path_, &info); | 144 pdf_path_, &info); |
| 163 if (is_loaded && !want_loaded) { | 145 if (is_loaded && !want_loaded) { |
| 164 UnregisterPDFPlugin(); | 146 UnregisterPDFPlugin(); |
| 165 is_loaded = PluginService::GetInstance()->GetPluginInfoByPath( | 147 is_loaded = PluginService::GetInstance()->GetPluginInfoByPath( |
| 166 pdf_path_, &info); | 148 pdf_path_, &info); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 request->SetUserData(NULL, info); | 182 request->SetUserData(NULL, info); |
| 201 request->set_context(context->request_context()); | 183 request->set_context(context->request_context()); |
| 202 } | 184 } |
| 203 | 185 |
| 204 protected: | 186 protected: |
| 205 base::ShadowingAtExitManager at_exit_manager_; // Deletes PluginService. | 187 base::ShadowingAtExitManager at_exit_manager_; // Deletes PluginService. |
| 206 MessageLoopForIO message_loop_; | 188 MessageLoopForIO message_loop_; |
| 207 content::TestBrowserThread ui_thread_; | 189 content::TestBrowserThread ui_thread_; |
| 208 content::TestBrowserThread file_thread_; | 190 content::TestBrowserThread file_thread_; |
| 209 content::TestBrowserThread io_thread_; | 191 content::TestBrowserThread io_thread_; |
| 210 TestingPrefService prefs_; | 192 webkit::npapi::MockPluginList plugin_list_; |
| 193 TestingProfile profile_; | |
|
Bernhard Bauer
2011/11/15 23:00:20
Why do we need to set up a TestingProfile now inst
Robert Sesek
2011/11/15 23:02:02
Because the NOTIFICATION_PLUGIN_ENABLE_STATUS_CHAN
Bernhard Bauer
2011/11/15 23:14:15
Hm, maybe we could just ignore a NULL source there
Robert Sesek
2011/11/15 23:15:36
Yeah, I wasn't sure if we wanted to do that. It se
| |
| 211 scoped_refptr<PluginPrefs> plugin_prefs_; | 194 scoped_refptr<PluginPrefs> plugin_prefs_; |
| 212 net::URLRequestJobFactory job_factory_; | 195 net::URLRequestJobFactory job_factory_; |
| 213 const net::URLRequestJobFactory* old_factory_; | 196 const net::URLRequestJobFactory* old_factory_; |
| 214 scoped_refptr<ResourceHandler> handler_; | 197 scoped_refptr<ResourceHandler> handler_; |
| 215 TestDelegate test_delegate_; | 198 TestDelegate test_delegate_; |
| 216 FilePath pdf_path_; | 199 FilePath pdf_path_; |
| 217 }; | 200 }; |
| 218 | 201 |
| 219 TEST_F(GViewRequestInterceptorTest, DoNotInterceptHtml) { | 202 TEST_F(GViewRequestInterceptorTest, DoNotInterceptHtml) { |
| 220 net::URLRequest request(GURL(kHtmlUrl), &test_delegate_); | 203 net::URLRequest request(GURL(kHtmlUrl), &test_delegate_); |
| 221 SetupRequest(&request); | 204 SetupRequest(&request); |
| 222 request.Start(); | 205 request.Start(); |
| 223 MessageLoop::current()->Run(); | 206 MessageLoop::current()->Run(); |
| 224 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 207 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 225 EXPECT_EQ(GURL(kHtmlUrl), request.url()); | 208 EXPECT_EQ(GURL(kHtmlUrl), request.url()); |
| 226 } | 209 } |
| 227 | 210 |
| 228 TEST_F(GViewRequestInterceptorTest, DoNotInterceptDownload) { | 211 TEST_F(GViewRequestInterceptorTest, DoNotInterceptDownload) { |
| 229 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); | 212 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
| 230 SetupRequest(&request); | 213 SetupRequest(&request); |
| 231 request.set_load_flags(net::LOAD_IS_DOWNLOAD); | 214 request.set_load_flags(net::LOAD_IS_DOWNLOAD); |
| 232 request.Start(); | 215 request.Start(); |
| 233 MessageLoop::current()->Run(); | 216 MessageLoop::current()->Run(); |
| 234 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 217 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 235 EXPECT_EQ(GURL(kPdfUrl), request.url()); | 218 EXPECT_EQ(GURL(kPdfUrl), request.url()); |
| 236 } | 219 } |
| 237 | 220 |
| 238 TEST_F(GViewRequestInterceptorTest, DISABLED_DoNotInterceptPdfWhenEnabled) { | 221 TEST_F(GViewRequestInterceptorTest, DoNotInterceptPdfWhenEnabled) { |
| 239 SetPDFPluginLoadedState(true); | 222 SetPDFPluginLoadedState(true); |
| 240 plugin_prefs_->EnablePlugin(true, pdf_path_); | 223 plugin_prefs_->EnablePlugin(true, pdf_path_); |
| 241 | 224 |
| 242 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); | 225 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
| 243 SetupRequest(&request); | 226 SetupRequest(&request); |
| 244 request.Start(); | 227 request.Start(); |
| 245 MessageLoop::current()->Run(); | 228 MessageLoop::current()->Run(); |
| 246 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 229 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 247 EXPECT_EQ(GURL(kPdfUrl), request.url()); | 230 EXPECT_EQ(GURL(kPdfUrl), request.url()); |
| 248 } | 231 } |
| 249 | 232 |
| 250 TEST_F(GViewRequestInterceptorTest, DISABLED_InterceptPdfWhenDisabled) { | 233 TEST_F(GViewRequestInterceptorTest, InterceptPdfWhenDisabled) { |
| 251 SetPDFPluginLoadedState(true); | 234 SetPDFPluginLoadedState(true); |
| 252 plugin_prefs_->EnablePlugin(false, pdf_path_); | 235 plugin_prefs_->EnablePlugin(false, pdf_path_); |
| 253 | 236 |
| 254 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); | 237 net::URLRequest request(GURL(kPdfUrl), &test_delegate_); |
| 255 SetupRequest(&request); | 238 SetupRequest(&request); |
| 256 request.Start(); | 239 request.Start(); |
| 257 MessageLoop::current()->Run(); | 240 MessageLoop::current()->Run(); |
| 258 EXPECT_EQ(1, test_delegate_.received_redirect_count()); | 241 EXPECT_EQ(1, test_delegate_.received_redirect_count()); |
| 259 EXPECT_EQ(GURL(kPdfUrlIntercepted), request.url()); | 242 EXPECT_EQ(GURL(kPdfUrlIntercepted), request.url()); |
| 260 } | 243 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 SetupRequest(&request); | 281 SetupRequest(&request); |
| 299 request.Start(); | 282 request.Start(); |
| 300 MessageLoop::current()->Run(); | 283 MessageLoop::current()->Run(); |
| 301 EXPECT_EQ(0, test_delegate_.received_redirect_count()); | 284 EXPECT_EQ(0, test_delegate_.received_redirect_count()); |
| 302 EXPECT_EQ(GURL(kPdfBlob), request.url()); | 285 EXPECT_EQ(GURL(kPdfBlob), request.url()); |
| 303 } | 286 } |
| 304 | 287 |
| 305 } // namespace | 288 } // namespace |
| 306 | 289 |
| 307 } // namespace chromeos | 290 } // namespace chromeos |
| OLD | NEW |