| OLD | NEW |
| 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; | 5 package org.chromium.net.impl; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Handler; | 8 import android.os.Handler; |
| 9 import android.os.Looper; | 9 import android.os.Looper; |
| 10 import android.os.Process; | 10 import android.os.Process; |
| 11 import android.util.Log; | 11 import android.util.Log; |
| 12 | 12 |
| 13 import org.chromium.base.VisibleForTesting; |
| 13 import org.chromium.base.annotations.CalledByNative; | 14 import org.chromium.base.annotations.CalledByNative; |
| 14 import org.chromium.base.annotations.JNINamespace; | 15 import org.chromium.base.annotations.JNINamespace; |
| 16 import org.chromium.net.CronetEngine; |
| 15 | 17 |
| 16 /** | 18 /** |
| 17 * Provides context for the native HTTP operations. | 19 * Provides context for the native HTTP operations. |
| 18 * @deprecated Use {@link CronetEngine} instead. | 20 * @deprecated Use {@link CronetEngine} instead. |
| 19 */ | 21 */ |
| 20 @JNINamespace("cronet") | 22 @JNINamespace("cronet") |
| 21 @Deprecated | 23 @Deprecated |
| 22 public class ChromiumUrlRequestContext { | 24 public class ChromiumUrlRequestContext { |
| 23 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 25 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
| 24 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | 26 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) |
| 25 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) | 27 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) |
| 26 static final String LOG_TAG = "ChromiumNetwork"; | 28 static final String LOG_TAG = "ChromiumNetwork"; |
| 27 | 29 |
| 28 /** | 30 /** |
| 29 * Native adapter object, owned by ChromiumUrlRequestContext. | 31 * Native adapter object, owned by ChromiumUrlRequestContext. |
| 30 */ | 32 */ |
| 31 private long mChromiumUrlRequestContextAdapter; | 33 private long mChromiumUrlRequestContextAdapter; |
| 32 | 34 |
| 33 /** | 35 /** |
| 34 * Constructor. | 36 * Constructor. |
| 35 */ | 37 */ |
| 36 protected ChromiumUrlRequestContext( | 38 protected ChromiumUrlRequestContext( |
| 37 final Context context, String userAgent, CronetEngine.Builder config
) { | 39 final Context context, String userAgent, CronetEngine.Builder config
) { |
| 38 CronetLibraryLoader.ensureInitialized(context, config); | 40 CronetLibraryLoader.ensureInitialized(context, config); |
| 39 mChromiumUrlRequestContextAdapter = nativeCreateRequestContextAdapter(us
erAgent, | 41 mChromiumUrlRequestContextAdapter = nativeCreateRequestContextAdapter(us
erAgent, |
| 40 getLoggingLevel(), | 42 getLoggingLevel(), |
| 41 CronetUrlRequestContext.createNativeUrlRequestContextConfig(cont
ext, config)); | 43 CronetUrlRequestContext.createNativeUrlRequestContextConfig(cont
ext, config)); |
| 42 if (mChromiumUrlRequestContextAdapter == 0) { | 44 if (mChromiumUrlRequestContextAdapter == 0) { |
| 43 throw new NullPointerException("Context Adapter creation failed"); | 45 throw new NullPointerException("Context Adapter creation failed"); |
| 44 } | 46 } |
| 45 // Post a task to UI thread to init native Chromium URLRequestContext. | 47 // Post a task to UI thread to init native Chromium URLRequestContext. |
| 46 // TODO(xunjieli): This constructor is not supposed to be invoked on | 48 // TODO(xunjieli): This constructor is not supposed to be invoked on |
| 47 // the main thread. Consider making the following code into a blocking | 49 // the main thread. Consider making the following code into a blocking |
| 48 // API to handle the case where we are already on main thread. | 50 // API to handle the case where we are already on main thread. |
| 49 Runnable task = new Runnable() { | 51 Runnable task = new Runnable() { |
| 50 public void run() { | 52 public void run() { |
| 51 nativeInitRequestContextOnMainThread( | 53 nativeInitRequestContextOnMainThread(mChromiumUrlRequestContextA
dapter); |
| 52 mChromiumUrlRequestContextAdapter); | |
| 53 } | 54 } |
| 54 }; | 55 }; |
| 55 new Handler(Looper.getMainLooper()).post(task); | 56 new Handler(Looper.getMainLooper()).post(task); |
| 56 } | 57 } |
| 57 | 58 |
| 58 /** | 59 /** |
| 59 * Returns the version of this network stack formatted as N.N.N.N/X where | 60 * Returns the version of this network stack formatted as N.N.N.N/X where |
| 60 * N.N.N.N is the version of Chromium and X is the revision number. | 61 * N.N.N.N is the version of Chromium and X is the revision number. |
| 61 */ | 62 */ |
| 62 public static String getVersion() { | 63 public static String getVersion() { |
| 63 return Version.getVersion(); | 64 return ImplVersion.getVersion(); |
| 64 } | 65 } |
| 65 | 66 |
| 66 /** | 67 /** |
| 67 * Initializes statistics recorder. | 68 * Initializes statistics recorder. |
| 68 */ | 69 */ |
| 69 public void initializeStatistics() { | 70 public void initializeStatistics() { |
| 70 nativeInitializeStatistics(); | 71 nativeInitializeStatistics(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 /** | 74 /** |
| (...skipping 11 matching lines...) Expand all Loading... |
| 85 * The IncludeSocketBytes() mode includes basic events, user cookies, | 86 * The IncludeSocketBytes() mode includes basic events, user cookies, |
| 86 * credentials and all transferred bytes in the log. | 87 * credentials and all transferred bytes in the log. |
| 87 * @param fileName The complete file path. It must not be empty. If file | 88 * @param fileName The complete file path. It must not be empty. If file |
| 88 * exists, it is truncated before starting. If actively logging, | 89 * exists, it is truncated before starting. If actively logging, |
| 89 * this method is ignored. | 90 * this method is ignored. |
| 90 * @param logAll {@code true} to use the | 91 * @param logAll {@code true} to use the |
| 91 * NetLogCaptureMode::IncludeSocketBytes() logging level. If | 92 * NetLogCaptureMode::IncludeSocketBytes() logging level. If |
| 92 * false, NetLogCaptureMode::Default() is used instead. | 93 * false, NetLogCaptureMode::Default() is used instead. |
| 93 */ | 94 */ |
| 94 public void startNetLogToFile(String fileName, boolean logAll) { | 95 public void startNetLogToFile(String fileName, boolean logAll) { |
| 95 nativeStartNetLogToFile(mChromiumUrlRequestContextAdapter, fileName, | 96 nativeStartNetLogToFile(mChromiumUrlRequestContextAdapter, fileName, log
All); |
| 96 logAll); | |
| 97 } | 97 } |
| 98 | 98 |
| 99 /** | 99 /** |
| 100 * Stops NetLog logging and flushes file to disk. If a logging session is | 100 * Stops NetLog logging and flushes file to disk. If a logging session is |
| 101 * not in progress, this call is ignored. | 101 * not in progress, this call is ignored. |
| 102 */ | 102 */ |
| 103 public void stopNetLog() { | 103 public void stopNetLog() { |
| 104 nativeStopNetLog(mChromiumUrlRequestContextAdapter); | 104 nativeStopNetLog(mChromiumUrlRequestContextAdapter); |
| 105 } | 105 } |
| 106 | 106 |
| 107 @CalledByNative | 107 @CalledByNative |
| 108 private void initNetworkThread() { | 108 private void initNetworkThread() { |
| 109 Thread.currentThread().setName("ChromiumNet"); | 109 Thread.currentThread().setName("ChromiumNet"); |
| 110 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); | 110 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 111 } | 111 } |
| 112 | 112 |
| 113 @Override | 113 @Override |
| 114 protected void finalize() throws Throwable { | 114 protected void finalize() throws Throwable { |
| 115 if (mChromiumUrlRequestContextAdapter != 0) { | 115 if (mChromiumUrlRequestContextAdapter != 0) { |
| 116 nativeReleaseRequestContextAdapter(mChromiumUrlRequestContextAdapter
); | 116 nativeReleaseRequestContextAdapter(mChromiumUrlRequestContextAdapter
); |
| 117 } | 117 } |
| 118 super.finalize(); | 118 super.finalize(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 protected long getUrlRequestContextAdapter() { | 121 @VisibleForTesting |
| 122 public long getUrlRequestContextAdapter() { |
| 122 return mChromiumUrlRequestContextAdapter; | 123 return mChromiumUrlRequestContextAdapter; |
| 123 } | 124 } |
| 124 | 125 |
| 125 /** | 126 /** |
| 126 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and | 127 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and |
| 127 * {@link #LOG_VERBOSE}. | 128 * {@link #LOG_VERBOSE}. |
| 128 */ | 129 */ |
| 129 private int getLoggingLevel() { | 130 private int getLoggingLevel() { |
| 130 int loggingLevel; | 131 int loggingLevel; |
| 131 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { | 132 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { |
| 132 loggingLevel = LOG_VERBOSE; | 133 loggingLevel = LOG_VERBOSE; |
| 133 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { | 134 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { |
| 134 loggingLevel = LOG_DEBUG; | 135 loggingLevel = LOG_DEBUG; |
| 135 } else { | 136 } else { |
| 136 loggingLevel = LOG_NONE; | 137 loggingLevel = LOG_NONE; |
| 137 } | 138 } |
| 138 return loggingLevel; | 139 return loggingLevel; |
| 139 } | 140 } |
| 140 | 141 |
| 141 // Returns an instance ChromiumUrlRequestContextAdapter to be stored in | 142 // Returns an instance ChromiumUrlRequestContextAdapter to be stored in |
| 142 // mChromiumUrlRequestContextAdapter. | 143 // mChromiumUrlRequestContextAdapter. |
| 143 private native long nativeCreateRequestContextAdapter( | 144 private native long nativeCreateRequestContextAdapter( |
| 144 String userAgent, int loggingLevel, long config); | 145 String userAgent, int loggingLevel, long config); |
| 145 | 146 |
| 146 private native void nativeReleaseRequestContextAdapter( | 147 private native void nativeReleaseRequestContextAdapter(long chromiumUrlReque
stContextAdapter); |
| 147 long chromiumUrlRequestContextAdapter); | |
| 148 | 148 |
| 149 private native void nativeInitializeStatistics(); | 149 private native void nativeInitializeStatistics(); |
| 150 | 150 |
| 151 private native String nativeGetStatisticsJSON(String filter); | 151 private native String nativeGetStatisticsJSON(String filter); |
| 152 | 152 |
| 153 private native void nativeStartNetLogToFile( | 153 private native void nativeStartNetLogToFile( |
| 154 long chromiumUrlRequestContextAdapter, String fileName, | 154 long chromiumUrlRequestContextAdapter, String fileName, boolean logA
ll); |
| 155 boolean logAll); | |
| 156 | 155 |
| 157 private native void nativeStopNetLog(long chromiumUrlRequestContextAdapter); | 156 private native void nativeStopNetLog(long chromiumUrlRequestContextAdapter); |
| 158 | 157 |
| 159 private native void nativeInitRequestContextOnMainThread( | 158 private native void nativeInitRequestContextOnMainThread(long chromiumUrlReq
uestContextAdapter); |
| 160 long chromiumUrlRequestContextAdapter); | |
| 161 } | 159 } |
| OLD | NEW |