Revert of Refactor the avatar button/icon class (patchset #14 id:320001 of https...
[chromium-blink-merge.git] / components / cronet / android / cronet_upload_data_stream_delegate.h
blob1c3b9b2285d2e2bb7f796aa784676a1ed3d3c440
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_
8 #include <jni.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"
17 namespace base {
18 class SingleThreadTaskRunner;
19 } // namespace base
21 namespace cronet {
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
25 // has completed.
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 {
37 public:
38 CronetUploadDataStreamDelegate(JNIEnv* env, jobject jupload_data_stream);
39 ~CronetUploadDataStreamDelegate() override;
41 // CronetUploadDataStreamAdapter::Delegate implementation. Called on network
42 // thread.
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,
51 jobject obj,
52 int bytes_read,
53 bool final_chunk);
54 void OnRewindSucceeded(JNIEnv* env, jobject obj);
56 private:
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
66 // received.
67 scoped_refptr<net::IOBuffer> buffer_;
69 DISALLOW_COPY_AND_ASSIGN(CronetUploadDataStreamDelegate);
72 // Explicitly register static JNI functions.
73 bool CronetUploadDataStreamDelegateRegisterJni(JNIEnv* env);
75 } // namespace cronet
77 #endif // COMPONENTS_CRONET_ANDROID_CRONET_UPLOAD_DATA_STREAM_DELEGATE_H_