summaryrefslogtreecommitdiffstats
path: root/ios/crnet/sdch_owner_pref_storage.cc
blob: 3af81f5f7a681dae091cbabd4a0f0d548b1a7ee2 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2015 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 "ios/crnet/sdch_owner_pref_storage.h"

#include "base/values.h"
#include "components/prefs/persistent_pref_store.h"

namespace {

static const char kStorageKey[] = "SDCH";

}  // namespace

SdchOwnerPrefStorage::SdchOwnerPrefStorage(PersistentPrefStore* storage)
    : storage_(storage),
      storage_key_(kStorageKey),
      init_observer_(nullptr) {
}

SdchOwnerPrefStorage::~SdchOwnerPrefStorage() {
  if (init_observer_)
    storage_->RemoveObserver(this);
}

net::SdchOwner::PrefStorage::ReadError
SdchOwnerPrefStorage::GetReadError() const {
  PersistentPrefStore::PrefReadError error = storage_->GetReadError();

  DCHECK_NE(error,
            PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE);
  DCHECK_NE(error, PersistentPrefStore::PREF_READ_ERROR_MAX_ENUM);

  switch (error) {
    case PersistentPrefStore::PREF_READ_ERROR_NONE:
      return PERSISTENCE_FAILURE_NONE;

    case PersistentPrefStore::PREF_READ_ERROR_NO_FILE:
      return PERSISTENCE_FAILURE_REASON_NO_FILE;

    case PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE:
    case PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE:
    case PersistentPrefStore::PREF_READ_ERROR_FILE_OTHER:
    case PersistentPrefStore::PREF_READ_ERROR_FILE_LOCKED:
    case PersistentPrefStore::PREF_READ_ERROR_JSON_REPEAT:
      return PERSISTENCE_FAILURE_REASON_READ_FAILED;

    case PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED:
    case PersistentPrefStore::PREF_READ_ERROR_FILE_NOT_SPECIFIED:
    case PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE:
    case PersistentPrefStore::PREF_READ_ERROR_MAX_ENUM:
    default:
      // We don't expect these other failures given our usage of prefs.
      NOTREACHED();
      return PERSISTENCE_FAILURE_REASON_OTHER;
  }
}

bool SdchOwnerPrefStorage::GetValue(
      const base::DictionaryValue** result) const {
  const base::Value* result_value = nullptr;
  if (!storage_->GetValue(storage_key_, &result_value))
    return false;
  return result_value->GetAsDictionary(result);
}

bool SdchOwnerPrefStorage::GetMutableValue(base::DictionaryValue** result) {
  base::Value* result_value = nullptr;
  if (!storage_->GetMutableValue(storage_key_, &result_value))
    return false;
  return result_value->GetAsDictionary(result);
}

void SdchOwnerPrefStorage::SetValue(scoped_ptr<base::DictionaryValue> value) {
  storage_->SetValue(storage_key_, std::move(value),
                     WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
}

void SdchOwnerPrefStorage::ReportValueChanged() {
  storage_->ReportValueChanged(storage_key_,
                               WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
}

bool SdchOwnerPrefStorage::IsInitializationComplete() {
  return storage_->IsInitializationComplete();
}

void SdchOwnerPrefStorage::StartObservingInit(net::SdchOwner* observer) {
  DCHECK(!init_observer_);
  init_observer_ = observer;
  storage_->AddObserver(this);
}

void SdchOwnerPrefStorage::StopObservingInit() {
  DCHECK(init_observer_);
  init_observer_ = nullptr;
  storage_->RemoveObserver(this);
}

void SdchOwnerPrefStorage::OnPrefValueChanged(const std::string& key) {
}

void SdchOwnerPrefStorage::OnInitializationCompleted(bool succeeded) {
  DCHECK(init_observer_);
  init_observer_->OnPrefStorageInitializationComplete(succeeded);
}