Enable converting V8 value to gfx::Rect.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 22 Nov 2013 06:22:28 +0000 (14:22 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 22 Nov 2013 06:22:28 +0000 (14:22 +0800)
common/v8_conversions.h

index bd5f72a..b560611 100644 (file)
@@ -11,6 +11,7 @@
 #include "base/files/file_path.h"
 #include "base/string16.h"
 #include "browser/api/atom_api_window.h"
+#include "ui/gfx/rect.h"
 #include "v8/include/v8.h"
 
 // Convert V8 value to arbitrary supported types.
@@ -38,6 +39,20 @@ struct FromV8Value {
     return base::FilePath::FromUTF8Unsafe(FromV8Value(value_));
   }
 
+  operator gfx::Rect() {
+    v8::Handle<v8::Object> rect = value_->ToObject();
+    v8::Handle<v8::Value> x = rect->Get(v8::String::New("x"));
+    v8::Handle<v8::Value> y = rect->Get(v8::String::New("y"));
+    v8::Handle<v8::Value> width = rect->Get(v8::String::New("width"));
+    v8::Handle<v8::Value> height = rect->Get(v8::String::New("height"));
+    if (!x->IsNumber() || !y->IsNumber() ||
+        !width->IsNumber() || !height->IsNumber())
+      return gfx::Rect();
+    else
+      return gfx::Rect(x->IntegerValue(), y->IntegerValue(),
+                       width->IntegerValue(), height->IntegerValue());
+  }
+
   operator std::vector<std::string>() {
     std::vector<std::string> array;
     v8::Handle<v8::Array> v8_array = v8::Handle<v8::Array>::Cast(value_);
@@ -58,11 +73,9 @@ struct FromV8Value {
   }
 
   operator v8::Persistent<v8::Function>() {
-    return value_->IsFunction() ?
-      v8::Persistent<v8::Function>::New(
-          node::node_isolate,
-          v8::Handle<v8::Function>::Cast(value_)) :
-      v8::Persistent<v8::Function>();
+    return v8::Persistent<v8::Function>::New(
+        node::node_isolate,
+        v8::Handle<v8::Function>::Cast(value_));
   }
 
   v8::Handle<v8::Value> value_;
@@ -138,6 +151,11 @@ bool V8ValueCanBeConvertedTo<base::FilePath>(v8::Handle<v8::Value> value) {
 }
 
 template<> inline
+bool V8ValueCanBeConvertedTo<gfx::Rect>(v8::Handle<v8::Value> value) {
+  return value->IsObject();
+}
+
+template<> inline
 bool V8ValueCanBeConvertedTo<std::vector<std::string>>(
     v8::Handle<v8::Value> value) {
   return value->IsArray();