| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_ | |
| 6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <queue> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/compiler_specific.h" | |
| 14 #include "base/location.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/threading/thread.h" | |
| 18 #include "net/log/net_log.h" | |
| 19 #include "net/url_request/url_request_context.h" | |
| 20 #include "net/url_request/url_request_context_getter.h" | |
| 21 | |
| 22 namespace net { | |
| 23 class WriteToFileNetLogObserver; | |
| 24 class ProxyConfigService; | |
| 25 class SdchOwner; | |
| 26 } // namespace net | |
| 27 | |
| 28 namespace cronet { | |
| 29 | |
| 30 struct URLRequestContextConfig; | |
| 31 typedef base::Callback<void(void)> RunAfterContextInitTask; | |
| 32 | |
| 33 // Implementation of the Chromium NetLog observer interface. | |
| 34 class NetLogObserver : public net::NetLog::ThreadSafeObserver { | |
| 35 public: | |
| 36 NetLogObserver() {} | |
| 37 | |
| 38 ~NetLogObserver() override {} | |
| 39 | |
| 40 void OnAddEntry(const net::NetLog::Entry& entry) override; | |
| 41 | |
| 42 private: | |
| 43 DISALLOW_COPY_AND_ASSIGN(NetLogObserver); | |
| 44 }; | |
| 45 | |
| 46 // Fully configured |URLRequestContext|. | |
| 47 class URLRequestContextAdapter : public net::URLRequestContextGetter { | |
| 48 public: | |
| 49 class URLRequestContextAdapterDelegate | |
| 50 : public base::RefCountedThreadSafe<URLRequestContextAdapterDelegate> { | |
| 51 public: | |
| 52 virtual void OnContextInitialized(URLRequestContextAdapter* context) = 0; | |
| 53 | |
| 54 protected: | |
| 55 friend class base::RefCountedThreadSafe<URLRequestContextAdapterDelegate>; | |
| 56 | |
| 57 virtual ~URLRequestContextAdapterDelegate() {} | |
| 58 }; | |
| 59 | |
| 60 URLRequestContextAdapter(URLRequestContextAdapterDelegate* delegate, | |
| 61 std::string user_agent); | |
| 62 void Initialize(std::unique_ptr<URLRequestContextConfig> config); | |
| 63 | |
| 64 // Posts a task that might depend on the context being initialized | |
| 65 // to the network thread. | |
| 66 void PostTaskToNetworkThread(const tracked_objects::Location& posted_from, | |
| 67 const RunAfterContextInitTask& callback); | |
| 68 | |
| 69 // Runs a task that might depend on the context being initialized. | |
| 70 // This method should only be run on the network thread. | |
| 71 void RunTaskAfterContextInitOnNetworkThread( | |
| 72 const RunAfterContextInitTask& callback); | |
| 73 | |
| 74 const std::string& GetUserAgent(const GURL& url) const; | |
| 75 | |
| 76 bool load_disable_cache() const { return load_disable_cache_; } | |
| 77 | |
| 78 // net::URLRequestContextGetter implementation: | |
| 79 net::URLRequestContext* GetURLRequestContext() override; | |
| 80 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() | |
| 81 const override; | |
| 82 | |
| 83 void StartNetLogToFile(const std::string& file_name, bool log_all); | |
| 84 void StopNetLog(); | |
| 85 | |
| 86 // Called on main Java thread to initialize URLRequestContext. | |
| 87 void InitRequestContextOnMainThread(); | |
| 88 | |
| 89 private: | |
| 90 ~URLRequestContextAdapter() override; | |
| 91 | |
| 92 // Initializes |context_| on the Network thread. | |
| 93 void InitRequestContextOnNetworkThread(); | |
| 94 | |
| 95 // Helper function to start writing NetLog data to file. This should only be | |
| 96 // run after context is initialized. | |
| 97 void StartNetLogToFileHelper(const std::string& file_name, bool log_all); | |
| 98 // Helper function to stop writing NetLog data to file. This should only be | |
| 99 // run after context is initialized. | |
| 100 void StopNetLogHelper(); | |
| 101 | |
| 102 scoped_refptr<URLRequestContextAdapterDelegate> delegate_; | |
| 103 std::unique_ptr<net::URLRequestContext> context_; | |
| 104 std::string user_agent_; | |
| 105 bool load_disable_cache_; | |
| 106 base::Thread* network_thread_; | |
| 107 std::unique_ptr<NetLogObserver> net_log_observer_; | |
| 108 std::unique_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; | |
| 109 std::unique_ptr<net::ProxyConfigService> proxy_config_service_; | |
| 110 std::unique_ptr<net::SdchOwner> sdch_owner_; | |
| 111 std::unique_ptr<URLRequestContextConfig> config_; | |
| 112 | |
| 113 // A queue of tasks that need to be run after context has been initialized. | |
| 114 std::queue<RunAfterContextInitTask> tasks_waiting_for_context_; | |
| 115 bool is_context_initialized_; | |
| 116 | |
| 117 DISALLOW_COPY_AND_ASSIGN(URLRequestContextAdapter); | |
| 118 }; | |
| 119 | |
| 120 } // namespace cronet | |
| 121 | |
| 122 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_ | |
| OLD | NEW |