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_URL_REQUEST_CONTEXT_ADAPTER_H_ | 5 #ifndef COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_ |
6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_ | 6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_ |
7 | 7 |
8 #include <queue> | |
8 #include <string> | 9 #include <string> |
9 | 10 |
11 #include "base/callback.h" | |
10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
11 #include "base/macros.h" | 13 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
14 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
15 #include "net/base/net_log.h" | 17 #include "net/base/net_log.h" |
16 #include "net/base/network_change_notifier.h" | 18 #include "net/base/network_change_notifier.h" |
17 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
18 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
19 | 21 |
20 namespace net { | 22 namespace net { |
21 class NetLogLogger; | 23 class NetLogLogger; |
22 } // namespace net | 24 } // namespace net |
23 | 25 |
24 namespace cronet { | 26 namespace cronet { |
25 | 27 |
26 struct URLRequestContextConfig; | 28 struct URLRequestContextConfig; |
29 typedef base::Callback<void(void)> RunAfterContextInitTask; | |
27 | 30 |
28 // Implementation of the Chromium NetLog observer interface. | 31 // Implementation of the Chromium NetLog observer interface. |
29 class NetLogObserver : public net::NetLog::ThreadSafeObserver { | 32 class NetLogObserver : public net::NetLog::ThreadSafeObserver { |
30 public: | 33 public: |
31 explicit NetLogObserver() {} | 34 explicit NetLogObserver() {} |
32 | 35 |
33 virtual ~NetLogObserver() {} | 36 virtual ~NetLogObserver() {} |
34 | 37 |
35 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; | 38 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; |
36 | 39 |
(...skipping 12 matching lines...) Expand all Loading... | |
49 protected: | 52 protected: |
50 friend class base::RefCountedThreadSafe<URLRequestContextAdapterDelegate>; | 53 friend class base::RefCountedThreadSafe<URLRequestContextAdapterDelegate>; |
51 | 54 |
52 virtual ~URLRequestContextAdapterDelegate() {} | 55 virtual ~URLRequestContextAdapterDelegate() {} |
53 }; | 56 }; |
54 | 57 |
55 URLRequestContextAdapter(URLRequestContextAdapterDelegate* delegate, | 58 URLRequestContextAdapter(URLRequestContextAdapterDelegate* delegate, |
56 std::string user_agent); | 59 std::string user_agent); |
57 void Initialize(scoped_ptr<URLRequestContextConfig> config); | 60 void Initialize(scoped_ptr<URLRequestContextConfig> config); |
58 | 61 |
62 // Runs a task that might depend on the context being initialized. | |
63 // This method can be called on any thread. | |
64 void RunTaskAfterContextInit(const RunAfterContextInitTask& callback); | |
65 | |
66 // Runs a task that might depend on the context being initialized. | |
67 // This method should only be run on the network thread. | |
68 void RunTaskAfterContextInitOnNetworkThread( | |
69 const RunAfterContextInitTask& callback); | |
70 | |
59 const std::string& GetUserAgent(const GURL& url) const; | 71 const std::string& GetUserAgent(const GURL& url) const; |
60 | 72 |
61 // net::URLRequestContextGetter implementation: | 73 // net::URLRequestContextGetter implementation: |
62 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE; | 74 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE; |
63 virtual scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() | 75 virtual scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() |
64 const OVERRIDE; | 76 const OVERRIDE; |
65 | 77 |
66 void StartNetLogToFile(const std::string& file_name); | 78 void StartNetLogToFile(const std::string& file_name); |
67 void StopNetLog(); | 79 void StopNetLog(); |
68 | 80 |
69 private: | 81 private: |
70 scoped_refptr<URLRequestContextAdapterDelegate> delegate_; | 82 scoped_refptr<URLRequestContextAdapterDelegate> delegate_; |
71 scoped_ptr<net::URLRequestContext> context_; | 83 scoped_ptr<net::URLRequestContext> context_; |
72 std::string user_agent_; | 84 std::string user_agent_; |
73 base::Thread* network_thread_; | 85 base::Thread* network_thread_; |
74 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 86 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
75 scoped_ptr<NetLogObserver> net_log_observer_; | 87 scoped_ptr<NetLogObserver> net_log_observer_; |
76 scoped_ptr<net::NetLogLogger> net_log_logger_; | 88 scoped_ptr<net::NetLogLogger> net_log_logger_; |
77 | 89 |
90 // A queue of tasks that need to be run after context has been initialized. | |
91 std::queue<RunAfterContextInitTask> tasks_waiting_for_context_; | |
92 bool is_context_initialized_; | |
93 | |
78 virtual ~URLRequestContextAdapter(); | 94 virtual ~URLRequestContextAdapter(); |
79 | 95 |
80 // Initializes |context_| on the Network thread. | 96 // Initializes |context_| on the Network thread. |
81 void InitializeURLRequestContext(scoped_ptr<URLRequestContextConfig> config); | 97 void InitializeURLRequestContext(scoped_ptr<URLRequestContextConfig> config); |
82 | 98 |
99 // Helper function to start netlog. This should only be run after context is | |
mmenke
2014/10/03 20:26:58
nit: "...to start writing NetLog data to file."
xunjieli
2014/10/03 20:39:52
Done.
| |
100 // initialized. | |
101 void StartNetLogToFileHelper(const std::string& file_name); | |
102 // Helper function to stop netlog. This should only be run after context is | |
mmenke
2014/10/03 20:26:58
nit: "...to stop writing NetLog data file file."
xunjieli
2014/10/03 20:39:52
Done.
| |
103 // initialized. | |
104 void StopNetLogHelper(); | |
105 | |
83 DISALLOW_COPY_AND_ASSIGN(URLRequestContextAdapter); | 106 DISALLOW_COPY_AND_ASSIGN(URLRequestContextAdapter); |
84 }; | 107 }; |
85 | 108 |
86 } // namespace cronet | 109 } // namespace cronet |
87 | 110 |
88 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_ | 111 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_ |
OLD | NEW |