1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set ts=4 sw=4 cindent et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
24 * Prasad Sunkari <prasad@medhas.org>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsIOService.h"
41 #include "nsIProtocolHandler.h"
42 #include "nsIFileProtocolHandler.h"
44 #include "nsIServiceManager.h"
46 #include "nsIStreamListener.h"
49 #include "nsLoadGroup.h"
50 #include "nsInputStreamChannel.h"
51 #include "nsXPIDLString.h"
52 #include "nsReadableUtils.h"
53 #include "nsIErrorService.h"
55 #include "nsIObserverService.h"
56 #include "nsIPrefService.h"
57 #include "nsIPrefBranch2.h"
58 #include "nsIPrefLocalizedString.h"
59 #include "nsICategoryManager.h"
61 #include "nsISupportsPrimitives.h"
62 #include "nsIProxiedProtocolHandler.h"
63 #include "nsIProxyInfo.h"
64 #include "nsITimelineService.h"
67 #include "nsIRecyclingAllocator.h"
68 #include "nsISocketTransport.h"
70 #include "nsINestedURI.h"
71 #include "nsNetUtil.h"
72 #include "nsThreadUtils.h"
73 #include "nsIPermissionManager.h"
76 #include "nsNativeConnectionHelper.h"
79 #define PORT_PREF_PREFIX "network.security.ports."
80 #define PORT_PREF(x) PORT_PREF_PREFIX x
81 #define AUTODIAL_PREF "network.autodial-helper.enabled"
83 #define MAX_RECURSION_COUNT 50
85 nsIOService
* gIOService
= nsnull
;
87 // A general port blacklist. Connections to these ports will not be allowed unless
88 // the protocol overrides.
90 // TODO: I am sure that there are more ports to be added.
91 // This cut is based on the classic mozilla codebase
93 PRInt16 gBadPortList
[] = {
127 135, // loc-srv / epmap
152 0, // This MUST be zero so that we can populating the array
155 static const char kProfileChangeNetTeardownTopic
[] = "profile-change-net-teardown";
156 static const char kProfileChangeNetRestoreTopic
[] = "profile-change-net-restore";
158 // Necko buffer cache
159 nsIMemory
* nsIOService::gBufferCache
= nsnull
;
161 ////////////////////////////////////////////////////////////////////////////////
163 nsIOService::nsIOService()
165 , mOfflineForProfileChange(PR_FALSE
)
166 , mManageOfflineStatus(PR_FALSE
)
167 , mChannelEventSinks(NS_CHANNEL_EVENT_SINK_CATEGORY
)
168 , mContentSniffers(NS_CONTENT_SNIFFER_CATEGORY
)
170 // Get the allocator ready
174 nsCOMPtr
<nsIRecyclingAllocator
> recyclingAllocator
=
175 do_CreateInstance(NS_RECYCLINGALLOCATOR_CONTRACTID
, &rv
);
178 rv
= recyclingAllocator
->Init(NS_NECKO_BUFFER_CACHE_COUNT
,
179 NS_NECKO_15_MINS
, "necko");
183 nsCOMPtr
<nsIMemory
> eyeMemory
= do_QueryInterface(recyclingAllocator
);
184 gBufferCache
= eyeMemory
.get();
185 NS_IF_ADDREF(gBufferCache
);
194 // We need to get references to these services so that we can shut them
195 // down later. If we wait until the nsIOService is being shut down,
196 // GetService will fail at that point.
198 // TODO(darin): Load the Socket and DNS services lazily.
200 mSocketTransportService
= do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID
, &rv
);
202 NS_WARNING("failed to get socket transport service");
206 mDNSService
= do_GetService(NS_DNSSERVICE_CONTRACTID
, &rv
);
208 NS_WARNING("failed to get DNS service");
212 // XXX hack until xpidl supports error info directly (bug 13423)
213 nsCOMPtr
<nsIErrorService
> errorService
= do_GetService(NS_ERRORSERVICE_CONTRACTID
);
215 errorService
->RegisterErrorStringBundle(NS_ERROR_MODULE_NETWORK
, NECKO_MSGS_URL
);
218 NS_WARNING("failed to get error service");
220 // setup our bad port list stuff
221 for(int i
=0; gBadPortList
[i
]; i
++)
222 mRestrictedPortList
.AppendElement(reinterpret_cast<void *>(gBadPortList
[i
]));
224 // Further modifications to the port list come from prefs
225 nsCOMPtr
<nsIPrefBranch2
> prefBranch
;
226 GetPrefBranch(getter_AddRefs(prefBranch
));
228 prefBranch
->AddObserver(PORT_PREF_PREFIX
, this, PR_TRUE
);
229 prefBranch
->AddObserver(AUTODIAL_PREF
, this, PR_TRUE
);
230 PrefsChanged(prefBranch
);
233 // Register for profile change notifications
234 nsCOMPtr
<nsIObserverService
> observerService
=
235 do_GetService("@mozilla.org/observer-service;1");
236 if (observerService
) {
237 observerService
->AddObserver(this, kProfileChangeNetTeardownTopic
, PR_TRUE
);
238 observerService
->AddObserver(this, kProfileChangeNetRestoreTopic
, PR_TRUE
);
239 observerService
->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID
, PR_TRUE
);
240 observerService
->AddObserver(this, NS_NETWORK_LINK_TOPIC
, PR_TRUE
);
243 NS_WARNING("failed to get observer service");
247 // go into managed mode if we can
248 mNetworkLinkService
= do_GetService(NS_NETWORK_LINK_SERVICE_CONTRACTID
);
249 if (mNetworkLinkService
) {
250 mManageOfflineStatus
= PR_TRUE
;
251 TrackNetworkLinkStatusForOffline();
258 nsIOService::~nsIOService()
264 nsIOService::GetInstance() {
266 gIOService
= new nsIOService();
269 NS_ADDREF(gIOService
);
271 nsresult rv
= gIOService
->Init();
273 NS_RELEASE(gIOService
);
278 NS_ADDREF(gIOService
);
282 NS_IMPL_THREADSAFE_ISUPPORTS6(nsIOService
,
287 nsISupportsWeakReference
,
288 nsINetUtil_MOZILLA_1_9_1
)
290 ////////////////////////////////////////////////////////////////////////////////
293 nsIOService::OnChannelRedirect(nsIChannel
* oldChan
, nsIChannel
* newChan
,
296 nsCOMPtr
<nsIChannelEventSink
> sink
=
297 do_GetService(NS_GLOBAL_CHANNELEVENTSINK_CONTRACTID
);
299 nsresult rv
= sink
->OnChannelRedirect(oldChan
, newChan
, flags
);
304 // Finally, our category
305 const nsCOMArray
<nsIChannelEventSink
>& entries
=
306 mChannelEventSinks
.GetEntries();
307 PRInt32 len
= entries
.Count();
308 for (PRInt32 i
= 0; i
< len
; ++i
) {
309 nsresult rv
= entries
[i
]->OnChannelRedirect(oldChan
, newChan
, flags
);
318 nsIOService::CacheProtocolHandler(const char *scheme
, nsIProtocolHandler
*handler
)
320 for (unsigned int i
=0; i
<NS_N(gScheme
); i
++)
322 if (!nsCRT::strcasecmp(scheme
, gScheme
[i
]))
325 NS_ASSERTION(!mWeakHandler
[i
], "Protocol handler already cached");
326 // Make sure the handler supports weak references.
327 nsCOMPtr
<nsISupportsWeakReference
> factoryPtr
= do_QueryInterface(handler
, &rv
);
330 // Don't cache handlers that don't support weak reference as
331 // there is real danger of a circular reference.
333 printf("DEBUG: %s protcol handler doesn't support weak ref. Not cached.\n", scheme
);
334 #endif /* DEBUG_dp */
335 return NS_ERROR_FAILURE
;
337 mWeakHandler
[i
] = do_GetWeakReference(handler
);
341 return NS_ERROR_FAILURE
;
345 nsIOService::GetCachedProtocolHandler(const char *scheme
, nsIProtocolHandler
**result
, PRUint32 start
, PRUint32 end
)
347 PRUint32 len
= end
- start
- 1;
348 for (unsigned int i
=0; i
<NS_N(gScheme
); i
++)
350 if (!mWeakHandler
[i
])
353 // handle unterminated strings
354 // start is inclusive, end is exclusive, len = end - start - 1
355 if (end
? (!nsCRT::strncasecmp(scheme
+ start
, gScheme
[i
], len
)
356 && gScheme
[i
][len
] == '\0')
357 : (!nsCRT::strcasecmp(scheme
, gScheme
[i
])))
359 return CallQueryReferent(mWeakHandler
[i
].get(), result
);
362 return NS_ERROR_FAILURE
;
366 nsIOService::GetProtocolHandler(const char* scheme
, nsIProtocolHandler
* *result
)
370 NS_ENSURE_ARG_POINTER(scheme
);
371 // XXX we may want to speed this up by introducing our own protocol
372 // scheme -> protocol handler mapping, avoiding the string manipulation
373 // and service manager stuff
375 rv
= GetCachedProtocolHandler(scheme
, result
);
376 if (NS_SUCCEEDED(rv
))
379 PRBool externalProtocol
= PR_FALSE
;
380 PRBool listedProtocol
= PR_TRUE
;
381 nsCOMPtr
<nsIPrefBranch2
> prefBranch
;
382 GetPrefBranch(getter_AddRefs(prefBranch
));
384 nsCAutoString
externalProtocolPref("network.protocol-handler.external.");
385 externalProtocolPref
+= scheme
;
386 rv
= prefBranch
->GetBoolPref(externalProtocolPref
.get(), &externalProtocol
);
388 externalProtocol
= PR_FALSE
;
389 listedProtocol
= PR_FALSE
;
393 if (!externalProtocol
) {
394 nsCAutoString
contractID(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX
);
395 contractID
+= scheme
;
396 ToLowerCase(contractID
);
398 rv
= CallGetService(contractID
.get(), result
);
399 if (NS_SUCCEEDED(rv
)) {
400 CacheProtocolHandler(scheme
, *result
);
405 // check to see whether GnomeVFS can handle this URI scheme. if it can
406 // create a nsIURI for the "scheme:", then we assume it has support for
407 // the requested protocol. otherwise, we failover to using the default
410 // XXX should this be generalized into something that searches a
411 // category? (see bug 234714)
413 rv
= CallGetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX
"moz-gnomevfs",
415 if (NS_SUCCEEDED(rv
)) {
416 nsCAutoString
spec(scheme
);
420 rv
= (*result
)->NewURI(spec
, nsnull
, nsnull
, &uri
);
421 if (NS_SUCCEEDED(rv
)) {
431 // Okay we don't have a protocol handler to handle this url type, so use
432 // the default protocol handler. This will cause urls to get dispatched
433 // out to the OS ('cause we can't do anything with them) when we try to
434 // read from a channel created by the default protocol handler.
436 rv
= CallGetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX
"default",
439 return NS_ERROR_UNKNOWN_PROTOCOL
;
445 nsIOService::ExtractScheme(const nsACString
&inURI
, nsACString
&scheme
)
447 return net_ExtractURLScheme(inURI
, nsnull
, nsnull
, &scheme
);
451 nsIOService::GetProtocolFlags(const char* scheme
, PRUint32
*flags
)
453 nsCOMPtr
<nsIProtocolHandler
> handler
;
454 nsresult rv
= GetProtocolHandler(scheme
, getter_AddRefs(handler
));
455 if (NS_FAILED(rv
)) return rv
;
457 rv
= handler
->GetProtocolFlags(flags
);
464 AutoIncrement(PRUint32
*var
) : mVar(var
)
477 nsIOService::NewURI(const nsACString
&aSpec
, const char *aCharset
, nsIURI
*aBaseURI
, nsIURI
**result
)
479 NS_ASSERTION(NS_IsMainThread(), "wrong thread");
481 static PRUint32 recursionCount
= 0;
482 if (recursionCount
>= MAX_RECURSION_COUNT
)
483 return NS_ERROR_MALFORMED_URI
;
484 AutoIncrement
inc(&recursionCount
);
486 nsCAutoString scheme
;
487 nsresult rv
= ExtractScheme(aSpec
, scheme
);
489 // then aSpec is relative
491 return NS_ERROR_MALFORMED_URI
;
493 rv
= aBaseURI
->GetScheme(scheme
);
494 if (NS_FAILED(rv
)) return rv
;
497 // now get the handler for this scheme
498 nsCOMPtr
<nsIProtocolHandler
> handler
;
499 rv
= GetProtocolHandler(scheme
.get(), getter_AddRefs(handler
));
500 if (NS_FAILED(rv
)) return rv
;
502 return handler
->NewURI(aSpec
, aCharset
, aBaseURI
, result
);
507 nsIOService::NewFileURI(nsIFile
*file
, nsIURI
**result
)
510 NS_ENSURE_ARG_POINTER(file
);
512 nsCOMPtr
<nsIProtocolHandler
> handler
;
514 rv
= GetProtocolHandler("file", getter_AddRefs(handler
));
515 if (NS_FAILED(rv
)) return rv
;
517 nsCOMPtr
<nsIFileProtocolHandler
> fileHandler( do_QueryInterface(handler
, &rv
) );
518 if (NS_FAILED(rv
)) return rv
;
520 return fileHandler
->NewFileURI(file
, result
);
524 nsIOService::NewChannelFromURI(nsIURI
*aURI
, nsIChannel
**result
)
527 NS_ENSURE_ARG_POINTER(aURI
);
528 NS_TIMELINE_MARK_URI("nsIOService::NewChannelFromURI(%s)", aURI
);
530 nsCAutoString scheme
;
531 rv
= aURI
->GetScheme(scheme
);
535 nsCOMPtr
<nsIProtocolHandler
> handler
;
536 rv
= GetProtocolHandler(scheme
.get(), getter_AddRefs(handler
));
541 rv
= handler
->GetProtocolFlags(&protoFlags
);
545 // Talk to the PPS if the protocol handler allows proxying. Otherwise,
546 // skip this step. This allows us to lazily load the PPS at startup.
547 if (protoFlags
& nsIProtocolHandler::ALLOWS_PROXY
) {
548 nsCOMPtr
<nsIProxyInfo
> pi
;
549 if (!mProxyService
) {
550 mProxyService
= do_GetService(NS_PROTOCOLPROXYSERVICE_CONTRACTID
);
552 NS_WARNING("failed to get protocol proxy service");
555 rv
= mProxyService
->Resolve(aURI
, 0, getter_AddRefs(pi
));
561 if (NS_SUCCEEDED(pi
->GetType(type
)) && type
.EqualsLiteral("http")) {
562 // we are going to proxy this channel using an http proxy
563 rv
= GetProtocolHandler("http", getter_AddRefs(handler
));
567 nsCOMPtr
<nsIProxiedProtocolHandler
> pph
= do_QueryInterface(handler
);
569 return pph
->NewProxiedChannel(aURI
, pi
, result
);
573 return handler
->NewChannel(aURI
, result
);
577 nsIOService::NewChannel(const nsACString
&aSpec
, const char *aCharset
, nsIURI
*aBaseURI
, nsIChannel
**result
)
580 nsCOMPtr
<nsIURI
> uri
;
581 rv
= NewURI(aSpec
, aCharset
, aBaseURI
, getter_AddRefs(uri
));
582 if (NS_FAILED(rv
)) return rv
;
584 return NewChannelFromURI(uri
, result
);
588 nsIOService::GetOffline(PRBool
*offline
)
595 nsIOService::SetOffline(PRBool offline
)
597 nsCOMPtr
<nsIObserverService
> observerService
=
598 do_GetService("@mozilla.org/observer-service;1");
601 if (offline
&& !mOffline
) {
602 NS_NAMED_LITERAL_STRING(offlineString
, NS_IOSERVICE_OFFLINE
);
603 mOffline
= PR_TRUE
; // indicate we're trying to shutdown
605 // don't care if notification fails
606 // this allows users to attempt a little cleanup before dns and socket transport are shut down.
608 observerService
->NotifyObservers(static_cast<nsIIOService
*>(this),
609 NS_IOSERVICE_GOING_OFFLINE_TOPIC
,
610 offlineString
.get());
612 // be sure to try and shutdown both (even if the first fails)...
613 // shutdown dns service first, because it has callbacks for socket transport
615 rv
= mDNSService
->Shutdown();
616 NS_ASSERTION(NS_SUCCEEDED(rv
), "DNS service shutdown failed");
618 if (mSocketTransportService
) {
619 rv
= mSocketTransportService
->Shutdown();
620 NS_ASSERTION(NS_SUCCEEDED(rv
), "socket transport service shutdown failed");
623 // don't care if notification fails
625 observerService
->NotifyObservers(static_cast<nsIIOService
*>(this),
626 NS_IOSERVICE_OFFLINE_STATUS_TOPIC
,
627 offlineString
.get());
629 else if (!offline
&& mOffline
) {
632 rv
= mDNSService
->Init();
633 NS_ASSERTION(NS_SUCCEEDED(rv
), "DNS service init failed");
635 if (mSocketTransportService
) {
636 rv
= mSocketTransportService
->Init();
637 NS_ASSERTION(NS_SUCCEEDED(rv
), "socket transport service init failed");
639 mOffline
= PR_FALSE
; // indicate success only AFTER we've
640 // brought up the services
642 // trigger a PAC reload when we come back online
644 mProxyService
->ReloadPAC();
646 // don't care if notification fails
648 observerService
->NotifyObservers(static_cast<nsIIOService
*>(this),
649 NS_IOSERVICE_OFFLINE_STATUS_TOPIC
,
650 NS_LITERAL_STRING(NS_IOSERVICE_ONLINE
).get());
657 nsIOService::AllowPort(PRInt32 inPort
, const char *scheme
, PRBool
*_retval
)
659 PRInt16 port
= inPort
;
665 // first check to see if the port is in our blacklist:
666 PRInt32 badPortListCnt
= mRestrictedPortList
.Count();
667 for (int i
=0; i
<badPortListCnt
; i
++)
669 if (port
== (PRInt32
) NS_PTR_TO_INT32(mRestrictedPortList
[i
]))
673 // check to see if the protocol wants to override
677 nsCOMPtr
<nsIProtocolHandler
> handler
;
678 nsresult rv
= GetProtocolHandler(scheme
, getter_AddRefs(handler
));
679 if (NS_FAILED(rv
)) return rv
;
681 // let the protocol handler decide
682 return handler
->AllowPort(port
, scheme
, _retval
);
690 ////////////////////////////////////////////////////////////////////////////////
693 nsIOService::PrefsChanged(nsIPrefBranch
*prefs
, const char *pref
)
697 // Look for extra ports to block
698 if (!pref
|| strcmp(pref
, PORT_PREF("banned")) == 0)
699 ParsePortList(prefs
, PORT_PREF("banned"), PR_FALSE
);
701 // ...as well as previous blocks to remove.
702 if (!pref
|| strcmp(pref
, PORT_PREF("banned.override")) == 0)
703 ParsePortList(prefs
, PORT_PREF("banned.override"), PR_TRUE
);
705 if (!pref
|| strcmp(pref
, AUTODIAL_PREF
) == 0) {
706 PRBool enableAutodial
= PR_FALSE
;
707 nsresult rv
= prefs
->GetBoolPref(AUTODIAL_PREF
, &enableAutodial
);
708 // If pref not found, default to disabled.
709 if (NS_SUCCEEDED(rv
)) {
710 if (mSocketTransportService
)
711 mSocketTransportService
->SetAutodialEnabled(enableAutodial
);
717 nsIOService::ParsePortList(nsIPrefBranch
*prefBranch
, const char *pref
, PRBool remove
)
719 nsXPIDLCString portList
;
721 // Get a pref string and chop it up into a list of ports.
722 prefBranch
->GetCharPref(pref
, getter_Copies(portList
));
724 nsCStringArray portListArray
;
725 portListArray
.ParseString(portList
.get(), ",");
727 for (index
=0; index
< portListArray
.Count(); index
++) {
728 portListArray
[index
]->StripWhitespace();
729 PRInt32 aErrorCode
, portBegin
, portEnd
;
731 if (PR_sscanf(portListArray
[index
]->get(), "%d-%d", &portBegin
, &portEnd
) == 2) {
732 if ((portBegin
< 65536) && (portEnd
< 65536)) {
735 for (curPort
=portBegin
; curPort
<= portEnd
; curPort
++)
736 mRestrictedPortList
.RemoveElement((void*)curPort
);
738 for (curPort
=portBegin
; curPort
<= portEnd
; curPort
++)
739 mRestrictedPortList
.AppendElement((void*)curPort
);
743 PRInt32 port
= portListArray
[index
]->ToInteger(&aErrorCode
);
744 if (NS_SUCCEEDED(aErrorCode
) && port
< 65536) {
746 mRestrictedPortList
.RemoveElement((void*)port
);
748 mRestrictedPortList
.AppendElement((void*)port
);
757 nsIOService::GetPrefBranch(nsIPrefBranch2
**result
)
760 CallGetService(NS_PREFSERVICE_CONTRACTID
, result
);
763 // nsIObserver interface
765 nsIOService::Observe(nsISupports
*subject
,
767 const PRUnichar
*data
)
769 if (!strcmp(topic
, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID
)) {
770 nsCOMPtr
<nsIPrefBranch
> prefBranch
= do_QueryInterface(subject
);
772 PrefsChanged(prefBranch
, NS_ConvertUTF16toUTF8(data
).get());
774 else if (!strcmp(topic
, kProfileChangeNetTeardownTopic
)) {
777 mOfflineForProfileChange
= PR_TRUE
;
780 else if (!strcmp(topic
, kProfileChangeNetRestoreTopic
)) {
781 if (mOfflineForProfileChange
) {
782 mOfflineForProfileChange
= PR_FALSE
;
783 if (!mManageOfflineStatus
||
784 NS_FAILED(TrackNetworkLinkStatusForOffline())) {
785 SetOffline(PR_FALSE
);
789 else if (!strcmp(topic
, NS_XPCOM_SHUTDOWN_OBSERVER_ID
)) {
792 // Break circular reference.
793 mProxyService
= nsnull
;
795 else if (!strcmp(topic
, NS_NETWORK_LINK_TOPIC
)) {
796 if (!mOfflineForProfileChange
&& mManageOfflineStatus
) {
797 TrackNetworkLinkStatusForOffline();
804 // nsINetUtil interface
806 nsIOService::ParseContentType(const nsACString
&aTypeHeader
,
807 nsACString
&aCharset
,
809 nsACString
&aContentType
)
811 net_ParseContentType(aTypeHeader
, aContentType
, aCharset
, aHadCharset
);
816 nsIOService::ProtocolHasFlags(nsIURI
*uri
,
823 nsCAutoString scheme
;
824 nsresult rv
= uri
->GetScheme(scheme
);
825 NS_ENSURE_SUCCESS(rv
, rv
);
827 PRUint32 protocolFlags
;
828 rv
= GetProtocolFlags(scheme
.get(), &protocolFlags
);
830 if (NS_SUCCEEDED(rv
)) {
831 *result
= (protocolFlags
& flags
) == flags
;
838 nsIOService::URIChainHasFlags(nsIURI
*uri
,
842 nsresult rv
= ProtocolHasFlags(uri
, flags
, result
);
843 NS_ENSURE_SUCCESS(rv
, rv
);
849 // Dig deeper into the chain. Note that this is not a do/while loop to
850 // avoid the extra addref/release on |uri| in the common (non-nested) case.
851 nsCOMPtr
<nsINestedURI
> nestedURI
= do_QueryInterface(uri
);
853 nsCOMPtr
<nsIURI
> innerURI
;
854 rv
= nestedURI
->GetInnerURI(getter_AddRefs(innerURI
));
855 NS_ENSURE_SUCCESS(rv
, rv
);
857 rv
= ProtocolHasFlags(innerURI
, flags
, result
);
863 nestedURI
= do_QueryInterface(innerURI
);
870 nsIOService::ToImmutableURI(nsIURI
* uri
, nsIURI
** result
)
877 nsresult rv
= NS_EnsureSafeToReturn(uri
, result
);
878 NS_ENSURE_SUCCESS(rv
, rv
);
880 NS_TryToSetImmutable(*result
);
885 nsIOService::SetManageOfflineStatus(PRBool aManage
) {
886 PRBool wasManaged
= mManageOfflineStatus
;
887 mManageOfflineStatus
= aManage
;
888 if (mManageOfflineStatus
&& !wasManaged
)
889 return TrackNetworkLinkStatusForOffline();
894 nsIOService::GetManageOfflineStatus(PRBool
* aManage
) {
895 *aManage
= mManageOfflineStatus
;
900 nsIOService::TrackNetworkLinkStatusForOffline()
902 NS_ASSERTION(mManageOfflineStatus
,
903 "Don't call this unless we're managing the offline status");
904 if (!mNetworkLinkService
)
905 return NS_ERROR_FAILURE
;
907 // check to make sure this won't collide with Autodial
908 if (mSocketTransportService
) {
909 PRBool autodialEnabled
= PR_FALSE
;
910 mSocketTransportService
->GetAutodialEnabled(&autodialEnabled
);
911 // If autodialing-on-link-down is enabled, check if the OS auto dial
912 // option is set to always autodial. If so, then we are
913 // always up for the purposes of offline management.
914 if (autodialEnabled
) {
915 #if defined(XP_WIN) && !defined(WINCE)
916 // On Windows, need to do some registry checking to see if
917 // autodial is enabled at the OS level. Only if that is
918 // enabled are we always up for the purposes of offline
920 if(nsNativeConnectionHelper::IsAutodialEnabled())
921 return SetOffline(PR_FALSE
);
923 return SetOffline(PR_FALSE
);
929 nsresult rv
= mNetworkLinkService
->GetIsLinkUp(&isUp
);
930 NS_ENSURE_SUCCESS(rv
, rv
);
931 return SetOffline(!isUp
);
935 nsIOService::EscapeString(const nsACString
& aString
,
936 PRUint32 aEscapeType
,
939 NS_ENSURE_ARG_RANGE(aEscapeType
, 0, 4);
941 nsCAutoString
stringCopy(aString
);
944 if (!NS_Escape(stringCopy
, result
, (nsEscapeMask
) aEscapeType
))
945 return NS_ERROR_OUT_OF_MEMORY
;
947 aResult
.Assign(result
);
953 nsIOService::EscapeURL(const nsACString
&aStr
,
954 PRUint32 aFlags
, nsACString
&aResult
)
957 NS_EscapeURL(aStr
.BeginReading(), aStr
.Length(),
958 aFlags
| esc_AlwaysCopy
, aResult
);
963 nsIOService::UnescapeString(const nsACString
&aStr
,
964 PRUint32 aFlags
, nsACString
&aResult
)
967 NS_UnescapeURL(aStr
.BeginReading(), aStr
.Length(),
968 aFlags
| esc_AlwaysCopy
, aResult
);
973 nsIOService::ExtractCharsetFromContentType(const nsACString
&aTypeHeader
,
974 nsACString
&aCharset
,
975 PRInt32
*aCharsetStart
,
976 PRInt32
*aCharsetEnd
,
979 nsCAutoString ignored
;
980 net_ParseContentType(aTypeHeader
, ignored
, aCharset
, aHadCharset
,
981 aCharsetStart
, aCharsetEnd
);
982 if (*aHadCharset
&& *aCharsetStart
== *aCharsetEnd
) {
983 *aHadCharset
= PR_FALSE
;
989 nsIOService::OfflineAppAllowed(nsIURI
*aURI
,
990 nsIPrefBranch
*aPrefBranch
,
993 *aAllowed
= PR_FALSE
;
995 nsCOMPtr
<nsIURI
> innerURI
= NS_GetInnermostURI(aURI
);
999 // only http and https applications can use offline APIs.
1001 nsresult rv
= innerURI
->SchemeIs("http", &match
);
1002 NS_ENSURE_SUCCESS(rv
, rv
);
1005 rv
= innerURI
->SchemeIs("https", &match
);
1006 NS_ENSURE_SUCCESS(rv
, rv
);
1012 nsCOMPtr
<nsIPermissionManager
> permissionManager
=
1013 do_GetService(NS_PERMISSIONMANAGER_CONTRACTID
);
1014 if (!permissionManager
) {
1019 permissionManager
->TestExactPermission(innerURI
, "offline-app", &perm
);
1021 if (perm
== nsIPermissionManager::UNKNOWN_ACTION
) {
1022 nsCOMPtr
<nsIPrefBranch
> branch
= aPrefBranch
;
1024 branch
= do_GetService(NS_PREFSERVICE_CONTRACTID
);
1027 rv
= branch
->GetBoolPref("offline-apps.allow_by_default", aAllowed
);
1028 NS_ENSURE_SUCCESS(rv
, rv
);
1034 if (perm
== nsIPermissionManager::DENY_ACTION
) {
1038 *aAllowed
= PR_TRUE
;