1 // Copyright 2015 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_UPLOAD_DATA_STREAM_ADAPTER_H_
6 #define COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_ADAPTER_H_
10 #include "base/android/scoped_java_ref.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "components/cronet/android/cronet_upload_data_stream.h"
15 #include "net/base/io_buffer.h"
18 class SingleThreadTaskRunner
;
23 // The Adapter holds onto a reference to the IOBuffer that is currently being
24 // written to in Java, so may not be deleted until any read operation in Java
27 // The Adapter is owned by the Java CronetUploadDataStream, and also owns a
28 // reference to it. The Adapter is only destroyed after the net::URLRequest
29 // destroys the C++ CronetUploadDataStream and the Java CronetUploadDataStream
30 // has no read operation pending, at which point it also releases its reference
31 // to the Java CronetUploadDataStream.
33 // Failures don't go back through the Adapter, but directly to the Java request
34 // object, since normally reads aren't allowed to fail during an upload.
35 class CronetUploadDataStreamAdapter
: public CronetUploadDataStream::Delegate
{
37 CronetUploadDataStreamAdapter(JNIEnv
* env
, jobject jupload_data_stream
);
38 ~CronetUploadDataStreamAdapter() override
;
40 // CronetUploadDataStream::Delegate implementation. Called on network thread.
41 void InitializeOnNetworkThread(
42 base::WeakPtr
<CronetUploadDataStream
> upload_data_stream
) override
;
43 void Read(net::IOBuffer
* buffer
, int buf_len
) override
;
44 void Rewind() override
;
45 void OnUploadDataStreamDestroyed() override
;
47 // Callbacks from Java, called on some Java thread.
48 void OnReadSucceeded(JNIEnv
* env
,
52 void OnRewindSucceeded(JNIEnv
* env
, jobject obj
);
55 // Initialized on construction, effectively constant.
56 base::android::ScopedJavaGlobalRef
<jobject
> jupload_data_stream_
;
58 // These are initialized in InitializeOnNetworkThread, so are safe to access
59 // during Java callbacks, which all happen after initialization.
60 scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner_
;
61 base::WeakPtr
<CronetUploadDataStream
> upload_data_stream_
;
63 // Used to keep the read buffer alive until the callback from Java has been
65 scoped_refptr
<net::IOBuffer
> buffer_
;
67 DISALLOW_COPY_AND_ASSIGN(CronetUploadDataStreamAdapter
);
70 // Explicitly register static JNI functions.
71 bool CronetUploadDataStreamAdapterRegisterJni(JNIEnv
* env
);
75 #endif // COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_ADAPTER_H_