OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/web_ui_extension.h" | 5 #include "content/renderer/web_ui_extension.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.h" |
10 #include "content/public/child/v8_value_converter.h" | 10 #include "content/public/child/v8_value_converter.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 // Exposes two methods: | 58 // Exposes two methods: |
59 // - chrome.send: Used to send messages to the browser. Requires the message | 59 // - chrome.send: Used to send messages to the browser. Requires the message |
60 // name as the first argument and can have an optional second argument that | 60 // name as the first argument and can have an optional second argument that |
61 // should be an array. | 61 // should be an array. |
62 // - chrome.getVariableValue: Returns value for the input variable name if such | 62 // - chrome.getVariableValue: Returns value for the input variable name if such |
63 // a value was set by the browser. Else will return an empty string. | 63 // a value was set by the browser. Else will return an empty string. |
64 void WebUIExtension::Install(blink::WebFrame* frame) { | 64 void WebUIExtension::Install(blink::WebFrame* frame) { |
65 v8::Isolate* isolate = blink::mainThreadIsolate(); | 65 v8::Isolate* isolate = blink::mainThreadIsolate(); |
66 v8::HandleScope handle_scope(isolate); | 66 v8::HandleScope handle_scope(isolate); |
67 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | 67 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
68 if (context.IsEmpty()) | 68 if (context.IsEmpty()) |
69 return; | 69 return; |
70 | 70 |
71 v8::Context::Scope context_scope(context); | 71 v8::Context::Scope context_scope(context); |
72 | 72 |
73 v8::Handle<v8::Object> chrome = GetOrCreateChromeObject(isolate, | 73 v8::Local<v8::Object> chrome = GetOrCreateChromeObject(isolate, |
74 context->Global()); | 74 context->Global()); |
75 chrome->Set(gin::StringToSymbol(isolate, "send"), | 75 chrome->Set(gin::StringToSymbol(isolate, "send"), |
76 gin::CreateFunctionTemplate( | 76 gin::CreateFunctionTemplate( |
77 isolate, base::Bind(&WebUIExtension::Send))->GetFunction()); | 77 isolate, base::Bind(&WebUIExtension::Send))->GetFunction()); |
78 chrome->Set(gin::StringToSymbol(isolate, "getVariableValue"), | 78 chrome->Set(gin::StringToSymbol(isolate, "getVariableValue"), |
79 gin::CreateFunctionTemplate( | 79 gin::CreateFunctionTemplate( |
80 isolate, base::Bind(&WebUIExtension::GetVariableValue)) | 80 isolate, base::Bind(&WebUIExtension::GetVariableValue)) |
81 ->GetFunction()); | 81 ->GetFunction()); |
82 } | 82 } |
83 | 83 |
84 // static | 84 // static |
85 void WebUIExtension::Send(gin::Arguments* args) { | 85 void WebUIExtension::Send(gin::Arguments* args) { |
86 blink::WebFrame* frame; | 86 blink::WebFrame* frame; |
87 RenderView* render_view; | 87 RenderView* render_view; |
88 if (!ShouldRespondToRequest(&frame, &render_view)) | 88 if (!ShouldRespondToRequest(&frame, &render_view)) |
89 return; | 89 return; |
90 | 90 |
91 std::string message; | 91 std::string message; |
92 if (!args->GetNext(&message)) { | 92 if (!args->GetNext(&message)) { |
93 args->ThrowError(); | 93 args->ThrowError(); |
94 return; | 94 return; |
95 } | 95 } |
96 | 96 |
97 // If they've provided an optional message parameter, convert that into a | 97 // If they've provided an optional message parameter, convert that into a |
98 // Value to send to the browser process. | 98 // Value to send to the browser process. |
99 scoped_ptr<base::ListValue> content; | 99 scoped_ptr<base::ListValue> content; |
100 if (args->PeekNext().IsEmpty() || args->PeekNext()->IsUndefined()) { | 100 if (args->PeekNext().IsEmpty() || args->PeekNext()->IsUndefined()) { |
101 content.reset(new base::ListValue()); | 101 content.reset(new base::ListValue()); |
102 } else { | 102 } else { |
103 v8::Handle<v8::Object> obj; | 103 v8::Local<v8::Object> obj; |
104 if (!args->GetNext(&obj)) { | 104 if (!args->GetNext(&obj)) { |
105 args->ThrowError(); | 105 args->ThrowError(); |
106 return; | 106 return; |
107 } | 107 } |
108 | 108 |
109 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 109 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
110 | 110 |
111 base::Value* value = | 111 base::Value* value = |
112 converter->FromV8Value(obj, frame->mainWorldScriptContext()); | 112 converter->FromV8Value(obj, frame->mainWorldScriptContext()); |
113 base::ListValue* list = NULL; | 113 base::ListValue* list = NULL; |
(...skipping 13 matching lines...) Expand all Loading... |
127 std::string WebUIExtension::GetVariableValue(const std::string& name) { | 127 std::string WebUIExtension::GetVariableValue(const std::string& name) { |
128 blink::WebFrame* frame; | 128 blink::WebFrame* frame; |
129 RenderView* render_view; | 129 RenderView* render_view; |
130 if (!ShouldRespondToRequest(&frame, &render_view)) | 130 if (!ShouldRespondToRequest(&frame, &render_view)) |
131 return std::string(); | 131 return std::string(); |
132 | 132 |
133 return WebUIExtensionData::Get(render_view)->GetValue(name); | 133 return WebUIExtensionData::Get(render_view)->GetValue(name); |
134 } | 134 } |
135 | 135 |
136 } // namespace content | 136 } // namespace content |
OLD | NEW |