arguments.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE.chromium file.
  4. #ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_ARGUMENTS_H_
  5. #define ELECTRON_SHELL_COMMON_GIN_HELPER_ARGUMENTS_H_
  6. #include "gin/arguments.h"
  7. namespace gin_helper {
  8. // Provides additional APIs to the gin::Arguments class.
  9. class Arguments : public gin::Arguments {
  10. public:
  11. // Get the next argument, if conversion to T fails then state is unchanged.
  12. //
  13. // This is difference from gin::Arguments::GetNext which always advances the
  14. // |next_| counter no matter whether the conversion succeeds.
  15. template <typename T>
  16. bool GetNext(T* out) {
  17. v8::Local<v8::Value> val = PeekNext();
  18. if (val.IsEmpty())
  19. return false;
  20. if (!gin::ConvertFromV8(isolate(), val, out))
  21. return false;
  22. Skip();
  23. return true;
  24. }
  25. // Gin always returns true when converting V8 value to boolean, we do not want
  26. // this behavior when parsing parameters.
  27. bool GetNext(bool* out) {
  28. v8::Local<v8::Value> val = PeekNext();
  29. if (val.IsEmpty() || !val->IsBoolean())
  30. return false;
  31. *out = val->BooleanValue(isolate());
  32. Skip();
  33. return true;
  34. }
  35. // Throw error with custom error message.
  36. void ThrowError() const;
  37. void ThrowError(base::StringPiece message) const;
  38. private:
  39. // MUST NOT ADD ANY DATA MEMBER.
  40. };
  41. } // namespace gin_helper
  42. #endif // ELECTRON_SHELL_COMMON_GIN_HELPER_ARGUMENTS_H_