| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // This file contains an implementation of the ResourceLoaderBridge class. | 5 // This file contains an implementation of the ResourceLoaderBridge class. |
| 6 // The class is implemented using URLRequest, meaning it is a "simple" version | 6 // The class is implemented using URLRequest, meaning it is a "simple" version |
| 7 // that directly issues requests. The more complicated one used in the | 7 // that directly issues requests. The more complicated one used in the |
| 8 // browser uses IPC. | 8 // browser uses IPC. |
| 9 // | 9 // |
| 10 // Because URLRequest only provides an asynchronous resource loading API, this | 10 // Because URLRequest only provides an asynchronous resource loading API, this |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 // alternate implementation that defers fetching to another process. | 31 // alternate implementation that defers fetching to another process. |
| 32 | 32 |
| 33 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" | 33 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" |
| 34 | 34 |
| 35 #include "base/message_loop.h" | 35 #include "base/message_loop.h" |
| 36 #include "base/ref_counted.h" | 36 #include "base/ref_counted.h" |
| 37 #include "base/time.h" | 37 #include "base/time.h" |
| 38 #include "base/timer.h" | 38 #include "base/timer.h" |
| 39 #include "base/thread.h" | 39 #include "base/thread.h" |
| 40 #include "base/waitable_event.h" | 40 #include "base/waitable_event.h" |
| 41 #include "net/base/cookie_policy.h" |
| 41 #include "net/base/io_buffer.h" | 42 #include "net/base/io_buffer.h" |
| 42 #include "net/base/load_flags.h" | 43 #include "net/base/load_flags.h" |
| 43 #include "net/base/net_errors.h" | 44 #include "net/base/net_errors.h" |
| 44 #include "net/base/net_util.h" | 45 #include "net/base/net_util.h" |
| 45 #include "net/base/upload_data.h" | 46 #include "net/base/upload_data.h" |
| 46 #include "net/http/http_response_headers.h" | 47 #include "net/http/http_response_headers.h" |
| 47 #include "net/proxy/proxy_service.h" | 48 #include "net/proxy/proxy_service.h" |
| 48 #include "net/url_request/url_request.h" | 49 #include "net/url_request/url_request.h" |
| 49 #include "webkit/appcache/appcache_interfaces.h" | 50 #include "webkit/appcache/appcache_interfaces.h" |
| 50 #include "webkit/glue/resource_loader_bridge.h" | 51 #include "webkit/glue/resource_loader_bridge.h" |
| 51 #include "webkit/tools/test_shell/simple_appcache_system.h" | 52 #include "webkit/tools/test_shell/simple_appcache_system.h" |
| 52 #include "webkit/tools/test_shell/test_shell_request_context.h" | 53 #include "webkit/tools/test_shell/test_shell_request_context.h" |
| 53 | 54 |
| 54 using webkit_glue::ResourceLoaderBridge; | 55 using webkit_glue::ResourceLoaderBridge; |
| 56 using net::CookiePolicy; |
| 55 using net::HttpResponseHeaders; | 57 using net::HttpResponseHeaders; |
| 56 | 58 |
| 57 namespace { | 59 namespace { |
| 58 | 60 |
| 59 //----------------------------------------------------------------------------- | 61 //----------------------------------------------------------------------------- |
| 60 | 62 |
| 61 URLRequestContext* request_context = NULL; | 63 URLRequestContext* request_context = NULL; |
| 62 base::Thread* io_thread = NULL; | 64 base::Thread* io_thread = NULL; |
| 63 | 65 |
| 64 class IOThread : public base::Thread { | 66 class IOThread : public base::Thread { |
| (...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 // Make sure to stop any existing IO thread since it may be using the | 648 // Make sure to stop any existing IO thread since it may be using the |
| 647 // current request context. | 649 // current request context. |
| 648 Shutdown(); | 650 Shutdown(); |
| 649 | 651 |
| 650 if (context) { | 652 if (context) { |
| 651 request_context = context; | 653 request_context = context; |
| 652 } else { | 654 } else { |
| 653 request_context = new TestShellRequestContext(); | 655 request_context = new TestShellRequestContext(); |
| 654 } | 656 } |
| 655 request_context->AddRef(); | 657 request_context->AddRef(); |
| 658 SimpleResourceLoaderBridge::SetAcceptAllCookies(false); |
| 656 } | 659 } |
| 657 | 660 |
| 658 // static | 661 // static |
| 659 void SimpleResourceLoaderBridge::Shutdown() { | 662 void SimpleResourceLoaderBridge::Shutdown() { |
| 660 if (io_thread) { | 663 if (io_thread) { |
| 661 delete io_thread; | 664 delete io_thread; |
| 662 io_thread = NULL; | 665 io_thread = NULL; |
| 663 | 666 |
| 664 DCHECK(!request_context) << "should have been nulled by thread dtor"; | 667 DCHECK(!request_context) << "should have been nulled by thread dtor"; |
| 665 } | 668 } |
| 666 } | 669 } |
| 667 | 670 |
| 671 // static |
| 668 void SimpleResourceLoaderBridge::SetCookie(const GURL& url, | 672 void SimpleResourceLoaderBridge::SetCookie(const GURL& url, |
| 669 const GURL& first_party_for_cookies, | 673 const GURL& first_party_for_cookies, |
| 670 const std::string& cookie) { | 674 const std::string& cookie) { |
| 671 // Proxy to IO thread to synchronize w/ network loading. | 675 // Proxy to IO thread to synchronize w/ network loading. |
| 672 | 676 |
| 673 if (!EnsureIOThread()) { | 677 if (!EnsureIOThread()) { |
| 674 NOTREACHED(); | 678 NOTREACHED(); |
| 675 return; | 679 return; |
| 676 } | 680 } |
| 677 | 681 |
| 678 scoped_refptr<CookieSetter> cookie_setter = new CookieSetter(); | 682 scoped_refptr<CookieSetter> cookie_setter = new CookieSetter(); |
| 679 io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 683 io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 680 cookie_setter.get(), &CookieSetter::Set, url, cookie)); | 684 cookie_setter.get(), &CookieSetter::Set, url, cookie)); |
| 681 } | 685 } |
| 682 | 686 |
| 687 // static |
| 683 std::string SimpleResourceLoaderBridge::GetCookies( | 688 std::string SimpleResourceLoaderBridge::GetCookies( |
| 684 const GURL& url, const GURL& first_party_for_cookies) { | 689 const GURL& url, const GURL& first_party_for_cookies) { |
| 685 // Proxy to IO thread to synchronize w/ network loading | 690 // Proxy to IO thread to synchronize w/ network loading |
| 686 | 691 |
| 687 if (!EnsureIOThread()) { | 692 if (!EnsureIOThread()) { |
| 688 NOTREACHED(); | 693 NOTREACHED(); |
| 689 return std::string(); | 694 return std::string(); |
| 690 } | 695 } |
| 691 | 696 |
| 692 scoped_refptr<CookieGetter> getter = new CookieGetter(); | 697 scoped_refptr<CookieGetter> getter = new CookieGetter(); |
| 693 | 698 |
| 694 io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 699 io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 695 getter.get(), &CookieGetter::Get, url)); | 700 getter.get(), &CookieGetter::Get, url)); |
| 696 | 701 |
| 697 return getter->GetResult(); | 702 return getter->GetResult(); |
| 698 } | 703 } |
| 699 | 704 |
| 705 // static |
| 700 bool SimpleResourceLoaderBridge::EnsureIOThread() { | 706 bool SimpleResourceLoaderBridge::EnsureIOThread() { |
| 701 if (io_thread) | 707 if (io_thread) |
| 702 return true; | 708 return true; |
| 703 | 709 |
| 704 if (!request_context) | 710 if (!request_context) |
| 705 SimpleResourceLoaderBridge::Init(NULL); | 711 SimpleResourceLoaderBridge::Init(NULL); |
| 706 | 712 |
| 707 io_thread = new IOThread(); | 713 io_thread = new IOThread(); |
| 708 base::Thread::Options options; | 714 base::Thread::Options options; |
| 709 options.message_loop_type = MessageLoop::TYPE_IO; | 715 options.message_loop_type = MessageLoop::TYPE_IO; |
| 710 return io_thread->StartWithOptions(options); | 716 return io_thread->StartWithOptions(options); |
| 711 } | 717 } |
| 718 |
| 719 // static |
| 720 void SimpleResourceLoaderBridge::SetAcceptAllCookies(bool accept_all_cookies) { |
| 721 CookiePolicy::Type policy_type = accept_all_cookies ? |
| 722 CookiePolicy::ALLOW_ALL_COOKIES : CookiePolicy::BLOCK_THIRD_PARTY_COOKIES; |
| 723 request_context->cookie_policy()->set_type(policy_type); |
| 724 } |
| OLD | NEW |