Home | History | Annotate | Download | only in renderer
      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.
      4 
      5 #include "extensions/renderer/v8_context_native_handler.h"
      6 
      7 #include "base/bind.h"
      8 #include "extensions/common/features/feature.h"
      9 #include "extensions/renderer/dispatcher.h"
     10 #include "extensions/renderer/script_context.h"
     11 
     12 namespace extensions {
     13 
     14 V8ContextNativeHandler::V8ContextNativeHandler(ScriptContext* context,
     15                                                Dispatcher* dispatcher)
     16     : ObjectBackedNativeHandler(context),
     17       context_(context),
     18       dispatcher_(dispatcher) {
     19   RouteFunction("GetAvailability",
     20                 base::Bind(&V8ContextNativeHandler::GetAvailability,
     21                            base::Unretained(this)));
     22   RouteFunction("GetModuleSystem",
     23                 base::Bind(&V8ContextNativeHandler::GetModuleSystem,
     24                            base::Unretained(this)));
     25 }
     26 
     27 void V8ContextNativeHandler::GetAvailability(
     28     const v8::FunctionCallbackInfo<v8::Value>& args) {
     29   CHECK_EQ(args.Length(), 1);
     30   v8::Isolate* isolate = args.GetIsolate();
     31   std::string api_name = *v8::String::Utf8Value(args[0]->ToString());
     32   Feature::Availability availability = context_->GetAvailability(api_name);
     33 
     34   v8::Handle<v8::Object> ret = v8::Object::New(isolate);
     35   ret->Set(v8::String::NewFromUtf8(isolate, "is_available"),
     36            v8::Boolean::New(isolate, availability.is_available()));
     37   ret->Set(v8::String::NewFromUtf8(isolate, "message"),
     38            v8::String::NewFromUtf8(isolate, availability.message().c_str()));
     39   ret->Set(v8::String::NewFromUtf8(isolate, "result"),
     40            v8::Integer::New(isolate, availability.result()));
     41   args.GetReturnValue().Set(ret);
     42 }
     43 
     44 void V8ContextNativeHandler::GetModuleSystem(
     45     const v8::FunctionCallbackInfo<v8::Value>& args) {
     46   CHECK_EQ(args.Length(), 1);
     47   CHECK(args[0]->IsObject());
     48   v8::Handle<v8::Context> v8_context =
     49       v8::Handle<v8::Object>::Cast(args[0])->CreationContext();
     50   ScriptContext* context =
     51       dispatcher_->script_context_set().GetByV8Context(v8_context);
     52   args.GetReturnValue().Set(context->module_system()->NewInstance());
     53 }
     54 
     55 }  // namespace extensions
     56