| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/browser/webui/web_ui_impl.h" | 5 #include "content/browser/webui/web_ui_impl.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "content/browser/child_process_security_policy_impl.h" | 10 #include "content/browser/child_process_security_policy_impl.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "content/public/common/bindings_policy.h" | 22 #include "content/public/common/bindings_policy.h" |
| 23 #include "content/public/common/content_client.h" | 23 #include "content/public/common/content_client.h" |
| 24 | 24 |
| 25 namespace content { | 25 namespace content { |
| 26 | 26 |
| 27 const WebUI::TypeID WebUI::kNoWebUI = NULL; | 27 const WebUI::TypeID WebUI::kNoWebUI = NULL; |
| 28 | 28 |
| 29 // static | 29 // static |
| 30 base::string16 WebUI::GetJavascriptCall( | 30 base::string16 WebUI::GetJavascriptCall( |
| 31 const std::string& function_name, | 31 const std::string& function_name, |
| 32 const std::vector<const Value*>& arg_list) { | 32 const std::vector<const base::Value*>& arg_list) { |
| 33 base::string16 parameters; | 33 base::string16 parameters; |
| 34 std::string json; | 34 std::string json; |
| 35 for (size_t i = 0; i < arg_list.size(); ++i) { | 35 for (size_t i = 0; i < arg_list.size(); ++i) { |
| 36 if (i > 0) | 36 if (i > 0) |
| 37 parameters += char16(','); | 37 parameters += char16(','); |
| 38 | 38 |
| 39 base::JSONWriter::Write(arg_list[i], &json); | 39 base::JSONWriter::Write(arg_list[i], &json); |
| 40 parameters += UTF8ToUTF16(json); | 40 parameters += UTF8ToUTF16(json); |
| 41 } | 41 } |
| 42 return ASCIIToUTF16(function_name) + | 42 return ASCIIToUTF16(function_name) + |
| (...skipping 19 matching lines...) Expand all Loading... |
| 62 bool handled = true; | 62 bool handled = true; |
| 63 IPC_BEGIN_MESSAGE_MAP(WebUIImpl, message) | 63 IPC_BEGIN_MESSAGE_MAP(WebUIImpl, message) |
| 64 IPC_MESSAGE_HANDLER(ViewHostMsg_WebUISend, OnWebUISend) | 64 IPC_MESSAGE_HANDLER(ViewHostMsg_WebUISend, OnWebUISend) |
| 65 IPC_MESSAGE_UNHANDLED(handled = false) | 65 IPC_MESSAGE_UNHANDLED(handled = false) |
| 66 IPC_END_MESSAGE_MAP() | 66 IPC_END_MESSAGE_MAP() |
| 67 return handled; | 67 return handled; |
| 68 } | 68 } |
| 69 | 69 |
| 70 void WebUIImpl::OnWebUISend(const GURL& source_url, | 70 void WebUIImpl::OnWebUISend(const GURL& source_url, |
| 71 const std::string& message, | 71 const std::string& message, |
| 72 const ListValue& args) { | 72 const base::ListValue& args) { |
| 73 WebContentsDelegate* delegate = web_contents_->GetDelegate(); | 73 WebContentsDelegate* delegate = web_contents_->GetDelegate(); |
| 74 bool data_urls_allowed = delegate && delegate->CanLoadDataURLsInWebUI(); | 74 bool data_urls_allowed = delegate && delegate->CanLoadDataURLsInWebUI(); |
| 75 if (!ChildProcessSecurityPolicyImpl::GetInstance()-> | 75 if (!ChildProcessSecurityPolicyImpl::GetInstance()-> |
| 76 HasWebUIBindings(web_contents_->GetRenderProcessHost()->GetID()) || | 76 HasWebUIBindings(web_contents_->GetRenderProcessHost()->GetID()) || |
| 77 !WebUIControllerFactoryRegistry::GetInstance()->IsURLAcceptableForWebUI( | 77 !WebUIControllerFactoryRegistry::GetInstance()->IsURLAcceptableForWebUI( |
| 78 web_contents_->GetBrowserContext(), source_url, data_urls_allowed)) { | 78 web_contents_->GetBrowserContext(), source_url, data_urls_allowed)) { |
| 79 NOTREACHED() << "Blocked unauthorized use of WebUIBindings."; | 79 NOTREACHED() << "Blocked unauthorized use of WebUIBindings."; |
| 80 return; | 80 return; |
| 81 } | 81 } |
| 82 | 82 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 controller_.reset(controller); | 142 controller_.reset(controller); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void WebUIImpl::CallJavascriptFunction(const std::string& function_name) { | 145 void WebUIImpl::CallJavascriptFunction(const std::string& function_name) { |
| 146 DCHECK(IsStringASCII(function_name)); | 146 DCHECK(IsStringASCII(function_name)); |
| 147 base::string16 javascript = ASCIIToUTF16(function_name + "();"); | 147 base::string16 javascript = ASCIIToUTF16(function_name + "();"); |
| 148 ExecuteJavascript(javascript); | 148 ExecuteJavascript(javascript); |
| 149 } | 149 } |
| 150 | 150 |
| 151 void WebUIImpl::CallJavascriptFunction(const std::string& function_name, | 151 void WebUIImpl::CallJavascriptFunction(const std::string& function_name, |
| 152 const Value& arg) { | 152 const base::Value& arg) { |
| 153 DCHECK(IsStringASCII(function_name)); | 153 DCHECK(IsStringASCII(function_name)); |
| 154 std::vector<const Value*> args; | 154 std::vector<const base::Value*> args; |
| 155 args.push_back(&arg); | 155 args.push_back(&arg); |
| 156 ExecuteJavascript(GetJavascriptCall(function_name, args)); | 156 ExecuteJavascript(GetJavascriptCall(function_name, args)); |
| 157 } | 157 } |
| 158 | 158 |
| 159 void WebUIImpl::CallJavascriptFunction( | 159 void WebUIImpl::CallJavascriptFunction( |
| 160 const std::string& function_name, | 160 const std::string& function_name, |
| 161 const Value& arg1, const Value& arg2) { | 161 const base::Value& arg1, const base::Value& arg2) { |
| 162 DCHECK(IsStringASCII(function_name)); | 162 DCHECK(IsStringASCII(function_name)); |
| 163 std::vector<const Value*> args; | 163 std::vector<const base::Value*> args; |
| 164 args.push_back(&arg1); | 164 args.push_back(&arg1); |
| 165 args.push_back(&arg2); | 165 args.push_back(&arg2); |
| 166 ExecuteJavascript(GetJavascriptCall(function_name, args)); | 166 ExecuteJavascript(GetJavascriptCall(function_name, args)); |
| 167 } | 167 } |
| 168 | 168 |
| 169 void WebUIImpl::CallJavascriptFunction( | 169 void WebUIImpl::CallJavascriptFunction( |
| 170 const std::string& function_name, | 170 const std::string& function_name, |
| 171 const Value& arg1, const Value& arg2, const Value& arg3) { | 171 const base::Value& arg1, const base::Value& arg2, const base::Value& arg3) { |
| 172 DCHECK(IsStringASCII(function_name)); | 172 DCHECK(IsStringASCII(function_name)); |
| 173 std::vector<const Value*> args; | 173 std::vector<const base::Value*> args; |
| 174 args.push_back(&arg1); | 174 args.push_back(&arg1); |
| 175 args.push_back(&arg2); | 175 args.push_back(&arg2); |
| 176 args.push_back(&arg3); | 176 args.push_back(&arg3); |
| 177 ExecuteJavascript(GetJavascriptCall(function_name, args)); | 177 ExecuteJavascript(GetJavascriptCall(function_name, args)); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void WebUIImpl::CallJavascriptFunction( | 180 void WebUIImpl::CallJavascriptFunction( |
| 181 const std::string& function_name, | 181 const std::string& function_name, |
| 182 const Value& arg1, | 182 const base::Value& arg1, |
| 183 const Value& arg2, | 183 const base::Value& arg2, |
| 184 const Value& arg3, | 184 const base::Value& arg3, |
| 185 const Value& arg4) { | 185 const base::Value& arg4) { |
| 186 DCHECK(IsStringASCII(function_name)); | 186 DCHECK(IsStringASCII(function_name)); |
| 187 std::vector<const Value*> args; | 187 std::vector<const base::Value*> args; |
| 188 args.push_back(&arg1); | 188 args.push_back(&arg1); |
| 189 args.push_back(&arg2); | 189 args.push_back(&arg2); |
| 190 args.push_back(&arg3); | 190 args.push_back(&arg3); |
| 191 args.push_back(&arg4); | 191 args.push_back(&arg4); |
| 192 ExecuteJavascript(GetJavascriptCall(function_name, args)); | 192 ExecuteJavascript(GetJavascriptCall(function_name, args)); |
| 193 } | 193 } |
| 194 | 194 |
| 195 void WebUIImpl::CallJavascriptFunction( | 195 void WebUIImpl::CallJavascriptFunction( |
| 196 const std::string& function_name, | 196 const std::string& function_name, |
| 197 const std::vector<const Value*>& args) { | 197 const std::vector<const base::Value*>& args) { |
| 198 DCHECK(IsStringASCII(function_name)); | 198 DCHECK(IsStringASCII(function_name)); |
| 199 ExecuteJavascript(GetJavascriptCall(function_name, args)); | 199 ExecuteJavascript(GetJavascriptCall(function_name, args)); |
| 200 } | 200 } |
| 201 | 201 |
| 202 void WebUIImpl::RegisterMessageCallback(const std::string &message, | 202 void WebUIImpl::RegisterMessageCallback(const std::string &message, |
| 203 const MessageCallback& callback) { | 203 const MessageCallback& callback) { |
| 204 message_callbacks_.insert(std::make_pair(message, callback)); | 204 message_callbacks_.insert(std::make_pair(message, callback)); |
| 205 } | 205 } |
| 206 | 206 |
| 207 void WebUIImpl::ProcessWebUIMessage(const GURL& source_url, | 207 void WebUIImpl::ProcessWebUIMessage(const GURL& source_url, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 230 handlers_.push_back(handler); | 230 handlers_.push_back(handler); |
| 231 } | 231 } |
| 232 | 232 |
| 233 void WebUIImpl::ExecuteJavascript(const base::string16& javascript) { | 233 void WebUIImpl::ExecuteJavascript(const base::string16& javascript) { |
| 234 static_cast<RenderViewHostImpl*>( | 234 static_cast<RenderViewHostImpl*>( |
| 235 web_contents_->GetRenderViewHost())->ExecuteJavascriptInWebFrame( | 235 web_contents_->GetRenderViewHost())->ExecuteJavascriptInWebFrame( |
| 236 ASCIIToUTF16(frame_xpath_), javascript); | 236 ASCIIToUTF16(frame_xpath_), javascript); |
| 237 } | 237 } |
| 238 | 238 |
| 239 } // namespace content | 239 } // namespace content |
| OLD | NEW |