Bug 1366511: Part 2 - Allow autoconverting Err(nsresult) to nsresult. r=ehsan,nbp

This allows MOZ_TRY and MOZ_TRY_VAR to be transparently used in XPCOM methods
when compatible Result types are used.

Also removes a compatibility macro in SimpleChannel.cpp, and an identical
specialization in AddonManagerStartup, which are no longer necessary after
this change.

MozReview-Commit-ID: 94iNrPDJEnN

--HG--
extra : rebase_source : 24ad4a54cbd170eb04ded21794530e56b1dfbd82
This commit is contained in:
Kris Maglione 2017-08-29 21:28:22 -07:00
Родитель 92dcd409a3
Коммит c9899cb3fa
11 изменённых файлов: 49 добавлений и 60 удалений

Просмотреть файл

@ -11,7 +11,7 @@
#include "mozilla/CheckedInt.h"
#include "mozilla/EnumSet.h"
#include "mozilla/Range.h"
#include "mozilla/Result.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/ScriptSettings.h"
#include "nsString.h"
@ -21,21 +21,6 @@
namespace mozilla {
// A specialization of GenericErrorResult which auto-converts to a nsresult.
// This should be removed when bug 1366511 is fixed.
template <>
class MOZ_MUST_USE_TYPE GenericErrorResult<nsresult>
{
nsresult mErrorValue;
template<typename V, typename E2> friend class Result;
public:
explicit GenericErrorResult(nsresult aErrorValue) : mErrorValue(aErrorValue) {}
operator nsresult() { return mErrorValue; }
};
namespace loader {
using mozilla::dom::AutoJSAPI;

Просмотреть файл

@ -4,8 +4,8 @@
* 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/. */
#include "mozilla/ScriptPreloader.h"
#include "ScriptPreloader-inl.h"
#include "mozilla/ScriptPreloader.h"
#include "mozilla/loader/ScriptCacheActors.h"
#include "mozilla/ArrayUtils.h"

37
mfbt/ResultExtensions.h Normal file
Просмотреть файл

@ -0,0 +1,37 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* 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/. */
/* Extensions to the Result type to enable simpler handling of XPCOM/NSPR results. */
#ifndef mozilla_ResultExtensions_h
#define mozilla_ResultExtensions_h
#include "mozilla/Assertions.h"
#include "nscore.h"
namespace mozilla {
// Allow nsresult errors to automatically convert to nsresult values, so MOZ_TRY
// can be used in XPCOM methods with Result<T, nserror> results.
template <>
class MOZ_MUST_USE_TYPE GenericErrorResult<nsresult>
{
nsresult mErrorValue;
template<typename V, typename E2> friend class Result;
public:
explicit GenericErrorResult(nsresult aErrorValue) : mErrorValue(aErrorValue)
{
MOZ_ASSERT(NS_FAILED(aErrorValue));
}
operator nsresult() { return mErrorValue; }
};
} // namespace mozilla
#endif // mozilla_ResultExtensions_h

Просмотреть файл

@ -76,6 +76,7 @@ EXPORTS.mozilla = [
'RefCountType.h',
'RefPtr.h',
'Result.h',
'ResultExtensions.h',
'ReverseIterator.h',
'RollingMean.h',
'Saturate.h',

Просмотреть файл

@ -21,18 +21,6 @@
namespace mozilla {
namespace net {
// Like MOZ_TRY, but returns the unwrapped error value rather than a
// GenericErrorResult on failure.
#define TRY_VAR(target, expr) \
do { \
auto result = (expr); \
if (result.isErr()) { \
return result.unwrapErr(); \
} \
(target) = result.unwrap(); \
} while (0)
class SimpleChannel : public nsBaseChannel
{
public:
@ -63,7 +51,7 @@ SimpleChannel::OpenContentStream(bool async, nsIInputStream **streamOut, nsIChan
NS_ENSURE_TRUE(mCallbacks, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIInputStream> stream;
TRY_VAR(stream, mCallbacks->OpenContentStream(async, this));
MOZ_TRY_VAR(stream, mCallbacks->OpenContentStream(async, this));
MOZ_ASSERT(stream);
mCallbacks = nullptr;
@ -79,7 +67,7 @@ SimpleChannel::BeginAsyncRead(nsIStreamListener* listener, nsIRequest** request)
NS_ENSURE_TRUE(mCallbacks, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIRequest> req;
TRY_VAR(req, mCallbacks->StartAsyncRead(listener, this));
MOZ_TRY_VAR(req, mCallbacks->StartAsyncRead(listener, this));
mCallbacks = nullptr;
@ -87,8 +75,6 @@ SimpleChannel::BeginAsyncRead(nsIStreamListener* listener, nsIRequest** request)
return NS_OK;
}
#undef TRY_VAR
class SimpleChannelChild final : public SimpleChannel
, public nsIChildChannel
, public PSimpleChannelChild

Просмотреть файл

@ -6,7 +6,7 @@
#ifndef SimpleChannel_h
#define SimpleChannel_h
#include "mozilla/Result.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/UniquePtr.h"
#include "nsCOMPtr.h"

Просмотреть файл

@ -52,19 +52,6 @@ using OptionalIPCStream = mozilla::ipc::OptionalIPCStream;
namespace mozilla {
template <>
class MOZ_MUST_USE_TYPE GenericErrorResult<nsresult>
{
nsresult mErrorValue;
template<typename V, typename E2> friend class Result;
public:
explicit GenericErrorResult(nsresult aErrorValue) : mErrorValue(aErrorValue) {}
operator nsresult() { return mErrorValue; }
};
namespace net {
using extensions::URLInfo;

Просмотреть файл

@ -8,6 +8,7 @@
#include "mozilla/net/NeckoParent.h"
#include "mozilla/LazyIdleThread.h"
#include "mozilla/Result.h"
#include "SubstitutingProtocolHandler.h"
namespace mozilla {

Просмотреть файл

@ -8,6 +8,7 @@
#include "mozilla/extensions/WebExtensionPolicy.h"
#include "mozilla/AddonManagerWebAPI.h"
#include "mozilla/ResultExtensions.h"
#include "nsEscape.h"
#include "nsISubstitutingProtocolHandler.h"
#include "nsNetUtil.h"

Просмотреть файл

@ -16,6 +16,7 @@
#include "mozilla/Compression.h"
#include "mozilla/LinkedList.h"
#include "mozilla/Preferences.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Services.h"
#include "mozilla/Unused.h"
@ -42,19 +43,6 @@
namespace mozilla {
template <>
class MOZ_MUST_USE_TYPE GenericErrorResult<nsresult>
{
nsresult mErrorValue;
template<typename V, typename E2> friend class Result;
public:
explicit GenericErrorResult(nsresult aErrorValue) : mErrorValue(aErrorValue) {}
operator nsresult() { return mErrorValue; }
};
static inline Result<Ok, nsresult>
WrapNSResult(PRStatus aRv)
{

Просмотреть файл

@ -212,6 +212,9 @@ struct UnusedZero<nsresult>
static const bool value = true;
};
} // namespace detail
template <typename T> class MOZ_MUST_USE_TYPE GenericErrorResult;
template <> class MOZ_MUST_USE_TYPE GenericErrorResult<nsresult>;
} // namespace mozilla
/*