1// Copyright (c) 2011 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 "chrome/browser/ui/webui/quota_internals/quota_internals_types.h"
6
7#include "base/logging.h"
8#include "base/memory/scoped_ptr.h"
9#include "base/values.h"
10#include "net/base/net_util.h"
11
12namespace {
13
14std::string StorageTypeToString(storage::StorageType type) {
15  switch (type) {
16    case storage::kStorageTypeTemporary:
17      return "temporary";
18    case storage::kStorageTypePersistent:
19      return "persistent";
20    case storage::kStorageTypeSyncable:
21      return "syncable";
22    case storage::kStorageTypeQuotaNotManaged:
23      return "quota not managed";
24    case storage::kStorageTypeUnknown:
25      return "unknown";
26  }
27  return "unknown";
28}
29
30}  // anonymous namespace
31
32namespace quota_internals {
33
34GlobalStorageInfo::GlobalStorageInfo(storage::StorageType type)
35    : type_(type), usage_(-1), unlimited_usage_(-1), quota_(-1) {
36}
37
38GlobalStorageInfo::~GlobalStorageInfo() {}
39
40base::Value* GlobalStorageInfo::NewValue() const {
41  // TODO(tzik): Add CreateLongIntegerValue to base/values.h and remove
42  // all static_cast<double> in this file.
43  base::DictionaryValue* dict = new base::DictionaryValue;
44  dict->SetString("type", StorageTypeToString(type_));
45  if (usage_ >= 0)
46    dict->SetDouble("usage", static_cast<double>(usage_));
47  if (unlimited_usage_ >= 0)
48    dict->SetDouble("unlimitedUsage", static_cast<double>(unlimited_usage_));
49  if (quota_ >= 0)
50    dict->SetDouble("quota", static_cast<double>(quota_));
51  return dict;
52}
53
54PerHostStorageInfo::PerHostStorageInfo(const std::string& host,
55                                       storage::StorageType type)
56    : host_(host), type_(type), usage_(-1), quota_(-1) {
57}
58
59PerHostStorageInfo::~PerHostStorageInfo() {}
60
61base::Value* PerHostStorageInfo::NewValue() const {
62  base::DictionaryValue* dict = new base::DictionaryValue;
63  DCHECK(!host_.empty());
64  dict->SetString("host", host_);
65  dict->SetString("type", StorageTypeToString(type_));
66  if (usage_ >= 0)
67    dict->SetDouble("usage", static_cast<double>(usage_));
68  if (quota_ >= 0)
69    dict->SetDouble("quota", static_cast<double>(quota_));
70  return dict;
71}
72
73PerOriginStorageInfo::PerOriginStorageInfo(const GURL& origin,
74                                           storage::StorageType type)
75    : origin_(origin),
76      type_(type),
77      host_(net::GetHostOrSpecFromURL(origin)),
78      in_use_(-1),
79      used_count_(-1) {
80}
81
82PerOriginStorageInfo::~PerOriginStorageInfo() {}
83
84base::Value* PerOriginStorageInfo::NewValue() const {
85  base::DictionaryValue* dict = new base::DictionaryValue;
86  DCHECK(!origin_.is_empty());
87  DCHECK(!host_.empty());
88  dict->SetString("origin", origin_.spec());
89  dict->SetString("type", StorageTypeToString(type_));
90  dict->SetString("host", host_);
91  if (in_use_ >= 0)
92    dict->SetBoolean("inUse", (in_use_ > 0));
93  if (used_count_ >= 0)
94    dict->SetInteger("usedCount", used_count_);
95  if (!last_access_time_.is_null())
96    dict->SetDouble("lastAccessTime", last_access_time_.ToDoubleT() * 1000.0);
97  if (!last_modified_time_.is_null()) {
98    dict->SetDouble("lastModifiedTime",
99                    last_modified_time_.ToDoubleT() * 1000.0);
100  }
101  return dict;
102}
103
104}  // namespace quota_internals
105