| 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 <map> | 5 #include <map> |
| 6 #include <queue> | 6 #include <queue> |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 13 #include "base/json/json_string_value_serializer.h" | 13 #include "base/json/json_string_value_serializer.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 15 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 16 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 17 #include "base/prefs/pref_member.h" | 18 #include "base/prefs/pref_member.h" |
| 18 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 19 #include "base/strings/string_piece.h" | 20 #include "base/strings/string_piece.h" |
| 20 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 23 #include "chrome/browser/content_settings/cookie_settings.h" | 24 #include "chrome/browser/content_settings/cookie_settings.h" |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 ipc_sender_factory.GetWeakPtr()); | 232 ipc_sender_factory.GetWeakPtr()); |
| 232 | 233 |
| 233 net::URLRequestJobFactoryImpl job_factory; | 234 net::URLRequestJobFactoryImpl job_factory; |
| 234 job_factory.SetProtocolHandler( | 235 job_factory.SetProtocolHandler( |
| 235 url::kAboutScheme, new chrome_browser_net::AboutProtocolHandler()); | 236 url::kAboutScheme, new chrome_browser_net::AboutProtocolHandler()); |
| 236 context_->set_job_factory(&job_factory); | 237 context_->set_job_factory(&job_factory); |
| 237 | 238 |
| 238 GURL redirect_url("about:redirected"); | 239 GURL redirect_url("about:redirected"); |
| 239 GURL not_chosen_redirect_url("about:not_chosen"); | 240 GURL not_chosen_redirect_url("about:not_chosen"); |
| 240 | 241 |
| 241 net::URLRequest request( | 242 scoped_ptr<net::URLRequest> request(context_->CreateRequest( |
| 242 GURL("about:blank"), net::DEFAULT_PRIORITY, &delegate_, context_.get()); | 243 GURL("about:blank"), net::DEFAULT_PRIORITY, &delegate_, NULL)); |
| 243 { | 244 { |
| 244 // onBeforeRequest will be dispatched twice initially. The second response - | 245 // onBeforeRequest will be dispatched twice initially. The second response - |
| 245 // the redirect - should win, since it has a later |install_time|. The | 246 // the redirect - should win, since it has a later |install_time|. The |
| 246 // redirect will dispatch another pair of onBeforeRequest. There, the first | 247 // redirect will dispatch another pair of onBeforeRequest. There, the first |
| 247 // response should win (later |install_time|). | 248 // response should win (later |install_time|). |
| 248 ExtensionWebRequestEventRouter::EventResponse* response = NULL; | 249 ExtensionWebRequestEventRouter::EventResponse* response = NULL; |
| 249 | 250 |
| 250 // Extension1 response. Arrives first, but ignored due to install_time. | 251 // Extension1 response. Arrives first, but ignored due to install_time. |
| 251 response = new ExtensionWebRequestEventRouter::EventResponse( | 252 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 252 extension1_id, base::Time::FromDoubleT(1)); | 253 extension1_id, base::Time::FromDoubleT(1)); |
| 253 response->new_url = not_chosen_redirect_url; | 254 response->new_url = not_chosen_redirect_url; |
| 254 ipc_sender_.PushTask( | 255 ipc_sender_.PushTask( |
| 255 base::Bind(&EventHandledOnIOThread, | 256 base::Bind(&EventHandledOnIOThread, |
| 256 &profile_, extension1_id, kEventName, kEventName + "/1", | 257 &profile_, extension1_id, kEventName, kEventName + "/1", |
| 257 request.identifier(), response)); | 258 request->identifier(), response)); |
| 258 | 259 |
| 259 // Extension2 response. Arrives second, and chosen because of install_time. | 260 // Extension2 response. Arrives second, and chosen because of install_time. |
| 260 response = new ExtensionWebRequestEventRouter::EventResponse( | 261 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 261 extension2_id, base::Time::FromDoubleT(2)); | 262 extension2_id, base::Time::FromDoubleT(2)); |
| 262 response->new_url = redirect_url; | 263 response->new_url = redirect_url; |
| 263 ipc_sender_.PushTask( | 264 ipc_sender_.PushTask( |
| 264 base::Bind(&EventHandledOnIOThread, | 265 base::Bind(&EventHandledOnIOThread, |
| 265 &profile_, extension2_id, kEventName, kEventName + "/2", | 266 &profile_, extension2_id, kEventName, kEventName + "/2", |
| 266 request.identifier(), response)); | 267 request->identifier(), response)); |
| 267 | 268 |
| 268 // Extension2 response to the redirected URL. Arrives first, and chosen. | 269 // Extension2 response to the redirected URL. Arrives first, and chosen. |
| 269 response = new ExtensionWebRequestEventRouter::EventResponse( | 270 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 270 extension2_id, base::Time::FromDoubleT(2)); | 271 extension2_id, base::Time::FromDoubleT(2)); |
| 271 ipc_sender_.PushTask( | 272 ipc_sender_.PushTask( |
| 272 base::Bind(&EventHandledOnIOThread, | 273 base::Bind(&EventHandledOnIOThread, |
| 273 &profile_, extension2_id, kEventName, kEventName + "/2", | 274 &profile_, extension2_id, kEventName, kEventName + "/2", |
| 274 request.identifier(), response)); | 275 request->identifier(), response)); |
| 275 | 276 |
| 276 // Extension1 response to the redirected URL. Arrives second, and ignored. | 277 // Extension1 response to the redirected URL. Arrives second, and ignored. |
| 277 response = new ExtensionWebRequestEventRouter::EventResponse( | 278 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 278 extension1_id, base::Time::FromDoubleT(1)); | 279 extension1_id, base::Time::FromDoubleT(1)); |
| 279 ipc_sender_.PushTask( | 280 ipc_sender_.PushTask( |
| 280 base::Bind(&EventHandledOnIOThread, | 281 base::Bind(&EventHandledOnIOThread, |
| 281 &profile_, extension1_id, kEventName, kEventName + "/1", | 282 &profile_, extension1_id, kEventName, kEventName + "/1", |
| 282 request.identifier(), response)); | 283 request->identifier(), response)); |
| 283 | 284 |
| 284 request.Start(); | 285 request->Start(); |
| 285 base::MessageLoop::current()->Run(); | 286 base::MessageLoop::current()->Run(); |
| 286 | 287 |
| 287 EXPECT_TRUE(!request.is_pending()); | 288 EXPECT_TRUE(!request->is_pending()); |
| 288 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 289 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
| 289 EXPECT_EQ(0, request.status().error()); | 290 EXPECT_EQ(0, request->status().error()); |
| 290 EXPECT_EQ(redirect_url, request.url()); | 291 EXPECT_EQ(redirect_url, request->url()); |
| 291 EXPECT_EQ(2U, request.url_chain().size()); | 292 EXPECT_EQ(2U, request->url_chain().size()); |
| 292 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); | 293 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); |
| 293 } | 294 } |
| 294 | 295 |
| 295 // Now test the same thing but the extensions answer in reverse order. | 296 // Now test the same thing but the extensions answer in reverse order. |
| 296 net::URLRequest request2( | 297 scoped_ptr<net::URLRequest> request2(context_->CreateRequest( |
| 297 GURL("about:blank"), net::DEFAULT_PRIORITY, &delegate_, context_.get()); | 298 GURL("about:blank"), net::DEFAULT_PRIORITY, &delegate_, NULL)); |
| 298 { | 299 { |
| 299 ExtensionWebRequestEventRouter::EventResponse* response = NULL; | 300 ExtensionWebRequestEventRouter::EventResponse* response = NULL; |
| 300 | 301 |
| 301 // Extension2 response. Arrives first, and chosen because of install_time. | 302 // Extension2 response. Arrives first, and chosen because of install_time. |
| 302 response = new ExtensionWebRequestEventRouter::EventResponse( | 303 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 303 extension2_id, base::Time::FromDoubleT(2)); | 304 extension2_id, base::Time::FromDoubleT(2)); |
| 304 response->new_url = redirect_url; | 305 response->new_url = redirect_url; |
| 305 ipc_sender_.PushTask( | 306 ipc_sender_.PushTask( |
| 306 base::Bind(&EventHandledOnIOThread, | 307 base::Bind(&EventHandledOnIOThread, |
| 307 &profile_, extension2_id, kEventName, kEventName + "/2", | 308 &profile_, extension2_id, kEventName, kEventName + "/2", |
| 308 request2.identifier(), response)); | 309 request2->identifier(), response)); |
| 309 | 310 |
| 310 // Extension1 response. Arrives second, but ignored due to install_time. | 311 // Extension1 response. Arrives second, but ignored due to install_time. |
| 311 response = new ExtensionWebRequestEventRouter::EventResponse( | 312 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 312 extension1_id, base::Time::FromDoubleT(1)); | 313 extension1_id, base::Time::FromDoubleT(1)); |
| 313 response->new_url = not_chosen_redirect_url; | 314 response->new_url = not_chosen_redirect_url; |
| 314 ipc_sender_.PushTask( | 315 ipc_sender_.PushTask( |
| 315 base::Bind(&EventHandledOnIOThread, | 316 base::Bind(&EventHandledOnIOThread, |
| 316 &profile_, extension1_id, kEventName, kEventName + "/1", | 317 &profile_, extension1_id, kEventName, kEventName + "/1", |
| 317 request2.identifier(), response)); | 318 request2->identifier(), response)); |
| 318 | 319 |
| 319 // Extension2 response to the redirected URL. Arrives first, and chosen. | 320 // Extension2 response to the redirected URL. Arrives first, and chosen. |
| 320 response = new ExtensionWebRequestEventRouter::EventResponse( | 321 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 321 extension2_id, base::Time::FromDoubleT(2)); | 322 extension2_id, base::Time::FromDoubleT(2)); |
| 322 ipc_sender_.PushTask( | 323 ipc_sender_.PushTask( |
| 323 base::Bind(&EventHandledOnIOThread, | 324 base::Bind(&EventHandledOnIOThread, |
| 324 &profile_, extension2_id, kEventName, kEventName + "/2", | 325 &profile_, extension2_id, kEventName, kEventName + "/2", |
| 325 request2.identifier(), response)); | 326 request2->identifier(), response)); |
| 326 | 327 |
| 327 // Extension1 response to the redirected URL. Arrives second, and ignored. | 328 // Extension1 response to the redirected URL. Arrives second, and ignored. |
| 328 response = new ExtensionWebRequestEventRouter::EventResponse( | 329 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 329 extension1_id, base::Time::FromDoubleT(1)); | 330 extension1_id, base::Time::FromDoubleT(1)); |
| 330 ipc_sender_.PushTask( | 331 ipc_sender_.PushTask( |
| 331 base::Bind(&EventHandledOnIOThread, | 332 base::Bind(&EventHandledOnIOThread, |
| 332 &profile_, extension1_id, kEventName, kEventName + "/1", | 333 &profile_, extension1_id, kEventName, kEventName + "/1", |
| 333 request2.identifier(), response)); | 334 request2->identifier(), response)); |
| 334 | 335 |
| 335 request2.Start(); | 336 request2->Start(); |
| 336 base::MessageLoop::current()->Run(); | 337 base::MessageLoop::current()->Run(); |
| 337 | 338 |
| 338 EXPECT_TRUE(!request2.is_pending()); | 339 EXPECT_TRUE(!request2->is_pending()); |
| 339 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request2.status().status()); | 340 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request2->status().status()); |
| 340 EXPECT_EQ(0, request2.status().error()); | 341 EXPECT_EQ(0, request2->status().error()); |
| 341 EXPECT_EQ(redirect_url, request2.url()); | 342 EXPECT_EQ(redirect_url, request2->url()); |
| 342 EXPECT_EQ(2U, request2.url_chain().size()); | 343 EXPECT_EQ(2U, request2->url_chain().size()); |
| 343 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); | 344 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); |
| 344 } | 345 } |
| 345 | 346 |
| 346 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 347 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( |
| 347 &profile_, extension1_id, kEventName + "/1"); | 348 &profile_, extension1_id, kEventName + "/1"); |
| 348 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 349 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( |
| 349 &profile_, extension2_id, kEventName + "/2"); | 350 &profile_, extension2_id, kEventName + "/2"); |
| 350 } | 351 } |
| 351 | 352 |
| 352 // Test that a request is canceled if this is requested by any extension | 353 // Test that a request is canceled if this is requested by any extension |
| 353 // regardless whether it is the extension with the highest precedence. | 354 // regardless whether it is the extension with the highest precedence. |
| 354 TEST_F(ExtensionWebRequestTest, BlockingEventPrecedenceCancel) { | 355 TEST_F(ExtensionWebRequestTest, BlockingEventPrecedenceCancel) { |
| 355 std::string extension1_id("1"); | 356 std::string extension1_id("1"); |
| 356 std::string extension2_id("2"); | 357 std::string extension2_id("2"); |
| 357 ExtensionWebRequestEventRouter::RequestFilter filter; | 358 ExtensionWebRequestEventRouter::RequestFilter filter; |
| 358 const std::string kEventName(web_request::OnBeforeRequest::kEventName); | 359 const std::string kEventName(web_request::OnBeforeRequest::kEventName); |
| 359 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); | 360 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); |
| 360 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 361 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 361 &profile_, extension1_id, extension1_id, kEventName, kEventName + "/1", | 362 &profile_, extension1_id, extension1_id, kEventName, kEventName + "/1", |
| 362 filter, ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, -1, -1, | 363 filter, ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, -1, -1, |
| 363 ipc_sender_factory.GetWeakPtr()); | 364 ipc_sender_factory.GetWeakPtr()); |
| 364 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 365 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 365 &profile_, extension2_id, extension2_id, kEventName, kEventName + "/2", | 366 &profile_, extension2_id, extension2_id, kEventName, kEventName + "/2", |
| 366 filter, ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, -1, -1, | 367 filter, ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, -1, -1, |
| 367 ipc_sender_factory.GetWeakPtr()); | 368 ipc_sender_factory.GetWeakPtr()); |
| 368 | 369 |
| 369 GURL request_url("about:blank"); | 370 GURL request_url("about:blank"); |
| 370 net::URLRequest request( | 371 scoped_ptr<net::URLRequest> request(context_->CreateRequest( |
| 371 request_url, net::DEFAULT_PRIORITY, &delegate_, context_.get()); | 372 request_url, net::DEFAULT_PRIORITY, &delegate_, NULL)); |
| 372 | 373 |
| 373 // onBeforeRequest will be dispatched twice. The second response - | 374 // onBeforeRequest will be dispatched twice. The second response - |
| 374 // the redirect - would win, since it has a later |install_time|, but | 375 // the redirect - would win, since it has a later |install_time|, but |
| 375 // the first response takes precedence because cancel >> redirect. | 376 // the first response takes precedence because cancel >> redirect. |
| 376 GURL redirect_url("about:redirected"); | 377 GURL redirect_url("about:redirected"); |
| 377 ExtensionWebRequestEventRouter::EventResponse* response = NULL; | 378 ExtensionWebRequestEventRouter::EventResponse* response = NULL; |
| 378 | 379 |
| 379 // Extension1 response. Arrives first, would be ignored in principle due to | 380 // Extension1 response. Arrives first, would be ignored in principle due to |
| 380 // install_time but "cancel" always wins. | 381 // install_time but "cancel" always wins. |
| 381 response = new ExtensionWebRequestEventRouter::EventResponse( | 382 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 382 extension1_id, base::Time::FromDoubleT(1)); | 383 extension1_id, base::Time::FromDoubleT(1)); |
| 383 response->cancel = true; | 384 response->cancel = true; |
| 384 ipc_sender_.PushTask( | 385 ipc_sender_.PushTask( |
| 385 base::Bind(&EventHandledOnIOThread, | 386 base::Bind(&EventHandledOnIOThread, |
| 386 &profile_, extension1_id, kEventName, kEventName + "/1", | 387 &profile_, extension1_id, kEventName, kEventName + "/1", |
| 387 request.identifier(), response)); | 388 request->identifier(), response)); |
| 388 | 389 |
| 389 // Extension2 response. Arrives second, but has higher precedence | 390 // Extension2 response. Arrives second, but has higher precedence |
| 390 // due to its later install_time. | 391 // due to its later install_time. |
| 391 response = new ExtensionWebRequestEventRouter::EventResponse( | 392 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 392 extension2_id, base::Time::FromDoubleT(2)); | 393 extension2_id, base::Time::FromDoubleT(2)); |
| 393 response->new_url = redirect_url; | 394 response->new_url = redirect_url; |
| 394 ipc_sender_.PushTask( | 395 ipc_sender_.PushTask( |
| 395 base::Bind(&EventHandledOnIOThread, | 396 base::Bind(&EventHandledOnIOThread, |
| 396 &profile_, extension2_id, kEventName, kEventName + "/2", | 397 &profile_, extension2_id, kEventName, kEventName + "/2", |
| 397 request.identifier(), response)); | 398 request->identifier(), response)); |
| 398 | 399 |
| 399 request.Start(); | 400 request->Start(); |
| 400 | 401 |
| 401 base::MessageLoop::current()->Run(); | 402 base::MessageLoop::current()->Run(); |
| 402 | 403 |
| 403 EXPECT_TRUE(!request.is_pending()); | 404 EXPECT_TRUE(!request->is_pending()); |
| 404 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 405 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
| 405 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, request.status().error()); | 406 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, request->status().error()); |
| 406 EXPECT_EQ(request_url, request.url()); | 407 EXPECT_EQ(request_url, request->url()); |
| 407 EXPECT_EQ(1U, request.url_chain().size()); | 408 EXPECT_EQ(1U, request->url_chain().size()); |
| 408 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); | 409 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); |
| 409 | 410 |
| 410 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 411 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( |
| 411 &profile_, extension1_id, kEventName + "/1"); | 412 &profile_, extension1_id, kEventName + "/1"); |
| 412 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 413 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( |
| 413 &profile_, extension2_id, kEventName + "/2"); | 414 &profile_, extension2_id, kEventName + "/2"); |
| 414 } | 415 } |
| 415 | 416 |
| 416 TEST_F(ExtensionWebRequestTest, SimulateChancelWhileBlocked) { | 417 TEST_F(ExtensionWebRequestTest, SimulateChancelWhileBlocked) { |
| 417 // We subscribe to OnBeforeRequest and OnErrorOccurred. | 418 // We subscribe to OnBeforeRequest and OnErrorOccurred. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 428 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); | 429 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); |
| 429 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 430 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 430 &profile_, extension_id, extension_id, kEventName, kEventName + "/1", | 431 &profile_, extension_id, extension_id, kEventName, kEventName + "/1", |
| 431 filter, ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, -1, -1, | 432 filter, ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, -1, -1, |
| 432 ipc_sender_factory.GetWeakPtr()); | 433 ipc_sender_factory.GetWeakPtr()); |
| 433 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 434 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 434 &profile_, extension_id, extension_id, kEventName2, kEventName2 + "/1", | 435 &profile_, extension_id, extension_id, kEventName2, kEventName2 + "/1", |
| 435 filter, 0, -1, -1, ipc_sender_factory.GetWeakPtr()); | 436 filter, 0, -1, -1, ipc_sender_factory.GetWeakPtr()); |
| 436 | 437 |
| 437 GURL request_url("about:blank"); | 438 GURL request_url("about:blank"); |
| 438 net::URLRequest request( | 439 scoped_ptr<net::URLRequest> request(context_->CreateRequest( |
| 439 request_url, net::DEFAULT_PRIORITY, &delegate_, context_.get()); | 440 request_url, net::DEFAULT_PRIORITY, &delegate_, NULL)); |
| 440 | 441 |
| 441 ExtensionWebRequestEventRouter::EventResponse* response = NULL; | 442 ExtensionWebRequestEventRouter::EventResponse* response = NULL; |
| 442 | 443 |
| 443 // Extension response for the OnBeforeRequest handler. This should not be | 444 // Extension response for the OnBeforeRequest handler. This should not be |
| 444 // processed because request is canceled before the handler responds. | 445 // processed because request is canceled before the handler responds. |
| 445 response = new ExtensionWebRequestEventRouter::EventResponse( | 446 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 446 extension_id, base::Time::FromDoubleT(1)); | 447 extension_id, base::Time::FromDoubleT(1)); |
| 447 GURL redirect_url("about:redirected"); | 448 GURL redirect_url("about:redirected"); |
| 448 response->new_url = redirect_url; | 449 response->new_url = redirect_url; |
| 449 ipc_sender_.PushTask( | 450 ipc_sender_.PushTask( |
| 450 base::Bind(&EventHandledOnIOThread, | 451 base::Bind(&EventHandledOnIOThread, |
| 451 &profile_, extension_id, kEventName, kEventName + "/1", | 452 &profile_, extension_id, kEventName, kEventName + "/1", |
| 452 request.identifier(), response)); | 453 request->identifier(), response)); |
| 453 | 454 |
| 454 // Extension response for OnErrorOccurred: Terminate the message loop. | 455 // Extension response for OnErrorOccurred: Terminate the message loop. |
| 455 ipc_sender_.PushTask( | 456 ipc_sender_.PushTask( |
| 456 base::Bind(&base::MessageLoop::PostTask, | 457 base::Bind(&base::MessageLoop::PostTask, |
| 457 base::Unretained(base::MessageLoop::current()), | 458 base::Unretained(base::MessageLoop::current()), |
| 458 FROM_HERE, base::MessageLoop::QuitClosure())); | 459 FROM_HERE, base::MessageLoop::QuitClosure())); |
| 459 | 460 |
| 460 request.Start(); | 461 request->Start(); |
| 461 // request.Start() will have submitted OnBeforeRequest by the time we cancel. | 462 // request->Start() will have submitted OnBeforeRequest by the time we cancel. |
| 462 request.Cancel(); | 463 request->Cancel(); |
| 463 base::MessageLoop::current()->Run(); | 464 base::MessageLoop::current()->Run(); |
| 464 | 465 |
| 465 EXPECT_TRUE(!request.is_pending()); | 466 EXPECT_TRUE(!request->is_pending()); |
| 466 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status()); | 467 EXPECT_EQ(net::URLRequestStatus::CANCELED, request->status().status()); |
| 467 EXPECT_EQ(net::ERR_ABORTED, request.status().error()); | 468 EXPECT_EQ(net::ERR_ABORTED, request->status().error()); |
| 468 EXPECT_EQ(request_url, request.url()); | 469 EXPECT_EQ(request_url, request->url()); |
| 469 EXPECT_EQ(1U, request.url_chain().size()); | 470 EXPECT_EQ(1U, request->url_chain().size()); |
| 470 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); | 471 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); |
| 471 | 472 |
| 472 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 473 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( |
| 473 &profile_, extension_id, kEventName + "/1"); | 474 &profile_, extension_id, kEventName + "/1"); |
| 474 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 475 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( |
| 475 &profile_, extension_id, kEventName2 + "/1"); | 476 &profile_, extension_id, kEventName2 + "/1"); |
| 476 } | 477 } |
| 477 | 478 |
| 478 namespace { | 479 namespace { |
| 479 | 480 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 492 | 493 |
| 493 } // namespace | 494 } // namespace |
| 494 | 495 |
| 495 void ExtensionWebRequestTest::FireURLRequestWithData( | 496 void ExtensionWebRequestTest::FireURLRequestWithData( |
| 496 const std::string& method, | 497 const std::string& method, |
| 497 const char* content_type, | 498 const char* content_type, |
| 498 const std::vector<char>& bytes_1, | 499 const std::vector<char>& bytes_1, |
| 499 const std::vector<char>& bytes_2) { | 500 const std::vector<char>& bytes_2) { |
| 500 // The request URL can be arbitrary but must have an HTTP or HTTPS scheme. | 501 // The request URL can be arbitrary but must have an HTTP or HTTPS scheme. |
| 501 GURL request_url("http://www.example.com"); | 502 GURL request_url("http://www.example.com"); |
| 502 net::URLRequest request( | 503 scoped_ptr<net::URLRequest> request(context_->CreateRequest( |
| 503 request_url, net::DEFAULT_PRIORITY, &delegate_, context_.get()); | 504 request_url, net::DEFAULT_PRIORITY, &delegate_, NULL)); |
| 504 request.set_method(method); | 505 request->set_method(method); |
| 505 if (content_type != NULL) | 506 if (content_type != NULL) { |
| 506 request.SetExtraRequestHeaderByName(net::HttpRequestHeaders::kContentType, | 507 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kContentType, |
| 507 content_type, | 508 content_type, |
| 508 true /* overwrite */); | 509 true /* overwrite */); |
| 510 } |
| 509 ScopedVector<net::UploadElementReader> element_readers; | 511 ScopedVector<net::UploadElementReader> element_readers; |
| 510 element_readers.push_back(new net::UploadBytesElementReader( | 512 element_readers.push_back(new net::UploadBytesElementReader( |
| 511 &(bytes_1[0]), bytes_1.size())); | 513 &(bytes_1[0]), bytes_1.size())); |
| 512 element_readers.push_back( | 514 element_readers.push_back( |
| 513 new net::UploadFileElementReader(base::MessageLoopProxy::current().get(), | 515 new net::UploadFileElementReader(base::MessageLoopProxy::current().get(), |
| 514 base::FilePath(), | 516 base::FilePath(), |
| 515 0, | 517 0, |
| 516 0, | 518 0, |
| 517 base::Time())); | 519 base::Time())); |
| 518 element_readers.push_back( | 520 element_readers.push_back( |
| 519 new net::UploadBytesElementReader(&(bytes_2[0]), bytes_2.size())); | 521 new net::UploadBytesElementReader(&(bytes_2[0]), bytes_2.size())); |
| 520 request.set_upload(make_scoped_ptr( | 522 request->set_upload(make_scoped_ptr( |
| 521 new net::UploadDataStream(element_readers.Pass(), 0))); | 523 new net::UploadDataStream(element_readers.Pass(), 0))); |
| 522 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); | 524 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); |
| 523 request.Start(); | 525 request->Start(); |
| 524 } | 526 } |
| 525 | 527 |
| 526 TEST_F(ExtensionWebRequestTest, AccessRequestBodyData) { | 528 TEST_F(ExtensionWebRequestTest, AccessRequestBodyData) { |
| 527 // We verify that URLRequest body is accessible to OnBeforeRequest listeners. | 529 // We verify that URLRequest body is accessible to OnBeforeRequest listeners. |
| 528 // These testing steps are repeated twice in a row: | 530 // These testing steps are repeated twice in a row: |
| 529 // 1. Register an extension requesting "requestBody" in ExtraInfoSpec and | 531 // 1. Register an extension requesting "requestBody" in ExtraInfoSpec and |
| 530 // file a POST URLRequest with a multipart-encoded form. See it getting | 532 // file a POST URLRequest with a multipart-encoded form. See it getting |
| 531 // parsed. | 533 // parsed. |
| 532 // 2. Do the same, but without requesting "requestBody". Nothing should be | 534 // 2. Do the same, but without requesting "requestBody". Nothing should be |
| 533 // parsed. | 535 // parsed. |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 706 | 708 |
| 707 // Subscribe to OnBeforeRequest with requestBody requirement. | 709 // Subscribe to OnBeforeRequest with requestBody requirement. |
| 708 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 710 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 709 &profile_, extension_id, extension_id, kEventName, kEventName + "/1", | 711 &profile_, extension_id, extension_id, kEventName, kEventName + "/1", |
| 710 filter, extra_info_spec, -1, -1, ipc_sender_factory.GetWeakPtr()); | 712 filter, extra_info_spec, -1, -1, ipc_sender_factory.GetWeakPtr()); |
| 711 | 713 |
| 712 // The request URL can be arbitrary but must have an HTTP or HTTPS scheme. | 714 // The request URL can be arbitrary but must have an HTTP or HTTPS scheme. |
| 713 const GURL request_url("http://www.example.com"); | 715 const GURL request_url("http://www.example.com"); |
| 714 | 716 |
| 715 for (size_t i = 0; i < arraysize(kMethods); ++i) { | 717 for (size_t i = 0; i < arraysize(kMethods); ++i) { |
| 716 net::URLRequest request( | 718 scoped_ptr<net::URLRequest> request(context_->CreateRequest( |
| 717 request_url, net::DEFAULT_PRIORITY, &delegate_, context_.get()); | 719 request_url, net::DEFAULT_PRIORITY, &delegate_, NULL)); |
| 718 request.set_method(kMethods[i]); | 720 request->set_method(kMethods[i]); |
| 719 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); | 721 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); |
| 720 request.Start(); | 722 request->Start(); |
| 721 } | 723 } |
| 722 | 724 |
| 723 // We inspect the result in the message list of |ipc_sender_| later. | 725 // We inspect the result in the message list of |ipc_sender_| later. |
| 724 base::MessageLoop::current()->RunUntilIdle(); | 726 base::MessageLoop::current()->RunUntilIdle(); |
| 725 | 727 |
| 726 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 728 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( |
| 727 &profile_, extension_id, kEventName + "/1"); | 729 &profile_, extension_id, kEventName + "/1"); |
| 728 | 730 |
| 729 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin(); | 731 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin(); |
| 730 for (size_t test = 0; test < arraysize(kMethods); ++test, ++i) { | 732 for (size_t test = 0; test < arraysize(kMethods); ++test, ++i) { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 826 ipc_sender_factory.GetWeakPtr()); | 828 ipc_sender_factory.GetWeakPtr()); |
| 827 | 829 |
| 828 // Install one extension that observes the final headers. | 830 // Install one extension that observes the final headers. |
| 829 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 831 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 830 &profile_, extension3_id, extension3_id, keys::kOnSendHeadersEvent, | 832 &profile_, extension3_id, extension3_id, keys::kOnSendHeadersEvent, |
| 831 std::string(keys::kOnSendHeadersEvent) + "/3", filter, | 833 std::string(keys::kOnSendHeadersEvent) + "/3", filter, |
| 832 ExtensionWebRequestEventRouter::ExtraInfoSpec::REQUEST_HEADERS, -1, -1, | 834 ExtensionWebRequestEventRouter::ExtraInfoSpec::REQUEST_HEADERS, -1, -1, |
| 833 ipc_sender_factory.GetWeakPtr()); | 835 ipc_sender_factory.GetWeakPtr()); |
| 834 | 836 |
| 835 GURL request_url("http://doesnotexist/does_not_exist.html"); | 837 GURL request_url("http://doesnotexist/does_not_exist.html"); |
| 836 net::URLRequest request( | 838 scoped_ptr<net::URLRequest> request(context_->CreateRequest( |
| 837 request_url, net::DEFAULT_PRIORITY, &delegate_, context_.get()); | 839 request_url, net::DEFAULT_PRIORITY, &delegate_, NULL)); |
| 838 | 840 |
| 839 // Initialize headers available before extensions are notified of the | 841 // Initialize headers available before extensions are notified of the |
| 840 // onBeforeSendHeaders event. | 842 // onBeforeSendHeaders event. |
| 841 HeaderModificationTest test = GetParam(); | 843 HeaderModificationTest test = GetParam(); |
| 842 net::HttpRequestHeaders before_headers; | 844 net::HttpRequestHeaders before_headers; |
| 843 for (int i = 0; i < test.before_size; ++i) | 845 for (int i = 0; i < test.before_size; ++i) |
| 844 before_headers.SetHeader(test.before[i].name, test.before[i].value); | 846 before_headers.SetHeader(test.before[i].name, test.before[i].value); |
| 845 request.SetExtraRequestHeaders(before_headers); | 847 request->SetExtraRequestHeaders(before_headers); |
| 846 | 848 |
| 847 // Gather the modifications to the headers for the respective extensions. | 849 // Gather the modifications to the headers for the respective extensions. |
| 848 // We assume here that all modifications of one extension are listed | 850 // We assume here that all modifications of one extension are listed |
| 849 // in a continuous block of |test.modifications_|. | 851 // in a continuous block of |test.modifications_|. |
| 850 ExtensionWebRequestEventRouter::EventResponse* response = NULL; | 852 ExtensionWebRequestEventRouter::EventResponse* response = NULL; |
| 851 for (int i = 0; i < test.modification_size; ++i) { | 853 for (int i = 0; i < test.modification_size; ++i) { |
| 852 const HeaderModificationTest_Modification& mod = test.modification[i]; | 854 const HeaderModificationTest_Modification& mod = test.modification[i]; |
| 853 if (response == NULL) { | 855 if (response == NULL) { |
| 854 response = new ExtensionWebRequestEventRouter::EventResponse( | 856 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 855 mod.extension_id == 1 ? extension1_id : extension2_id, | 857 mod.extension_id == 1 ? extension1_id : extension2_id, |
| 856 base::Time::FromDoubleT(mod.extension_id)); | 858 base::Time::FromDoubleT(mod.extension_id)); |
| 857 response->request_headers.reset(new net::HttpRequestHeaders()); | 859 response->request_headers.reset(new net::HttpRequestHeaders()); |
| 858 response->request_headers->MergeFrom(request.extra_request_headers()); | 860 response->request_headers->MergeFrom(request->extra_request_headers()); |
| 859 } | 861 } |
| 860 | 862 |
| 861 switch (mod.type) { | 863 switch (mod.type) { |
| 862 case HeaderModificationTest_Modification::SET: | 864 case HeaderModificationTest_Modification::SET: |
| 863 response->request_headers->SetHeader(mod.key, mod.value); | 865 response->request_headers->SetHeader(mod.key, mod.value); |
| 864 break; | 866 break; |
| 865 case HeaderModificationTest_Modification::REMOVE: | 867 case HeaderModificationTest_Modification::REMOVE: |
| 866 response->request_headers->RemoveHeader(mod.key); | 868 response->request_headers->RemoveHeader(mod.key); |
| 867 break; | 869 break; |
| 868 } | 870 } |
| 869 | 871 |
| 870 // Trigger the result when this is the last modification statement or | 872 // Trigger the result when this is the last modification statement or |
| 871 // the block of modifications for the next extension starts. | 873 // the block of modifications for the next extension starts. |
| 872 if (i+1 == test.modification_size || | 874 if (i+1 == test.modification_size || |
| 873 mod.extension_id != test.modification[i+1].extension_id) { | 875 mod.extension_id != test.modification[i+1].extension_id) { |
| 874 ipc_sender_.PushTask( | 876 ipc_sender_.PushTask( |
| 875 base::Bind(&EventHandledOnIOThread, | 877 base::Bind(&EventHandledOnIOThread, |
| 876 &profile_, mod.extension_id == 1 ? extension1_id : extension2_id, | 878 &profile_, mod.extension_id == 1 ? extension1_id : extension2_id, |
| 877 kEventName, kEventName + (mod.extension_id == 1 ? "/1" : "/2"), | 879 kEventName, kEventName + (mod.extension_id == 1 ? "/1" : "/2"), |
| 878 request.identifier(), response)); | 880 request->identifier(), response)); |
| 879 response = NULL; | 881 response = NULL; |
| 880 } | 882 } |
| 881 } | 883 } |
| 882 | 884 |
| 883 // Don't do anything for the onSendHeaders message. | 885 // Don't do anything for the onSendHeaders message. |
| 884 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); | 886 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); |
| 885 | 887 |
| 886 // Note that we mess up the headers slightly: | 888 // Note that we mess up the headers slightly: |
| 887 // request.Start() will first add additional headers (e.g. the User-Agent) | 889 // request->Start() will first add additional headers (e.g. the User-Agent) |
| 888 // and then send an event to the extension. When we have prepared our | 890 // and then send an event to the extension. When we have prepared our |
| 889 // answers to the onBeforeSendHeaders events above, these headers did not | 891 // answers to the onBeforeSendHeaders events above, these headers did not |
| 890 // exists and are therefore not listed in the responses. This makes | 892 // exists and are therefore not listed in the responses. This makes |
| 891 // them seem deleted. | 893 // them seem deleted. |
| 892 request.Start(); | 894 request->Start(); |
| 893 base::MessageLoop::current()->Run(); | 895 base::MessageLoop::current()->Run(); |
| 894 | 896 |
| 895 EXPECT_TRUE(!request.is_pending()); | 897 EXPECT_TRUE(!request->is_pending()); |
| 896 // This cannot succeed as we send the request to a server that does not exist. | 898 // This cannot succeed as we send the request to a server that does not exist. |
| 897 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 899 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
| 898 EXPECT_EQ(request_url, request.url()); | 900 EXPECT_EQ(request_url, request->url()); |
| 899 EXPECT_EQ(1U, request.url_chain().size()); | 901 EXPECT_EQ(1U, request->url_chain().size()); |
| 900 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); | 902 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); |
| 901 | 903 |
| 902 // Calculate the expected headers. | 904 // Calculate the expected headers. |
| 903 net::HttpRequestHeaders expected_headers; | 905 net::HttpRequestHeaders expected_headers; |
| 904 for (int i = 0; i < test.after_size; ++i) { | 906 for (int i = 0; i < test.after_size; ++i) { |
| 905 expected_headers.SetHeader(test.after[i].name, | 907 expected_headers.SetHeader(test.after[i].name, |
| 906 test.after[i].value); | 908 test.after[i].value); |
| 907 } | 909 } |
| 908 | 910 |
| 909 // Counter for the number of observed onSendHeaders events. | 911 // Counter for the number of observed onSendHeaders events. |
| (...skipping 1313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2223 EXPECT_TRUE(credentials_set); | 2225 EXPECT_TRUE(credentials_set); |
| 2224 EXPECT_FALSE(auth3.Empty()); | 2226 EXPECT_FALSE(auth3.Empty()); |
| 2225 EXPECT_EQ(username, auth1.username()); | 2227 EXPECT_EQ(username, auth1.username()); |
| 2226 EXPECT_EQ(password, auth1.password()); | 2228 EXPECT_EQ(password, auth1.password()); |
| 2227 EXPECT_EQ(1u, warning_set.size()); | 2229 EXPECT_EQ(1u, warning_set.size()); |
| 2228 EXPECT_TRUE(HasWarning(warning_set, "extid2")); | 2230 EXPECT_TRUE(HasWarning(warning_set, "extid2")); |
| 2229 EXPECT_EQ(3u, capturing_net_log.GetSize()); | 2231 EXPECT_EQ(3u, capturing_net_log.GetSize()); |
| 2230 } | 2232 } |
| 2231 | 2233 |
| 2232 } // namespace extensions | 2234 } // namespace extensions |
| OLD | NEW |