| 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/common/bindings_policy.h" | 10 #include "content/public/common/bindings_policy.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 public: | 47 public: |
| 48 WebUIExtensionWrapper(); | 48 WebUIExtensionWrapper(); |
| 49 virtual ~WebUIExtensionWrapper(); | 49 virtual ~WebUIExtensionWrapper(); |
| 50 | 50 |
| 51 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 51 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 52 v8::Handle<v8::String> name) OVERRIDE; | 52 v8::Handle<v8::String> name) OVERRIDE; |
| 53 static void Send(const v8::FunctionCallbackInfo<v8::Value>& args); | 53 static void Send(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 54 static void GetVariableValue(const v8::FunctionCallbackInfo<v8::Value>& args); | 54 static void GetVariableValue(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 55 | 55 |
| 56 private: | 56 private: |
| 57 static bool ShouldRespondToRequest(WebKit::WebFrame** frame_ptr, | 57 static bool ShouldRespondToRequest(blink::WebFrame** frame_ptr, |
| 58 RenderView** render_view_ptr); | 58 RenderView** render_view_ptr); |
| 59 | 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(WebUIExtensionWrapper); | 60 DISALLOW_COPY_AND_ASSIGN(WebUIExtensionWrapper); |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 WebUIExtensionWrapper::WebUIExtensionWrapper() | 63 WebUIExtensionWrapper::WebUIExtensionWrapper() |
| 64 : v8::Extension(kWebUIExtensionName, kWebUIExtensionJS) {} | 64 : v8::Extension(kWebUIExtensionName, kWebUIExtensionJS) {} |
| 65 | 65 |
| 66 WebUIExtensionWrapper::~WebUIExtensionWrapper() {} | 66 WebUIExtensionWrapper::~WebUIExtensionWrapper() {} |
| 67 | 67 |
| 68 v8::Handle<v8::FunctionTemplate> WebUIExtensionWrapper::GetNativeFunction( | 68 v8::Handle<v8::FunctionTemplate> WebUIExtensionWrapper::GetNativeFunction( |
| 69 v8::Handle<v8::String> name) { | 69 v8::Handle<v8::String> name) { |
| 70 if (name->Equals(v8::String::New("Send"))) | 70 if (name->Equals(v8::String::New("Send"))) |
| 71 return v8::FunctionTemplate::New(Send); | 71 return v8::FunctionTemplate::New(Send); |
| 72 if (name->Equals(v8::String::New("GetVariableValue"))) | 72 if (name->Equals(v8::String::New("GetVariableValue"))) |
| 73 return v8::FunctionTemplate::New(GetVariableValue); | 73 return v8::FunctionTemplate::New(GetVariableValue); |
| 74 return v8::Handle<v8::FunctionTemplate>(); | 74 return v8::Handle<v8::FunctionTemplate>(); |
| 75 } | 75 } |
| 76 | 76 |
| 77 // static | 77 // static |
| 78 bool WebUIExtensionWrapper::ShouldRespondToRequest( | 78 bool WebUIExtensionWrapper::ShouldRespondToRequest( |
| 79 WebKit::WebFrame** frame_ptr, | 79 blink::WebFrame** frame_ptr, |
| 80 RenderView** render_view_ptr) { | 80 RenderView** render_view_ptr) { |
| 81 WebKit::WebFrame* frame = WebKit::WebFrame::frameForCurrentContext(); | 81 blink::WebFrame* frame = blink::WebFrame::frameForCurrentContext(); |
| 82 if (!frame || !frame->view()) | 82 if (!frame || !frame->view()) |
| 83 return false; | 83 return false; |
| 84 | 84 |
| 85 RenderView* render_view = RenderView::FromWebView(frame->view()); | 85 RenderView* render_view = RenderView::FromWebView(frame->view()); |
| 86 if (!render_view) | 86 if (!render_view) |
| 87 return false; | 87 return false; |
| 88 | 88 |
| 89 GURL frame_url = frame->document().url(); | 89 GURL frame_url = frame->document().url(); |
| 90 | 90 |
| 91 bool webui_enabled = | 91 bool webui_enabled = |
| 92 (render_view->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI) && | 92 (render_view->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI) && |
| 93 (frame_url.SchemeIs(chrome::kChromeUIScheme) || | 93 (frame_url.SchemeIs(chrome::kChromeUIScheme) || |
| 94 frame_url.SchemeIs(chrome::kDataScheme)); | 94 frame_url.SchemeIs(chrome::kDataScheme)); |
| 95 | 95 |
| 96 if (!webui_enabled) | 96 if (!webui_enabled) |
| 97 return false; | 97 return false; |
| 98 | 98 |
| 99 *frame_ptr = frame; | 99 *frame_ptr = frame; |
| 100 *render_view_ptr = render_view; | 100 *render_view_ptr = render_view; |
| 101 return true; | 101 return true; |
| 102 } | 102 } |
| 103 | 103 |
| 104 // static | 104 // static |
| 105 void WebUIExtensionWrapper::Send( | 105 void WebUIExtensionWrapper::Send( |
| 106 const v8::FunctionCallbackInfo<v8::Value>& args) { | 106 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 107 WebKit::WebFrame* frame; | 107 blink::WebFrame* frame; |
| 108 RenderView* render_view; | 108 RenderView* render_view; |
| 109 if (!ShouldRespondToRequest(&frame, &render_view)) | 109 if (!ShouldRespondToRequest(&frame, &render_view)) |
| 110 return; | 110 return; |
| 111 | 111 |
| 112 // We expect at least two parameters - a string message identifier, and | 112 // We expect at least two parameters - a string message identifier, and |
| 113 // an object parameter. The object param can be undefined. | 113 // an object parameter. The object param can be undefined. |
| 114 if (args.Length() != 2 || !args[0]->IsString()) | 114 if (args.Length() != 2 || !args[0]->IsString()) |
| 115 return; | 115 return; |
| 116 | 116 |
| 117 const std::string message = *v8::String::Utf8Value(args[0]->ToString()); | 117 const std::string message = *v8::String::Utf8Value(args[0]->ToString()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 138 // Send the message up to the browser. | 138 // Send the message up to the browser. |
| 139 render_view->Send(new ViewHostMsg_WebUISend(render_view->GetRoutingID(), | 139 render_view->Send(new ViewHostMsg_WebUISend(render_view->GetRoutingID(), |
| 140 frame->document().url(), | 140 frame->document().url(), |
| 141 message, | 141 message, |
| 142 *content)); | 142 *content)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 // static | 145 // static |
| 146 void WebUIExtensionWrapper::GetVariableValue( | 146 void WebUIExtensionWrapper::GetVariableValue( |
| 147 const v8::FunctionCallbackInfo<v8::Value>& args) { | 147 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 148 WebKit::WebFrame* frame; | 148 blink::WebFrame* frame; |
| 149 RenderView* render_view; | 149 RenderView* render_view; |
| 150 if (!ShouldRespondToRequest(&frame, &render_view)) | 150 if (!ShouldRespondToRequest(&frame, &render_view)) |
| 151 return; | 151 return; |
| 152 | 152 |
| 153 if (!args.Length() || !args[0]->IsString()) | 153 if (!args.Length() || !args[0]->IsString()) |
| 154 return; | 154 return; |
| 155 | 155 |
| 156 std::string key = *v8::String::Utf8Value(args[0]->ToString()); | 156 std::string key = *v8::String::Utf8Value(args[0]->ToString()); |
| 157 std::string value = WebUIExtensionData::Get(render_view)->GetValue(key); | 157 std::string value = WebUIExtensionData::Get(render_view)->GetValue(key); |
| 158 args.GetReturnValue().Set(v8::String::New(value.c_str(), value.length())); | 158 args.GetReturnValue().Set(v8::String::New(value.c_str(), value.length())); |
| 159 } | 159 } |
| 160 | 160 |
| 161 // static | 161 // static |
| 162 v8::Extension* WebUIExtension::Get() { | 162 v8::Extension* WebUIExtension::Get() { |
| 163 return new WebUIExtensionWrapper(); | 163 return new WebUIExtensionWrapper(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 } // namespace content | 166 } // namespace content |
| OLD | NEW |