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