普通文本  |  87行  |  3.17 KB

// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/process_info_native_handler.h"

#include "base/bind.h"
#include "base/command_line.h"
#include "extensions/renderer/script_context.h"

namespace extensions {

ProcessInfoNativeHandler::ProcessInfoNativeHandler(
    ScriptContext* context,
    const std::string& extension_id,
    const std::string& context_type,
    bool is_incognito_context,
    int manifest_version,
    bool send_request_disabled)
    : ObjectBackedNativeHandler(context),
      extension_id_(extension_id),
      context_type_(context_type),
      is_incognito_context_(is_incognito_context),
      manifest_version_(manifest_version),
      send_request_disabled_(send_request_disabled) {
  RouteFunction("GetExtensionId",
                base::Bind(&ProcessInfoNativeHandler::GetExtensionId,
                           base::Unretained(this)));
  RouteFunction("GetContextType",
                base::Bind(&ProcessInfoNativeHandler::GetContextType,
                           base::Unretained(this)));
  RouteFunction("InIncognitoContext",
                base::Bind(&ProcessInfoNativeHandler::InIncognitoContext,
                           base::Unretained(this)));
  RouteFunction("GetManifestVersion",
                base::Bind(&ProcessInfoNativeHandler::GetManifestVersion,
                           base::Unretained(this)));
  RouteFunction("IsSendRequestDisabled",
                base::Bind(&ProcessInfoNativeHandler::IsSendRequestDisabled,
                           base::Unretained(this)));
  RouteFunction(
      "HasSwitch",
      base::Bind(&ProcessInfoNativeHandler::HasSwitch, base::Unretained(this)));
}

void ProcessInfoNativeHandler::GetExtensionId(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
  args.GetReturnValue().Set(
      v8::String::NewFromUtf8(args.GetIsolate(), extension_id_.c_str()));
}

void ProcessInfoNativeHandler::GetContextType(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
  args.GetReturnValue().Set(
      v8::String::NewFromUtf8(args.GetIsolate(), context_type_.c_str()));
}

void ProcessInfoNativeHandler::InIncognitoContext(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
  args.GetReturnValue().Set(is_incognito_context_);
}

void ProcessInfoNativeHandler::GetManifestVersion(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
  args.GetReturnValue().Set(static_cast<int32_t>(manifest_version_));
}

void ProcessInfoNativeHandler::IsSendRequestDisabled(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
  if (send_request_disabled_) {
    args.GetReturnValue().Set(v8::String::NewFromUtf8(
        args.GetIsolate(),
        "sendRequest and onRequest are obsolete."
        " Please use sendMessage and onMessage instead."));
  }
}

void ProcessInfoNativeHandler::HasSwitch(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
  CHECK(args.Length() == 1 && args[0]->IsString());
  bool has_switch = CommandLine::ForCurrentProcess()->HasSwitch(
      *v8::String::Utf8Value(args[0]));
  args.GetReturnValue().Set(v8::Boolean::New(args.GetIsolate(), has_switch));
}

}  // namespace extensions