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
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "FontTableURIProtocolHandler.h"
8 #include "nsIUUIDGenerator.h"
9 #include "nsNetUtil.h"
10 #include "nsSimpleURI.h"
11 
12 using namespace mozilla;
13 using namespace mozilla::dom;
14 
15 /* static */
GenerateURIString(nsACString & aUri)16 nsresult FontTableURIProtocolHandler::GenerateURIString(nsACString& aUri) {
17   nsresult rv;
18   nsCOMPtr<nsIUUIDGenerator> uuidgen =
19       do_GetService("@mozilla.org/uuid-generator;1", &rv);
20   NS_ENSURE_SUCCESS(rv, rv);
21 
22   nsID id;
23   rv = uuidgen->GenerateUUIDInPlace(&id);
24   NS_ENSURE_SUCCESS(rv, rv);
25 
26   char chars[NSID_LENGTH];
27   id.ToProvidedString(chars);
28 
29   aUri = FONTTABLEURI_SCHEME;
30   aUri.Append(':');
31 
32   aUri += Substring(chars + 1, chars + NSID_LENGTH - 2);
33 
34   return NS_OK;
35 }
36 
37 FontTableURIProtocolHandler::FontTableURIProtocolHandler() = default;
38 FontTableURIProtocolHandler::~FontTableURIProtocolHandler() = default;
39 
NS_IMPL_ISUPPORTS(FontTableURIProtocolHandler,nsIProtocolHandler,nsISupportsWeakReference)40 NS_IMPL_ISUPPORTS(FontTableURIProtocolHandler, nsIProtocolHandler,
41                   nsISupportsWeakReference)
42 
43 NS_IMETHODIMP
44 FontTableURIProtocolHandler::GetDefaultPort(int32_t* result) {
45   *result = -1;
46   return NS_OK;
47 }
48 
49 NS_IMETHODIMP
GetProtocolFlags(uint32_t * result)50 FontTableURIProtocolHandler::GetProtocolFlags(uint32_t* result) {
51   *result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_SUBSUMERS |
52             URI_NON_PERSISTABLE | URI_IS_LOCAL_RESOURCE;
53   return NS_OK;
54 }
55 
56 NS_IMETHODIMP
GetFlagsForURI(nsIURI * aURI,uint32_t * aResult)57 FontTableURIProtocolHandler::GetFlagsForURI(nsIURI* aURI, uint32_t* aResult) {
58   return FontTableURIProtocolHandler::GetProtocolFlags(aResult);
59 }
60 
61 NS_IMETHODIMP
NewChannel(nsIURI * uri,nsILoadInfo * aLoadInfo,nsIChannel ** result)62 FontTableURIProtocolHandler::NewChannel(nsIURI* uri, nsILoadInfo* aLoadInfo,
63                                         nsIChannel** result) {
64   return NS_ERROR_DOM_BAD_URI;
65 }
66 
67 NS_IMETHODIMP
AllowPort(int32_t port,const char * scheme,bool * _retval)68 FontTableURIProtocolHandler::AllowPort(int32_t port, const char* scheme,
69                                        bool* _retval) {
70   *_retval = false;
71   return NS_OK;
72 }
73 
74 NS_IMETHODIMP
GetScheme(nsACString & result)75 FontTableURIProtocolHandler::GetScheme(nsACString& result) {
76   result.AssignLiteral(FONTTABLEURI_SCHEME);
77   return NS_OK;
78 }
79