| 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 "rlz/chromeos/lib/rlz_value_store_chromeos.h" | 5 #include "rlz/chromeos/lib/rlz_value_store_chromeos.h" |
| 6 | 6 |
| 7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/important_file_writer.h" | 9 #include "base/files/important_file_writer.h" |
| 10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
| 11 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
| 12 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 15 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
| 16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/values.h" | 18 #include "base/values.h" |
| 18 #include "rlz/lib/lib_values.h" | 19 #include "rlz/lib/lib_values.h" |
| 19 #include "rlz/lib/recursive_cross_process_lock_posix.h" | 20 #include "rlz/lib/recursive_cross_process_lock_posix.h" |
| 20 #include "rlz/lib/rlz_lib.h" | 21 #include "rlz/lib/rlz_lib.h" |
| 21 | 22 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 34 | 35 |
| 35 // RLZ store filename. | 36 // RLZ store filename. |
| 36 const base::FilePath::CharType kRLZDataFileName[] = | 37 const base::FilePath::CharType kRLZDataFileName[] = |
| 37 FILE_PATH_LITERAL("RLZ Data"); | 38 FILE_PATH_LITERAL("RLZ Data"); |
| 38 | 39 |
| 39 // RLZ store lock filename | 40 // RLZ store lock filename |
| 40 const base::FilePath::CharType kRLZLockFileName[] = | 41 const base::FilePath::CharType kRLZLockFileName[] = |
| 41 FILE_PATH_LITERAL("RLZ Data.lock"); | 42 FILE_PATH_LITERAL("RLZ Data.lock"); |
| 42 | 43 |
| 43 // RLZ store path for testing. | 44 // RLZ store path for testing. |
| 44 base::FilePath g_testing_rlz_store_path_; | 45 base::LazyInstance<base::FilePath>::Leaky g_testing_rlz_store_path = |
| 46 LAZY_INSTANCE_INITIALIZER; |
| 47 |
| 48 base::FilePath GetRlzStorePathCommon() { |
| 49 base::FilePath homedir; |
| 50 PathService::Get(base::DIR_HOME, &homedir); |
| 51 return g_testing_rlz_store_path.Get().empty() |
| 52 ? homedir |
| 53 : g_testing_rlz_store_path.Get(); |
| 54 } |
| 45 | 55 |
| 46 // Returns file path of the RLZ storage. | 56 // Returns file path of the RLZ storage. |
| 47 base::FilePath GetRlzStorePath() { | 57 base::FilePath GetRlzStorePath() { |
| 48 base::FilePath homedir; | 58 return GetRlzStorePathCommon().Append(kRLZDataFileName); |
| 49 PathService::Get(base::DIR_HOME, &homedir); | |
| 50 return g_testing_rlz_store_path_.empty() ? | |
| 51 homedir.Append(kRLZDataFileName) : | |
| 52 g_testing_rlz_store_path_.Append(kRLZDataFileName); | |
| 53 } | 59 } |
| 54 | 60 |
| 55 // Returns file path of the RLZ storage lock file. | 61 // Returns file path of the RLZ storage lock file. |
| 56 base::FilePath GetRlzStoreLockPath() { | 62 base::FilePath GetRlzStoreLockPath() { |
| 57 base::FilePath homedir; | 63 return GetRlzStorePathCommon().Append(kRLZLockFileName); |
| 58 PathService::Get(base::DIR_HOME, &homedir); | |
| 59 return g_testing_rlz_store_path_.empty() ? | |
| 60 homedir.Append(kRLZLockFileName) : | |
| 61 g_testing_rlz_store_path_.Append(kRLZLockFileName); | |
| 62 } | 64 } |
| 63 | 65 |
| 64 // Returns the dictionary key for storing access point-related prefs. | 66 // Returns the dictionary key for storing access point-related prefs. |
| 65 std::string GetKeyName(const std::string& key, AccessPoint access_point) { | 67 std::string GetKeyName(const std::string& key, AccessPoint access_point) { |
| 66 std::string brand = SupplementaryBranding::GetBrand(); | 68 std::string brand = SupplementaryBranding::GetBrand(); |
| 67 if (brand.empty()) | 69 if (brand.empty()) |
| 68 brand = kNoSupplementaryBrand; | 70 brand = kNoSupplementaryBrand; |
| 69 return key + "." + GetAccessPointName(access_point) + "." + brand; | 71 return key + "." + GetAccessPointName(access_point) + "." + brand; |
| 70 } | 72 } |
| 71 | 73 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 const char* event_rlz) { | 150 const char* event_rlz) { |
| 149 DCHECK(CalledOnValidThread()); | 151 DCHECK(CalledOnValidThread()); |
| 150 return AddValueToList(GetKeyName(kProductEventKey, product), | 152 return AddValueToList(GetKeyName(kProductEventKey, product), |
| 151 base::MakeUnique<base::Value>(event_rlz)); | 153 base::MakeUnique<base::Value>(event_rlz)); |
| 152 } | 154 } |
| 153 | 155 |
| 154 bool RlzValueStoreChromeOS::ReadProductEvents( | 156 bool RlzValueStoreChromeOS::ReadProductEvents( |
| 155 Product product, | 157 Product product, |
| 156 std::vector<std::string>* events) { | 158 std::vector<std::string>* events) { |
| 157 DCHECK(CalledOnValidThread()); | 159 DCHECK(CalledOnValidThread()); |
| 158 base::ListValue* events_list = NULL; ; | 160 base::ListValue* events_list = nullptr; |
| 159 if (!rlz_store_->GetList(GetKeyName(kProductEventKey, product), &events_list)) | 161 if (!rlz_store_->GetList(GetKeyName(kProductEventKey, product), &events_list)) |
| 160 return false; | 162 return false; |
| 161 events->clear(); | 163 events->clear(); |
| 162 for (size_t i = 0; i < events_list->GetSize(); ++i) { | 164 for (size_t i = 0; i < events_list->GetSize(); ++i) { |
| 163 std::string event; | 165 std::string event; |
| 164 if (events_list->GetString(i, &event)) | 166 if (events_list->GetString(i, &event)) |
| 165 events->push_back(event); | 167 events->push_back(event); |
| 166 } | 168 } |
| 167 return true; | 169 return true; |
| 168 } | 170 } |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 } | 306 } |
| 305 | 307 |
| 306 // This is the topmost lock, create a new store object. | 308 // This is the topmost lock, create a new store object. |
| 307 DCHECK(!g_store); | 309 DCHECK(!g_store); |
| 308 g_store = new RlzValueStoreChromeOS(GetRlzStorePath()); | 310 g_store = new RlzValueStoreChromeOS(GetRlzStorePath()); |
| 309 store_.reset(g_store); | 311 store_.reset(g_store); |
| 310 } | 312 } |
| 311 | 313 |
| 312 ScopedRlzValueStoreLock::~ScopedRlzValueStoreLock() { | 314 ScopedRlzValueStoreLock::~ScopedRlzValueStoreLock() { |
| 313 --g_lock_depth; | 315 --g_lock_depth; |
| 314 DCHECK(g_lock_depth >= 0); | 316 DCHECK_GE(g_lock_depth, 0); |
| 315 | 317 |
| 316 if (g_lock_depth > 0) { | 318 if (g_lock_depth > 0) { |
| 317 // Other locks are still using store_, so don't free it yet. | 319 // Other locks are still using store_, so don't free it yet. |
| 318 ignore_result(store_.release()); | 320 ignore_result(store_.release()); |
| 319 return; | 321 return; |
| 320 } | 322 } |
| 321 | 323 |
| 322 g_store = NULL; | 324 g_store = NULL; |
| 323 | 325 |
| 324 g_recursive_lock.ReleaseLock(); | 326 g_recursive_lock.ReleaseLock(); |
| 325 } | 327 } |
| 326 | 328 |
| 327 RlzValueStore* ScopedRlzValueStoreLock::GetStore() { | 329 RlzValueStore* ScopedRlzValueStoreLock::GetStore() { |
| 328 return store_.get(); | 330 return store_.get(); |
| 329 } | 331 } |
| 330 | 332 |
| 331 namespace testing { | 333 namespace testing { |
| 332 | 334 |
| 333 void SetRlzStoreDirectory(const base::FilePath& directory) { | 335 void SetRlzStoreDirectory(const base::FilePath& directory) { |
| 334 g_testing_rlz_store_path_ = directory; | 336 g_testing_rlz_store_path.Get() = directory; |
| 335 } | 337 } |
| 336 | 338 |
| 337 std::string RlzStoreFilenameStr() { | 339 std::string RlzStoreFilenameStr() { |
| 338 return GetRlzStorePath().value(); | 340 return GetRlzStorePath().value(); |
| 339 } | 341 } |
| 340 | 342 |
| 341 } // namespace testing | 343 } // namespace testing |
| 342 | 344 |
| 343 } // namespace rlz_lib | 345 } // namespace rlz_lib |
| OLD | NEW |