|
1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
|
2 /* vim: set ts=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 file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "mozilla/dom/FileSystemUtils.h" |
|
8 |
|
9 #include "nsXULAppAPI.h" |
|
10 |
|
11 namespace mozilla { |
|
12 namespace dom { |
|
13 |
|
14 // static |
|
15 void |
|
16 FileSystemUtils::LocalPathToNormalizedPath(const nsAString& aLocal, |
|
17 nsAString& aNorm) |
|
18 { |
|
19 nsString result; |
|
20 result = aLocal; |
|
21 #if defined(XP_WIN) |
|
22 char16_t* cur = result.BeginWriting(); |
|
23 char16_t* end = result.EndWriting(); |
|
24 for (; cur < end; ++cur) { |
|
25 if (char16_t('\\') == *cur) |
|
26 *cur = char16_t('/'); |
|
27 } |
|
28 #endif |
|
29 aNorm = result; |
|
30 } |
|
31 |
|
32 // static |
|
33 void |
|
34 FileSystemUtils::NormalizedPathToLocalPath(const nsAString& aNorm, |
|
35 nsAString& aLocal) |
|
36 { |
|
37 nsString result; |
|
38 result = aNorm; |
|
39 #if defined(XP_WIN) |
|
40 char16_t* cur = result.BeginWriting(); |
|
41 char16_t* end = result.EndWriting(); |
|
42 for (; cur < end; ++cur) { |
|
43 if (char16_t('/') == *cur) |
|
44 *cur = char16_t('\\'); |
|
45 } |
|
46 #endif |
|
47 aLocal = result; |
|
48 } |
|
49 |
|
50 // static |
|
51 bool |
|
52 FileSystemUtils::IsDescendantPath(const nsAString& aPath, |
|
53 const nsAString& aDescendantPath) |
|
54 { |
|
55 // The descendant path should begin with its ancestor path. |
|
56 nsAutoString prefix; |
|
57 prefix = aPath + NS_LITERAL_STRING(FILESYSTEM_DOM_PATH_SEPARATOR); |
|
58 |
|
59 // Check the sub-directory path to see if it has the parent path as prefix. |
|
60 if (aDescendantPath.Length() < prefix.Length() || |
|
61 !StringBeginsWith(aDescendantPath, prefix)) { |
|
62 return false; |
|
63 } |
|
64 |
|
65 return true; |
|
66 } |
|
67 |
|
68 // static |
|
69 bool |
|
70 FileSystemUtils::IsParentProcess() |
|
71 { |
|
72 return XRE_GetProcessType() == GeckoProcessType_Default; |
|
73 } |
|
74 |
|
75 } // namespace dom |
|
76 } // namespace mozilla |