Bug 1428543 - 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 747572 c8d17e3d929ac3c41a86e3ee265e7998c13f3962
parent 747571 1203a047d15ed9a954467fce4fb031e2b8800a39
child 747573 033ad22d426105c348670bc2bc339955f64812ad
push id96939
push userVYV03354@nifty.ne.jp
push dateFri, 26 Jan 2018 11:24:45 +0000
reviewersfroydnj
bugs1428543
milestone60.0a1
Bug 1428543 - Add mozilla::filesystem::Path and use it in nsIFile. r?froydnj Currently only |value_type| is implemented. 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,32 @@
+/* -*- 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 {
+
+/*
+ * Mozilla vaiant of std::filesystem::path.
+ * Only |value_type| is implemented at the moment.
+ */
+class Path
+{
+public:
+#ifdef XP_WIN
+  using value_type = char16_t;
+#else
+  using value_type = char;
+#endif
+};
+
+}  /* 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)
+PathString
+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,28 @@
  * 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"
+#include "nsStringFwd.h"
+namespace mozilla {
+using PathString = nsTString<filesystem::Path::value_type>;
+using PathSubstring = nsTSubstring<filesystem::Path::value_type>;
+} // namespace mozilla
 %}
 
 [ptr] native PRFileDescStar(PRFileDesc);
 [ptr] native PRLibraryStar(PRLibrary);
 [ptr] native FILE(FILE);
+native PathString(mozilla::PathString);
 
 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 +253,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
@@ -586,20 +586,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();