Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
nsPACMan Class Reference

This class provides an abstraction layer above the PAC thread. More...

#include <nsPACMan.h>

Inheritance diagram for nsPACMan:
Inheritance graph
[legend]
Collaboration diagram for nsPACMan:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS nsPACMan ()
void Shutdown ()
 This method may be called to shutdown the PAC manager.
nsresult GetProxyForURI (nsIURI *uri, nsACString &result)
 This method queries a PAC result synchronously.
nsresult AsyncGetProxyForURI (nsIURI *uri, nsPACManCallback *callback)
 This method queries a PAC result asynchronously.
nsresult LoadPACFromURI (nsIURI *pacURI)
 This method may be called to reload the PAC file.
PRBool IsLoading ()
 Returns true if we are currently loading the PAC file.
void onStreamComplete (in nsIStreamLoader loader, in nsISupports ctxt, in nsresult status, in unsigned long resultLength,[const, array, size_is(resultLength)] in octet result)
void getInterface (in nsIIDRef uuid,[iid_is(uuid), retval] out nsQIResult result)
 Retrieves the specified interface pointer.
void onChannelRedirect (in nsIChannel oldChannel, in nsIChannel newChannel, in unsigned long flags)
 Called when a redirect occurs.

Public Attributes

const unsigned long REDIRECT_TEMPORARY = 1 << 0
 This is a temporary redirect.
const unsigned long REDIRECT_PERMANENT = 1 << 1
 This is a permanent redirect.
const unsigned long REDIRECT_INTERNAL = 1 << 2
 This is an internal redirect, i.e.

Private Member Functions

NS_DECL_NSISTREAMLOADEROBSERVER
NS_DECL_NSIINTERFACEREQUESTOR
NS_DECL_NSICHANNELEVENTSINK 
~nsPACMan ()
void CancelExistingLoad ()
 Cancel any existing load if any.
void ProcessPendingQ (nsresult status)
 Process mPendingQ.
nsresult StartLoading ()
 Start loading the PAC file.
void MaybeReloadPAC ()
 Reload the PAC file if there is reason to.
void OnLoadFailure ()
 Called when we fail to load the PAC file.
PRBool IsPACURI (nsIURI *uri)
 Returns true if the given URI matches the URI of our PAC file.
 PR_STATIC_CALLBACK (void *) LoadEvent_Handle(PLEvent *)
 Event fu for calling StartLoading asynchronously.
 PR_STATIC_CALLBACK (void) LoadEvent_Destroy(PLEvent *)

Private Attributes

nsCOMPtr< nsIProxyAutoConfigmPAC
nsCOMPtr< nsIURImPACURI
PRCList mPendingQ
nsCOMPtr< nsIStreamLoadermLoader
PLEventmLoadEvent
PRBool mShutdown
PRTime mScheduledReload
PRUint32 mLoadFailureCount

Detailed Description

This class provides an abstraction layer above the PAC thread.

The methods defined on this class are intended to be called on the main thread only.

Definition at line 74 of file nsPACMan.h.


Constructor & Destructor Documentation

nsPACMan::~nsPACMan ( ) [private]

Definition at line 185 of file nsPACMan.cpp.

{
  NS_ASSERTION(mLoader == nsnull, "pac man not shutdown properly");
  NS_ASSERTION(mPAC == nsnull, "pac man not shutdown properly");
  NS_ASSERTION(PR_CLIST_IS_EMPTY(&mPendingQ), "pac man not shutdown properly");
}

Member Function Documentation

nsresult nsPACMan::AsyncGetProxyForURI ( nsIURI uri,
nsPACManCallback *  callback 
)

This method queries a PAC result asynchronously.

The callback runs on the calling thread. If the PAC file has not yet been loaded, then this method will queue up the request, and complete it once the PAC file has been loaded.

Parameters:
uriThe URI to query.
callbackThe callback to run once the PAC result is available.

Definition at line 227 of file nsPACMan.cpp.

{
  NS_ENSURE_STATE(!mShutdown);

  MaybeReloadPAC();

  PendingPACQuery *query = new PendingPACQuery(this, uri, callback);
  if (!query)
    return NS_ERROR_OUT_OF_MEMORY;
  NS_ADDREF(query);
  PR_APPEND_LINK(query, &mPendingQ);

  // If we're waiting for the PAC file to load, then delay starting the query.
  // See OnStreamComplete.  However, if this is the PAC URI then query right
  // away since we know the result will be DIRECT.  We could shortcut some code
  // in this case by issuing the callback directly from here, but that would
  // require extra code, so we just go through the usual async code path.
  if (IsLoading() && !IsPACURI(uri))
    return NS_OK;

  nsresult rv = query->Start();
  if (NS_FAILED(rv)) {
    NS_WARNING("failed to start PAC query");
    PR_REMOVE_LINK(query);
    NS_RELEASE(query);
  }

  return rv;
}

Here is the call graph for this function:

Cancel any existing load if any.

Definition at line 380 of file nsPACMan.cpp.

{
  if (mLoader) {
    nsCOMPtr<nsIRequest> request;
    mLoader->GetRequest(getter_AddRefs(request));
    if (request)
      request->Cancel(NS_ERROR_ABORT);
    mLoader = nsnull;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsIInterfaceRequestor::getInterface ( in nsIIDRef  uuid,
[iid_is(uuid), retval] out nsQIResult  result 
) [inherited]

Retrieves the specified interface pointer.

Parameters:
uuidThe IID of the interface being requested.
result[out] The interface pointer to be filled in if the interface is accessible.
Returns:
NS_OK - interface was successfully returned. NS_NOINTERFACE - interface not accessible. NS_ERROR* - method failure.
nsresult nsPACMan::GetProxyForURI ( nsIURI uri,
nsACString &  result 
)

This method queries a PAC result synchronously.

Parameters:
uriThe URI to query.
resultHolds the PAC result string upon return.
Returns:
NS_ERROR_IN_PROGRESS if the PAC file is not yet loaded.
NS_ERROR_NOT_AVAILABLE if the PAC file could not be loaded.

Definition at line 203 of file nsPACMan.cpp.

{
  NS_ENSURE_STATE(!mShutdown);

  if (IsPACURI(uri)) {
    result.Truncate();
    return NS_OK;
  }

  MaybeReloadPAC();

  if (IsLoading())
    return NS_ERROR_IN_PROGRESS;
  if (!mPAC)
    return NS_ERROR_NOT_AVAILABLE;

  nsCAutoString spec, host;
  uri->GetAsciiSpec(spec);
  uri->GetAsciiHost(host);

  return mPAC->GetProxyForURI(spec, host, result);
}

Here is the call graph for this function:

Returns true if we are currently loading the PAC file.

Definition at line 129 of file nsPACMan.h.

{ return mLoader != nsnull; }

Here is the caller graph for this function:

PRBool nsPACMan::IsPACURI ( nsIURI uri) [inline, private]

Returns true if the given URI matches the URI of our PAC file.

Definition at line 168 of file nsPACMan.h.

                               {
    PRBool result;
    return mPACURI && NS_SUCCEEDED(mPACURI->Equals(uri, &result)) && result;
  }

Here is the caller graph for this function:

This method may be called to reload the PAC file.

While we are loading the PAC file, any asynchronous PAC queries will be queued up to be processed once the PAC file finishes loading.

Parameters:
pacURIThe nsIURI of the PAC file to load.

Definition at line 274 of file nsPACMan.cpp.

{
  NS_ENSURE_STATE(!mShutdown);

  nsCOMPtr<nsIStreamLoader> loader =
      do_CreateInstance(NS_STREAMLOADER_CONTRACTID);
  NS_ENSURE_STATE(loader);

  // Since we might get called from nsProtocolProxyService::Init, we need to
  // post an event back to the main thread before we try to use the IO service.
  //
  // But, we need to flag ourselves as loading, so that we queue up any PAC
  // queries the enter between now and when we actually load the PAC file.

  if (!mLoadEvent) {
    mLoadEvent = new PLEvent;
    if (!mLoadEvent)
      return NS_ERROR_OUT_OF_MEMORY;

    NS_ADDREF_THIS();
    PL_InitEvent(mLoadEvent, this, LoadEvent_Handle, LoadEvent_Destroy);

    nsCOMPtr<nsIEventQueue> eventQ;
    nsresult rv = NS_GetCurrentEventQ(getter_AddRefs(eventQ));
    if (NS_FAILED(rv) || NS_FAILED(rv = eventQ->PostEvent(mLoadEvent))) {
      PL_DestroyEvent(mLoadEvent);
      return rv;
    }
  }

  CancelExistingLoad();

  mLoader = loader;
  mPACURI = pacURI;
  mPAC = nsnull;
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Reload the PAC file if there is reason to.

Definition at line 343 of file nsPACMan.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void nsIChannelEventSink::onChannelRedirect ( in nsIChannel  oldChannel,
in nsIChannel  newChannel,
in unsigned long  flags 
) [inherited]

Called when a redirect occurs.

This may happen due to an HTTP 3xx status code.

Parameters:
oldChannelThe channel that's being redirected.
newChannelThe new channel. This channel is not opened yet.
flagsFlags indicating the type of redirect. A bitmask consisting of flags from above. One of REDIRECT_TEMPORARY and REDIRECT_PERMANENT will always be set.
Exceptions:
<any>Throwing an exception will cancel the load. No network request for the new channel will be made.

Called when we fail to load the PAC file.

Definition at line 355 of file nsPACMan.cpp.

{
  PRInt32 minInterval = 5;    // 5 seconds
  PRInt32 maxInterval = 300;  // 5 minutes

  nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
  if (prefs) {
    prefs->GetIntPref("network.proxy.autoconfig_retry_interval_min",
                      &minInterval);
    prefs->GetIntPref("network.proxy.autoconfig_retry_interval_max",
                      &maxInterval);
  }

  PRInt32 interval = minInterval << mLoadFailureCount++;  // seconds
  if (!interval || interval > maxInterval)
    interval = maxInterval;

#ifdef DEBUG
  printf("PAC load failure: will retry in %d seconds\n", interval);
#endif

  mScheduledReload = PR_Now() + PRInt64(interval) * PR_USEC_PER_SEC;
}

Here is the call graph for this function:

void nsIStreamLoaderObserver::onStreamComplete ( in nsIStreamLoader  loader,
in nsISupports  ctxt,
in nsresult  status,
in unsigned long  resultLength,
[const, array, size_is(resultLength)] in octet  result 
) [inherited]

Event fu for calling StartLoading asynchronously.

void nsPACMan::ProcessPendingQ ( nsresult  status) [private]

Process mPendingQ.

If status is a failure code, then the pending queue will be emptied. If status is a success code, then the pending requests will be processed (i.e., their Start method will be called).

Definition at line 392 of file nsPACMan.cpp.

{
  // Now, start any pending queries
  PRCList *node = PR_LIST_HEAD(&mPendingQ);
  while (node != &mPendingQ) {
    PendingPACQuery *query = NS_STATIC_CAST(PendingPACQuery *, node);
    node = PR_NEXT_LINK(node);
    if (NS_SUCCEEDED(status)) {
      // keep the query in the list (so we can complete it from Shutdown if
      // necessary).
      status = query->Start();
    }
    if (NS_FAILED(status)) {
      // remove the query from the list
      PR_REMOVE_LINK(query);
      query->Complete(status, EmptyCString());
      NS_RELEASE(query);
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

This method may be called to shutdown the PAC manager.

Any async queries that have not yet completed will either finish normally or be canceled by the time this method returns.

Definition at line 193 of file nsPACMan.cpp.

Here is the call graph for this function:

Start loading the PAC file.

Definition at line 313 of file nsPACMan.cpp.

{
  // CancelExistingLoad was called...
  if (!mLoader) {
    ProcessPendingQ(NS_ERROR_ABORT);
    return NS_OK;
  }

  // Always hit the origin server when loading PAC.
  nsCOMPtr<nsIIOService> ios = do_GetIOService();
  if (ios) {
    nsCOMPtr<nsIChannel> channel;

    // NOTE: This results in GetProxyForURI being called
    ios->NewChannelFromURI(mPACURI, getter_AddRefs(channel));

    if (channel) {
      channel->SetLoadFlags(nsIRequest::LOAD_BYPASS_CACHE);
      channel->SetNotificationCallbacks(this);
      if (NS_SUCCEEDED(mLoader->Init(channel, this, nsnull)))
        return NS_OK;
    }
  }

  CancelExistingLoad();
  ProcessPendingQ(NS_ERROR_UNEXPECTED);
  return NS_OK;
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 183 of file nsPACMan.h.

Definition at line 184 of file nsPACMan.h.

Definition at line 187 of file nsPACMan.h.

Definition at line 180 of file nsPACMan.h.

Definition at line 181 of file nsPACMan.h.

PRCList nsPACMan::mPendingQ [private]

Definition at line 182 of file nsPACMan.h.

Definition at line 186 of file nsPACMan.h.

Definition at line 185 of file nsPACMan.h.

const unsigned long nsIChannelEventSink::REDIRECT_INTERNAL = 1 << 2 [inherited]

This is an internal redirect, i.e.

it was not initiated by the remote server, but is specific to the channel implementation.

The new URI may be identical to the old one.

Definition at line 81 of file nsIChannelEventSink.idl.

const unsigned long nsIChannelEventSink::REDIRECT_PERMANENT = 1 << 1 [inherited]

This is a permanent redirect.

New requests for this resource should use the URI of the new channel (This might be an HTTP 301 reponse). If this flag is not set, this is a temporary redirect.

The new URI may be identical to the old one.

Definition at line 73 of file nsIChannelEventSink.idl.

const unsigned long nsIChannelEventSink::REDIRECT_TEMPORARY = 1 << 0 [inherited]

This is a temporary redirect.

New requests for this resource should continue to use the URI of the old channel.

The new URI may be identical to the old one.

Definition at line 64 of file nsIChannelEventSink.idl.


The documentation for this class was generated from the following files: