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/log/net_log.h"
19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_getter.h"
23 class WriteToFileNetLogObserver
;
24 class ProxyConfigService
;
30 struct URLRequestContextConfig
;
31 typedef base::Callback
<void(void)> RunAfterContextInitTask
;
33 // Implementation of the Chromium NetLog observer interface.
34 class NetLogObserver
: public net::NetLog::ThreadSafeObserver
{
38 ~NetLogObserver() override
{}
40 void OnAddEntry(const net::NetLog::Entry
& entry
) override
;
43 DISALLOW_COPY_AND_ASSIGN(NetLogObserver
);
46 // Fully configured |URLRequestContext|.
47 class URLRequestContextAdapter
: public net::URLRequestContextGetter
{
49 class URLRequestContextAdapterDelegate
50 : public base::RefCountedThreadSafe
<URLRequestContextAdapterDelegate
> {
52 virtual void OnContextInitialized(URLRequestContextAdapter
* context
) = 0;
55 friend class base::RefCountedThreadSafe
<URLRequestContextAdapterDelegate
>;
57 virtual ~URLRequestContextAdapterDelegate() {}
60 URLRequestContextAdapter(URLRequestContextAdapterDelegate
* delegate
,
61 std::string user_agent
);
62 void Initialize(scoped_ptr
<URLRequestContextConfig
> config
);
64 // Posts a task that might depend on the context being initialized
65 // to the network thread.
66 void PostTaskToNetworkThread(const tracked_objects::Location
& posted_from
,
67 const RunAfterContextInitTask
& callback
);
69 // Runs a task that might depend on the context being initialized.
70 // This method should only be run on the network thread.
71 void RunTaskAfterContextInitOnNetworkThread(
72 const RunAfterContextInitTask
& callback
);
74 const std::string
& GetUserAgent(const GURL
& url
) const;
76 bool load_disable_cache() const { return load_disable_cache_
; }
78 // net::URLRequestContextGetter implementation:
79 net::URLRequestContext
* GetURLRequestContext() override
;
80 scoped_refptr
<base::SingleThreadTaskRunner
> GetNetworkTaskRunner()
83 void StartNetLogToFile(const std::string
& file_name
, bool log_all
);
86 // Called on main Java thread to initialize URLRequestContext.
87 void InitRequestContextOnMainThread();
90 ~URLRequestContextAdapter() override
;
92 // Initializes |context_| on the Network thread.
93 void InitRequestContextOnNetworkThread();
95 // Helper function to start writing NetLog data to file. This should only be
96 // run after context is initialized.
97 void StartNetLogToFileHelper(const std::string
& file_name
, bool log_all
);
98 // Helper function to stop writing NetLog data to file. This should only be
99 // run after context is initialized.
100 void StopNetLogHelper();
102 scoped_refptr
<URLRequestContextAdapterDelegate
> delegate_
;
103 scoped_ptr
<net::URLRequestContext
> context_
;
104 std::string user_agent_
;
105 bool load_disable_cache_
;
106 base::Thread
* network_thread_
;
107 scoped_ptr
<NetLogObserver
> net_log_observer_
;
108 scoped_ptr
<net::WriteToFileNetLogObserver
> write_to_file_observer_
;
109 scoped_ptr
<net::ProxyConfigService
> proxy_config_service_
;
110 scoped_ptr
<net::SdchOwner
> sdch_owner_
;
111 scoped_ptr
<URLRequestContextConfig
> config_
;
113 // A queue of tasks that need to be run after context has been initialized.
114 std::queue
<RunAfterContextInitTask
> tasks_waiting_for_context_
;
115 bool is_context_initialized_
;
117 DISALLOW_COPY_AND_ASSIGN(URLRequestContextAdapter
);
120 } // namespace cronet
122 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_