summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/principals_extension_bindings.cc
blob: 33ef1c07e0ed448a1cc847ebbe537a555ee7a515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2013 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 "chrome/renderer/principals_extension_bindings.h"

#include "chrome/common/render_messages.h"
#include "content/public/renderer/render_view.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "v8/include/v8.h"

using blink::WebLocalFrame;
using blink::WebView;
using content::RenderView;

namespace {

class PrincipalsExtensionWrapper : public v8::Extension {
 public:
  PrincipalsExtensionWrapper();

 private:
  // v8::Extension overrides.
  virtual v8::Handle<v8::FunctionTemplate> GetNativeFunctionTemplate(
      v8::Isolate* isolate,
      v8::Handle<v8::String> name) OVERRIDE;

  static RenderView* GetRenderView();

  static void GetManagedAccounts(
      const v8::FunctionCallbackInfo<v8::Value>& args);
  static void ShowBrowserAccountManagementUI(
      const v8::FunctionCallbackInfo<v8::Value>& args);

  DISALLOW_COPY_AND_ASSIGN(PrincipalsExtensionWrapper);
};

const char* kPrincipalsExtensionName = "v8/Principals";
const char* kPrincipalsExtensionCode =
    "var chrome;"
    "if (!chrome)"
    "  chrome = {};"
    "if (!chrome.principals)"
    "  chrome.principals = {};"
    "chrome.principals.getManagedAccounts = function() {"
    "  native function NativeGetManagedAccounts();"
    "  return NativeGetManagedAccounts();"
    "};"
    "chrome.principals.showBrowserAccountManagementUI = function() {"
    "  native function ShowBrowserAccountManagementUI();"
    "  ShowBrowserAccountManagementUI();"
    "};";

PrincipalsExtensionWrapper::PrincipalsExtensionWrapper() : v8::Extension(
    kPrincipalsExtensionName, kPrincipalsExtensionCode) {}

v8::Handle<v8::FunctionTemplate>
    PrincipalsExtensionWrapper::GetNativeFunctionTemplate(
        v8::Isolate* isolate, v8::Handle<v8::String> name) {
  if (name->Equals(v8::String::NewFromUtf8(
      isolate, "NativeGetManagedAccounts"))) {
    return v8::FunctionTemplate::New(isolate, GetManagedAccounts);
  } else if (name->Equals(v8::String::NewFromUtf8(
      isolate, "ShowBrowserAccountManagementUI"))) {
    return v8::FunctionTemplate::New(isolate, ShowBrowserAccountManagementUI);
  }
  return v8::Handle<v8::FunctionTemplate>();
}

void PrincipalsExtensionWrapper::GetManagedAccounts(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
  RenderView* render_view = GetRenderView();
  if (!render_view) {
    args.GetReturnValue().SetNull();
    return;
  }

  std::vector<std::string> accounts;
  render_view->Send(new ChromeViewHostMsg_GetManagedAccounts(
      WebLocalFrame::frameForCurrentContext()->document().url(),
      &accounts));

  v8::Isolate* isolate = args.GetIsolate();
  v8::Local<v8::Array> v8_result = v8::Array::New(isolate);
  int v8_index = 0;
  for (std::vector<std::string>::const_iterator it = accounts.begin();
       it != accounts.end(); ++it) {
    v8_result->Set(v8::Integer::New(isolate, v8_index++),
        v8::String::NewFromUtf8(isolate, it->c_str(),
                                v8::String::kNormalString, it->length()));
  }
  args.GetReturnValue().Set(v8_result);
}

void PrincipalsExtensionWrapper::ShowBrowserAccountManagementUI(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
  // TODO(guohui): need to figure out how to prevent abuse.
  RenderView* render_view = GetRenderView();
  if (!render_view) return;

  render_view->Send(new ChromeViewHostMsg_ShowBrowserAccountManagementUI());
}

RenderView* PrincipalsExtensionWrapper::GetRenderView() {
  WebLocalFrame* webframe = WebLocalFrame::frameForCurrentContext();
  DCHECK(webframe) << "There should be an active frame since we just got "
      "a native function called.";
  if (!webframe)
    return NULL;

  WebView* webview = webframe->view();
  if (!webview)  // can happen during closing
    return NULL;

  return RenderView::FromWebView(webview);
}

}  // namespace

namespace extensions_v8 {

v8::Extension* PrincipalsExtension::Get() {
  return new PrincipalsExtensionWrapper();
}

}  // namespace extensions_v8