Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1 | // Copyright 2013 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/child/plugin_param_traits.h" |
| 6 | |
| 7 | #include "base/strings/string_number_conversions.h" |
| 8 | #include "ipc/ipc_message_utils.h" |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 9 | #include "third_party/WebKit/public/web/WebBindings.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 10 | |
| 11 | namespace content { |
| 12 | |
| 13 | NPIdentifier_Param::NPIdentifier_Param() |
| 14 | : identifier() { |
| 15 | } |
| 16 | |
| 17 | NPIdentifier_Param::~NPIdentifier_Param() { |
| 18 | } |
| 19 | |
| 20 | NPVariant_Param::NPVariant_Param() |
| 21 | : type(NPVARIANT_PARAM_VOID), |
| 22 | bool_value(false), |
| 23 | int_value(0), |
| 24 | double_value(0), |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 25 | npobject_routing_id(-1), |
| 26 | npobject_owner_id(-1) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | NPVariant_Param::~NPVariant_Param() { |
| 30 | } |
| 31 | |
| 32 | } // namespace content |
| 33 | |
| 34 | using content::NPIdentifier_Param; |
| 35 | using content::NPVariant_Param; |
| 36 | |
| 37 | namespace IPC { |
| 38 | |
| 39 | void ParamTraits<NPVariant_Param>::Write(Message* m, const param_type& p) { |
| 40 | WriteParam(m, static_cast<int>(p.type)); |
| 41 | if (p.type == content::NPVARIANT_PARAM_BOOL) { |
| 42 | WriteParam(m, p.bool_value); |
| 43 | } else if (p.type == content::NPVARIANT_PARAM_INT) { |
| 44 | WriteParam(m, p.int_value); |
| 45 | } else if (p.type == content::NPVARIANT_PARAM_DOUBLE) { |
| 46 | WriteParam(m, p.double_value); |
| 47 | } else if (p.type == content::NPVARIANT_PARAM_STRING) { |
| 48 | WriteParam(m, p.string_value); |
| 49 | } else if (p.type == content::NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID || |
| 50 | p.type == content::NPVARIANT_PARAM_RECEIVER_OBJECT_ROUTING_ID) { |
| 51 | // This is the routing id used to connect NPObjectProxy in the other |
| 52 | // process with NPObjectStub in this process or to identify the raw |
| 53 | // npobject pointer to be used in the callee process. |
| 54 | WriteParam(m, p.npobject_routing_id); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 55 | // This is a routing Id used to identify the plugin instance that owns |
| 56 | // the object, for ownership-tracking purposes. |
| 57 | WriteParam(m, p.npobject_owner_id); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 58 | } else { |
| 59 | DCHECK(p.type == content::NPVARIANT_PARAM_VOID || |
| 60 | p.type == content::NPVARIANT_PARAM_NULL); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | bool ParamTraits<NPVariant_Param>::Read(const Message* m, |
| 65 | PickleIterator* iter, |
| 66 | param_type* r) { |
| 67 | int type; |
| 68 | if (!ReadParam(m, iter, &type)) |
| 69 | return false; |
| 70 | |
| 71 | bool result = false; |
| 72 | r->type = static_cast<content::NPVariant_ParamEnum>(type); |
| 73 | if (r->type == content::NPVARIANT_PARAM_BOOL) { |
| 74 | result = ReadParam(m, iter, &r->bool_value); |
| 75 | } else if (r->type == content::NPVARIANT_PARAM_INT) { |
| 76 | result = ReadParam(m, iter, &r->int_value); |
| 77 | } else if (r->type == content::NPVARIANT_PARAM_DOUBLE) { |
| 78 | result = ReadParam(m, iter, &r->double_value); |
| 79 | } else if (r->type == content::NPVARIANT_PARAM_STRING) { |
| 80 | result = ReadParam(m, iter, &r->string_value); |
| 81 | } else if (r->type == content::NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID || |
| 82 | r->type == content::NPVARIANT_PARAM_RECEIVER_OBJECT_ROUTING_ID) { |
| 83 | result = ReadParam(m, iter, &r->npobject_routing_id); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 84 | result = ReadParam(m, iter, &r->npobject_owner_id); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 85 | } else if ((r->type == content::NPVARIANT_PARAM_VOID) || |
| 86 | (r->type == content::NPVARIANT_PARAM_NULL)) { |
| 87 | result = true; |
| 88 | } else { |
| 89 | NOTREACHED(); |
| 90 | } |
| 91 | |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | void ParamTraits<NPVariant_Param>::Log(const param_type& p, std::string* l) { |
| 96 | l->append( |
| 97 | base::StringPrintf("NPVariant_Param(%d, ", static_cast<int>(p.type))); |
| 98 | if (p.type == content::NPVARIANT_PARAM_BOOL) { |
| 99 | LogParam(p.bool_value, l); |
| 100 | } else if (p.type == content::NPVARIANT_PARAM_INT) { |
| 101 | LogParam(p.int_value, l); |
| 102 | } else if (p.type == content::NPVARIANT_PARAM_DOUBLE) { |
| 103 | LogParam(p.double_value, l); |
| 104 | } else if (p.type == content::NPVARIANT_PARAM_STRING) { |
| 105 | LogParam(p.string_value, l); |
| 106 | } else if (p.type == content::NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID || |
| 107 | p.type == content::NPVARIANT_PARAM_RECEIVER_OBJECT_ROUTING_ID) { |
| 108 | LogParam(p.npobject_routing_id, l); |
| 109 | } else { |
| 110 | l->append("<none>"); |
| 111 | } |
| 112 | l->append(")"); |
| 113 | } |
| 114 | |
| 115 | void ParamTraits<NPIdentifier_Param>::Write(Message* m, const param_type& p) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 116 | content::SerializeNPIdentifier(p.identifier, m); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | bool ParamTraits<NPIdentifier_Param>::Read(const Message* m, |
| 120 | PickleIterator* iter, |
| 121 | param_type* r) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 122 | return content::DeserializeNPIdentifier(iter, &r->identifier); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void ParamTraits<NPIdentifier_Param>::Log(const param_type& p, std::string* l) { |
| 126 | if (WebKit::WebBindings::identifierIsString(p.identifier)) { |
| 127 | NPUTF8* str = WebKit::WebBindings::utf8FromIdentifier(p.identifier); |
| 128 | l->append(str); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 129 | free(str); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 130 | } else { |
| 131 | l->append(base::IntToString( |
| 132 | WebKit::WebBindings::intFromIdentifier(p.identifier))); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } // namespace IPC |