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
;
30 class ProxyConfigService
;
32 class URLRequestContext
;
33 class WriteToFileNetLogObserver
;
38 #if defined(DATA_REDUCTION_PROXY_SUPPORT)
39 class CronetDataReductionProxy
;
42 struct URLRequestContextConfig
;
44 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv
* env
);
46 // Adapter between Java CronetUrlRequestContext and net::URLRequestContext.
47 class CronetURLRequestContextAdapter
{
49 explicit CronetURLRequestContextAdapter(
50 scoped_ptr
<URLRequestContextConfig
> context_config
);
52 ~CronetURLRequestContextAdapter();
54 // Called on main Java thread to initialize URLRequestContext.
55 void InitRequestContextOnMainThread(JNIEnv
* env
, jobject jcaller
);
57 // Releases all resources for the request context and deletes the object.
58 // Blocks until network thread is destroyed after running all pending tasks.
59 void Destroy(JNIEnv
* env
, jobject jcaller
);
61 // Posts a task that might depend on the context being initialized
62 // to the network thread.
63 void PostTaskToNetworkThread(const tracked_objects::Location
& posted_from
,
64 const base::Closure
& callback
);
66 bool IsOnNetworkThread() const;
68 net::URLRequestContext
* GetURLRequestContext();
70 void StartNetLogToFile(JNIEnv
* env
, jobject jcaller
, jstring jfile_name
,
73 void StopNetLog(JNIEnv
* env
, jobject jcaller
);
75 // Default net::LOAD flags used to create requests.
76 int default_load_flags() const { return default_load_flags_
; }
78 // Called on main Java thread to initialize URLRequestContext.
79 void InitRequestContextOnMainThread();
82 // Initializes |context_| on the Network thread.
83 void InitializeOnNetworkThread(scoped_ptr
<URLRequestContextConfig
> config
,
84 const base::android::ScopedJavaGlobalRef
<
85 jobject
>& jcronet_url_request_context
);
87 // Runs a task that might depend on the context being initialized.
88 // This method should only be run on the network thread.
89 void RunTaskAfterContextInitOnNetworkThread(
90 const base::Closure
& task_to_run_after_context_init
);
92 scoped_refptr
<base::SingleThreadTaskRunner
> GetNetworkTaskRunner() const;
94 void StartNetLogToFileOnNetworkThread(const std::string
& file_name
,
97 void StopNetLogOnNetworkThread();
99 // Gets the file thread. Create one if there is none.
100 base::Thread
* GetFileThread();
102 // Network thread is owned by |this|, but is destroyed from java thread.
103 base::Thread
* network_thread_
;
105 // File thread should be destroyed last.
106 scoped_ptr
<base::Thread
> file_thread_
;
108 // |write_to_file_observer_| and |context_| should only be accessed on
110 scoped_ptr
<net::WriteToFileNetLogObserver
> write_to_file_observer_
;
112 // |pref_service_| should outlive the HttpServerPropertiesManager owned by
114 scoped_ptr
<PrefService
> pref_service_
;
115 scoped_ptr
<net::NetLog
> net_log_
;
116 scoped_ptr
<net::URLRequestContext
> context_
;
117 scoped_ptr
<net::ProxyConfigService
> proxy_config_service_
;
118 scoped_refptr
<JsonPrefStore
> json_pref_store_
;
119 net::HttpServerPropertiesManager
* http_server_properties_manager_
;
121 // |sdch_owner_| should be destroyed before |json_pref_store_|, because
122 // tearing down |sdch_owner_| forces |json_pref_store_| to flush pending
123 // writes to the disk.
124 scoped_ptr
<net::SdchOwner
> sdch_owner_
;
126 // Context config is only valid until context is initialized.
127 scoped_ptr
<URLRequestContextConfig
> context_config_
;
129 // A queue of tasks that need to be run after context has been initialized.
130 std::queue
<base::Closure
> tasks_waiting_for_context_
;
131 bool is_context_initialized_
;
132 int default_load_flags_
;
134 #if defined(DATA_REDUCTION_PROXY_SUPPORT)
135 scoped_ptr
<CronetDataReductionProxy
> data_reduction_proxy_
;
138 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestContextAdapter
);
141 } // namespace cronet
143 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_