summaryrefslogtreecommitdiffstats
path: root/content/common/indexed_db/indexed_db_key_path.cc
blob: f042d012e581eabb8e977a0d9c0b0a7bcca5d4c1 (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
// 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_key_path.h"

#include "base/logging.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"

namespace content {

using WebKit::WebIDBKeyPath;
using WebKit::WebString;
using WebKit::WebVector;

IndexedDBKeyPath::IndexedDBKeyPath()
    : type_(WebIDBKeyPath::NullType) {
}

IndexedDBKeyPath::IndexedDBKeyPath(const WebIDBKeyPath& key) {
  Set(key);
}

IndexedDBKeyPath::~IndexedDBKeyPath() {
}

void IndexedDBKeyPath::SetNull() {
  type_ = WebIDBKeyPath::NullType;
}

void IndexedDBKeyPath::SetArray(const std::vector<string16>& array) {
  type_ = WebIDBKeyPath::ArrayType;
  array_ = array;
}

void IndexedDBKeyPath::SetString(const string16& string) {
  type_ = WebIDBKeyPath::StringType;
  string_ = string;
}

void IndexedDBKeyPath::Set(const WebIDBKeyPath& keyPath) {
  type_ = keyPath.type();
  array_.clear();
  string_.clear();
  switch (type_) {
    case WebIDBKeyPath::ArrayType: {
      WebVector<WebString> array = keyPath.array();
      for (size_t i = 0; i < array.size(); ++i)
        array_.push_back(static_cast<string16>(array[i]));
      break;
    }
    case WebIDBKeyPath::NullType:
      break;
    case WebIDBKeyPath::StringType:
      string_ = static_cast<string16>(keyPath.string());
  }
}

bool IndexedDBKeyPath::IsValid() const {
  WebIDBKeyPath key_path = *this;
  return key_path.isValid();
}

IndexedDBKeyPath::operator WebIDBKeyPath() const {
  switch (type_) {
    case WebIDBKeyPath::ArrayType:
      return WebIDBKeyPath::create(array_);
    case WebIDBKeyPath::StringType:
      return WebIDBKeyPath::create(WebString(string_));
    case WebIDBKeyPath::NullType:
      return WebIDBKeyPath::createNull();
  }
  NOTREACHED();
  return WebIDBKeyPath::createNull();
}

}  // namespace content