Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / renderer / extensions / object_backed_native_handler.cc
1 // Copyright (c) 2012 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 // TODO(dcarney): Remove this when UnsafePersistent is removed.
6 #define V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
7
8 #include "chrome/renderer/extensions/object_backed_native_handler.h"
9
10 #include "base/logging.h"
11 #include "base/memory/linked_ptr.h"
12 #include "chrome/renderer/extensions/chrome_v8_context.h"
13 #include "chrome/renderer/extensions/console.h"
14 #include "chrome/renderer/extensions/module_system.h"
15 #include "v8/include/v8.h"
16
17 namespace extensions {
18
19 namespace {
20 // Key for the base::Bound routed function.
21 const char* kHandlerFunction = "handler_function";
22 }  // namespace
23
24 ObjectBackedNativeHandler::ObjectBackedNativeHandler(
25     ChromeV8Context* context)
26     : context_(context),
27       object_template_(v8::ObjectTemplate::New(
28           context->v8_context()->GetIsolate())) {
29 }
30
31 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() {
32   Invalidate();
33 }
34
35 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() {
36   return object_template_.NewHandle(v8::Isolate::GetCurrent())->NewInstance();
37 }
38
39 // static
40 void ObjectBackedNativeHandler::Router(
41     const v8::FunctionCallbackInfo<v8::Value>& args) {
42   v8::HandleScope handle_scope(args.GetIsolate());
43   v8::Handle<v8::Object> data = args.Data().As<v8::Object>();
44
45   v8::Handle<v8::Value> handler_function_value =
46       data->Get(v8::String::NewFromUtf8(args.GetIsolate(), kHandlerFunction));
47   // See comment in header file for why we do this.
48   if (handler_function_value.IsEmpty() ||
49       handler_function_value->IsUndefined()) {
50     console::Error(args.GetIsolate()->GetCallingContext(),
51                    "Extension view no longer exists");
52     return;
53   }
54   DCHECK(handler_function_value->IsExternal());
55   static_cast<HandlerFunction*>(
56       handler_function_value.As<v8::External>()->Value())->Run(args);
57 }
58
59 void ObjectBackedNativeHandler::RouteFunction(
60     const std::string& name,
61     const HandlerFunction& handler_function) {
62   v8::Isolate* isolate = v8::Isolate::GetCurrent();
63   v8::HandleScope handle_scope(isolate);
64   v8::Context::Scope context_scope(context_->v8_context());
65
66   v8::Persistent<v8::Object> data(isolate, v8::Object::New(isolate));
67   v8::Local<v8::Object> local_data = v8::Local<v8::Object>::New(isolate, data);
68   local_data->Set(
69       v8::String::NewFromUtf8(isolate, kHandlerFunction),
70       v8::External::New(isolate, new HandlerFunction(handler_function)));
71   v8::Handle<v8::FunctionTemplate> function_template =
72       v8::FunctionTemplate::New(isolate, Router, local_data);
73   object_template_.NewHandle(isolate)
74       ->Set(isolate, name.c_str(), function_template);
75   router_data_.push_back(UnsafePersistent<v8::Object>(&data));
76 }
77
78 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const {
79   return context_->isolate();
80 }
81
82 void ObjectBackedNativeHandler::Invalidate() {
83   if (!is_valid())
84     return;
85   v8::Isolate* isolate = v8::Isolate::GetCurrent();
86   v8::HandleScope handle_scope(isolate);
87   v8::Context::Scope context_scope(context_->v8_context());
88
89   for (RouterData::iterator it = router_data_.begin();
90        it != router_data_.end(); ++it) {
91     v8::Handle<v8::Object> data = it->newLocal(isolate);
92     v8::Handle<v8::Value> handler_function_value =
93         data->Get(v8::String::NewFromUtf8(isolate, kHandlerFunction));
94     CHECK(!handler_function_value.IsEmpty());
95     delete static_cast<HandlerFunction*>(
96         handler_function_value.As<v8::External>()->Value());
97     data->Delete(v8::String::NewFromUtf8(isolate, kHandlerFunction));
98     it->dispose();
99   }
100   object_template_.reset();
101   context_ = NULL;
102   NativeHandler::Invalidate();
103 }
104
105 }   // namespace extensions