Use "= delete" for DISALLOW_COPY and DISALLOW_ASSIGN.
[chromium-blink-merge.git] / content / renderer / java / gin_java_bridge_object.cc
blob00e9aec45daa605a9f671f04fafb666cfd305ce5
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
3 // found in the LICENSE file.
5 #include "content/renderer/java/gin_java_bridge_object.h"
7 #include "content/renderer/java/gin_java_function_invocation_helper.h"
8 #include "gin/function_template.h"
9 #include "third_party/WebKit/public/web/WebFrame.h"
10 #include "third_party/WebKit/public/web/WebKit.h"
12 namespace content {
14 // static
15 GinJavaBridgeObject* GinJavaBridgeObject::InjectNamed(
16 blink::WebFrame* frame,
17 const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher,
18 const std::string& object_name,
19 GinJavaBridgeDispatcher::ObjectID object_id) {
20 v8::Isolate* isolate = blink::mainThreadIsolate();
21 v8::HandleScope handle_scope(isolate);
22 v8::Local<v8::Context> context = frame->mainWorldScriptContext();
23 if (context.IsEmpty())
24 return NULL;
26 GinJavaBridgeObject* object =
27 new GinJavaBridgeObject(isolate, dispatcher, object_id);
29 v8::Context::Scope context_scope(context);
30 v8::Local<v8::Object> global = context->Global();
31 gin::Handle<GinJavaBridgeObject> controller =
32 gin::CreateHandle(isolate, object);
33 // WrappableBase instance deletes itself in case of a wrapper
34 // creation failure, thus there is no need to delete |object|.
35 if (controller.IsEmpty())
36 return NULL;
38 global->Set(gin::StringToV8(isolate, object_name), controller.ToV8());
39 return object;
42 // static
43 GinJavaBridgeObject* GinJavaBridgeObject::InjectAnonymous(
44 const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher,
45 GinJavaBridgeDispatcher::ObjectID object_id) {
46 return new GinJavaBridgeObject(
47 blink::mainThreadIsolate(), dispatcher, object_id);
50 GinJavaBridgeObject::GinJavaBridgeObject(
51 v8::Isolate* isolate,
52 const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher,
53 GinJavaBridgeDispatcher::ObjectID object_id)
54 : gin::NamedPropertyInterceptor(isolate, this),
55 dispatcher_(dispatcher),
56 object_id_(object_id),
57 template_cache_(isolate) {
60 GinJavaBridgeObject::~GinJavaBridgeObject() {
61 if (dispatcher_)
62 dispatcher_->OnGinJavaBridgeObjectDeleted(this);
65 gin::ObjectTemplateBuilder GinJavaBridgeObject::GetObjectTemplateBuilder(
66 v8::Isolate* isolate) {
67 return gin::Wrappable<GinJavaBridgeObject>::GetObjectTemplateBuilder(isolate)
68 .AddNamedPropertyInterceptor();
71 v8::Local<v8::Value> GinJavaBridgeObject::GetNamedProperty(
72 v8::Isolate* isolate,
73 const std::string& property) {
74 std::map<std::string, bool>::iterator method_pos =
75 known_methods_.find(property);
76 if (method_pos == known_methods_.end()) {
77 if (!dispatcher_) {
78 return v8::Local<v8::Value>();
80 known_methods_[property] = dispatcher_->HasJavaMethod(object_id_, property);
82 if (known_methods_[property])
83 return GetFunctionTemplate(isolate, property)->GetFunction();
84 else
85 return v8::Local<v8::Value>();
88 std::vector<std::string> GinJavaBridgeObject::EnumerateNamedProperties(
89 v8::Isolate* isolate) {
90 std::set<std::string> method_names;
91 if (dispatcher_)
92 dispatcher_->GetJavaMethods(object_id_, &method_names);
93 return std::vector<std::string> (method_names.begin(), method_names.end());
96 v8::Local<v8::FunctionTemplate> GinJavaBridgeObject::GetFunctionTemplate(
97 v8::Isolate* isolate,
98 const std::string& name) {
99 v8::Local<v8::FunctionTemplate> function_template = template_cache_.Get(name);
100 if (!function_template.IsEmpty())
101 return function_template;
102 function_template = gin::CreateFunctionTemplate(
103 isolate, base::Bind(&GinJavaFunctionInvocationHelper::Invoke,
104 base::Owned(new GinJavaFunctionInvocationHelper(
105 name, dispatcher_))));
106 template_cache_.Set(name, function_template);
107 return function_template;
110 gin::WrapperInfo GinJavaBridgeObject::kWrapperInfo = {gin::kEmbedderNativeGin};
112 } // namespace content