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
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "StorageUtils.h"
8 :
9 : namespace mozilla {
10 : namespace dom {
11 : namespace StorageUtils {
12 :
13 : nsresult
14 4 : GenerateOriginKey(nsIPrincipal* aPrincipal, nsACString& aOriginAttrSuffix,
15 : nsACString& aOriginKey)
16 : {
17 4 : if (NS_WARN_IF(!aPrincipal)) {
18 0 : return NS_ERROR_UNEXPECTED;
19 : }
20 :
21 4 : aPrincipal->OriginAttributesRef().CreateSuffix(aOriginAttrSuffix);
22 :
23 8 : nsCOMPtr<nsIURI> uri;
24 4 : nsresult rv = aPrincipal->GetURI(getter_AddRefs(uri));
25 4 : NS_ENSURE_SUCCESS(rv, rv);
26 4 : if (!uri) {
27 0 : return NS_ERROR_UNEXPECTED;
28 : }
29 :
30 8 : nsAutoCString domainOrigin;
31 4 : rv = uri->GetAsciiHost(domainOrigin);
32 4 : NS_ENSURE_SUCCESS(rv, rv);
33 :
34 4 : if (domainOrigin.IsEmpty()) {
35 : // For the file:/// protocol use the exact directory as domain.
36 2 : bool isScheme = false;
37 2 : if (NS_SUCCEEDED(uri->SchemeIs("file", &isScheme)) && isScheme) {
38 0 : nsCOMPtr<nsIURL> url = do_QueryInterface(uri, &rv);
39 0 : NS_ENSURE_SUCCESS(rv, rv);
40 0 : rv = url->GetDirectory(domainOrigin);
41 0 : NS_ENSURE_SUCCESS(rv, rv);
42 : }
43 : }
44 :
45 : // Append reversed domain
46 8 : nsAutoCString reverseDomain;
47 4 : rv = CreateReversedDomain(domainOrigin, reverseDomain);
48 4 : if (NS_FAILED(rv)) {
49 2 : return rv;
50 : }
51 :
52 2 : aOriginKey.Append(reverseDomain);
53 :
54 : // Append scheme
55 4 : nsAutoCString scheme;
56 2 : rv = uri->GetScheme(scheme);
57 2 : NS_ENSURE_SUCCESS(rv, rv);
58 :
59 2 : aOriginKey.Append(':');
60 2 : aOriginKey.Append(scheme);
61 :
62 : // Append port if any
63 2 : int32_t port = NS_GetRealPort(uri);
64 2 : if (port != -1) {
65 2 : aOriginKey.Append(nsPrintfCString(":%d", port));
66 : }
67 :
68 2 : return NS_OK;
69 : }
70 :
71 : bool
72 0 : PrincipalsEqual(nsIPrincipal* aObjectPrincipal,
73 : nsIPrincipal* aSubjectPrincipal)
74 : {
75 0 : if (!aSubjectPrincipal) {
76 0 : return true;
77 : }
78 :
79 0 : if (!aObjectPrincipal) {
80 0 : return false;
81 : }
82 :
83 0 : return aSubjectPrincipal->Equals(aObjectPrincipal);
84 : }
85 :
86 : void
87 3 : ReverseString(const nsACString& aSource, nsACString& aResult)
88 : {
89 3 : nsACString::const_iterator sourceBegin, sourceEnd;
90 3 : aSource.BeginReading(sourceBegin);
91 3 : aSource.EndReading(sourceEnd);
92 :
93 3 : aResult.SetLength(aSource.Length());
94 3 : nsACString::iterator destEnd;
95 3 : aResult.EndWriting(destEnd);
96 :
97 57 : while (sourceBegin != sourceEnd) {
98 27 : *(--destEnd) = *sourceBegin;
99 27 : ++sourceBegin;
100 : }
101 3 : }
102 :
103 : nsresult
104 5 : CreateReversedDomain(const nsACString& aAsciiDomain,
105 : nsACString& aKey)
106 : {
107 5 : if (aAsciiDomain.IsEmpty()) {
108 2 : return NS_ERROR_NOT_AVAILABLE;
109 : }
110 :
111 3 : ReverseString(aAsciiDomain, aKey);
112 :
113 3 : aKey.Append('.');
114 3 : return NS_OK;
115 : }
116 :
117 : } // StorageUtils namespace
118 : } // dom namespace
119 9 : } // mozilla namespace
|