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/threading/thread.h"
21 class SingleThreadTaskRunner
;
25 class WriteToFileNetLogObserver
;
26 class URLRequestContext
;
27 class ProxyConfigService
;
33 struct URLRequestContextConfig
;
35 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv
* env
);
37 // Adapter between Java CronetUrlRequestContext and net::URLRequestContext.
38 class CronetURLRequestContextAdapter
{
40 explicit CronetURLRequestContextAdapter(
41 scoped_ptr
<URLRequestContextConfig
> context_config
);
43 ~CronetURLRequestContextAdapter();
45 // Called on main Java thread to initialize URLRequestContext.
46 void InitRequestContextOnMainThread(JNIEnv
* env
, jobject jcaller
);
48 // Releases all resources for the request context and deletes the object.
49 // Blocks until network thread is destroyed after running all pending tasks.
50 void Destroy(JNIEnv
* env
, jobject jcaller
);
52 // Posts a task that might depend on the context being initialized
53 // to the network thread.
54 void PostTaskToNetworkThread(const tracked_objects::Location
& posted_from
,
55 const base::Closure
& callback
);
57 bool IsOnNetworkThread() const;
59 net::URLRequestContext
* GetURLRequestContext();
61 void StartNetLogToFile(JNIEnv
* env
, jobject jcaller
, jstring jfile_name
,
64 void StopNetLog(JNIEnv
* env
, jobject jcaller
);
66 // Default net::LOAD flags used to create requests.
67 int default_load_flags() const { return default_load_flags_
; }
69 // Called on main Java thread to initialize URLRequestContext.
70 void InitRequestContextOnMainThread();
73 // Initializes |context_| on the Network thread.
74 void InitializeOnNetworkThread(scoped_ptr
<URLRequestContextConfig
> config
,
75 const base::android::ScopedJavaGlobalRef
<
76 jobject
>& jcronet_url_request_context
);
78 // Runs a task that might depend on the context being initialized.
79 // This method should only be run on the network thread.
80 void RunTaskAfterContextInitOnNetworkThread(
81 const base::Closure
& task_to_run_after_context_init
);
83 scoped_refptr
<base::SingleThreadTaskRunner
> GetNetworkTaskRunner() const;
85 void StartNetLogToFileOnNetworkThread(const std::string
& file_name
,
88 void StopNetLogOnNetworkThread();
90 // Network thread is owned by |this|, but is destroyed from java thread.
91 base::Thread
* network_thread_
;
92 // |write_to_file_observer_| and |context_| should only be accessed on
94 scoped_ptr
<net::WriteToFileNetLogObserver
> write_to_file_observer_
;
95 scoped_ptr
<net::URLRequestContext
> context_
;
96 scoped_ptr
<net::ProxyConfigService
> proxy_config_service_
;
97 scoped_ptr
<net::SdchOwner
> sdch_owner_
;
99 // Context config is only valid untng context is initialized.
100 scoped_ptr
<URLRequestContextConfig
> context_config_
;
102 // A queue of tasks that need to be run after context has been initialized.
103 std::queue
<base::Closure
> tasks_waiting_for_context_
;
104 bool is_context_initialized_
;
105 int default_load_flags_
;
107 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestContextAdapter
);
110 } // namespace cronet
112 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_