Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/protocol/res/nsResProtocolHandler.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "mozilla/chrome/RegistryMessageUtils.h"
7
#include "mozilla/dom/ContentParent.h"
8
#include "mozilla/Unused.h"
9
10
#include "nsResProtocolHandler.h"
11
#include "nsIIOService.h"
12
#include "nsIFile.h"
13
#include "nsNetCID.h"
14
#include "nsNetUtil.h"
15
#include "nsURLHelper.h"
16
#include "nsEscape.h"
17
18
#include "mozilla/Omnijar.h"
19
20
using mozilla::dom::ContentParent;
21
using mozilla::LogLevel;
22
using mozilla::Unused;
23
24
27
#define kAPP           "app"
25
27
#define kGRE           "gre"
26
27
nsresult
28
nsResProtocolHandler::Init()
29
3
{
30
3
    nsresult rv;
31
3
    rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::APP, mAppURI);
32
3
    NS_ENSURE_SUCCESS(rv, rv);
33
3
    rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::GRE, mGREURI);
34
3
    NS_ENSURE_SUCCESS(rv, rv);
35
3
36
3
    // mozilla::Omnijar::GetURIString always returns a string ending with /,
37
3
    // and we want to remove it.
38
3
    mGREURI.Truncate(mGREURI.Length() - 1);
39
3
    if (mAppURI.Length()) {
40
0
      mAppURI.Truncate(mAppURI.Length() - 1);
41
3
    } else {
42
3
      mAppURI = mGREURI;
43
3
    }
44
3
45
3
    //XXXbsmedberg Neil wants a resource://pchrome/ for the profile chrome dir...
46
3
    // but once I finish multiple chrome registration I'm not sure that it is needed
47
3
48
3
    // XXX dveditz: resource://pchrome/ defeats profile directory salting
49
3
    // if web content can load it. Tread carefully.
50
3
51
3
    return rv;
52
3
}
53
54
//----------------------------------------------------------------------------
55
// nsResProtocolHandler::nsISupports
56
//----------------------------------------------------------------------------
57
58
NS_IMPL_QUERY_INTERFACE(nsResProtocolHandler, nsIResProtocolHandler,
59
                        nsISubstitutingProtocolHandler, nsIProtocolHandler,
60
                        nsISupportsWeakReference)
61
NS_IMPL_ADDREF_INHERITED(nsResProtocolHandler, SubstitutingProtocolHandler)
62
NS_IMPL_RELEASE_INHERITED(nsResProtocolHandler, SubstitutingProtocolHandler)
63
64
NS_IMETHODIMP
65
nsResProtocolHandler::AllowContentToAccess(nsIURI *aURI, bool *aResult)
66
0
{
67
0
    *aResult = false;
68
0
69
0
    nsAutoCString host;
70
0
    nsresult rv = aURI->GetAsciiHost(host);
71
0
    NS_ENSURE_SUCCESS(rv, rv);
72
0
73
0
    uint32_t flags;
74
0
    rv = GetSubstitutionFlags(host, &flags);
75
0
    NS_ENSURE_SUCCESS(rv, rv);
76
0
77
0
    *aResult = flags & nsISubstitutingProtocolHandler::ALLOW_CONTENT_ACCESS;
78
0
    return NS_OK;
79
0
}
80
81
nsresult
82
nsResProtocolHandler::GetSubstitutionInternal(const nsACString& aRoot,
83
                                              nsIURI** aResult,
84
                                              uint32_t* aFlags)
85
0
{
86
0
    nsAutoCString uri;
87
0
88
0
    if (!ResolveSpecialCases(aRoot, NS_LITERAL_CSTRING("/"), NS_LITERAL_CSTRING("/"), uri)) {
89
0
        return NS_ERROR_NOT_AVAILABLE;
90
0
    }
91
0
92
0
    *aFlags = 0; // No content access.
93
0
    return NS_NewURI(aResult, uri);
94
0
}
95
96
bool
97
nsResProtocolHandler::ResolveSpecialCases(const nsACString& aHost,
98
                                          const nsACString& aPath,
99
                                          const nsACString& aPathname,
100
                                          nsACString& aResult)
101
27
{
102
27
    if (aHost.EqualsLiteral("") || aHost.EqualsLiteral(kAPP)) {
103
0
        aResult.Assign(mAppURI);
104
27
    } else if (aHost.Equals(kGRE)) {
105
27
        aResult.Assign(mGREURI);
106
27
    } else {
107
0
        return false;
108
0
    }
109
27
    aResult.Append(aPath);
110
27
    return true;
111
27
}
112
113
nsresult
114
nsResProtocolHandler::SetSubstitution(const nsACString& aRoot, nsIURI* aBaseURI)
115
0
{
116
0
    MOZ_ASSERT(!aRoot.EqualsLiteral(""));
117
0
    MOZ_ASSERT(!aRoot.EqualsLiteral(kAPP));
118
0
    MOZ_ASSERT(!aRoot.EqualsLiteral(kGRE));
119
0
    return SubstitutingProtocolHandler::SetSubstitution(aRoot, aBaseURI);
120
0
}
121
122
nsresult
123
nsResProtocolHandler::SetSubstitutionWithFlags(const nsACString& aRoot,
124
                                               nsIURI* aBaseURI,
125
                                               uint32_t aFlags)
126
30
{
127
30
    MOZ_ASSERT(!aRoot.EqualsLiteral(""));
128
30
    MOZ_ASSERT(!aRoot.EqualsLiteral(kAPP));
129
30
    MOZ_ASSERT(!aRoot.EqualsLiteral(kGRE));
130
30
    return SubstitutingProtocolHandler::SetSubstitutionWithFlags(aRoot, aBaseURI, aFlags);
131
30
}