xref: /aosp_15_r20/external/cronet/components/cronet/android/cronet_context_adapter.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_CONTEXT_ADAPTER_H_
6 #define COMPONENTS_CRONET_ANDROID_CRONET_CONTEXT_ADAPTER_H_
7 
8 #include <jni.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 
13 #include "base/android/scoped_java_ref.h"
14 #include "base/containers/queue.h"
15 #include "base/functional/callback.h"
16 #include "base/memory/raw_ptr.h"
17 #include "base/threading/thread.h"
18 #include "components/cronet/cronet_context.h"
19 #include "components/prefs/json_pref_store.h"
20 #include "net/base/network_handle.h"
21 #include "net/nqe/effective_connection_type.h"
22 #include "net/nqe/effective_connection_type_observer.h"
23 #include "net/nqe/network_quality_estimator.h"
24 #include "net/nqe/network_quality_observation_source.h"
25 #include "net/nqe/rtt_throughput_estimates_observer.h"
26 
27 namespace net {
28 class NetLog;
29 class URLRequestContext;
30 }  // namespace net
31 
32 namespace cronet {
33 class TestUtil;
34 
35 struct URLRequestContextConfig;
36 
37 // Adapter between Java CronetUrlRequestContext and CronetContext.
38 class CronetContextAdapter : public CronetContext::Callback {
39  public:
40   explicit CronetContextAdapter(
41       std::unique_ptr<URLRequestContextConfig> context_config);
42 
43   CronetContextAdapter(const CronetContextAdapter&) = delete;
44   CronetContextAdapter& operator=(const CronetContextAdapter&) = delete;
45 
46   ~CronetContextAdapter() override;
47 
48   // Called on init Java thread to initialize URLRequestContext.
49   void InitRequestContextOnInitThread(
50       JNIEnv* env,
51       const base::android::JavaParamRef<jobject>& jcaller);
52 
53   // Releases all resources for the request context and deletes the object.
54   // Blocks until network thread is destroyed after running all pending tasks.
55   void Destroy(JNIEnv* env,
56                const base::android::JavaParamRef<jobject>& jcaller);
57 
58   // Posts a task that might depend on the context being initialized
59   // to the network thread.
60   void PostTaskToNetworkThread(const base::Location& posted_from,
61                                base::OnceClosure callback);
62 
63   bool IsOnNetworkThread() const;
64 
65   net::URLRequestContext* GetURLRequestContext(
66       net::handles::NetworkHandle network =
67           net::handles::kInvalidNetworkHandle);
68 
69   // TODO(xunjieli): Keep only one version of StartNetLog().
70 
71   // Starts NetLog logging to file. This can be called on any thread.
72   // Return false if |jfile_name| cannot be opened.
73   bool StartNetLogToFile(JNIEnv* env,
74                          const base::android::JavaParamRef<jobject>& jcaller,
75                          const base::android::JavaParamRef<jstring>& jfile_name,
76                          jboolean jlog_all);
77 
78   // Starts NetLog logging to disk with a bounded amount of disk space. This
79   // can be called on any thread.
80   void StartNetLogToDisk(JNIEnv* env,
81                          const base::android::JavaParamRef<jobject>& jcaller,
82                          const base::android::JavaParamRef<jstring>& jdir_name,
83                          jboolean jlog_all,
84                          jint jmax_size);
85 
86   // Stops NetLog logging to file. This can be called on any thread. This will
87   // flush any remaining writes to disk.
88   void StopNetLog(JNIEnv* env,
89                   const base::android::JavaParamRef<jobject>& jcaller);
90 
91   void FlushWritePropertiesForTesting(
92       JNIEnv* env,
93       const base::android::JavaParamRef<jobject>& jcaller);
94 
95   // Default net::LOAD flags used to create requests.
96   int default_load_flags() const;
97 
98   // Called on init Java thread to initialize URLRequestContext.
99   void InitRequestContextOnInitThread();
100 
101   // Configures the network quality estimator to observe requests to localhost,
102   // to use smaller responses when estimating throughput, and to disable the
103   // device offline checks when computing the effective connection type or when
104   // writing the prefs. This should only be used for testing. This can be
105   // called only after the network quality estimator has been enabled.
106   void ConfigureNetworkQualityEstimatorForTesting(
107       JNIEnv* env,
108       const base::android::JavaParamRef<jobject>& jcaller,
109       jboolean use_local_host_requests,
110       jboolean use_smaller_responses,
111       jboolean disable_offline_check);
112 
113   bool URLRequestContextExistsForTesting(jlong network);
114 
115   // Request that RTT and/or throughput observations should or should not be
116   // provided by the network quality estimator.
117   void ProvideRTTObservations(
118       JNIEnv* env,
119       const base::android::JavaParamRef<jobject>& jcaller,
120       bool should);
121   void ProvideThroughputObservations(
122       JNIEnv* env,
123       const base::android::JavaParamRef<jobject>& jcaller,
124       bool should);
125 
cronet_url_request_context()126   CronetContext* cronet_url_request_context() const { return context_; }
127 
128   // CronetContext::Callback
129   void OnInitNetworkThread() override;
130   void OnDestroyNetworkThread() override;
131   void OnEffectiveConnectionTypeChanged(
132       net::EffectiveConnectionType effective_connection_type) override;
133   void OnRTTOrThroughputEstimatesComputed(
134       int32_t http_rtt_ms,
135       int32_t transport_rtt_ms,
136       int32_t downstream_throughput_kbps) override;
137   void OnRTTObservation(int32_t rtt_ms,
138                         int32_t timestamp_ms,
139                         net::NetworkQualityObservationSource source) override;
140   void OnThroughputObservation(
141       int32_t throughput_kbps,
142       int32_t timestamp_ms,
143       net::NetworkQualityObservationSource source) override;
144   void OnStopNetLogCompleted() override;
145 
146  private:
147   friend class TestUtil;
148 
149   // Native Cronet URL Request Context.
150   raw_ptr<CronetContext> context_;
151 
152   // Java object that owns this CronetContextAdapter.
153   base::android::ScopedJavaGlobalRef<jobject> jcronet_url_request_context_;
154 };
155 
156 }  // namespace cronet
157 
158 #endif  // COMPONENTS_CRONET_ANDROID_CRONET_CONTEXT_ADAPTER_H_
159