Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequest.java

Issue 2514783002: [Cronet] Add callback wrapper classes to enforce API version checking. (Closed)
Patch Set: fix JNIAdditionalImport Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 package org.chromium.net.impl; 5 package org.chromium.net.impl;
6 6
7 import org.chromium.base.Log; 7 import org.chromium.base.Log;
8 import org.chromium.base.VisibleForTesting; 8 import org.chromium.base.VisibleForTesting;
9 import org.chromium.base.annotations.CalledByNative; 9 import org.chromium.base.annotations.CalledByNative;
10 import org.chromium.base.annotations.JNIAdditionalImport; 10 import org.chromium.base.annotations.JNIAdditionalImport;
(...skipping 21 matching lines...) Expand all
32 /** 32 /**
33 * UrlRequest using Chromium HTTP stack implementation. Could be accessed from 33 * UrlRequest using Chromium HTTP stack implementation. Could be accessed from
34 * any thread on Executor. Cancel can be called from any thread. 34 * any thread on Executor. Cancel can be called from any thread.
35 * All @CallByNative methods are called on native network thread 35 * All @CallByNative methods are called on native network thread
36 * and post tasks with listener calls onto Executor. Upon return from listener 36 * and post tasks with listener calls onto Executor. Upon return from listener
37 * callback native request adapter is called on executive thread and posts 37 * callback native request adapter is called on executive thread and posts
38 * native tasks to native network thread. Because Cancel could be called from 38 * native tasks to native network thread. Because Cancel could be called from
39 * any thread it is protected by mUrlRequestAdapterLock. 39 * any thread it is protected by mUrlRequestAdapterLock.
40 */ 40 */
41 @JNINamespace("cronet") 41 @JNINamespace("cronet")
42 // Qualifies UrlRequest.StatusListener which is used in onStatus, a JNI method. 42 // Qualifies VersionSafeCallbacks.UrlRequestStatusListener which is used in onSt atus, a JNI method.
43 @JNIAdditionalImport(UrlRequest.class) 43 @JNIAdditionalImport(VersionSafeCallbacks.class)
44 @VisibleForTesting 44 @VisibleForTesting
45 public final class CronetUrlRequest extends UrlRequestBase { 45 public final class CronetUrlRequest extends UrlRequestBase {
46 private final boolean mAllowDirectExecutor; 46 private final boolean mAllowDirectExecutor;
47 47
48 /* Native adapter object, owned by UrlRequest. */ 48 /* Native adapter object, owned by UrlRequest. */
49 @GuardedBy("mUrlRequestAdapterLock") 49 @GuardedBy("mUrlRequestAdapterLock")
50 private long mUrlRequestAdapter; 50 private long mUrlRequestAdapter;
51 51
52 @GuardedBy("mUrlRequestAdapterLock") 52 @GuardedBy("mUrlRequestAdapterLock")
53 private boolean mStarted = false; 53 private boolean mStarted = false;
(...skipping 13 matching lines...) Expand all
67 private final Executor mExecutor; 67 private final Executor mExecutor;
68 68
69 /* 69 /*
70 * URL chain contains the URL currently being requested, and 70 * URL chain contains the URL currently being requested, and
71 * all URLs previously requested. New URLs are added before 71 * all URLs previously requested. New URLs are added before
72 * mCallback.onRedirectReceived is called. 72 * mCallback.onRedirectReceived is called.
73 */ 73 */
74 private final List<String> mUrlChain = new ArrayList<String>(); 74 private final List<String> mUrlChain = new ArrayList<String>();
75 private long mReceivedBytesCountFromRedirects; 75 private long mReceivedBytesCountFromRedirects;
76 76
77 private final UrlRequest.Callback mCallback; 77 private final VersionSafeCallbacks.UrlRequestCallback mCallback;
78 private final String mInitialUrl; 78 private final String mInitialUrl;
79 private final int mPriority; 79 private final int mPriority;
80 private String mInitialMethod; 80 private String mInitialMethod;
81 private final HeadersList mRequestHeaders = new HeadersList(); 81 private final HeadersList mRequestHeaders = new HeadersList();
82 private final Collection<Object> mRequestAnnotations; 82 private final Collection<Object> mRequestAnnotations;
83 @RequestFinishedInfoImpl.FinishedReason 83 @RequestFinishedInfoImpl.FinishedReason
84 private int mFinishedReason; 84 private int mFinishedReason;
85 private UrlRequestException mException; 85 private UrlRequestException mException;
86 private final boolean mDisableCache; 86 private final boolean mDisableCache;
87 private final boolean mDisableConnectionMigration; 87 private final boolean mDisableConnectionMigration;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 } 136 }
137 if (executor == null) { 137 if (executor == null) {
138 throw new NullPointerException("Executor is required"); 138 throw new NullPointerException("Executor is required");
139 } 139 }
140 140
141 mAllowDirectExecutor = allowDirectExecutor; 141 mAllowDirectExecutor = allowDirectExecutor;
142 mRequestContext = requestContext; 142 mRequestContext = requestContext;
143 mInitialUrl = url; 143 mInitialUrl = url;
144 mUrlChain.add(url); 144 mUrlChain.add(url);
145 mPriority = convertRequestPriority(priority); 145 mPriority = convertRequestPriority(priority);
146 mCallback = callback; 146 mCallback = new VersionSafeCallbacks.UrlRequestCallback(callback);
147 mExecutor = executor; 147 mExecutor = executor;
148 mRequestAnnotations = requestAnnotations; 148 mRequestAnnotations = requestAnnotations;
149 mDisableCache = disableCache; 149 mDisableCache = disableCache;
150 mDisableConnectionMigration = disableConnectionMigration; 150 mDisableConnectionMigration = disableConnectionMigration;
151 } 151 }
152 152
153 @Override 153 @Override
154 public void setHttpMethod(String method) { 154 public void setHttpMethod(String method) {
155 checkNotStarted(); 155 checkNotStarted();
156 if (method == null) { 156 if (method == null) {
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 return isDoneLocked(); 307 return isDoneLocked();
308 } 308 }
309 } 309 }
310 310
311 @GuardedBy("mUrlRequestAdapterLock") 311 @GuardedBy("mUrlRequestAdapterLock")
312 private boolean isDoneLocked() { 312 private boolean isDoneLocked() {
313 return mStarted && mUrlRequestAdapter == 0; 313 return mStarted && mUrlRequestAdapter == 0;
314 } 314 }
315 315
316 @Override 316 @Override
317 public void getStatus(final UrlRequest.StatusListener listener) { 317 public void getStatus(UrlRequest.StatusListener unsafeListener) {
318 final VersionSafeCallbacks.UrlRequestStatusListener listener =
319 new VersionSafeCallbacks.UrlRequestStatusListener(unsafeListener );
318 synchronized (mUrlRequestAdapterLock) { 320 synchronized (mUrlRequestAdapterLock) {
319 if (mUrlRequestAdapter != 0) { 321 if (mUrlRequestAdapter != 0) {
320 nativeGetStatus(mUrlRequestAdapter, listener); 322 nativeGetStatus(mUrlRequestAdapter, listener);
321 return; 323 return;
322 } 324 }
323 } 325 }
324 Runnable task = new Runnable() { 326 Runnable task = new Runnable() {
325 @Override 327 @Override
326 public void run() { 328 public void run() {
327 listener.onStatus(UrlRequest.Status.INVALID); 329 listener.onStatus(UrlRequest.Status.INVALID);
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 }; 670 };
669 postTaskToExecutor(task); 671 postTaskToExecutor(task);
670 } 672 }
671 673
672 /** 674 /**
673 * Called by the native code when request status is fetched from the 675 * Called by the native code when request status is fetched from the
674 * native stack. 676 * native stack.
675 */ 677 */
676 @SuppressWarnings("unused") 678 @SuppressWarnings("unused")
677 @CalledByNative 679 @CalledByNative
678 private void onStatus(final UrlRequest.StatusListener listener, final int lo adState) { 680 private void onStatus(
681 final VersionSafeCallbacks.UrlRequestStatusListener listener, final int loadState) {
679 Runnable task = new Runnable() { 682 Runnable task = new Runnable() {
680 @Override 683 @Override
681 public void run() { 684 public void run() {
682 listener.onStatus(convertLoadState(loadState)); 685 listener.onStatus(convertLoadState(loadState));
683 } 686 }
684 }; 687 };
685 postTaskToExecutor(task); 688 postTaskToExecutor(task);
686 } 689 }
687 690
688 /** 691 /**
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
770 private native void nativeFollowDeferredRedirect(long nativePtr); 773 private native void nativeFollowDeferredRedirect(long nativePtr);
771 774
772 @NativeClassQualifiedName("CronetURLRequestAdapter") 775 @NativeClassQualifiedName("CronetURLRequestAdapter")
773 private native boolean nativeReadData( 776 private native boolean nativeReadData(
774 long nativePtr, ByteBuffer byteBuffer, int position, int capacity); 777 long nativePtr, ByteBuffer byteBuffer, int position, int capacity);
775 778
776 @NativeClassQualifiedName("CronetURLRequestAdapter") 779 @NativeClassQualifiedName("CronetURLRequestAdapter")
777 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); 780 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled);
778 781
779 @NativeClassQualifiedName("CronetURLRequestAdapter") 782 @NativeClassQualifiedName("CronetURLRequestAdapter")
780 private native void nativeGetStatus(long nativePtr, UrlRequest.StatusListene r listener); 783 private native void nativeGetStatus(
784 long nativePtr, VersionSafeCallbacks.UrlRequestStatusListener listen er);
781 } 785 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698