OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 "chrome/renderer/principals_extension_bindings.h" | |
6 | |
7 #include "chrome/common/render_messages.h" | |
8 #include "content/public/renderer/render_view.h" | |
9 #include "third_party/WebKit/public/web/WebDocument.h" | |
10 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
11 #include "third_party/WebKit/public/web/WebView.h" | |
12 #include "v8/include/v8.h" | |
13 | |
14 using blink::WebLocalFrame; | |
15 using blink::WebView; | |
16 using content::RenderView; | |
17 | |
18 namespace { | |
19 | |
20 class PrincipalsExtensionWrapper : public v8::Extension { | |
21 public: | |
22 PrincipalsExtensionWrapper(); | |
23 | |
24 private: | |
25 // v8::Extension overrides. | |
26 v8::Handle<v8::FunctionTemplate> GetNativeFunctionTemplate( | |
27 v8::Isolate* isolate, | |
28 v8::Handle<v8::String> name) override; | |
29 | |
30 static RenderView* GetRenderView(); | |
31 | |
32 static void GetManagedAccounts( | |
33 const v8::FunctionCallbackInfo<v8::Value>& args); | |
34 static void ShowBrowserAccountManagementUI( | |
35 const v8::FunctionCallbackInfo<v8::Value>& args); | |
36 | |
37 DISALLOW_COPY_AND_ASSIGN(PrincipalsExtensionWrapper); | |
38 }; | |
39 | |
40 const char kPrincipalsExtensionName[] = "v8/Principals"; | |
41 const char kPrincipalsExtensionCode[] = | |
42 "var chrome;" | |
43 "if (!chrome)" | |
44 " chrome = {};" | |
45 "if (!chrome.principals)" | |
46 " chrome.principals = {};" | |
47 "chrome.principals.getManagedAccounts = function() {" | |
48 " native function NativeGetManagedAccounts();" | |
49 " return NativeGetManagedAccounts();" | |
50 "};" | |
51 "chrome.principals.showBrowserAccountManagementUI = function() {" | |
52 " native function ShowBrowserAccountManagementUI();" | |
53 " ShowBrowserAccountManagementUI();" | |
54 "};"; | |
55 | |
56 PrincipalsExtensionWrapper::PrincipalsExtensionWrapper() : v8::Extension( | |
57 kPrincipalsExtensionName, kPrincipalsExtensionCode) {} | |
58 | |
59 v8::Handle<v8::FunctionTemplate> | |
60 PrincipalsExtensionWrapper::GetNativeFunctionTemplate( | |
61 v8::Isolate* isolate, v8::Handle<v8::String> name) { | |
62 if (name->Equals(v8::String::NewFromUtf8( | |
63 isolate, "NativeGetManagedAccounts"))) { | |
64 return v8::FunctionTemplate::New(isolate, GetManagedAccounts); | |
65 } else if (name->Equals(v8::String::NewFromUtf8( | |
66 isolate, "ShowBrowserAccountManagementUI"))) { | |
67 return v8::FunctionTemplate::New(isolate, ShowBrowserAccountManagementUI); | |
68 } | |
69 return v8::Handle<v8::FunctionTemplate>(); | |
70 } | |
71 | |
72 void PrincipalsExtensionWrapper::GetManagedAccounts( | |
73 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
74 RenderView* render_view = GetRenderView(); | |
75 if (!render_view) { | |
76 args.GetReturnValue().SetNull(); | |
77 return; | |
78 } | |
79 | |
80 std::vector<std::string> accounts; | |
81 render_view->Send(new ChromeViewHostMsg_GetManagedAccounts( | |
82 WebLocalFrame::frameForCurrentContext()->document().url(), | |
83 &accounts)); | |
84 | |
85 v8::Isolate* isolate = args.GetIsolate(); | |
86 v8::Local<v8::Array> v8_result = v8::Array::New(isolate); | |
87 int v8_index = 0; | |
88 for (std::vector<std::string>::const_iterator it = accounts.begin(); | |
89 it != accounts.end(); ++it) { | |
90 v8_result->Set(v8::Integer::New(isolate, v8_index++), | |
91 v8::String::NewFromUtf8(isolate, it->c_str(), | |
92 v8::String::kNormalString, it->length())); | |
93 } | |
94 args.GetReturnValue().Set(v8_result); | |
95 } | |
96 | |
97 void PrincipalsExtensionWrapper::ShowBrowserAccountManagementUI( | |
98 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
99 // TODO(guohui): need to figure out how to prevent abuse. | |
100 RenderView* render_view = GetRenderView(); | |
101 if (!render_view) return; | |
102 | |
103 render_view->Send(new ChromeViewHostMsg_ShowBrowserAccountManagementUI()); | |
104 } | |
105 | |
106 RenderView* PrincipalsExtensionWrapper::GetRenderView() { | |
107 WebLocalFrame* webframe = WebLocalFrame::frameForCurrentContext(); | |
108 DCHECK(webframe) << "There should be an active frame since we just got " | |
109 "a native function called."; | |
110 if (!webframe) | |
111 return NULL; | |
112 | |
113 WebView* webview = webframe->view(); | |
114 if (!webview) // can happen during closing | |
115 return NULL; | |
116 | |
117 return RenderView::FromWebView(webview); | |
118 } | |
119 | |
120 } // namespace | |
121 | |
122 namespace extensions_v8 { | |
123 | |
124 v8::Extension* PrincipalsExtension::Get() { | |
125 return new PrincipalsExtensionWrapper(); | |
126 } | |
127 | |
128 } // namespace extensions_v8 | |
OLD | NEW |