summaryrefslogtreecommitdiffstats
path: root/content/common/indexed_db/indexed_db_param_traits.cc
blob: 8593f752fedca025574b893ad40bb2dd782a3d8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/common/indexed_db/indexed_db_param_traits.h"

#include "content/common/indexed_db/indexed_db_key.h"
#include "content/common/indexed_db/indexed_db_key_path.h"
#include "content/common/indexed_db/indexed_db_key_range.h"
#include "ipc/ipc_message_utils.h"

using content::IndexedDBKey;
using content::IndexedDBKeyPath;
using content::IndexedDBKeyRange;

using WebKit::WebIDBKey;
using WebKit::WebIDBKeyPath;

namespace IPC {

void ParamTraits<IndexedDBKey>::Write(Message* m, const param_type& p) {
  WriteParam(m, int(p.type()));
  switch (p.type()) {
    case WebIDBKey::ArrayType:
      WriteParam(m, p.array());
      return;
    case WebIDBKey::StringType:
      WriteParam(m, p.string());
      return;
    case WebIDBKey::DateType:
      WriteParam(m, p.date());
      return;
    case WebIDBKey::NumberType:
      WriteParam(m, p.number());
      return;
    case WebIDBKey::InvalidType:
    case WebIDBKey::NullType:
      return;
    default:
      // This is a placeholder for WebKit::WebIDBKey::MinType
      NOTREACHED();
      return;
  }
}

bool ParamTraits<IndexedDBKey>::Read(const Message* m,
                                     PickleIterator* iter,
                                     param_type* r) {
  int type;
  if (!ReadParam(m, iter, &type))
    return false;
  WebIDBKey::Type web_type = static_cast<WebIDBKey::Type>(type);

  switch (web_type) {
    case WebIDBKey::ArrayType: {
      std::vector<IndexedDBKey> array;
      if (!ReadParam(m, iter, &array))
        return false;
      *r = IndexedDBKey(array);
      return true;
    }
    case WebIDBKey::StringType: {
      string16 string;
      if (!ReadParam(m, iter, &string))
        return false;
      *r = IndexedDBKey(string);
      return true;
    }
    case WebIDBKey::DateType:
    case WebIDBKey::NumberType: {
      double number;
      if (!ReadParam(m, iter, &number))
        return false;
      *r = IndexedDBKey(number, web_type);
      return true;
    }
    case WebIDBKey::InvalidType:
    case WebIDBKey::NullType:
      *r = IndexedDBKey(web_type);
      return true;
    default:
      // This is a placeholder for WebKit::WebIDBKey::MinType
      NOTREACHED();
      return false;
  }
}

void ParamTraits<IndexedDBKey>::Log(const param_type& p, std::string* l) {
  l->append("<IndexedDBKey>(");
  LogParam(int(p.type()), l);
  l->append(", ");
  l->append("[");
  std::vector<IndexedDBKey>::const_iterator it = p.array().begin();
  while (it != p.array().end()) {
    Log(*it, l);
    ++it;
    if (it != p.array().end())
      l->append(", ");
  }
  l->append("], ");
  LogParam(p.string(), l);
  l->append(", ");
  LogParam(p.date(), l);
  l->append(", ");
  LogParam(p.number(), l);
  l->append(")");
}

void ParamTraits<IndexedDBKeyPath>::Write(Message* m, const param_type& p) {
  WriteParam(m, int(p.type()));
  switch (p.type()) {
    case WebIDBKeyPath::ArrayType:
      WriteParam(m, p.array());
      return;
    case WebIDBKeyPath::StringType:
      WriteParam(m, p.string());
      return;
    case WebIDBKeyPath::NullType:
      return;
  }
  NOTREACHED();
}

bool ParamTraits<IndexedDBKeyPath>::Read(const Message* m,
                                         PickleIterator* iter,
                                         param_type* r) {
  int type;
  if (!ReadParam(m, iter, &type))
    return false;

  switch (type) {
    case WebIDBKeyPath::ArrayType: {
      std::vector<string16> array;
      if (!ReadParam(m, iter, &array))
        return false;
      *r = IndexedDBKeyPath(array);
      return true;
    }
    case WebIDBKeyPath::StringType: {
      string16 string;
      if (!ReadParam(m, iter, &string))
        return false;
      *r = IndexedDBKeyPath(string);
      return true;
    }
    case WebIDBKeyPath::NullType:
      *r = IndexedDBKeyPath();
      return true;
  }
  NOTREACHED();
  return false;
}

void ParamTraits<IndexedDBKeyPath>::Log(const param_type& p, std::string* l) {
  l->append("<IndexedDBKeyPath>(");
  LogParam(int(p.type()), l);
  l->append(", ");
  LogParam(p.string(), l);
  l->append(", ");
  l->append("[");
  std::vector<string16>::const_iterator it = p.array().begin();
  while (it != p.array().end()) {
    LogParam(*it, l);
    ++it;
    if (it != p.array().end())
      l->append(", ");
  }
  l->append("])");
}

void ParamTraits<IndexedDBKeyRange>::Write(Message* m, const param_type& p) {
  WriteParam(m, p.lower());
  WriteParam(m, p.upper());
  WriteParam(m, p.lowerOpen());
  WriteParam(m, p.upperOpen());
}

bool ParamTraits<IndexedDBKeyRange>::Read(const Message* m,
                                          PickleIterator* iter,
                                          param_type* r) {
  IndexedDBKey lower;
  if (!ReadParam(m, iter, &lower))
    return false;

  IndexedDBKey upper;
  if (!ReadParam(m, iter, &upper))
    return false;

  bool lower_open;
  if (!ReadParam(m, iter, &lower_open))
    return false;

  bool upper_open;
  if (!ReadParam(m, iter, &upper_open))
    return false;

  *r = IndexedDBKeyRange(lower, upper, lower_open, upper_open);
  return true;
}

void ParamTraits<IndexedDBKeyRange>::Log(const param_type& p, std::string* l) {
  l->append("<IndexedDBKeyRange>(lower=");
  LogParam(p.lower(), l);
  l->append(", upper=");
  LogParam(p.upper(), l);
  l->append(", lower_open=");
  LogParam(p.lowerOpen(), l);
  l->append(", upper_open=");
  LogParam(p.upperOpen(), l);
  l->append(")");
}

}  // namespace IPC