OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "content/renderer/java/gin_java_bridge_object.h" | 5 #include "content/renderer/java/gin_java_bridge_object.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "content/common/android/gin_java_bridge_errors.h" | 8 #include "content/common/android/gin_java_bridge_errors.h" |
9 #include "content/common/android/gin_java_bridge_value.h" | 9 #include "content/common/android/gin_java_bridge_value.h" |
10 #include "content/public/child/v8_value_converter.h" | 10 #include "content/public/child/v8_value_converter.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 blink::mainThreadIsolate(), dispatcher, object_id); | 59 blink::mainThreadIsolate(), dispatcher, object_id); |
60 } | 60 } |
61 | 61 |
62 GinJavaBridgeObject::GinJavaBridgeObject( | 62 GinJavaBridgeObject::GinJavaBridgeObject( |
63 v8::Isolate* isolate, | 63 v8::Isolate* isolate, |
64 const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher, | 64 const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher, |
65 GinJavaBridgeDispatcher::ObjectID object_id) | 65 GinJavaBridgeDispatcher::ObjectID object_id) |
66 : gin::NamedPropertyInterceptor(isolate, this), | 66 : gin::NamedPropertyInterceptor(isolate, this), |
67 dispatcher_(dispatcher), | 67 dispatcher_(dispatcher), |
68 object_id_(object_id), | 68 object_id_(object_id), |
69 converter_(new GinJavaBridgeValueConverter()) { | 69 converter_(new GinJavaBridgeValueConverter()), |
| 70 template_cache_(isolate) { |
70 } | 71 } |
71 | 72 |
72 GinJavaBridgeObject::~GinJavaBridgeObject() { | 73 GinJavaBridgeObject::~GinJavaBridgeObject() { |
73 if (dispatcher_) | 74 if (dispatcher_) |
74 dispatcher_->OnGinJavaBridgeObjectDeleted(object_id_); | 75 dispatcher_->OnGinJavaBridgeObjectDeleted(object_id_); |
75 } | 76 } |
76 | 77 |
77 gin::ObjectTemplateBuilder GinJavaBridgeObject::GetObjectTemplateBuilder( | 78 gin::ObjectTemplateBuilder GinJavaBridgeObject::GetObjectTemplateBuilder( |
78 v8::Isolate* isolate) { | 79 v8::Isolate* isolate) { |
79 return gin::Wrappable<GinJavaBridgeObject>::GetObjectTemplateBuilder(isolate) | 80 return gin::Wrappable<GinJavaBridgeObject>::GetObjectTemplateBuilder(isolate) |
80 .AddNamedPropertyInterceptor(); | 81 .AddNamedPropertyInterceptor(); |
81 } | 82 } |
82 | 83 |
83 v8::Local<v8::Value> GinJavaBridgeObject::GetNamedProperty( | 84 v8::Local<v8::Value> GinJavaBridgeObject::GetNamedProperty( |
84 v8::Isolate* isolate, | 85 v8::Isolate* isolate, |
85 const std::string& property) { | 86 const std::string& property) { |
86 std::map<std::string, bool>::iterator method_pos = | 87 std::map<std::string, bool>::iterator method_pos = |
87 known_methods_.find(property); | 88 known_methods_.find(property); |
88 if (method_pos == known_methods_.end()) { | 89 if (method_pos == known_methods_.end()) { |
89 if (!dispatcher_) { | 90 if (!dispatcher_) { |
90 return v8::Local<v8::Value>(); | 91 return v8::Local<v8::Value>(); |
91 } | 92 } |
92 known_methods_[property] = dispatcher_->HasJavaMethod(object_id_, property); | 93 known_methods_[property] = dispatcher_->HasJavaMethod(object_id_, property); |
93 } | 94 } |
94 if (known_methods_[property]) { | 95 if (known_methods_[property]) |
95 return gin::CreateFunctionTemplate( | 96 return GetFunctionTemplate(isolate, property)->GetFunction(); |
96 isolate, | 97 else |
97 base::Bind(&GinJavaBridgeObject::InvokeMethod, | |
98 base::Unretained(this), | |
99 property))->GetFunction(); | |
100 } else { | |
101 return v8::Local<v8::Value>(); | 98 return v8::Local<v8::Value>(); |
102 } | |
103 } | 99 } |
104 | 100 |
105 std::vector<std::string> GinJavaBridgeObject::EnumerateNamedProperties( | 101 std::vector<std::string> GinJavaBridgeObject::EnumerateNamedProperties( |
106 v8::Isolate* isolate) { | 102 v8::Isolate* isolate) { |
107 std::set<std::string> method_names; | 103 std::set<std::string> method_names; |
108 if (dispatcher_) | 104 if (dispatcher_) |
109 dispatcher_->GetJavaMethods(object_id_, &method_names); | 105 dispatcher_->GetJavaMethods(object_id_, &method_names); |
110 return std::vector<std::string> (method_names.begin(), method_names.end()); | 106 return std::vector<std::string> (method_names.begin(), method_names.end()); |
111 } | 107 } |
112 | 108 |
| 109 v8::Local<v8::FunctionTemplate> GinJavaBridgeObject::GetFunctionTemplate( |
| 110 v8::Isolate* isolate, |
| 111 const std::string& name) { |
| 112 v8::Local<v8::FunctionTemplate> function_template = template_cache_.Get(name); |
| 113 if (!function_template.IsEmpty()) |
| 114 return function_template; |
| 115 function_template = gin::CreateFunctionTemplate( |
| 116 isolate, base::Bind(&GinJavaBridgeObject::InvokeMethod, |
| 117 base::Unretained(this), name)); |
| 118 template_cache_.Set(name, function_template); |
| 119 return function_template; |
| 120 } |
| 121 |
113 v8::Handle<v8::Value> GinJavaBridgeObject::InvokeMethod( | 122 v8::Handle<v8::Value> GinJavaBridgeObject::InvokeMethod( |
114 const std::string& name, | 123 const std::string& name, |
115 gin::Arguments* args) { | 124 gin::Arguments* args) { |
116 if (!dispatcher_) { | 125 if (!dispatcher_) { |
117 args->isolate()->ThrowException(v8::Exception::Error(gin::StringToV8( | 126 args->isolate()->ThrowException(v8::Exception::Error(gin::StringToV8( |
118 args->isolate(), kMethodInvocationErrorMessage))); | 127 args->isolate(), kMethodInvocationErrorMessage))); |
119 return v8::Undefined(args->isolate()); | 128 return v8::Undefined(args->isolate()); |
120 } | 129 } |
121 | 130 |
122 base::ListValue arguments; | 131 base::ListValue arguments; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 float float_value; | 175 float float_value; |
167 gin_value->GetAsNonFinite(&float_value); | 176 gin_value->GetAsNonFinite(&float_value); |
168 return v8::Number::New(args->isolate(), float_value); | 177 return v8::Number::New(args->isolate(), float_value); |
169 } | 178 } |
170 return v8::Undefined(args->isolate()); | 179 return v8::Undefined(args->isolate()); |
171 } | 180 } |
172 | 181 |
173 gin::WrapperInfo GinJavaBridgeObject::kWrapperInfo = {gin::kEmbedderNativeGin}; | 182 gin::WrapperInfo GinJavaBridgeObject::kWrapperInfo = {gin::kEmbedderNativeGin}; |
174 | 183 |
175 } // namespace content | 184 } // namespace content |
OLD | NEW |