| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/web_ui_bindings.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/stl_util.h" | |
| 11 #include "base/values.h" | |
| 12 #include "content/common/view_messages.h" | |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" | |
| 16 | |
| 17 using webkit_glue::CppArgumentList; | |
| 18 using webkit_glue::CppVariant; | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // Creates a Value which is a copy of the CppVariant |value|. All objects are | |
| 23 // treated as Lists for now since CppVariant does not distinguish arrays in any | |
| 24 // convenient way and we currently have no need of non array objects. | |
| 25 Value* CreateValueFromCppVariant(const CppVariant& value) { | |
| 26 if (value.isBool()) | |
| 27 return Value::CreateBooleanValue(value.ToBoolean()); | |
| 28 if (value.isDouble()) | |
| 29 return Value::CreateDoubleValue(value.ToDouble()); | |
| 30 if (value.isInt32()) | |
| 31 return Value::CreateIntegerValue(value.ToInt32()); | |
| 32 if (value.isString()) | |
| 33 return Value::CreateStringValue(value.ToString()); | |
| 34 | |
| 35 if (value.isObject()) { | |
| 36 // We currently assume all objects are arrays. | |
| 37 std::vector<CppVariant> vector = value.ToVector(); | |
| 38 ListValue* list = new ListValue(); | |
| 39 for (size_t i = 0; i < vector.size(); ++i) { | |
| 40 list->Append(CreateValueFromCppVariant(vector[i])); | |
| 41 } | |
| 42 return list; | |
| 43 } | |
| 44 | |
| 45 // Covers null and undefined. | |
| 46 return Value::CreateNullValue(); | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 DOMBoundBrowserObject::DOMBoundBrowserObject() { | |
| 52 } | |
| 53 | |
| 54 DOMBoundBrowserObject::~DOMBoundBrowserObject() { | |
| 55 STLDeleteContainerPointers(properties_.begin(), properties_.end()); | |
| 56 } | |
| 57 | |
| 58 WebUIBindings::WebUIBindings(IPC::Sender* sender, int routing_id) | |
| 59 : sender_(sender), routing_id_(routing_id) { | |
| 60 BindCallback("send", base::Bind(&WebUIBindings::Send, | |
| 61 base::Unretained(this))); | |
| 62 } | |
| 63 | |
| 64 WebUIBindings::~WebUIBindings() {} | |
| 65 | |
| 66 void WebUIBindings::Send(const CppArgumentList& args, CppVariant* result) { | |
| 67 // We expect at least a string message identifier, and optionally take | |
| 68 // an object parameter. If we get anything else we bail. | |
| 69 if (args.size() < 1 || args.size() > 2) | |
| 70 return; | |
| 71 | |
| 72 // Require the first parameter to be the message name. | |
| 73 if (!args[0].isString()) | |
| 74 return; | |
| 75 const std::string message = args[0].ToString(); | |
| 76 | |
| 77 // If they've provided an optional message parameter, convert that into a | |
| 78 // Value to send to the browser process. | |
| 79 scoped_ptr<Value> content; | |
| 80 if (args.size() == 2) { | |
| 81 if (!args[1].isObject()) | |
| 82 return; | |
| 83 | |
| 84 content.reset(CreateValueFromCppVariant(args[1])); | |
| 85 CHECK(content->IsType(Value::TYPE_LIST)); | |
| 86 } else { | |
| 87 content.reset(new ListValue()); | |
| 88 } | |
| 89 | |
| 90 // Retrieve the source document's url | |
| 91 GURL source_url; | |
| 92 WebKit::WebFrame* frame = WebKit::WebFrame::frameForCurrentContext(); | |
| 93 if (frame) | |
| 94 source_url = frame->document().url(); | |
| 95 | |
| 96 // Send the message up to the browser. | |
| 97 sender_->Send(new ViewHostMsg_WebUISend( | |
| 98 routing_id_, | |
| 99 source_url, | |
| 100 message, | |
| 101 *(static_cast<ListValue*>(content.get())))); | |
| 102 } | |
| 103 | |
| 104 void DOMBoundBrowserObject::SetProperty(const std::string& name, | |
| 105 const std::string& value) { | |
| 106 CppVariant* cpp_value = new CppVariant; | |
| 107 cpp_value->Set(value); | |
| 108 BindProperty(name, cpp_value); | |
| 109 properties_.push_back(cpp_value); | |
| 110 } | |
| OLD | NEW |