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