Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS 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 "shill/pending_activation_store.h" |
| 6 | |
| 7 | #include "shill/key_file_store.h" |
| 8 | #include "shill/logging.h" |
| 9 | |
| 10 | using base::FilePath; |
| 11 | using std::string; |
| 12 | |
| 13 | namespace shill { |
| 14 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 15 | namespace Logging { |
| 16 | static auto kModuleLogScope = ScopeLogger::kCellular; |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 17 | static string ObjectID(const PendingActivationStore* p) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 18 | return "(pending_activation_store)"; |
| 19 | } |
| 20 | } |
| 21 | |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 22 | const char PendingActivationStore::kIccidGroupId[] = "iccid_list"; |
| 23 | const char PendingActivationStore::kMeidGroupId[] = "meid_list"; |
| 24 | // We're keeping the old file name here for backwards compatibility. |
| 25 | const char PendingActivationStore::kStorageFileName[] = |
| 26 | "activating_iccid_store.profile"; |
| 27 | |
| 28 | PendingActivationStore::PendingActivationStore() {} |
| 29 | |
| 30 | PendingActivationStore::~PendingActivationStore() { |
| 31 | if (storage_.get()) |
| 32 | storage_->Flush(); // Make certain that everything is persisted. |
| 33 | } |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | string StateToString(PendingActivationStore::State state) { |
| 38 | switch (state) { |
| 39 | case PendingActivationStore::kStateUnknown: |
| 40 | return "Unknown"; |
| 41 | case PendingActivationStore::kStatePending: |
| 42 | return "Pending"; |
| 43 | case PendingActivationStore::kStateActivated: |
| 44 | return "Activated"; |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 45 | default: |
| 46 | return "Invalid"; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | string FormattedIdentifier(PendingActivationStore::IdentifierType type, |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 51 | const string& identifier) { |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 52 | string label; |
| 53 | switch (type) { |
| 54 | case PendingActivationStore::kIdentifierICCID: |
| 55 | label = "ICCID"; |
| 56 | break; |
| 57 | case PendingActivationStore::kIdentifierMEID: |
| 58 | label = "MEID"; |
| 59 | break; |
| 60 | default: |
| 61 | NOTREACHED(); |
| 62 | } |
| 63 | return "[" + label + "=" + identifier + "]"; |
| 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | |
| 68 | // static |
| 69 | string PendingActivationStore::IdentifierTypeToGroupId(IdentifierType type) { |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 70 | switch (type) { |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 71 | case kIdentifierICCID: |
| 72 | return kIccidGroupId; |
| 73 | case kIdentifierMEID: |
| 74 | return kMeidGroupId; |
| 75 | default: |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 76 | SLOG(Cellular, nullptr, 2) << "Incorrect identifier type: " << type; |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 77 | return ""; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | bool PendingActivationStore::InitStorage( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 82 | GLib* glib, |
| 83 | const FilePath& storage_path) { |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 84 | // Close the current file. |
| 85 | if (storage_.get()) { |
| 86 | storage_->Flush(); |
| 87 | storage_.reset(); // KeyFileStore closes the file in its destructor. |
| 88 | } |
| 89 | if (!glib) { |
| 90 | LOG(ERROR) << "Null pointer passed for |glib|."; |
| 91 | return false; |
| 92 | } |
| 93 | if (storage_path.empty()) { |
| 94 | LOG(ERROR) << "Empty storage directory path provided."; |
| 95 | return false; |
| 96 | } |
| 97 | FilePath path = storage_path.Append(kStorageFileName); |
Ben Chan | c20ed13 | 2014-10-16 12:25:03 -0700 | [diff] [blame] | 98 | std::unique_ptr<KeyFileStore> storage(new KeyFileStore(glib)); |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 99 | storage->set_path(path); |
| 100 | bool already_exists = storage->IsNonEmpty(); |
| 101 | if (!storage->Open()) { |
| 102 | LOG(ERROR) << "Failed to open file at '" << path.AsUTF8Unsafe() << "'"; |
| 103 | if (already_exists) |
| 104 | storage->MarkAsCorrupted(); |
| 105 | return false; |
| 106 | } |
| 107 | if (!already_exists) |
| 108 | storage->SetHeader("Identifiers pending cellular activation."); |
| 109 | storage_.reset(storage.release()); |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | PendingActivationStore::State PendingActivationStore::GetActivationState( |
| 114 | IdentifierType type, |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 115 | const string& identifier) const { |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 116 | string formatted_identifier = FormattedIdentifier(type, identifier); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 117 | SLOG(this, 2) << __func__ << ": " << formatted_identifier; |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 118 | if (!storage_.get()) { |
| 119 | LOG(ERROR) << "Underlying storage not initialized."; |
| 120 | return kStateUnknown; |
| 121 | } |
| 122 | int state = 0; |
| 123 | if (!storage_->GetInt(IdentifierTypeToGroupId(type), identifier, &state)) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 124 | SLOG(this, 2) << "No entry exists for " << formatted_identifier; |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 125 | return kStateUnknown; |
| 126 | } |
| 127 | if (state <= 0 || state >= kStateMax) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 128 | SLOG(this, 2) << "State value read for " << formatted_identifier |
| 129 | << " is invalid."; |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 130 | return kStateUnknown; |
| 131 | } |
| 132 | return static_cast<State>(state); |
| 133 | } |
| 134 | |
| 135 | bool PendingActivationStore::SetActivationState( |
| 136 | IdentifierType type, |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 137 | const string& identifier, |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 138 | State state) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 139 | SLOG(this, 2) << __func__ << ": State=" << StateToString(state) << ", " |
| 140 | << FormattedIdentifier(type, identifier); |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 141 | if (!storage_.get()) { |
| 142 | LOG(ERROR) << "Underlying storage not initialized."; |
| 143 | return false; |
| 144 | } |
| 145 | if (state == kStateUnknown) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 146 | SLOG(this, 2) << "kStateUnknown cannot be used as a value."; |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | if (state < 0 || state >= kStateMax) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 150 | SLOG(this, 2) << "Cannot set state to \"" << StateToString(state) |
| 151 | << "\""; |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | if (!storage_->SetInt( |
| 155 | IdentifierTypeToGroupId(type), identifier, static_cast<int>(state))) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 156 | SLOG(this, 2) << "Failed to store the given identifier and state " |
| 157 | << "values."; |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | return storage_->Flush(); |
| 161 | } |
| 162 | |
| 163 | bool PendingActivationStore::RemoveEntry(IdentifierType type, |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame^] | 164 | const std::string& identifier) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 165 | SLOG(this, 2) << __func__ << ": " |
| 166 | << FormattedIdentifier(type, identifier); |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 167 | if (!storage_.get()) { |
| 168 | LOG(ERROR) << "Underlying storage not initialized."; |
| 169 | return false; |
| 170 | } |
| 171 | if (!storage_->DeleteKey(IdentifierTypeToGroupId(type), identifier)) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 172 | SLOG(this, 2) << "Failed to remove the given identifier."; |
Arman Uguray | 41cc634 | 2013-03-29 16:34:39 -0700 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | return storage_->Flush(); |
| 176 | } |
| 177 | |
| 178 | } // namespace shill |