[M40 Merge][WAYLAND_BRINGUP] Display size for Wayland
[platform/framework/web/chromium-efl.git] / gin / data_object_builder.h
1 // Copyright 2017 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 file.
4
5 #ifndef GIN_DATA_OBJECT_BUILDER_H_
6 #define GIN_DATA_OBJECT_BUILDER_H_
7
8 #include <utility>
9
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/strings/string_piece.h"
13 #include "gin/converter.h"
14 #include "gin/gin_export.h"
15 #include "v8/include/v8.h"
16
17 namespace gin {
18
19 // Constructs a JavaScript object with a series of data properties.
20 // (As with default data properties in JavaScript, these properties are
21 // configurable, writable and enumerable.)
22 //
23 // Values are automatically converted using gin::Converter, though if
24 // using a type where the conversion may fail, callers must convert ahead of
25 // time.
26 //
27 // This class avoids the pitfall of using v8::Object::Set, which may invoke
28 // setters on the object prototype.
29 //
30 // Expected usage:
31 // v8::Local<v8::Object> object = gin::DataObjectBuilder(isolate)
32 //     .Set("boolean", true)
33 //     .Set("integer", 42)
34 //     .Build();
35 //
36 // Because this builder class contains local handles, callers must ensure it
37 // does not outlive the scope in which it is created.
38 class GIN_EXPORT DataObjectBuilder {
39  public:
40   explicit DataObjectBuilder(v8::Isolate* isolate);
41
42   template <typename T>
43   DataObjectBuilder& Set(base::StringPiece key, T&& value) {
44     DCHECK(!object_.IsEmpty());
45     v8::Local<v8::String> v8_key = StringToSymbol(isolate_, key);
46     v8::Local<v8::Value> v8_value =
47         ConvertToV8(isolate_, std::forward<T>(value));
48     CHECK(object_->CreateDataProperty(context_, v8_key, v8_value).ToChecked());
49     return *this;
50   }
51
52   template <typename T>
53   DataObjectBuilder& Set(uint32_t index, T&& value) {
54     DCHECK(!object_.IsEmpty());
55     v8::Local<v8::Value> v8_value =
56         ConvertToV8(isolate_, std::forward<T>(value));
57     CHECK(object_->CreateDataProperty(context_, index, v8_value).ToChecked());
58     return *this;
59   }
60
61   v8::Local<v8::Object> Build() {
62     DCHECK(!object_.IsEmpty());
63     v8::Local<v8::Object> result = object_;
64     object_.Clear();
65     return result;
66   }
67
68  private:
69   v8::Isolate* isolate_;
70   v8::Local<v8::Context> context_;
71   v8::Local<v8::Object> object_;
72
73   DISALLOW_COPY_AND_ASSIGN(DataObjectBuilder);
74 };
75
76 }  // namespace gin
77
78 #endif  // GIN_DATA_OBJECT_BUILDER_H_