| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/indexed_db/indexed_db_key_path.h" | 5 #include "content/common/indexed_db/indexed_db_key_path.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "third_party/WebKit/public/platform/WebString.h" | |
| 9 #include "third_party/WebKit/public/platform/WebVector.h" | |
| 10 | 8 |
| 11 namespace content { | 9 namespace content { |
| 12 | 10 |
| 13 using WebKit::WebIDBKeyPath; | |
| 14 using WebKit::WebIDBKeyPathTypeArray; | 11 using WebKit::WebIDBKeyPathTypeArray; |
| 15 using WebKit::WebIDBKeyPathTypeNull; | 12 using WebKit::WebIDBKeyPathTypeNull; |
| 16 using WebKit::WebIDBKeyPathTypeString; | 13 using WebKit::WebIDBKeyPathTypeString; |
| 17 using WebKit::WebString; | |
| 18 using WebKit::WebVector; | |
| 19 | |
| 20 namespace { | |
| 21 static std::vector<string16> CopyArray(const WebVector<WebString>& array) { | |
| 22 std::vector<string16> copy(array.size()); | |
| 23 for (size_t i = 0; i < array.size(); ++i) | |
| 24 copy[i] = array[i]; | |
| 25 return copy; | |
| 26 } | |
| 27 } // namespace | |
| 28 | 14 |
| 29 IndexedDBKeyPath::IndexedDBKeyPath() : type_(WebIDBKeyPathTypeNull) {} | 15 IndexedDBKeyPath::IndexedDBKeyPath() : type_(WebIDBKeyPathTypeNull) {} |
| 30 | 16 |
| 31 IndexedDBKeyPath::IndexedDBKeyPath(const string16& string) | 17 IndexedDBKeyPath::IndexedDBKeyPath(const string16& string) |
| 32 : type_(WebIDBKeyPathTypeString), string_(string) {} | 18 : type_(WebIDBKeyPathTypeString), string_(string) {} |
| 33 | 19 |
| 34 IndexedDBKeyPath::IndexedDBKeyPath(const std::vector<string16>& array) | 20 IndexedDBKeyPath::IndexedDBKeyPath(const std::vector<string16>& array) |
| 35 : type_(WebIDBKeyPathTypeArray), array_(array) {} | 21 : type_(WebIDBKeyPathTypeArray), array_(array) {} |
| 36 | 22 |
| 37 IndexedDBKeyPath::IndexedDBKeyPath(const WebIDBKeyPath& other) | |
| 38 : type_(other.keyPathType()), | |
| 39 string_(type_ == WebIDBKeyPathTypeString | |
| 40 ? static_cast<string16>(other.string()) : string16()), | |
| 41 array_(type_ == WebIDBKeyPathTypeArray | |
| 42 ? CopyArray(other.array()) : std::vector<string16>()) {} | |
| 43 | |
| 44 IndexedDBKeyPath::~IndexedDBKeyPath() {} | 23 IndexedDBKeyPath::~IndexedDBKeyPath() {} |
| 45 | 24 |
| 46 bool IndexedDBKeyPath::IsValid() const { | |
| 47 WebIDBKeyPath key_path = *this; | |
| 48 return key_path.isValid(); | |
| 49 } | |
| 50 | |
| 51 const std::vector<string16>& IndexedDBKeyPath::array() const { | 25 const std::vector<string16>& IndexedDBKeyPath::array() const { |
| 52 DCHECK(type_ == WebKit::WebIDBKeyPathTypeArray); | 26 DCHECK(type_ == WebKit::WebIDBKeyPathTypeArray); |
| 53 return array_; | 27 return array_; |
| 54 } | 28 } |
| 55 | 29 |
| 56 const string16& IndexedDBKeyPath::string() const { | 30 const string16& IndexedDBKeyPath::string() const { |
| 57 DCHECK(type_ == WebKit::WebIDBKeyPathTypeString); | 31 DCHECK(type_ == WebKit::WebIDBKeyPathTypeString); |
| 58 return string_; | 32 return string_; |
| 59 } | 33 } |
| 60 | 34 |
| 61 bool IndexedDBKeyPath::operator==(const IndexedDBKeyPath& other) const { | 35 bool IndexedDBKeyPath::operator==(const IndexedDBKeyPath& other) const { |
| 62 if (type_ != other.type_) | 36 if (type_ != other.type_) |
| 63 return false; | 37 return false; |
| 64 | 38 |
| 65 switch (type_) { | 39 switch (type_) { |
| 66 case WebIDBKeyPathTypeNull: | 40 case WebIDBKeyPathTypeNull: |
| 67 return true; | 41 return true; |
| 68 case WebIDBKeyPathTypeString: | 42 case WebIDBKeyPathTypeString: |
| 69 return string_ == other.string_; | 43 return string_ == other.string_; |
| 70 case WebIDBKeyPathTypeArray: | 44 case WebIDBKeyPathTypeArray: |
| 71 return array_ == other.array_; | 45 return array_ == other.array_; |
| 72 } | 46 } |
| 73 NOTREACHED(); | 47 NOTREACHED(); |
| 74 return false; | 48 return false; |
| 75 } | 49 } |
| 76 | 50 |
| 77 IndexedDBKeyPath::operator WebIDBKeyPath() const { | |
| 78 switch (type_) { | |
| 79 case WebIDBKeyPathTypeArray: | |
| 80 return WebIDBKeyPath::create(array_); | |
| 81 case WebIDBKeyPathTypeString: | |
| 82 return WebIDBKeyPath::create(WebString(string_)); | |
| 83 case WebIDBKeyPathTypeNull: | |
| 84 return WebIDBKeyPath::createNull(); | |
| 85 } | |
| 86 NOTREACHED(); | |
| 87 return WebIDBKeyPath::createNull(); | |
| 88 } | |
| 89 | |
| 90 } // namespace content | 51 } // namespace content |
| OLD | NEW |