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.
5 #ifndef COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_
6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/location.h"
14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/threading/thread.h"
18 #include "net/base/net_log.h"
19 #include "net/base/network_change_notifier.h"
20 #include "net/url_request/url_request_context.h"
21 #include "net/url_request/url_request_context_getter.h"
31 struct URLRequestContextConfig
;
32 typedef base::Callback
<void(void)> RunAfterContextInitTask
;
34 // Implementation of the Chromium NetLog observer interface.
35 class NetLogObserver
: public net::NetLog::ThreadSafeObserver
{
37 virtual NetLogObserver() {}
39 virtual ~NetLogObserver() {}
41 void OnAddEntry(const net::NetLog::Entry
& entry
) override
;
44 DISALLOW_COPY_AND_ASSIGN(NetLogObserver
);
47 // Fully configured |URLRequestContext|.
48 class URLRequestContextAdapter
: public net::URLRequestContextGetter
{
50 class URLRequestContextAdapterDelegate
51 : public base::RefCountedThreadSafe
<URLRequestContextAdapterDelegate
> {
53 virtual void OnContextInitialized(URLRequestContextAdapter
* context
) = 0;
56 friend class base::RefCountedThreadSafe
<URLRequestContextAdapterDelegate
>;
58 virtual ~URLRequestContextAdapterDelegate() {}
61 URLRequestContextAdapter(URLRequestContextAdapterDelegate
* delegate
,
62 std::string user_agent
);
63 void Initialize(scoped_ptr
<URLRequestContextConfig
> config
);
65 // Posts a task that might depend on the context being initialized
66 // to the network thread.
67 void PostTaskToNetworkThread(const tracked_objects::Location
& posted_from
,
68 const RunAfterContextInitTask
& callback
);
70 // Runs a task that might depend on the context being initialized.
71 // This method should only be run on the network thread.
72 void RunTaskAfterContextInitOnNetworkThread(
73 const RunAfterContextInitTask
& callback
);
75 const std::string
& GetUserAgent(const GURL
& url
) const;
77 // net::URLRequestContextGetter implementation:
78 net::URLRequestContext
* GetURLRequestContext() override
;
79 scoped_refptr
<base::SingleThreadTaskRunner
> GetNetworkTaskRunner()
82 void StartNetLogToFile(const std::string
& file_name
);
86 scoped_refptr
<URLRequestContextAdapterDelegate
> delegate_
;
87 scoped_ptr
<net::URLRequestContext
> context_
;
88 std::string user_agent_
;
89 base::Thread
* network_thread_
;
90 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
91 scoped_ptr
<NetLogObserver
> net_log_observer_
;
92 scoped_ptr
<net::NetLogLogger
> net_log_logger_
;
94 // A queue of tasks that need to be run after context has been initialized.
95 std::queue
<RunAfterContextInitTask
> tasks_waiting_for_context_
;
96 bool is_context_initialized_
;
98 virtual ~URLRequestContextAdapter();
100 // Initializes |context_| on the Network thread.
101 void InitializeURLRequestContext(scoped_ptr
<URLRequestContextConfig
> config
);
103 // Helper function to start writing NetLog data to file. This should only be
104 // run after context is initialized.
105 void StartNetLogToFileHelper(const std::string
& file_name
);
106 // Helper function to stop writing NetLog data to file. This should only be
107 // run after context is initialized.
108 void StopNetLogHelper();
110 DISALLOW_COPY_AND_ASSIGN(URLRequestContextAdapter
);
113 } // namespace cronet
115 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_