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_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_
6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_
13 #include "base/android/scoped_java_ref.h"
14 #include "base/callback.h"
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/prefs/json_pref_store.h"
19 #include "base/threading/thread.h"
24 class SingleThreadTaskRunner
;
28 class HttpServerPropertiesManager
;
29 class ProxyConfigService
;
31 class URLRequestContext
;
32 class WriteToFileNetLogObserver
;
37 #if defined(DATA_REDUCTION_PROXY_SUPPORT)
38 class CronetDataReductionProxy
;
41 struct URLRequestContextConfig
;
43 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv
* env
);
45 // Adapter between Java CronetUrlRequestContext and net::URLRequestContext.
46 class CronetURLRequestContextAdapter
{
48 explicit CronetURLRequestContextAdapter(
49 scoped_ptr
<URLRequestContextConfig
> context_config
);
51 ~CronetURLRequestContextAdapter();
53 // Called on main Java thread to initialize URLRequestContext.
54 void InitRequestContextOnMainThread(JNIEnv
* env
, jobject jcaller
);
56 // Releases all resources for the request context and deletes the object.
57 // Blocks until network thread is destroyed after running all pending tasks.
58 void Destroy(JNIEnv
* env
, jobject jcaller
);
60 // Posts a task that might depend on the context being initialized
61 // to the network thread.
62 void PostTaskToNetworkThread(const tracked_objects::Location
& posted_from
,
63 const base::Closure
& callback
);
65 bool IsOnNetworkThread() const;
67 net::URLRequestContext
* GetURLRequestContext();
69 void StartNetLogToFile(JNIEnv
* env
, jobject jcaller
, jstring jfile_name
,
72 void StopNetLog(JNIEnv
* env
, jobject jcaller
);
74 // Default net::LOAD flags used to create requests.
75 int default_load_flags() const { return default_load_flags_
; }
77 // Called on main Java thread to initialize URLRequestContext.
78 void InitRequestContextOnMainThread();
81 // Initializes |context_| on the Network thread.
82 void InitializeOnNetworkThread(scoped_ptr
<URLRequestContextConfig
> config
,
83 const base::android::ScopedJavaGlobalRef
<
84 jobject
>& jcronet_url_request_context
);
86 // Runs a task that might depend on the context being initialized.
87 // This method should only be run on the network thread.
88 void RunTaskAfterContextInitOnNetworkThread(
89 const base::Closure
& task_to_run_after_context_init
);
91 scoped_refptr
<base::SingleThreadTaskRunner
> GetNetworkTaskRunner() const;
93 void StartNetLogToFileOnNetworkThread(const std::string
& file_name
,
96 void StopNetLogOnNetworkThread();
98 // Gets the file thread. Create one if there is none.
99 base::Thread
* GetFileThread();
101 // Network thread is owned by |this|, but is destroyed from java thread.
102 base::Thread
* network_thread_
;
104 // File thread should be destroyed last.
105 scoped_ptr
<base::Thread
> file_thread_
;
107 // |write_to_file_observer_| and |context_| should only be accessed on
109 scoped_ptr
<net::WriteToFileNetLogObserver
> write_to_file_observer_
;
111 // |pref_service_| should outlive the HttpServerPropertiesManager owned by
113 scoped_ptr
<PrefService
> pref_service_
;
114 scoped_ptr
<net::URLRequestContext
> context_
;
115 scoped_ptr
<net::ProxyConfigService
> proxy_config_service_
;
116 scoped_refptr
<JsonPrefStore
> json_pref_store_
;
117 net::HttpServerPropertiesManager
* http_server_properties_manager_
;
119 // |sdch_owner_| should be destroyed before |json_pref_store_|, because
120 // tearing down |sdch_owner_| forces |json_pref_store_| to flush pending
121 // writes to the disk.
122 scoped_ptr
<net::SdchOwner
> sdch_owner_
;
124 // Context config is only valid until context is initialized.
125 scoped_ptr
<URLRequestContextConfig
> context_config_
;
127 // A queue of tasks that need to be run after context has been initialized.
128 std::queue
<base::Closure
> tasks_waiting_for_context_
;
129 bool is_context_initialized_
;
130 int default_load_flags_
;
132 #if defined(DATA_REDUCTION_PROXY_SUPPORT)
133 scoped_ptr
<CronetDataReductionProxy
> data_reduction_proxy_
;
136 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestContextAdapter
);
139 } // namespace cronet
141 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_