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 bool load_disable_cache() const { return load_disable_cache_
; }
80 // net::URLRequestContextGetter implementation:
81 net::URLRequestContext
* GetURLRequestContext() override
;
82 scoped_refptr
<base::SingleThreadTaskRunner
> GetNetworkTaskRunner()
85 void StartNetLogToFile(const std::string
& file_name
);
88 // Called on main Java thread to initialize URLRequestContext.
89 void InitRequestContextOnMainThread();
92 ~URLRequestContextAdapter() override
;
94 // Initializes |context_| on the Network thread.
95 void InitRequestContextOnNetworkThread();
97 // Helper function to start writing NetLog data to file. This should only be
98 // run after context is initialized.
99 void StartNetLogToFileHelper(const std::string
& file_name
);
100 // Helper function to stop writing NetLog data to file. This should only be
101 // run after context is initialized.
102 void StopNetLogHelper();
104 scoped_refptr
<URLRequestContextAdapterDelegate
> delegate_
;
105 scoped_ptr
<net::URLRequestContext
> context_
;
106 std::string user_agent_
;
107 bool load_disable_cache_
;
108 base::Thread
* network_thread_
;
109 scoped_ptr
<NetLogObserver
> net_log_observer_
;
110 scoped_ptr
<net::NetLogLogger
> net_log_logger_
;
111 scoped_ptr
<net::ProxyConfigService
> proxy_config_service_
;
112 scoped_ptr
<URLRequestContextConfig
> config_
;
114 // A queue of tasks that need to be run after context has been initialized.
115 std::queue
<RunAfterContextInitTask
> tasks_waiting_for_context_
;
116 bool is_context_initialized_
;
118 DISALLOW_COPY_AND_ASSIGN(URLRequestContextAdapter
);
121 } // namespace cronet
123 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_