FileDocCategorySizeDatePackage
Network.javaAPI DocAndroid 1.5 API10207Wed May 06 22:41:56 BST 2009android.webkit

Network

public class Network extends Object

Fields Summary
private static final String
LOGTAG
private static Network
sNetwork
Static instance of a Network object.
private static boolean
sPlatformNotifications
Flag to store the state of platform notifications, for the case when the Network object has not been constructed yet
private static int
sPlatformNotificationEnableRefCount
Reference count for platform notifications as the network class is a static and can exist over multiple activities, thus over multiple onPause/onResume pairs.
private String
mProxyUsername
Proxy username if known (used for pre-emptive proxy authentication).
private String
mProxyPassword
Proxy password if known (used for pre-emptive proxy authentication).
private RequestQueue
mRequestQueue
Network request queue (requests are added from the browser thread).
private SslErrorHandler
mSslErrorHandler
SSL error handler: takes care of synchronization of multiple async loaders with SSL-related problems.
private HttpAuthHandler
mHttpAuthHandler
HTTP authentication handler: takes care of synchronization of HTTP authentication requests.
Constructors Summary
private Network(android.content.Context context)
Creates a new Network object. XXX: Must be created in the same thread as WebCore!!!!!

        if (Config.DEBUG) {
            Assert.assertTrue(Thread.currentThread().
                    getName().equals(WebViewCore.THREAD_NAME));
        }
        mSslErrorHandler = new SslErrorHandler(this);
        mHttpAuthHandler = new HttpAuthHandler(this);

        mRequestQueue = new RequestQueue(context);
    
Methods Summary
public voidclearUserSslPrefTable()
Clears user SSL-error preference table.

        mSslErrorHandler.clear();
    
public static voiddisablePlatformNotifications()
If platform notifications are enabled, this should be called from onPause() or onStop()

        if (--sPlatformNotificationEnableRefCount == 0) {
            if (sNetwork != null) {
                sNetwork.mRequestQueue.disablePlatformNotifications();
            } else {
                sPlatformNotifications = false;
            }
        }
    
public static voidenablePlatformNotifications()
Enables data state and proxy tracking

        if (++sPlatformNotificationEnableRefCount == 1) {
            if (sNetwork != null) {
                sNetwork.mRequestQueue.enablePlatformNotifications();
            } else {
                sPlatformNotifications = true;
            }
        }
    
public static synchronized android.webkit.NetworkgetInstance(android.content.Context context)

return
The singleton instance of the network.


                
          
        if (sNetwork == null) {
            // Note Context of the Application is used here, rather than
            // the what is passed in (usually a Context derived from an 
            // Activity) so the intent receivers belong to the application
            // rather than an activity - this fixes the issue where 
            // Activities are created and destroyed during the lifetime of
            // an Application
            sNetwork = new Network(context.getApplicationContext());
            if (sPlatformNotifications) {
                // Adjust the ref count before calling enable as it is already
                // taken into account when the static function was called 
                // directly
                --sPlatformNotificationEnableRefCount;
                enablePlatformNotifications();
            }
        }
        return sNetwork;
    
public java.lang.StringgetProxyHostname()
Get the proxy hostname.

return
The proxy hostname obtained from the network queue and proxy settings.

        return mRequestQueue.getProxyHost().getHostName();
    
public synchronized java.lang.StringgetProxyPassword()

return
The proxy password or null if none.

        return mProxyPassword;
    
public synchronized java.lang.StringgetProxyUsername()

return
The proxy username or null if none.

        return mProxyUsername;
    
public voidhandleAuthRequest(LoadListener loader)
Handles authentication requests on their way up to the user (the user must provide credentials).

param
loader The loader that resulted in an HTTP authentication request.

        if (Config.DEBUG) Assert.assertNotNull(loader);
        if (loader != null) {
            mHttpAuthHandler.handleAuthRequest(loader);
        }
    
public voidhandleSslErrorRequest(LoadListener loader)
Handles SSL error(s) on the way up to the user: the user must decide whether errors should be ignored or not.

param
loader The loader that resulted in SSL errors.

        if (Config.DEBUG) Assert.assertNotNull(loader);
        if (loader != null) {
            mSslErrorHandler.handleSslErrorRequest(loader);
        }
    
public booleanisValidProxySet()

return
True iff there is a valid proxy set.

        // The proxy host and port can be set within a different thread during
        // an Intent broadcast.
        synchronized (mRequestQueue) {
            return mRequestQueue.getProxyHost() != null;
        }
    
public booleanrequestURL(java.lang.String method, java.util.Map headers, byte[] postData, LoadListener loader, boolean isHighPriority)
Request a url from either the network or the file system.

param
url The url to load.
param
method The http method.
param
headers The http headers.
param
postData The body of the request.
param
loader A LoadListener for receiving the results of the request.
param
isHighPriority True if this is high priority request.
return
True if the request was successfully queued.


        String url = loader.url();

        // Not a valid url, return false because we won't service the request!
        if (!URLUtil.isValidUrl(url)) {
            return false;
        }

        // asset, file system or data stream are handled in the other code path.
        // This only handles network request.
        if (URLUtil.isAssetUrl(url) || URLUtil.isFileUrl(url) ||
                URLUtil.isDataUrl(url)) {
            return false;
        }

        /* FIXME: this is lame.  Pass an InputStream in, rather than
           making this lame one here */
        InputStream bodyProvider = null;
        int bodyLength = 0;
        if (postData != null) {
            bodyLength = postData.length;
            bodyProvider = new ByteArrayInputStream(postData);
        }

        RequestQueue q = mRequestQueue;
        if (loader.isSynchronous()) {
            q = new RequestQueue(loader.getContext(), 1);
        }

        RequestHandle handle = q.queueRequest(
                url, loader.getWebAddress(), method, headers, loader,
                bodyProvider, bodyLength, isHighPriority);
        loader.attachRequestHandle(handle);

        if (loader.isSynchronous()) {
            handle.waitUntilComplete();
            loader.loadSynchronousMessages();
            q.shutdown();
        }
        return true;
    
public booleanrestoreState(Bundle inState)
Restores the state of network handlers (user SSL and HTTP-authentication preferences).

param
inState The in-state to load (read) from.
return
True iff succeeds.

        if (Config.LOGV) {
            Log.v(LOGTAG, "Network.restoreState()");
        }

        return mSslErrorHandler.restoreState(inState);
    
public booleansaveState(Bundle outState)
Saves the state of network handlers (user SSL and HTTP-authentication preferences).

param
outState The out-state to save (write) to.
return
True iff succeeds.

        if (Config.LOGV) {
            Log.v(LOGTAG, "Network.saveState()");
        }

        return mSslErrorHandler.saveState(outState);
    
public synchronized voidsetProxyPassword(java.lang.String proxyPassword)
Sets the proxy password.

param
proxyPassword Password to use when connecting through the proxy.

        if (Config.DEBUG) {
            Assert.assertTrue(isValidProxySet());
        }

        mProxyPassword = proxyPassword;
    
public synchronized voidsetProxyUsername(java.lang.String proxyUsername)
Sets the proxy username.

param
proxyUsername Username to use when connecting through the proxy.

        if (Config.DEBUG) {
            Assert.assertTrue(isValidProxySet());
        }

        mProxyUsername = proxyUsername;
    
public voidstartTiming()

        mRequestQueue.startTiming();
    
public voidstopTiming()

        mRequestQueue.stopTiming();