• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "content/child/indexed_db/indexed_db_key_builders.h"
6 
7 #include <algorithm>
8 #include <string>
9 #include <vector>
10 
11 #include "base/logging.h"
12 #include "third_party/WebKit/public/platform/WebVector.h"
13 
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;
26 
CopyKeyArray(const WebIDBKey & other)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]));
33   }
34   return result;
35 }
36 
CopyArray(const WebVector<WebString> & array)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;
43 }
44 
45 
46 namespace content {
47 
Build(const blink::WebIDBKey & key)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();
68   }
69 }
70 
Build(const IndexedDBKey & key)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]);
78       }
79       return WebIDBKey::createArray(web_array);
80     }
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();
97   }
98 }
99 
Build(const WebIDBKeyRange & key_range)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());
107 }
108 
Build(const blink::WebIDBKeyPath & key_path)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();
121   }
122 }
123 
Build(const IndexedDBKeyPath & key_path)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();
136   }
137 }
138 
139 }  // namespace content
140