| 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; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.ConditionVariable; | 8 import android.os.ConditionVariable; |
| 9 import android.os.Process; | 9 import android.os.Process; |
| 10 import android.util.Log; | 10 import android.util.Log; |
| 11 | 11 |
| 12 import org.chromium.base.CalledByNative; | 12 import org.chromium.base.CalledByNative; |
| 13 import org.chromium.base.JNINamespace; | 13 import org.chromium.base.JNINamespace; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * Provides context for the native HTTP operations. | 16 * Provides context for the native HTTP operations. |
| 17 */ | 17 */ |
| 18 @JNINamespace("cronet") | 18 @JNINamespace("cronet") |
| 19 public class UrlRequestContext { | 19 public class UrlRequestContext { |
| 20 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 20 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
| 21 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | 21 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) |
| 22 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) | 22 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) |
| 23 private static final String LOG_TAG = "ChromiumNetwork"; | 23 private static final String LOG_TAG = "ChromiumNetwork"; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * Native peer object, owned by UrlRequestContext. | 26 * Native adapter object, owned by UrlRequestContext. |
| 27 */ | 27 */ |
| 28 private long mUrlRequestContextPeer; | 28 private long mUrlRequestContextAdapter; |
| 29 | 29 |
| 30 private final ConditionVariable mStarted = new ConditionVariable(); | 30 private final ConditionVariable mStarted = new ConditionVariable(); |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * Constructor. | 33 * Constructor. |
| 34 * | 34 * |
| 35 */ | 35 */ |
| 36 protected UrlRequestContext(Context context, String userAgent, | 36 protected UrlRequestContext(Context context, String userAgent, |
| 37 String config) { | 37 String config) { |
| 38 mUrlRequestContextPeer = nativeCreateRequestContextPeer(context, | 38 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(context, |
| 39 userAgent, getLoggingLevel(), config); | 39 userAgent, getLoggingLevel(), config); |
| 40 if (mUrlRequestContextPeer == 0) | 40 if (mUrlRequestContextAdapter == 0) |
| 41 throw new NullPointerException("Context Peer creation failed"); | 41 throw new NullPointerException("Context Adapter creation failed"); |
| 42 | 42 |
| 43 // TODO(mef): Revisit the need of block here. | 43 // TODO(mef): Revisit the need of block here. |
| 44 mStarted.block(2000); | 44 mStarted.block(2000); |
| 45 } | 45 } |
| 46 | 46 |
| 47 /** | 47 /** |
| 48 * Returns the version of this network stack formatted as N.N.N.N/X where | 48 * Returns the version of this network stack formatted as N.N.N.N/X where |
| 49 * N.N.N.N is the version of Chromium and X is the revision number. | 49 * N.N.N.N is the version of Chromium and X is the revision number. |
| 50 */ | 50 */ |
| 51 public static String getVersion() { | 51 public static String getVersion() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 68 return nativeGetStatisticsJSON(filter); | 68 return nativeGetStatisticsJSON(filter); |
| 69 } | 69 } |
| 70 | 70 |
| 71 /** | 71 /** |
| 72 * Starts NetLog logging to a file named |fileName| in the | 72 * Starts NetLog logging to a file named |fileName| in the |
| 73 * application temporary directory. |fileName| must not be empty. Log level | 73 * application temporary directory. |fileName| must not be empty. Log level |
| 74 * is LOG_ALL_BUT_BYTES. If the file exists it is truncated before starting. | 74 * is LOG_ALL_BUT_BYTES. If the file exists it is truncated before starting. |
| 75 * If actively logging the call is ignored. | 75 * If actively logging the call is ignored. |
| 76 */ | 76 */ |
| 77 public void startNetLogToFile(String fileName) { | 77 public void startNetLogToFile(String fileName) { |
| 78 nativeStartNetLogToFile(mUrlRequestContextPeer, fileName); | 78 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName); |
| 79 } | 79 } |
| 80 | 80 |
| 81 /** | 81 /** |
| 82 * Stops NetLog logging and flushes file to disk. If a logging session is | 82 * Stops NetLog logging and flushes file to disk. If a logging session is |
| 83 * not in progress this call is ignored. | 83 * not in progress this call is ignored. |
| 84 */ | 84 */ |
| 85 public void stopNetLog() { | 85 public void stopNetLog() { |
| 86 nativeStopNetLog(mUrlRequestContextPeer); | 86 nativeStopNetLog(mUrlRequestContextAdapter); |
| 87 } | 87 } |
| 88 | 88 |
| 89 @CalledByNative | 89 @CalledByNative |
| 90 private void initNetworkThread() { | 90 private void initNetworkThread() { |
| 91 Thread.currentThread().setName("ChromiumNet"); | 91 Thread.currentThread().setName("ChromiumNet"); |
| 92 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); | 92 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 93 mStarted.open(); | 93 mStarted.open(); |
| 94 } | 94 } |
| 95 | 95 |
| 96 @Override | 96 @Override |
| 97 protected void finalize() throws Throwable { | 97 protected void finalize() throws Throwable { |
| 98 nativeReleaseRequestContextPeer(mUrlRequestContextPeer); | 98 nativeReleaseRequestContextAdapter(mUrlRequestContextAdapter); |
| 99 super.finalize(); | 99 super.finalize(); |
| 100 } | 100 } |
| 101 | 101 |
| 102 protected long getUrlRequestContextPeer() { | 102 protected long getUrlRequestContextAdapter() { |
| 103 return mUrlRequestContextPeer; | 103 return mUrlRequestContextAdapter; |
| 104 } | 104 } |
| 105 | 105 |
| 106 /** | 106 /** |
| 107 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and | 107 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and |
| 108 * {@link #LOG_VERBOSE}. | 108 * {@link #LOG_VERBOSE}. |
| 109 */ | 109 */ |
| 110 private int getLoggingLevel() { | 110 private int getLoggingLevel() { |
| 111 int loggingLevel; | 111 int loggingLevel; |
| 112 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { | 112 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { |
| 113 loggingLevel = LOG_VERBOSE; | 113 loggingLevel = LOG_VERBOSE; |
| 114 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { | 114 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { |
| 115 loggingLevel = LOG_DEBUG; | 115 loggingLevel = LOG_DEBUG; |
| 116 } else { | 116 } else { |
| 117 loggingLevel = LOG_NONE; | 117 loggingLevel = LOG_NONE; |
| 118 } | 118 } |
| 119 return loggingLevel; | 119 return loggingLevel; |
| 120 } | 120 } |
| 121 | 121 |
| 122 // Returns an instance URLRequestContextPeer to be stored in | 122 // Returns an instance URLRequestContextAdapter to be stored in |
| 123 // mUrlRequestContextPeer. | 123 // mUrlRequestContextAdapter. |
| 124 private native long nativeCreateRequestContextPeer(Context context, | 124 private native long nativeCreateRequestContextAdapter(Context context, |
| 125 String userAgent, int loggingLevel, String config); | 125 String userAgent, int loggingLevel, String config); |
| 126 | 126 |
| 127 private native void nativeReleaseRequestContextPeer( | 127 private native void nativeReleaseRequestContextAdapter( |
| 128 long urlRequestContextPeer); | 128 long urlRequestContextAdapter); |
| 129 | 129 |
| 130 private native void nativeInitializeStatistics(); | 130 private native void nativeInitializeStatistics(); |
| 131 | 131 |
| 132 private native String nativeGetStatisticsJSON(String filter); | 132 private native String nativeGetStatisticsJSON(String filter); |
| 133 | 133 |
| 134 private native void nativeStartNetLogToFile(long urlRequestContextPeer, | 134 private native void nativeStartNetLogToFile(long urlRequestContextAdapter, |
| 135 String fileName); | 135 String fileName); |
| 136 | 136 |
| 137 private native void nativeStopNetLog(long urlRequestContextPeer); | 137 private native void nativeStopNetLog(long urlRequestContextAdapter); |
| 138 } | 138 } |
| OLD | NEW |