OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GIN_DICTIONARY_H_ | 5 #ifndef GIN_DICTIONARY_H_ |
6 #define GIN_DICTIONARY_H_ | 6 #define GIN_DICTIONARY_H_ |
7 | 7 |
8 #include "gin/converter.h" | 8 #include "gin/converter.h" |
9 #include "gin/gin_export.h" | 9 #include "gin/gin_export.h" |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 class GIN_EXPORT Dictionary { | 25 class GIN_EXPORT Dictionary { |
26 public: | 26 public: |
27 explicit Dictionary(v8::Isolate* isolate); | 27 explicit Dictionary(v8::Isolate* isolate); |
28 Dictionary(v8::Isolate* isolate, v8::Local<v8::Object> object); | 28 Dictionary(v8::Isolate* isolate, v8::Local<v8::Object> object); |
29 ~Dictionary(); | 29 ~Dictionary(); |
30 | 30 |
31 static Dictionary CreateEmpty(v8::Isolate* isolate); | 31 static Dictionary CreateEmpty(v8::Isolate* isolate); |
32 | 32 |
33 template<typename T> | 33 template<typename T> |
34 bool Get(const std::string& key, T* out) { | 34 bool Get(const std::string& key, T* out) { |
35 v8::Local<v8::Value> val; | 35 v8::Local<v8::Value> val = object_->Get(StringToV8(isolate_, key)); |
36 if (!object_->Get(isolate_->GetCurrentContext(), StringToV8(isolate_, key)) | |
37 .ToLocal(&val)) { | |
38 return false; | |
39 } | |
40 return ConvertFromV8(isolate_, val, out); | 36 return ConvertFromV8(isolate_, val, out); |
41 } | 37 } |
42 | 38 |
43 template<typename T> | 39 template<typename T> |
44 bool Set(const std::string& key, T val) { | 40 bool Set(const std::string& key, T val) { |
45 v8::Local<v8::Value> v8_value; | 41 return object_->Set(StringToV8(isolate_, key), ConvertToV8(isolate_, val)); |
46 if (!TryConvertToV8(isolate_, val, &v8_value)) | |
47 return false; | |
48 v8::Maybe<bool> result = | |
49 object_->Set(isolate_->GetCurrentContext(), StringToV8(isolate_, key), | |
50 v8_value); | |
51 return !result.IsNothing() && result.FromJust(); | |
52 } | 42 } |
53 | 43 |
54 v8::Isolate* isolate() const { return isolate_; } | 44 v8::Isolate* isolate() const { return isolate_; } |
55 | 45 |
56 private: | 46 private: |
57 friend struct Converter<Dictionary>; | 47 friend struct Converter<Dictionary>; |
58 | 48 |
59 // TODO(aa): Remove this. Instead, get via FromV8(), Set(), and Get(). | 49 // TODO(aa): Remove this. Instead, get via FromV8(), Set(), and Get(). |
60 v8::Isolate* isolate_; | 50 v8::Isolate* isolate_; |
61 v8::Local<v8::Object> object_; | 51 v8::Local<v8::Object> object_; |
62 }; | 52 }; |
63 | 53 |
64 template<> | 54 template<> |
65 struct GIN_EXPORT Converter<Dictionary> { | 55 struct GIN_EXPORT Converter<Dictionary> { |
66 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, | 56 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
67 Dictionary val); | 57 Dictionary val); |
68 static bool FromV8(v8::Isolate* isolate, | 58 static bool FromV8(v8::Isolate* isolate, |
69 v8::Local<v8::Value> val, | 59 v8::Local<v8::Value> val, |
70 Dictionary* out); | 60 Dictionary* out); |
71 }; | 61 }; |
72 | 62 |
73 } // namespace gin | 63 } // namespace gin |
74 | 64 |
75 #endif // GIN_DICTIONARY_H_ | 65 #endif // GIN_DICTIONARY_H_ |
OLD | NEW |