Bug 1299072: P3. Add MediaResult object. r?cpearce draft
authorJean-Yves Avenard <jyavenard@mozilla.com>
Sat, 10 Sep 2016 01:23:31 +1000
changeset 412868 9e707c5242f893c7d834cda172fa5bcb85dca5a9
parent 412867 5a5f3ce57bc0b8ae4b0fc69e472a9164364ce311
child 412869 298a7d07a7fb71102ffd71c71e9eeed69776ac27
push id29276
push userbmo:jyavenard@mozilla.com
push dateTue, 13 Sep 2016 03:29:20 +0000
reviewerscpearce
bugs1299072
milestone51.0a1
Bug 1299072: P3. Add MediaResult object. r?cpearce MozReview-Commit-ID: CNedjeEi6PE
dom/media/MediaResult.h
dom/media/moz.build
new file mode 100644
--- /dev/null
+++ b/dom/media/MediaResult.h
@@ -0,0 +1,60 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et cindent: */
+/* 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/. */
+
+#ifndef MediaResult_h_
+#define MediaResult_h_
+
+#include "nsError.h"
+#include "nsPrintfCString.h"
+
+// MediaResult can be used interchangeably with nsresult.
+// It allows to store extra information such as where the error occurred.
+// While nsresult is typically passed by value; due to its potential size, using
+// MediaResult const references is recommended.
+namespace mozilla {
+
+class MediaResult
+{
+public:
+  MOZ_IMPLICIT MediaResult(nsresult aResult)
+    : mCode(aResult)
+  {
+  }
+  MediaResult(nsresult aResult, const nsACString& aMessage)
+    : mCode(aResult)
+    , mMessage(aMessage)
+  {
+  }
+  MediaResult(nsresult aResult, const char* aMessage)
+    : mCode(aResult)
+    , mMessage(aMessage)
+  {
+  }
+  MediaResult(const MediaResult& aOther) = default;
+  MediaResult(MediaResult&& aOther) = default;
+  MediaResult& operator=(const MediaResult& aOther) = default;
+  MediaResult& operator=(MediaResult&& aOther) = default;
+
+  nsresult Code() const { return mCode; }
+  const nsCString& Message() const { return mMessage; }
+
+  // Interoperations with nsresult.
+  bool operator==(nsresult aResult) const { return aResult == mCode; }
+  bool operator!=(nsresult aResult) const { return aResult != mCode; }
+  operator nsresult () const { return mCode; }
+
+  nsCString Description() const
+  {
+    return nsPrintfCString("0x%08x: %s", mCode, mMessage.get());
+  }
+
+private:
+  nsresult mCode;
+  nsCString mMessage;
+};
+
+} // namespace mozilla
+#endif // MediaResult_h_
\ No newline at end of file
--- a/dom/media/moz.build
+++ b/dom/media/moz.build
@@ -116,16 +116,17 @@ EXPORTS += [
     'MediaFormatReader.h',
     'MediaInfo.h',
     'MediaMetadataManager.h',
     'MediaPrefs.h',
     'MediaQueue.h',
     'MediaRecorder.h',
     'MediaResource.h',
     'MediaResourceCallback.h',
+    'MediaResult.h',
     'MediaSegment.h',
     'MediaStatistics.h',
     'MediaStreamGraph.h',
     'MediaStreamListener.h',
     'MediaStreamVideoSink.h',
     'MediaTimer.h',
     'MediaTrack.h',
     'MediaTrackList.h',