aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/StartHereProtocolHandler.cpp
blob: 47a1af310d20943c096411644ac0b4dab31d5beb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 *  Copyright (C) 2001 Matt Aubury, Philip Langdale
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "ephy-file-helpers.h"

#include "nsCOMPtr.h"
#include "nsIFactory.h"
#include "nsIIOService.h"
#include "nsIServiceManager.h"
#include "nsIURI.h"
#include "nsNetCID.h"
#include "nsNetUtil.h"
#include "nsXPComFactory.h"

static NS_DEFINE_CID(kSimpleURICID,     NS_SIMPLEURI_CID);

class GStartHereProtocolHandler : public nsIProtocolHandler
{
  public:
    NS_DECL_ISUPPORTS
    NS_DECL_NSIPROTOCOLHANDLER

    GStartHereProtocolHandler (void);
    virtual ~GStartHereProtocolHandler();

    nsCOMPtr<nsIChannel> mChannel;
    nsCOMPtr<nsIURI> mURI;
};

/* Implementation file */
NS_IMPL_ISUPPORTS1 (GStartHereProtocolHandler, nsIProtocolHandler)

GStartHereProtocolHandler::GStartHereProtocolHandler (void)
{
    NS_INIT_ISUPPORTS();

}

GStartHereProtocolHandler::~GStartHereProtocolHandler()
{
    /* destructor code */
}

/* readonly attribute string scheme; */
NS_IMETHODIMP GStartHereProtocolHandler::GetScheme(nsACString &aScheme)
{
    aScheme = NS_LITERAL_CSTRING("start-here");
    return NS_OK;
}

/* readonly attribute long defaultPort; */
NS_IMETHODIMP GStartHereProtocolHandler::GetDefaultPort(PRInt32 *aDefaultPort)
{
    nsresult rv = NS_OK;
    if (aDefaultPort)
        *aDefaultPort = -1;
    else
        rv = NS_ERROR_NULL_POINTER;
    return rv;
}

/* readonly attribute short protocolFlags; */
NS_IMETHODIMP GStartHereProtocolHandler::GetProtocolFlags(PRUint32 *aProtocolFlags)
{
    if (aProtocolFlags)
        *aProtocolFlags = nsIProtocolHandler::URI_STD;
    else
        return NS_ERROR_NULL_POINTER;
    return NS_OK;
} 

/* nsIURI newURI (in string aSpec, in nsIURI aBaseURI); */
NS_IMETHODIMP GStartHereProtocolHandler::NewURI(const nsACString &aSpec,
                            const char *aOriginCharset,
                            nsIURI *aBaseURI,
                            nsIURI **_retval)
{
    nsresult rv = NS_OK;
    nsCOMPtr <nsIURI> newUri;
    
    rv = nsComponentManager::CreateInstance(kSimpleURICID, NULL,
                        NS_GET_IID(nsIURI),
                        getter_AddRefs(newUri));

        if (NS_SUCCEEDED(rv)) 
        {
        newUri->SetSpec(aSpec);
        rv = newUri->QueryInterface(NS_GET_IID(nsIURI),
                        (void **) _retval);
        }
    return rv;
}

/* nsIChannel newChannel (in nsIURI aURI); */
NS_IMETHODIMP GStartHereProtocolHandler::NewChannel(nsIURI *aURI,
                                nsIChannel **_retval)
{
    nsresult rv;

    nsCAutoString path;
    rv = aURI->GetPath(path);
    if (NS_FAILED(rv)) return rv;

    char *httpSpec = g_strconcat ("file:///",
                      ephy_file ("start_here.html"),
                  NULL);
    
    if (!httpSpec) return NS_ERROR_OUT_OF_MEMORY;

    nsCOMPtr<nsIIOService> serv(do_GetIOService(&rv));
    if (NS_FAILED(rv)) return rv;

    // now we have an HTTP url, give the user an HTTP channel
    rv = serv->NewChannel(nsDependentCString(httpSpec), nsnull, nsnull, _retval);
    
    return rv;
}

/* boolean allowPort (in long port, in string scheme); */
NS_IMETHODIMP GStartHereProtocolHandler::AllowPort(PRInt32 port, const char *scheme,
                               PRBool *_retval)
{
    *_retval = PR_FALSE;
    return NS_OK;
}

NS_DEF_FACTORY (GStartHereProtocolHandler, GStartHereProtocolHandler);

/**
 * NS_NewStartHereProtocolHandlerFactory:
 */ 
nsresult NS_NewStartHereHandlerFactory(nsIFactory** aFactory)
{
    NS_ENSURE_ARG_POINTER(aFactory);
    *aFactory = nsnull;

    nsGStartHereProtocolHandlerFactory *result = new nsGStartHereProtocolHandlerFactory;
    if (result == NULL)
    {
        return NS_ERROR_OUT_OF_MEMORY;
    }
    
    NS_ADDREF(result);
    *aFactory = result;

    return NS_OK;
}