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/process_info_native_handler.h"
6
7#include "base/bind.h"
8#include "base/command_line.h"
9#include "extensions/renderer/script_context.h"
10
11namespace extensions {
12
13ProcessInfoNativeHandler::ProcessInfoNativeHandler(
14    ScriptContext* context,
15    const std::string& extension_id,
16    const std::string& context_type,
17    bool is_incognito_context,
18    int manifest_version,
19    bool send_request_disabled)
20    : ObjectBackedNativeHandler(context),
21      extension_id_(extension_id),
22      context_type_(context_type),
23      is_incognito_context_(is_incognito_context),
24      manifest_version_(manifest_version),
25      send_request_disabled_(send_request_disabled) {
26  RouteFunction("GetExtensionId",
27                base::Bind(&ProcessInfoNativeHandler::GetExtensionId,
28                           base::Unretained(this)));
29  RouteFunction("GetContextType",
30                base::Bind(&ProcessInfoNativeHandler::GetContextType,
31                           base::Unretained(this)));
32  RouteFunction("InIncognitoContext",
33                base::Bind(&ProcessInfoNativeHandler::InIncognitoContext,
34                           base::Unretained(this)));
35  RouteFunction("GetManifestVersion",
36                base::Bind(&ProcessInfoNativeHandler::GetManifestVersion,
37                           base::Unretained(this)));
38  RouteFunction("IsSendRequestDisabled",
39                base::Bind(&ProcessInfoNativeHandler::IsSendRequestDisabled,
40                           base::Unretained(this)));
41  RouteFunction(
42      "HasSwitch",
43      base::Bind(&ProcessInfoNativeHandler::HasSwitch, base::Unretained(this)));
44}
45
46void ProcessInfoNativeHandler::GetExtensionId(
47    const v8::FunctionCallbackInfo<v8::Value>& args) {
48  args.GetReturnValue().Set(
49      v8::String::NewFromUtf8(args.GetIsolate(), extension_id_.c_str()));
50}
51
52void ProcessInfoNativeHandler::GetContextType(
53    const v8::FunctionCallbackInfo<v8::Value>& args) {
54  args.GetReturnValue().Set(
55      v8::String::NewFromUtf8(args.GetIsolate(), context_type_.c_str()));
56}
57
58void ProcessInfoNativeHandler::InIncognitoContext(
59    const v8::FunctionCallbackInfo<v8::Value>& args) {
60  args.GetReturnValue().Set(is_incognito_context_);
61}
62
63void ProcessInfoNativeHandler::GetManifestVersion(
64    const v8::FunctionCallbackInfo<v8::Value>& args) {
65  args.GetReturnValue().Set(static_cast<int32_t>(manifest_version_));
66}
67
68void ProcessInfoNativeHandler::IsSendRequestDisabled(
69    const v8::FunctionCallbackInfo<v8::Value>& args) {
70  if (send_request_disabled_) {
71    args.GetReturnValue().Set(v8::String::NewFromUtf8(
72        args.GetIsolate(),
73        "sendRequest and onRequest are obsolete."
74        " Please use sendMessage and onMessage instead."));
75  }
76}
77
78void ProcessInfoNativeHandler::HasSwitch(
79    const v8::FunctionCallbackInfo<v8::Value>& args) {
80  CHECK(args.Length() == 1 && args[0]->IsString());
81  bool has_switch = CommandLine::ForCurrentProcess()->HasSwitch(
82      *v8::String::Utf8Value(args[0]));
83  args.GetReturnValue().Set(v8::Boolean::New(args.GetIsolate(), has_switch));
84}
85
86}  // namespace extensions
87