Bug 685236 - Add mozilla::filesystem::Path and use it in nsIFile. r?froydnj draft
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Thu, 28 Dec 2017 03:03:35 +0900
changeset 715682 3bf634d2d42d9970f9ea7d72432091c34281bd5b
parent 715681 7dbf79e7bf4fdce6f45775ee43793f00f0b05376
child 715683 12009117d1d1bc3cc0eae1e3095ae4b332ff5565
push id94228
push userVYV03354@nifty.ne.jp
push dateThu, 04 Jan 2018 11:56:10 +0000
reviewersfroydnj
bugs685236
milestone59.0a1
Bug 685236 - Add mozilla::filesystem::Path and use it in nsIFile. r?froydnj Currently this class only impleents type aliases. MozReview-Commit-ID: 1mejzvkuako
mfbt/Path.h
mfbt/moz.build
xpcom/io/FileDescriptorFile.cpp
xpcom/io/nsIFile.idl
xpcom/io/nsLocalFileUnix.cpp
xpcom/io/nsLocalFileWin.cpp
new file mode 100644
--- /dev/null
+++ b/mfbt/Path.h
@@ -0,0 +1,30 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* Represents the native path format on the platform. */
+
+#ifndef mozilla_Path_h
+#define mozilla_Path_h
+
+namespace mozilla {
+namespace filesystem {
+
+class Path
+{
+public:
+#ifdef XP_WIN
+  using value_type = char16_t;
+#else
+  using value_type = char;
+#endif
+  using string_type = nsTString<value_type>;
+  using substring_type = nsTSubstring<value_type>;
+};
+
+}  /* namespace filesystem */
+}  /* namespace mozilla */
+
+#endif /* mozilla_Path_h */
--- a/mfbt/moz.build
+++ b/mfbt/moz.build
@@ -58,16 +58,17 @@ EXPORTS.mozilla = [
     'MemoryChecking.h',
     'MemoryReporting.h',
     'Move.h',
     'NotNull.h',
     'NullPtr.h',
     'Opaque.h',
     'OperatorNewExtensions.h',
     'Pair.h',
+    'Path.h',
     'PodOperations.h',
     'Poison.h',
     'Range.h',
     'RangedArray.h',
     'RangedPtr.h',
     'ReentrancyGuard.h',
     'RefCounted.h',
     'RefCountType.h',
--- a/xpcom/io/FileDescriptorFile.cpp
+++ b/xpcom/io/FileDescriptorFile.cpp
@@ -115,20 +115,20 @@ FileDescriptorFile::GetNativeTarget(nsAC
 }
 
 nsresult
 FileDescriptorFile::GetPath(nsAString& aRetVal)
 {
   return mFile->GetPath(aRetVal);
 }
 
-NS_IMETHODIMP
-FileDescriptorFile::GetNativePath(nsACString& aRetVal)
+filesystem::Path::string_type
+FileDescriptorFile::NativePath()
 {
-  return mFile->GetNativePath(aRetVal);
+  return mFile->NativePath();
 }
 
 NS_IMETHODIMP
 FileDescriptorFile::Equals(nsIFile* aOther, bool* aRetVal)
 {
   return mFile->Equals(aOther, aRetVal);
 }
 
--- a/xpcom/io/nsIFile.idl
+++ b/xpcom/io/nsIFile.idl
@@ -4,21 +4,23 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsISupports.idl"
 
 %{C++
 struct PRFileDesc;
 struct PRLibrary;
 #include <stdio.h>
+#include "mozilla/Path.h"
 %}
 
 [ptr] native PRFileDescStar(PRFileDesc);
 [ptr] native PRLibraryStar(PRLibrary);
 [ptr] native FILE(FILE);
+native PathString(mozilla::filesystem::Path::string_type);
 
 interface nsISimpleEnumerator;
 
 /**
  * An nsIFile is an abstract representation of a filename. It manages
  * filename encoding issues, pathname component separators ('/' vs. '\\'
  * vs. ':') and weird stuff like differing volumes with identical names, as
  * on pre-Darwin Macintoshes.
@@ -246,17 +248,20 @@ interface nsIFile : nsISupports
      *
      *  Note that the ACString attributes are returned in the 
      *  native filesystem charset.
      *
      */
     readonly attribute AString target;
     [noscript] readonly attribute ACString nativeTarget;
     readonly attribute AString path;
-    [noscript] readonly attribute ACString nativePath;
+    [notxpcom,nostdcall] PathString nativePath();
+%{C++
+    nsresult GetNativePath(nsACString& aPath);
+%}
 
     boolean exists();
     boolean isWritable();
     boolean isReadable();
     boolean isExecutable();
     boolean isHidden();
     boolean isDirectory();
     boolean isFile();
--- a/xpcom/io/nsLocalFileUnix.cpp
+++ b/xpcom/io/nsLocalFileUnix.cpp
@@ -590,20 +590,26 @@ NS_IMETHODIMP
 nsLocalFile::SetNativeLeafName(const nsACString& aLeafName)
 {
   nsACString::const_iterator begin, end;
   LocateNativeLeafName(begin, end);
   mPath.Replace(begin.get() - mPath.get(), Distance(begin, end), aLeafName);
   return NS_OK;
 }
 
-NS_IMETHODIMP
-nsLocalFile::GetNativePath(nsACString& aResult)
+nsCString
+nsLocalFile::NativePath()
 {
-  aResult = mPath;
+  return mPath;
+}
+
+nsresult
+nsIFile::GetNativePath(nsACString& aResult)
+{
+  aResult = NativePath();
   return NS_OK;
 }
 
 nsresult
 nsLocalFile::GetNativeTargetPathName(nsIFile* aNewParent,
                                      const nsACString& aNewName,
                                      nsACString& aResult)
 {
--- a/xpcom/io/nsLocalFileWin.cpp
+++ b/xpcom/io/nsLocalFileWin.cpp
@@ -3547,27 +3547,27 @@ nsLocalFile::SetNativeLeafName(const nsA
   if (NS_SUCCEEDED(rv)) {
     return SetLeafName(tmp);
   }
 
   return rv;
 }
 
 
-NS_IMETHODIMP
-nsLocalFile::GetNativePath(nsACString& aResult)
+nsString
+nsLocalFile::NativePath()
+{
+  return mWorkingPath;
+}
+
+nsresult
+nsIFile::GetNativePath(nsACString& aResult)
 {
   //NS_WARNING("This API is lossy. Use GetPath !");
-  nsAutoString tmp;
-  nsresult rv = GetPath(tmp);
-  if (NS_SUCCEEDED(rv)) {
-    rv = NS_CopyUnicodeToNative(tmp, aResult);
-  }
-
-  return rv;
+  return NS_CopyUnicodeToNative(NativePath(), aResult);
 }
 
 
 NS_IMETHODIMP
 nsLocalFile::GetNativeCanonicalPath(nsACString& aResult)
 {
   NS_WARNING("This method is lossy. Use GetCanonicalPath !");
   EnsureShortPath();