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_DELEGATE_H_
6 #define COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_DELEGATE_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_adapter.h"
15 #include "net/base/io_buffer.h"
18 class SingleThreadTaskRunner
;
23 // The Delegate 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 Delegate is owned by the Java CronetUploadDataStream, and also owns a
28 // reference to it. The Delegate is only destroyed after the URLRequest
29 // destroys the adapter and the CronetUploadDataStream has no read operation
30 // pending, at which point it also releases its reference to the
31 // CronetUploadDataStream.
33 // Failures don't go through the delegate, but directly to the Java request
34 // object, since normally reads aren't allowed to fail during an upload.
35 class CronetUploadDataStreamDelegate
36 : public CronetUploadDataStreamAdapter::Delegate
{
38 CronetUploadDataStreamDelegate(JNIEnv
* env
, jobject jupload_data_stream
);
39 ~CronetUploadDataStreamDelegate() override
;
41 // CronetUploadDataStreamAdapter::Delegate implementation. Called on network
43 void InitializeOnNetworkThread(
44 base::WeakPtr
<CronetUploadDataStreamAdapter
> adapter
) override
;
45 void Read(net::IOBuffer
* buffer
, int buf_len
) override
;
46 void Rewind() override
;
47 void OnAdapterDestroyed() override
;
49 // Callbacks from Java, called on some Java thread.
50 void OnReadSucceeded(JNIEnv
* env
,
54 void OnRewindSucceeded(JNIEnv
* env
, jobject obj
);
57 // Initialized on construction, effectively constant.
58 base::android::ScopedJavaGlobalRef
<jobject
> jupload_data_stream_
;
60 // These are initialized in InitializeOnNetworkThread, so are safe to access
61 // during Java callbacks, which all happen after initialization.
62 scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner_
;
63 base::WeakPtr
<CronetUploadDataStreamAdapter
> adapter_
;
65 // Used to keep the read buffer alive until the callback from Java has been
67 scoped_refptr
<net::IOBuffer
> buffer_
;
69 DISALLOW_COPY_AND_ASSIGN(CronetUploadDataStreamDelegate
);
72 // Explicitly register static JNI functions.
73 bool CronetUploadDataStreamDelegateRegisterJni(JNIEnv
* env
);
77 #endif // COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_DELEGATE_H_