Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / indexed_db / indexed_db_key_builders.cc
blobfe3b3f0a9424fea613c1ca411352d04d887a15af
1 // Copyright (c) 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.
5 #include "content/child/indexed_db/indexed_db_key_builders.h"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
11 #include "base/logging.h"
12 #include "third_party/WebKit/public/platform/WebVector.h"
14 using blink::WebIDBKey;
15 using blink::WebIDBKeyRange;
16 using blink::WebIDBKeyTypeArray;
17 using blink::WebIDBKeyTypeBinary;
18 using blink::WebIDBKeyTypeDate;
19 using blink::WebIDBKeyTypeInvalid;
20 using blink::WebIDBKeyTypeMin;
21 using blink::WebIDBKeyTypeNull;
22 using blink::WebIDBKeyTypeNumber;
23 using blink::WebIDBKeyTypeString;
24 using blink::WebVector;
25 using blink::WebString;
27 static content::IndexedDBKey::KeyArray CopyKeyArray(const WebIDBKey& other) {
28 content::IndexedDBKey::KeyArray result;
29 if (other.keyType() == WebIDBKeyTypeArray) {
30 const WebVector<WebIDBKey>& array = other.array();
31 for (size_t i = 0; i < array.size(); ++i)
32 result.push_back(content::IndexedDBKeyBuilder::Build(array[i]));
34 return result;
37 static std::vector<base::string16> CopyArray(
38 const WebVector<WebString>& array) {
39 std::vector<base::string16> copy(array.size());
40 for (size_t i = 0; i < array.size(); ++i)
41 copy[i] = array[i];
42 return copy;
46 namespace content {
48 IndexedDBKey IndexedDBKeyBuilder::Build(const blink::WebIDBKey& key) {
49 switch (key.keyType()) {
50 case WebIDBKeyTypeArray:
51 return IndexedDBKey(CopyKeyArray(key));
52 case WebIDBKeyTypeBinary:
53 return IndexedDBKey(
54 std::string(key.binary().data(), key.binary().size()));
55 case WebIDBKeyTypeString:
56 return IndexedDBKey(key.string());
57 case WebIDBKeyTypeDate:
58 return IndexedDBKey(key.date(), WebIDBKeyTypeDate);
59 case WebIDBKeyTypeNumber:
60 return IndexedDBKey(key.number(), WebIDBKeyTypeNumber);
61 case WebIDBKeyTypeNull:
62 case WebIDBKeyTypeInvalid:
63 return IndexedDBKey(key.keyType());
64 case WebIDBKeyTypeMin:
65 default:
66 NOTREACHED();
67 return IndexedDBKey();
71 WebIDBKey WebIDBKeyBuilder::Build(const IndexedDBKey& key) {
72 switch (key.type()) {
73 case WebIDBKeyTypeArray: {
74 const IndexedDBKey::KeyArray& array = key.array();
75 blink::WebVector<WebIDBKey> web_array(array.size());
76 for (size_t i = 0; i < array.size(); ++i) {
77 web_array[i] = Build(array[i]);
79 return WebIDBKey::createArray(web_array);
81 case WebIDBKeyTypeBinary:
82 return WebIDBKey::createBinary(key.binary());
83 case WebIDBKeyTypeString:
84 return WebIDBKey::createString(key.string());
85 case WebIDBKeyTypeDate:
86 return WebIDBKey::createDate(key.date());
87 case WebIDBKeyTypeNumber:
88 return WebIDBKey::createNumber(key.number());
89 case WebIDBKeyTypeInvalid:
90 return WebIDBKey::createInvalid();
91 case WebIDBKeyTypeNull:
92 return WebIDBKey::createNull();
93 case WebIDBKeyTypeMin:
94 default:
95 NOTREACHED();
96 return WebIDBKey::createInvalid();
100 IndexedDBKeyRange IndexedDBKeyRangeBuilder::Build(
101 const WebIDBKeyRange& key_range) {
102 return IndexedDBKeyRange(
103 IndexedDBKeyBuilder::Build(key_range.lower()),
104 IndexedDBKeyBuilder::Build(key_range.upper()),
105 key_range.lowerOpen(),
106 key_range.upperOpen());
109 IndexedDBKeyPath IndexedDBKeyPathBuilder::Build(
110 const blink::WebIDBKeyPath& key_path) {
111 switch (key_path.keyPathType()) {
112 case blink::WebIDBKeyPathTypeString:
113 return IndexedDBKeyPath(key_path.string());
114 case blink::WebIDBKeyPathTypeArray:
115 return IndexedDBKeyPath(CopyArray(key_path.array()));
116 case blink::WebIDBKeyPathTypeNull:
117 return IndexedDBKeyPath();
118 default:
119 NOTREACHED();
120 return IndexedDBKeyPath();
124 blink::WebIDBKeyPath WebIDBKeyPathBuilder::Build(
125 const IndexedDBKeyPath& key_path) {
126 switch (key_path.type()) {
127 case blink::WebIDBKeyPathTypeString:
128 return blink::WebIDBKeyPath::create(WebString(key_path.string()));
129 case blink::WebIDBKeyPathTypeArray:
130 return blink::WebIDBKeyPath::create(CopyArray(key_path.array()));
131 case blink::WebIDBKeyPathTypeNull:
132 return blink::WebIDBKeyPath::createNull();
133 default:
134 NOTREACHED();
135 return blink::WebIDBKeyPath::createNull();
139 } // namespace content