Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_ | 5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_ |
| 6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_ | 6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_ |
| 7 | 7 |
| 8 #include <jni.h> | 8 #include <jni.h> |
| 9 | 9 |
| 10 #include <queue> | 10 #include <queue> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/prefs/json_pref_store.h" | 18 #include "base/prefs/json_pref_store.h" |
| 19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 20 #include "base/time/time.h" | |
|
mef
2015/08/28 20:53:16
unused?
bengr
2015/08/28 23:51:36
Done.
| |
| 21 #include "net/base/network_quality_estimator.h" | |
| 20 | 22 |
| 21 class PrefService; | 23 class PrefService; |
| 22 | 24 |
| 23 namespace base { | 25 namespace base { |
| 24 class SingleThreadTaskRunner; | 26 class SingleThreadTaskRunner; |
| 27 class Time; | |
|
mef
2015/08/28 20:53:16
unused?
bengr
2015/08/28 23:51:36
Done.
| |
| 25 } // namespace base | 28 } // namespace base |
| 26 | 29 |
| 27 namespace net { | 30 namespace net { |
| 28 class HttpServerPropertiesManager; | 31 class HttpServerPropertiesManager; |
| 32 class NetworkQualityEstimator; | |
|
mef
2015/08/28 20:53:15
do you need this if you have include?
bengr
2015/08/28 23:51:36
Done.
| |
| 29 class ProxyConfigService; | 33 class ProxyConfigService; |
| 30 class SdchOwner; | 34 class SdchOwner; |
| 31 class URLRequestContext; | 35 class URLRequestContext; |
| 32 class WriteToFileNetLogObserver; | 36 class WriteToFileNetLogObserver; |
| 33 } // namespace net | 37 } // namespace net |
| 34 | 38 |
| 35 namespace cronet { | 39 namespace cronet { |
| 36 | 40 |
| 37 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 41 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
| 38 class CronetDataReductionProxy; | 42 class CronetDataReductionProxy; |
| 39 #endif | 43 #endif |
| 40 | 44 |
| 41 struct URLRequestContextConfig; | 45 struct URLRequestContextConfig; |
| 42 | 46 |
| 43 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env); | 47 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env); |
| 44 | 48 |
| 45 // Adapter between Java CronetUrlRequestContext and net::URLRequestContext. | 49 // Adapter between Java CronetUrlRequestContext and net::URLRequestContext. |
| 46 class CronetURLRequestContextAdapter { | 50 class CronetURLRequestContextAdapter |
| 51 : public net::NetworkQualityEstimator::RTTObserver, | |
| 52 public net::NetworkQualityEstimator::ThroughputObserver { | |
| 47 public: | 53 public: |
| 48 explicit CronetURLRequestContextAdapter( | 54 explicit CronetURLRequestContextAdapter( |
| 49 scoped_ptr<URLRequestContextConfig> context_config); | 55 scoped_ptr<URLRequestContextConfig> context_config); |
| 50 | 56 |
| 51 ~CronetURLRequestContextAdapter(); | 57 ~CronetURLRequestContextAdapter() override; |
| 52 | 58 |
| 53 // Called on main Java thread to initialize URLRequestContext. | 59 // Called on main Java thread to initialize URLRequestContext. |
| 54 void InitRequestContextOnMainThread(JNIEnv* env, jobject jcaller); | 60 void InitRequestContextOnMainThread(JNIEnv* env, jobject jcaller); |
| 55 | 61 |
| 56 // Releases all resources for the request context and deletes the object. | 62 // Releases all resources for the request context and deletes the object. |
| 57 // Blocks until network thread is destroyed after running all pending tasks. | 63 // Blocks until network thread is destroyed after running all pending tasks. |
| 58 void Destroy(JNIEnv* env, jobject jcaller); | 64 void Destroy(JNIEnv* env, jobject jcaller); |
| 59 | 65 |
| 60 // Posts a task that might depend on the context being initialized | 66 // Posts a task that might depend on the context being initialized |
| 61 // to the network thread. | 67 // to the network thread. |
| 62 void PostTaskToNetworkThread(const tracked_objects::Location& posted_from, | 68 void PostTaskToNetworkThread(const tracked_objects::Location& posted_from, |
| 63 const base::Closure& callback); | 69 const base::Closure& callback); |
| 64 | 70 |
| 65 bool IsOnNetworkThread() const; | 71 bool IsOnNetworkThread() const; |
| 66 | 72 |
| 67 net::URLRequestContext* GetURLRequestContext(); | 73 net::URLRequestContext* GetURLRequestContext(); |
| 68 | 74 |
| 69 void StartNetLogToFile(JNIEnv* env, jobject jcaller, jstring jfile_name, | 75 void StartNetLogToFile(JNIEnv* env, jobject jcaller, jstring jfile_name, |
| 70 jboolean jlog_all); | 76 jboolean jlog_all); |
| 71 | 77 |
| 72 void StopNetLog(JNIEnv* env, jobject jcaller); | 78 void StopNetLog(JNIEnv* env, jobject jcaller); |
| 73 | 79 |
| 74 // Default net::LOAD flags used to create requests. | 80 // Default net::LOAD flags used to create requests. |
| 75 int default_load_flags() const { return default_load_flags_; } | 81 int default_load_flags() const { return default_load_flags_; } |
| 76 | 82 |
| 77 // Called on main Java thread to initialize URLRequestContext. | 83 // Called on main Java thread to initialize URLRequestContext. |
| 78 void InitRequestContextOnMainThread(); | 84 void InitRequestContextOnMainThread(); |
| 79 | 85 |
| 86 // Enables the network quality estimator and optionally configures it to | |
| 87 // observe localhost requests, and to consider smaller responses when | |
| 88 // observing throughput. It is recommended that both options be set to false. | |
| 89 void EnableNetworkQualityEstimator(JNIEnv* env, | |
| 90 jobject jcaller, | |
| 91 jboolean use_local_host_requests, | |
| 92 jboolean use_smaller_responses); | |
| 93 | |
| 94 // Request that RTT and/or throughput observations should or should not be | |
| 95 // provided by the network quality estimator. | |
| 96 void ProvideRTTObservations(JNIEnv* env, jobject jcaller, bool should); | |
| 97 void ProvideThroughputObservations(JNIEnv* env, jobject jcaller, bool should); | |
| 98 | |
| 80 private: | 99 private: |
| 81 // Initializes |context_| on the Network thread. | 100 // Initializes |context_| on the Network thread. |
| 82 void InitializeOnNetworkThread(scoped_ptr<URLRequestContextConfig> config, | 101 void InitializeOnNetworkThread(scoped_ptr<URLRequestContextConfig> config, |
| 83 const base::android::ScopedJavaGlobalRef< | 102 const base::android::ScopedJavaGlobalRef< |
| 84 jobject>& jcronet_url_request_context); | 103 jobject>& jcronet_url_request_context); |
| 85 | 104 |
| 86 // Runs a task that might depend on the context being initialized. | 105 // Runs a task that might depend on the context being initialized. |
| 87 // This method should only be run on the network thread. | 106 // This method should only be run on the network thread. |
| 88 void RunTaskAfterContextInitOnNetworkThread( | 107 void RunTaskAfterContextInitOnNetworkThread( |
| 89 const base::Closure& task_to_run_after_context_init); | 108 const base::Closure& task_to_run_after_context_init); |
| 90 | 109 |
| 91 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() const; | 110 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() const; |
| 92 | 111 |
| 93 void StartNetLogToFileOnNetworkThread(const std::string& file_name, | 112 void StartNetLogToFileOnNetworkThread(const std::string& file_name, |
| 94 bool log_all); | 113 bool log_all); |
| 95 | 114 |
| 96 void StopNetLogOnNetworkThread(); | 115 void StopNetLogOnNetworkThread(); |
| 97 | 116 |
| 98 // Gets the file thread. Create one if there is none. | 117 // Gets the file thread. Create one if there is none. |
| 99 base::Thread* GetFileThread(); | 118 base::Thread* GetFileThread(); |
| 100 | 119 |
| 120 // Instantiate and configure the network quality estimator. For default | |
| 121 // behavior, parameters should be set to false; otherwise the estimator | |
| 122 // can be configured to observe requests to localhost, as well as to use | |
| 123 // observe smaller responses when estimating throughput. | |
| 124 void EnableNetworkQualityEstimatorOnNetworkThread( | |
| 125 bool use_local_host_requests, | |
| 126 bool use_smaller_responses); | |
| 127 | |
| 128 void ProvideRTTObservationsOnNetworkThread(bool should); | |
| 129 void ProvideThroughputObservationsOnNetworkThread(bool should); | |
| 130 | |
| 131 // net::NetworkQualityEstimator::RTTObserver implementation. | |
| 132 void OnRTTObservation( | |
| 133 int32_t rtt_ms, | |
| 134 const base::TimeTicks& timestamp, | |
|
mef
2015/08/28 20:53:16
class TimeTicks on top?
bengr
2015/08/28 23:51:36
Done.
| |
| 135 net::NetworkQualityEstimator::ObservationSource source) override; | |
| 136 | |
| 137 // net::NetworkQualityEstimator::ThroughputObserver implementation. | |
| 138 void OnThroughputObservation( | |
| 139 int32_t throughput_kbps, | |
| 140 const base::TimeTicks& timestamp, | |
| 141 net::NetworkQualityEstimator::ObservationSource source) override; | |
| 142 | |
| 101 // Network thread is owned by |this|, but is destroyed from java thread. | 143 // Network thread is owned by |this|, but is destroyed from java thread. |
| 102 base::Thread* network_thread_; | 144 base::Thread* network_thread_; |
| 103 | 145 |
| 104 // File thread should be destroyed last. | 146 // File thread should be destroyed last. |
| 105 scoped_ptr<base::Thread> file_thread_; | 147 scoped_ptr<base::Thread> file_thread_; |
| 106 | 148 |
| 107 // |write_to_file_observer_| and |context_| should only be accessed on | 149 // |write_to_file_observer_| and |context_| should only be accessed on |
| 108 // network thread. | 150 // network thread. |
| 109 scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; | 151 scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; |
| 110 | 152 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 122 scoped_ptr<net::SdchOwner> sdch_owner_; | 164 scoped_ptr<net::SdchOwner> sdch_owner_; |
| 123 | 165 |
| 124 // Context config is only valid until context is initialized. | 166 // Context config is only valid until context is initialized. |
| 125 scoped_ptr<URLRequestContextConfig> context_config_; | 167 scoped_ptr<URLRequestContextConfig> context_config_; |
| 126 | 168 |
| 127 // A queue of tasks that need to be run after context has been initialized. | 169 // A queue of tasks that need to be run after context has been initialized. |
| 128 std::queue<base::Closure> tasks_waiting_for_context_; | 170 std::queue<base::Closure> tasks_waiting_for_context_; |
| 129 bool is_context_initialized_; | 171 bool is_context_initialized_; |
| 130 int default_load_flags_; | 172 int default_load_flags_; |
| 131 | 173 |
| 174 // A network quality estimator. | |
| 175 scoped_ptr<net::NetworkQualityEstimator> network_quality_estimator_; | |
| 176 | |
| 177 base::android::ScopedJavaGlobalRef<jobject> jcronet_url_request_context_; | |
|
mef
2015/08/28 20:53:16
add comment that this is java-side owner?
bengr
2015/08/28 23:51:36
Done.
| |
| 178 | |
| 132 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 179 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
| 133 scoped_ptr<CronetDataReductionProxy> data_reduction_proxy_; | 180 scoped_ptr<CronetDataReductionProxy> data_reduction_proxy_; |
| 134 #endif | 181 #endif |
| 135 | 182 |
| 136 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestContextAdapter); | 183 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestContextAdapter); |
| 137 }; | 184 }; |
| 138 | 185 |
| 139 } // namespace cronet | 186 } // namespace cronet |
| 140 | 187 |
| 141 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_ | 188 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_CONTEXT_ADAPTER_H_ |
| OLD | NEW |