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