| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.net; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.os.Process; |
| 9 import android.util.Log; |
| 10 |
| 11 import org.chromium.base.CalledByNative; |
| 12 import org.chromium.base.JNINamespace; |
| 13 |
| 14 import java.util.concurrent.Executor; |
| 15 |
| 16 /** |
| 17 * UrlRequest factory using Chromium HTTP stack implementation. |
| 18 */ |
| 19 @JNINamespace("cronet") |
| 20 class CronetUrlRequestContext implements UrlRequestFactory { |
| 21 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
| 22 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) |
| 23 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) |
| 24 static final String LOG_TAG = "ChromiumNetwork"; |
| 25 |
| 26 private long mUrlRequestContextAdapter = 0; |
| 27 private Thread mNetworkThread; |
| 28 |
| 29 /** |
| 30 * Constructor. |
| 31 * |
| 32 */ |
| 33 CronetUrlRequestContext(Context context, String userAgent, String config) { |
| 34 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( |
| 35 context, userAgent, getLoggingLevel(), config); |
| 36 if (mUrlRequestContextAdapter == 0) |
| 37 throw new NullPointerException("Context Adapter creation failed"); |
| 38 } |
| 39 |
| 40 @Override |
| 41 public UrlRequest createRequest(String url, UrlRequestListener listener, |
| 42 Executor executor) { |
| 43 return new CronetUrlRequest(this, url, |
| 44 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor); |
| 45 } |
| 46 |
| 47 long getCronetUrlRequestContextAdapter() { |
| 48 return mUrlRequestContextAdapter; |
| 49 } |
| 50 |
| 51 /** |
| 52 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and |
| 53 * {@link #LOG_VERBOSE}. |
| 54 */ |
| 55 private int getLoggingLevel() { |
| 56 int loggingLevel; |
| 57 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { |
| 58 loggingLevel = LOG_VERBOSE; |
| 59 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { |
| 60 loggingLevel = LOG_DEBUG; |
| 61 } else { |
| 62 loggingLevel = LOG_NONE; |
| 63 } |
| 64 return loggingLevel; |
| 65 } |
| 66 |
| 67 @CalledByNative |
| 68 private void initNetworkThread() { |
| 69 mNetworkThread = Thread.currentThread(); |
| 70 Thread.currentThread().setName("ChromiumNet"); |
| 71 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 72 } |
| 73 |
| 74 // Native methods are implemented in cronet_url_request_context.cc. |
| 75 private native long nativeCreateRequestContextAdapter(Context context, |
| 76 String userAgent, int loggingLevel, String config); |
| 77 |
| 78 private native void nativeReleaseRequestContextAdapter( |
| 79 long urlRequestContextAdapter); |
| 80 |
| 81 private native void nativeInitializeStatistics(); |
| 82 |
| 83 private native String nativeGetStatisticsJSON(String filter); |
| 84 |
| 85 private native void nativeStartNetLogToFile( |
| 86 long urlRequestContextAdapter, String fileName); |
| 87 |
| 88 private native void nativeStopNetLog(long urlRequestContextAdapter); |
| 89 } |
| OLD | NEW |