Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/protocol/res/SubstitutingProtocolHandler.h
Line
Count
Source (jump to first uncovered line)
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 "nsIOService.h"
13
#include "nsISubstitutionObserver.h"
14
#include "nsDataHashtable.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
  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() = default;
45
  void ConstructInternal();
46
47
  MOZ_MUST_USE nsresult SendSubstitution(const nsACString& aRoot, nsIURI* aBaseURI, uint32_t aFlags);
48
49
  nsresult GetSubstitutionFlags(const nsACString& root, uint32_t* flags);
50
51
  // Override this in the subclass to try additional lookups after checking
52
  // mSubstitutions.
53
  virtual MOZ_MUST_USE nsresult GetSubstitutionInternal(const nsACString& aRoot, nsIURI** aResult, uint32_t* aFlags)
54
0
  {
55
0
    *aResult = nullptr;
56
0
    *aFlags = 0;
57
0
    return NS_ERROR_NOT_AVAILABLE;
58
0
  }
59
60
  // Override this in the subclass to check for special case when resolving URIs
61
  // _before_ checking substitutions.
62
  virtual MOZ_MUST_USE bool ResolveSpecialCases(const nsACString& aHost,
63
                                                const nsACString& aPath,
64
                                                const nsACString& aPathname,
65
                                                nsACString& aResult)
66
0
  {
67
0
    return false;
68
0
  }
69
70
  // Override this in the subclass to check for special case when opening
71
  // channels.
72
  virtual MOZ_MUST_USE nsresult SubstituteChannel(nsIURI* uri, nsILoadInfo* aLoadInfo, nsIChannel** result)
73
4
  {
74
4
    return NS_OK;
75
4
  }
76
77
0
  nsIIOService* IOService() { return mIOService; }
78
79
private:
80
  struct SubstitutionEntry
81
  {
82
    SubstitutionEntry()
83
        : flags(0)
84
30
    {
85
30
    }
86
87
0
    ~SubstitutionEntry() = default;
88
89
    nsCOMPtr<nsIURI> baseURI;
90
    uint32_t flags;
91
  };
92
93
  // Notifies all observers that a new substitution from |aRoot| to
94
  // |aBaseURI| has been set/installed for this protocol handler.
95
  void NotifyObservers(const nsACString& aRoot, nsIURI* aBaseURI);
96
97
  nsCString mScheme;
98
  Maybe<uint32_t> mFlags;
99
  nsDataHashtable<nsCStringHashKey, SubstitutionEntry> mSubstitutions;
100
  nsCOMPtr<nsIIOService> mIOService;
101
102
  // The list of observers added with AddObserver that will be
103
  // notified when substitutions are set or unset.
104
  nsTArray<nsCOMPtr<nsISubstitutionObserver>> mObservers;
105
106
  // In general, we expect the principal of a document loaded from a
107
  // substituting URI to be a codebase principal for that URI (rather than
108
  // a principal for whatever is underneath). However, this only works if
109
  // the protocol handler for the underlying URI doesn't set an explicit
110
  // owner (which chrome:// does, for example). So we want to require that
111
  // substituting URIs only map to other URIs of the same type, or to
112
  // file:// and jar:// URIs.
113
  //
114
  // Enforcing this for ye olde resource:// URIs could carry compat risks, so
115
  // we just try to enforce it on new protocols going forward.
116
  bool mEnforceFileOrJar;
117
};
118
119
// SubstitutingURL : overrides nsStandardURL::GetFile to provide nsIFile resolution
120
class SubstitutingURL : public nsStandardURL
121
{
122
public:
123
  virtual nsStandardURL* StartClone() override;
124
  virtual MOZ_MUST_USE nsresult EnsureFile() override;
125
  NS_IMETHOD GetClassIDNoAlloc(nsCID *aCID) override;
126
127
private:
128
2.57k
  explicit SubstitutingURL() : nsStandardURL(true) {}
129
0
  explicit SubstitutingURL(bool aSupportsFileURL) : nsStandardURL(true) { MOZ_ASSERT(aSupportsFileURL); }
130
0
  virtual nsresult Clone(nsIURI** aURI) override { return nsStandardURL::Clone(aURI); }
131
132
public:
133
  class Mutator
134
    : public TemplatedMutator<SubstitutingURL>
135
  {
136
    NS_DECL_ISUPPORTS
137
  public:
138
2.57k
    explicit Mutator() = default;
139
  private:
140
    virtual ~Mutator() = default;
141
142
    SubstitutingURL* Create() override
143
2.57k
    {
144
2.57k
      return new SubstitutingURL();
145
2.57k
    }
146
  };
147
148
  NS_IMETHOD Mutate(nsIURIMutator** aMutator) override
149
0
  {
150
0
    RefPtr<SubstitutingURL::Mutator> mutator = new SubstitutingURL::Mutator();
151
0
    nsresult rv = mutator->InitFromURI(this);
152
0
    if (NS_FAILED(rv)) {
153
0
      return rv;
154
0
    }
155
0
    mutator.forget(aMutator);
156
0
    return NS_OK;
157
0
  }
158
159
  friend BaseURIMutator<SubstitutingURL>;
160
  friend TemplatedMutator<SubstitutingURL>;
161
};
162
163
} // namespace net
164
} // namespace mozilla
165
166
#endif /* SubstitutingProtocolHandler_h___ */