Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef SubstitutingProtocolHandler_h___
8 : #define SubstitutingProtocolHandler_h___
9 :
10 : #include "nsISubstitutingProtocolHandler.h"
11 :
12 : #include "nsInterfaceHashtable.h"
13 : #include "nsIOService.h"
14 : #include "nsISubstitutionObserver.h"
15 : #include "nsStandardURL.h"
16 : #include "mozilla/chrome/RegistryMessageUtils.h"
17 : #include "mozilla/Maybe.h"
18 :
19 : class nsIIOService;
20 :
21 : namespace mozilla {
22 : namespace net {
23 :
24 : //
25 : // Base class for resource://-like substitution protocols.
26 : //
27 : // If you add a new protocol, make sure to change nsChromeRegistryChrome
28 : // to properly invoke CollectSubstitutions at the right time.
29 : class SubstitutingProtocolHandler
30 : {
31 : public:
32 : SubstitutingProtocolHandler(const char* aScheme, uint32_t aFlags, bool aEnforceFileOrJar = true);
33 : explicit SubstitutingProtocolHandler(const char* aScheme);
34 :
35 12415 : NS_INLINE_DECL_REFCOUNTING(SubstitutingProtocolHandler);
36 : NS_DECL_NON_VIRTUAL_NSIPROTOCOLHANDLER;
37 : NS_DECL_NON_VIRTUAL_NSISUBSTITUTINGPROTOCOLHANDLER;
38 :
39 0 : bool HasSubstitution(const nsACString& aRoot) const { return mSubstitutions.Get(aRoot, nullptr); }
40 :
41 : MOZ_MUST_USE nsresult CollectSubstitutions(InfallibleTArray<SubstitutionMapping>& aResources);
42 :
43 : protected:
44 0 : virtual ~SubstitutingProtocolHandler() {}
45 : void ConstructInternal();
46 :
47 : MOZ_MUST_USE nsresult SendSubstitution(const nsACString& aRoot, nsIURI* aBaseURI);
48 :
49 : // Override this in the subclass to try additional lookups after checking
50 : // mSubstitutions.
51 0 : virtual MOZ_MUST_USE nsresult GetSubstitutionInternal(const nsACString& aRoot, nsIURI** aResult)
52 : {
53 0 : *aResult = nullptr;
54 0 : return NS_ERROR_NOT_AVAILABLE;
55 : }
56 :
57 : // Override this in the subclass to check for special case when resolving URIs
58 : // _before_ checking substitutions.
59 0 : virtual MOZ_MUST_USE bool ResolveSpecialCases(const nsACString& aHost,
60 : const nsACString& aPath,
61 : const nsACString& aPathname,
62 : nsACString& aResult)
63 : {
64 0 : return false;
65 : }
66 :
67 : // Override this in the subclass to check for special case when opening
68 : // channels.
69 1031 : virtual MOZ_MUST_USE nsresult SubstituteChannel(nsIURI* uri, nsILoadInfo* aLoadInfo, nsIChannel** result)
70 : {
71 1031 : return NS_OK;
72 : }
73 :
74 : nsIIOService* IOService() { return mIOService; }
75 :
76 : private:
77 : // Notifies all observers that a new substitution from |aRoot| to
78 : // |aBaseURI| has been set/installed for this protocol handler.
79 : void NotifyObservers(const nsACString& aRoot, nsIURI* aBaseURI);
80 :
81 : nsCString mScheme;
82 : Maybe<uint32_t> mFlags;
83 : nsInterfaceHashtable<nsCStringHashKey,nsIURI> mSubstitutions;
84 : nsCOMPtr<nsIIOService> mIOService;
85 :
86 : // The list of observers added with AddObserver that will be
87 : // notified when substitutions are set or unset.
88 : nsTArray<nsCOMPtr<nsISubstitutionObserver>> mObservers;
89 :
90 : // In general, we expect the principal of a document loaded from a
91 : // substituting URI to be a codebase principal for that URI (rather than
92 : // a principal for whatever is underneath). However, this only works if
93 : // the protocol handler for the underlying URI doesn't set an explicit
94 : // owner (which chrome:// does, for example). So we want to require that
95 : // substituting URIs only map to other URIs of the same type, or to
96 : // file:// and jar:// URIs.
97 : //
98 : // Enforcing this for ye olde resource:// URIs could carry compat risks, so
99 : // we just try to enforce it on new protocols going forward.
100 : bool mEnforceFileOrJar;
101 : };
102 :
103 : // SubstitutingURL : overrides nsStandardURL::GetFile to provide nsIFile resolution
104 3153 : class SubstitutingURL : public nsStandardURL
105 : {
106 : public:
107 1076 : SubstitutingURL() : nsStandardURL(true) {}
108 : virtual nsStandardURL* StartClone();
109 : virtual MOZ_MUST_USE nsresult EnsureFile();
110 : NS_IMETHOD GetClassIDNoAlloc(nsCID *aCID);
111 : };
112 :
113 : } // namespace net
114 : } // namespace mozilla
115 :
116 : #endif /* SubstitutingProtocolHandler_h___ */
|