| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_ | 5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_ |
| 6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_ | 6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_ |
| 7 | 7 |
| 8 #include <queue> |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 15 #include "net/base/net_log.h" | 16 #include "net/base/net_log.h" |
| 16 #include "net/base/network_change_notifier.h" | 17 #include "net/base/network_change_notifier.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 class SingleThreadTaskRunner; | 20 class SingleThreadTaskRunner; |
| 20 } // namespace base | 21 } // namespace base |
| 21 | 22 |
| 22 namespace net { | 23 namespace net { |
| 23 class NetLogLogger; | 24 class NetLogLogger; |
| 24 class URLRequestContext; | 25 class URLRequestContext; |
| 26 class ProxyConfigService; |
| 25 } // namespace net | 27 } // namespace net |
| 26 | 28 |
| 27 namespace cronet { | 29 namespace cronet { |
| 28 | 30 |
| 29 struct URLRequestContextConfig; | 31 struct URLRequestContextConfig; |
| 30 | 32 |
| 31 // Adapter between Java CronetUrlRequestContext and net::URLRequestContext. | 33 // Adapter between Java CronetUrlRequestContext and net::URLRequestContext. |
| 32 class CronetURLRequestContextAdapter { | 34 class CronetURLRequestContextAdapter { |
| 33 public: | 35 public: |
| 34 CronetURLRequestContextAdapter(); | 36 explicit CronetURLRequestContextAdapter( |
| 37 scoped_ptr<URLRequestContextConfig> context_config); |
| 35 | 38 |
| 36 ~CronetURLRequestContextAdapter(); | 39 ~CronetURLRequestContextAdapter(); |
| 37 | 40 |
| 38 void Initialize(scoped_ptr<URLRequestContextConfig> config, | 41 // Called on main Java thread to initialize URLRequestContext. |
| 39 const base::Closure& java_init_network_thread); | 42 void InitRequestContextOnMainThread( |
| 43 const base::Closure& java_init_network_thread); |
| 40 | 44 |
| 41 // Releases all resources for the request context and deletes the object. | 45 // Releases all resources for the request context and deletes the object. |
| 42 // Blocks until network thread is destroyed after running all pending tasks. | 46 // Blocks until network thread is destroyed after running all pending tasks. |
| 43 void Destroy(); | 47 void Destroy(); |
| 44 | 48 |
| 49 // Posts a task that might depend on the context being initialized |
| 50 // to the network thread. |
| 51 void PostTaskToNetworkThread(const tracked_objects::Location& posted_from, |
| 52 const base::Closure& callback); |
| 53 |
| 54 bool IsOnNetworkThread() const; |
| 55 |
| 45 net::URLRequestContext* GetURLRequestContext(); | 56 net::URLRequestContext* GetURLRequestContext(); |
| 46 | 57 |
| 47 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() const; | |
| 48 | |
| 49 void StartNetLogToFile(const std::string& file_name); | 58 void StartNetLogToFile(const std::string& file_name); |
| 50 | 59 |
| 51 void StopNetLog(); | 60 void StopNetLog(); |
| 52 | 61 |
| 53 // Default net::LOAD flags used to create requests. | 62 // Default net::LOAD flags used to create requests. |
| 54 int default_load_flags() const { return default_load_flags_; } | 63 int default_load_flags() const { return default_load_flags_; } |
| 55 | 64 |
| 65 // Called on main Java thread to initialize URLRequestContext. |
| 66 void InitRequestContextOnMainThread(); |
| 67 |
| 56 private: | 68 private: |
| 57 // Initializes |context_| on the Network thread. | 69 // Initializes |context_| on the Network thread. |
| 58 void InitializeOnNetworkThread( | 70 void InitializeOnNetworkThread( |
| 59 scoped_ptr<URLRequestContextConfig> config, | 71 scoped_ptr<URLRequestContextConfig> config, |
| 60 const base::Closure& java_init_network_thread); | 72 const base::Closure& java_init_network_thread); |
| 73 |
| 74 // Runs a task that might depend on the context being initialized. |
| 75 // This method should only be run on the network thread. |
| 76 void RunTaskAfterContextInitOnNetworkThread( |
| 77 const base::Closure& task_to_run_after_context_init); |
| 78 |
| 79 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() const; |
| 80 |
| 61 void StartNetLogToFileOnNetworkThread(const std::string& file_name); | 81 void StartNetLogToFileOnNetworkThread(const std::string& file_name); |
| 82 |
| 62 void StopNetLogOnNetworkThread(); | 83 void StopNetLogOnNetworkThread(); |
| 63 | 84 |
| 64 // Network thread is owned by |this|, but is destroyed from java thread. | 85 // Network thread is owned by |this|, but is destroyed from java thread. |
| 65 base::Thread* network_thread_; | 86 base::Thread* network_thread_; |
| 66 // |net_log_logger_| and |context_| should only be accessed on network thread. | 87 // |net_log_logger_| and |context_| should only be accessed on network thread. |
| 67 scoped_ptr<net::NetLogLogger> net_log_logger_; | 88 scoped_ptr<net::NetLogLogger> net_log_logger_; |
| 68 scoped_ptr<net::URLRequestContext> context_; | 89 scoped_ptr<net::URLRequestContext> context_; |
| 90 scoped_ptr<net::ProxyConfigService> proxy_config_service_; |
| 91 |
| 92 // Context config is only valid untng context is initialized. |
| 93 scoped_ptr<URLRequestContextConfig> context_config_; |
| 94 |
| 95 // A queue of tasks that need to be run after context has been initialized. |
| 96 std::queue<base::Closure> tasks_waiting_for_context_; |
| 97 bool is_context_initialized_; |
| 69 int default_load_flags_; | 98 int default_load_flags_; |
| 70 | 99 |
| 71 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestContextAdapter); | 100 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestContextAdapter); |
| 72 }; | 101 }; |
| 73 | 102 |
| 74 } // namespace cronet | 103 } // namespace cronet |
| 75 | 104 |
| 76 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_ | 105 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_ |
| OLD | NEW |