Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/os_crypt/key_storage_linux.h" | 5 #include "components/os_crypt/key_storage_linux.h" |
| 6 | 6 |
| 7 #include <string.h> | |
| 8 | |
| 9 #include "base/environment.h" | 7 #include "base/environment.h" |
| 10 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 11 #include "base/logging.h" | 9 #include "base/logging.h" |
| 12 #include "base/nix/xdg_util.h" | 10 #include "base/nix/xdg_util.h" |
| 13 | 11 |
| 14 #if defined(USE_LIBSECRET) | 12 #if defined(USE_LIBSECRET) |
| 15 #include "components/os_crypt/key_storage_libsecret.h" | 13 #include "components/os_crypt/key_storage_libsecret.h" |
| 16 #endif | 14 #endif |
| 17 | 15 |
| 16 #if defined(USE_KWALLET) | |
| 17 #include "components/os_crypt/key_storage_kwallet.h" | |
| 18 #endif | |
| 19 | |
| 18 base::LazyInstance<std::string> g_store_ = LAZY_INSTANCE_INITIALIZER; | 20 base::LazyInstance<std::string> g_store_ = LAZY_INSTANCE_INITIALIZER; |
| 21 base::LazyInstance<std::string> g_product_name_ = LAZY_INSTANCE_INITIALIZER; | |
|
Lei Zhang
2016/07/20 01:08:26
Sorry if I missed it earlier, but g_foo doesn't ne
cfroussios
2016/07/20 15:22:46
Done.
| |
| 19 | 22 |
| 20 // static | 23 // static |
| 21 void KeyStorageLinux::SetStore(const std::string& store_type) { | 24 void KeyStorageLinux::SetStore(const std::string& store_type) { |
| 22 g_store_.Get() = store_type; | 25 g_store_.Get() = store_type; |
| 23 VLOG(1) << "OSCrypt store set to " << store_type; | 26 VLOG(1) << "OSCrypt store set to " << store_type; |
| 24 } | 27 } |
| 25 | 28 |
| 26 // static | 29 // static |
| 30 void KeyStorageLinux::SetProductName(const std::string& product_name) { | |
| 31 g_product_name_.Get() = product_name; | |
| 32 } | |
| 33 | |
| 34 // static | |
| 27 std::unique_ptr<KeyStorageLinux> KeyStorageLinux::CreateService() { | 35 std::unique_ptr<KeyStorageLinux> KeyStorageLinux::CreateService() { |
| 28 base::nix::DesktopEnvironment used_desktop_env; | 36 base::nix::DesktopEnvironment used_desktop_env; |
| 29 if (g_store_.Get() == "kwallet") { | 37 if (g_store_.Get() == "kwallet") { |
| 30 used_desktop_env = base::nix::DESKTOP_ENVIRONMENT_KDE4; | 38 used_desktop_env = base::nix::DESKTOP_ENVIRONMENT_KDE4; |
| 31 } else if (g_store_.Get() == "kwallet5") { | 39 } else if (g_store_.Get() == "kwallet5") { |
| 32 used_desktop_env = base::nix::DESKTOP_ENVIRONMENT_KDE5; | 40 used_desktop_env = base::nix::DESKTOP_ENVIRONMENT_KDE5; |
| 33 } else if (g_store_.Get() == "gnome") { | 41 } else if (g_store_.Get() == "gnome") { |
| 34 used_desktop_env = base::nix::DESKTOP_ENVIRONMENT_GNOME; | 42 used_desktop_env = base::nix::DESKTOP_ENVIRONMENT_GNOME; |
| 35 } else if (g_store_.Get() == "basic") { | 43 } else if (g_store_.Get() == "basic") { |
| 36 used_desktop_env = base::nix::DESKTOP_ENVIRONMENT_OTHER; | 44 used_desktop_env = base::nix::DESKTOP_ENVIRONMENT_OTHER; |
| 37 } else { | 45 } else { |
| 38 std::unique_ptr<base::Environment> env(base::Environment::Create()); | 46 std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 39 used_desktop_env = base::nix::GetDesktopEnvironment(env.get()); | 47 used_desktop_env = base::nix::GetDesktopEnvironment(env.get()); |
| 40 } | 48 } |
| 41 | 49 |
| 42 // Try initializing the appropriate store for our environment. | 50 // Try initializing the appropriate store for our environment. |
| 43 std::unique_ptr<KeyStorageLinux> key_storage; | 51 std::unique_ptr<KeyStorageLinux> key_storage; |
| 44 if (used_desktop_env == base::nix::DESKTOP_ENVIRONMENT_GNOME || | 52 if (used_desktop_env == base::nix::DESKTOP_ENVIRONMENT_GNOME || |
| 45 used_desktop_env == base::nix::DESKTOP_ENVIRONMENT_UNITY || | 53 used_desktop_env == base::nix::DESKTOP_ENVIRONMENT_UNITY || |
| 46 used_desktop_env == base::nix::DESKTOP_ENVIRONMENT_XFCE) { | 54 used_desktop_env == base::nix::DESKTOP_ENVIRONMENT_XFCE) { |
| 47 #if defined(USE_LIBSECRET) | 55 #if defined(USE_LIBSECRET) |
| 48 key_storage.reset(new KeyStorageLibsecret()); | 56 key_storage.reset(new KeyStorageLibsecret()); |
| 49 if (key_storage->Init()) { | 57 if (key_storage->Init()) { |
| 50 VLOG(1) << "OSCrypt using Libsecret as backend."; | 58 VLOG(1) << "OSCrypt using Libsecret as backend."; |
| 51 return key_storage; | 59 return key_storage; |
| 52 } | 60 } |
| 53 #endif | 61 #endif |
| 62 } else if (used_desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE4 || | |
| 63 used_desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE5) { | |
| 64 #if defined(USE_KWALLET) | |
| 65 DCHECK(!g_product_name_.Get().empty()); | |
| 66 key_storage.reset( | |
| 67 new KeyStorageKWallet(used_desktop_env, g_product_name_.Get())); | |
| 68 if (key_storage->Init()) { | |
| 69 VLOG(1) << "OSCrypt using KWallet as backend."; | |
| 70 return key_storage; | |
| 71 } | |
| 72 #endif | |
| 54 } | 73 } |
| 55 | 74 |
| 56 // The appropriate store was not available. | 75 // The appropriate store was not available. |
| 57 VLOG(1) << "OSCrypt could not initialize a backend."; | 76 VLOG(1) << "OSCrypt could not initialize a backend."; |
| 58 return nullptr; | 77 return nullptr; |
| 59 } | 78 } |
| OLD | NEW |