Fix FullScreen crash in Webapp
[platform/framework/web/chromium-efl.git] / gin / arguments.h
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef GIN_ARGUMENTS_H_
6 #define GIN_ARGUMENTS_H_
7
8 #include "base/memory/raw_ptr.h"
9 #include "base/memory/raw_ptr_exclusion.h"
10 #include "gin/converter.h"
11 #include "gin/gin_export.h"
12
13 namespace gin {
14
15 // Arguments is a wrapper around v8::FunctionCallbackInfo that integrates
16 // with Converter to make it easier to marshall arguments and return values
17 // between V8 and C++.
18 //
19 // If constructed instead with a v8::PropertyCallbackInfo, behaves as though a
20 // function with no arguments had been called.
21 class GIN_EXPORT Arguments {
22  public:
23   Arguments();
24   explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
25   explicit Arguments(const v8::PropertyCallbackInfo<v8::Value>& info);
26   ~Arguments();
27
28   template <typename T>
29   bool GetHolder(T* out) const {
30     v8::Local<v8::Object> holder = is_for_property_
31                                        ? info_for_property_->Holder()
32                                        : info_for_function_->Holder();
33     return ConvertFromV8(isolate_, holder, out);
34   }
35
36   template<typename T>
37   bool GetData(T* out) {
38     v8::Local<v8::Value> data = is_for_property_ ? info_for_property_->Data()
39                                                  : info_for_function_->Data();
40     return ConvertFromV8(isolate_, data, out);
41   }
42
43   template<typename T>
44   bool GetNext(T* out) {
45     if (is_for_property_ || next_ >= info_for_function_->Length()) {
46       insufficient_arguments_ = true;
47       return false;
48     }
49     v8::Local<v8::Value> val = (*info_for_function_)[next_++];
50     return ConvertFromV8(isolate_, val, out);
51   }
52
53   template<typename T>
54   bool GetRemaining(std::vector<T>* out) {
55     if (is_for_property_ || next_ >= info_for_function_->Length()) {
56       insufficient_arguments_ = true;
57       return false;
58     }
59     int remaining = info_for_function_->Length() - next_;
60     out->resize(remaining);
61     for (int i = 0; i < remaining; ++i) {
62       v8::Local<v8::Value> val = (*info_for_function_)[next_++];
63       if (!ConvertFromV8(isolate_, val, &out->at(i)))
64         return false;
65     }
66     return true;
67   }
68
69   bool Skip() {
70     if (is_for_property_)
71       return false;
72     if (next_ >= info_for_function_->Length())
73       return false;
74     next_++;
75     return true;
76   }
77
78   int Length() const {
79     return is_for_property_ ? 0 : info_for_function_->Length();
80   }
81
82   template<typename T>
83   void Return(T val) {
84     v8::Local<v8::Value> v8_value;
85     if (!TryConvertToV8(isolate_, val, &v8_value))
86       return;
87     (is_for_property_ ? info_for_property_->GetReturnValue()
88                       : info_for_function_->GetReturnValue())
89         .Set(v8_value);
90   }
91
92   // Returns the creation context of the Holder.
93   v8::Local<v8::Context> GetHolderCreationContext() const;
94
95   // Always check the return value whether the handle is empty before
96   // dereferencing the handle.
97   v8::Local<v8::Value> PeekNext() const;
98
99   // Returns all arguments. Since this doesn't require any conversion, it
100   // cannot fail. This does not rely on or modify the current position in the
101   // array used by Get/PeekNext().
102   v8::LocalVector<v8::Value> GetAll() const;
103
104   // Returns the original v8::FunctionCallbackInfo used to construct this
105   // Arguments if it exists, or nullptr otherwise.
106   const v8::FunctionCallbackInfo<v8::Value>* GetFunctionCallbackInfo() const {
107     return info_for_function_;
108   }
109
110   void ThrowError() const;
111   void ThrowTypeError(const std::string& message) const;
112
113   v8::Isolate* isolate() const { return isolate_; }
114
115   // Allows the function handler to distinguish between normal invocation
116   // and object construction.
117   bool IsConstructCall() const;
118
119  private:
120   raw_ptr<v8::Isolate> isolate_;
121   union {
122     // This field is not a raw_ptr<> because it was filtered by the rewriter
123     // for: #union
124     RAW_PTR_EXCLUSION const v8::FunctionCallbackInfo<v8::Value>*
125         info_for_function_;
126     // This field is not a raw_ptr<> because it was filtered by the rewriter
127     // for: #union
128     RAW_PTR_EXCLUSION const v8::PropertyCallbackInfo<v8::Value>*
129         info_for_property_;
130   };
131   int next_ = 0;
132   bool insufficient_arguments_ = false;
133   bool is_for_property_ = false;
134 };
135
136 }  // namespace gin
137
138 #endif  // GIN_ARGUMENTS_H_