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
;
26 class URLRequestContext
;
27 class ProxyConfigService
;
32 struct URLRequestContextConfig
;
34 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv
* env
);
36 // Adapter between Java CronetUrlRequestContext and net::URLRequestContext.
37 class CronetURLRequestContextAdapter
{
39 explicit CronetURLRequestContextAdapter(
40 scoped_ptr
<URLRequestContextConfig
> context_config
);
42 ~CronetURLRequestContextAdapter();
44 // Called on main Java thread to initialize URLRequestContext.
45 void InitRequestContextOnMainThread(JNIEnv
* env
, jobject jcaller
);
47 // Releases all resources for the request context and deletes the object.
48 // Blocks until network thread is destroyed after running all pending tasks.
49 void Destroy(JNIEnv
* env
, jobject jcaller
);
51 // Posts a task that might depend on the context being initialized
52 // to the network thread.
53 void PostTaskToNetworkThread(const tracked_objects::Location
& posted_from
,
54 const base::Closure
& callback
);
56 bool IsOnNetworkThread() const;
58 net::URLRequestContext
* GetURLRequestContext();
60 void StartNetLogToFile(JNIEnv
* env
, jobject jcaller
, jstring jfile_name
);
62 void StopNetLog(JNIEnv
* env
, jobject jcaller
);
64 // Default net::LOAD flags used to create requests.
65 int default_load_flags() const { return default_load_flags_
; }
67 // Called on main Java thread to initialize URLRequestContext.
68 void InitRequestContextOnMainThread();
71 // Initializes |context_| on the Network thread.
72 void InitializeOnNetworkThread(scoped_ptr
<URLRequestContextConfig
> config
,
73 const base::android::ScopedJavaGlobalRef
<
74 jobject
>& jcronet_url_request_context
);
76 // Runs a task that might depend on the context being initialized.
77 // This method should only be run on the network thread.
78 void RunTaskAfterContextInitOnNetworkThread(
79 const base::Closure
& task_to_run_after_context_init
);
81 scoped_refptr
<base::SingleThreadTaskRunner
> GetNetworkTaskRunner() const;
83 void StartNetLogToFileOnNetworkThread(const std::string
& file_name
);
85 void StopNetLogOnNetworkThread();
87 // Network thread is owned by |this|, but is destroyed from java thread.
88 base::Thread
* network_thread_
;
89 // |net_log_logger_| and |context_| should only be accessed on network thread.
90 scoped_ptr
<net::NetLogLogger
> net_log_logger_
;
91 scoped_ptr
<net::URLRequestContext
> context_
;
92 scoped_ptr
<net::ProxyConfigService
> proxy_config_service_
;
94 // Context config is only valid untng context is initialized.
95 scoped_ptr
<URLRequestContextConfig
> context_config_
;
97 // A queue of tasks that need to be run after context has been initialized.
98 std::queue
<base::Closure
> tasks_waiting_for_context_
;
99 bool is_context_initialized_
;
100 int default_load_flags_
;
102 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestContextAdapter
);
105 } // namespace cronet
107 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_