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/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_getter.h"
26 class ProxyConfigService
;
32 struct URLRequestContextConfig
;
33 typedef base::Callback
<void(void)> RunAfterContextInitTask
;
35 // Implementation of the Chromium NetLog observer interface.
36 class NetLogObserver
: public net::NetLog::ThreadSafeObserver
{
40 virtual ~NetLogObserver() {}
42 void OnAddEntry(const net::NetLog::Entry
& entry
) override
;
45 DISALLOW_COPY_AND_ASSIGN(NetLogObserver
);
48 // Fully configured |URLRequestContext|.
49 class URLRequestContextAdapter
: public net::URLRequestContextGetter
{
51 class URLRequestContextAdapterDelegate
52 : public base::RefCountedThreadSafe
<URLRequestContextAdapterDelegate
> {
54 virtual void OnContextInitialized(URLRequestContextAdapter
* context
) = 0;
57 friend class base::RefCountedThreadSafe
<URLRequestContextAdapterDelegate
>;
59 virtual ~URLRequestContextAdapterDelegate() {}
62 URLRequestContextAdapter(URLRequestContextAdapterDelegate
* delegate
,
63 std::string user_agent
);
64 void Initialize(scoped_ptr
<URLRequestContextConfig
> config
);
66 // Posts a task that might depend on the context being initialized
67 // to the network thread.
68 void PostTaskToNetworkThread(const tracked_objects::Location
& posted_from
,
69 const RunAfterContextInitTask
& callback
);
71 // Runs a task that might depend on the context being initialized.
72 // This method should only be run on the network thread.
73 void RunTaskAfterContextInitOnNetworkThread(
74 const RunAfterContextInitTask
& callback
);
76 const std::string
& GetUserAgent(const GURL
& url
) const;
78 // net::URLRequestContextGetter implementation:
79 net::URLRequestContext
* GetURLRequestContext() override
;
80 scoped_refptr
<base::SingleThreadTaskRunner
> GetNetworkTaskRunner()
83 void StartNetLogToFile(const std::string
& file_name
);
86 // Called on main Java thread to initialize URLRequestContext.
87 void InitRequestContextOnMainThread();
90 scoped_refptr
<URLRequestContextAdapterDelegate
> delegate_
;
91 scoped_ptr
<net::URLRequestContext
> context_
;
92 std::string user_agent_
;
93 base::Thread
* network_thread_
;
94 scoped_ptr
<NetLogObserver
> net_log_observer_
;
95 scoped_ptr
<net::NetLogLogger
> net_log_logger_
;
96 scoped_ptr
<net::ProxyConfigService
> proxy_config_service_
;
97 scoped_ptr
<URLRequestContextConfig
> config_
;
99 // A queue of tasks that need to be run after context has been initialized.
100 std::queue
<RunAfterContextInitTask
> tasks_waiting_for_context_
;
101 bool is_context_initialized_
= false;
103 virtual ~URLRequestContextAdapter();
105 // Initializes |context_| on the Network thread.
106 void InitRequestContextOnNetworkThread();
108 // Helper function to start writing NetLog data to file. This should only be
109 // run after context is initialized.
110 void StartNetLogToFileHelper(const std::string
& file_name
);
111 // Helper function to stop writing NetLog data to file. This should only be
112 // run after context is initialized.
113 void StopNetLogHelper();
115 DISALLOW_COPY_AND_ASSIGN(URLRequestContextAdapter
);
118 } // namespace cronet
120 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_