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