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" |
| 10 #include "base/lazy_instance.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/nix/xdg_util.h" |
| 13 |
| 14 #if defined(USE_LIBSECRET) |
7 #include "components/os_crypt/key_storage_libsecret.h" | 15 #include "components/os_crypt/key_storage_libsecret.h" |
| 16 #endif |
| 17 |
| 18 base::LazyInstance<std::string> g_store_ = LAZY_INSTANCE_INITIALIZER; |
| 19 |
| 20 // static |
| 21 void KeyStorageLinux::SetStore(const std::string& store_type) { |
| 22 g_store_.Get() = store_type; |
| 23 VLOG(1) << "OSCrypt store set to " << store_type; |
| 24 } |
8 | 25 |
9 // static | 26 // static |
10 std::unique_ptr<KeyStorageLinux> KeyStorageLinux::CreateService() { | 27 std::unique_ptr<KeyStorageLinux> KeyStorageLinux::CreateService() { |
11 std::unique_ptr<KeyStorageLinux> key_storage(new KeyStorageLibsecret()); | 28 base::nix::DesktopEnvironment used_desktop_env; |
| 29 if (g_store_.Get() == "kwallet") { |
| 30 used_desktop_env = base::nix::DESKTOP_ENVIRONMENT_KDE4; |
| 31 } else if (g_store_.Get() == "kwallet5") { |
| 32 used_desktop_env = base::nix::DESKTOP_ENVIRONMENT_KDE5; |
| 33 } else if (g_store_.Get() == "gnome") { |
| 34 used_desktop_env = base::nix::DESKTOP_ENVIRONMENT_GNOME; |
| 35 } else if (g_store_.Get() == "basic") { |
| 36 used_desktop_env = base::nix::DESKTOP_ENVIRONMENT_OTHER; |
| 37 } else { |
| 38 std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 39 used_desktop_env = base::nix::GetDesktopEnvironment(env.get()); |
| 40 } |
12 | 41 |
13 if (key_storage->Init()) | 42 // Try initializing the appropriate store for our environment. |
14 return key_storage; | 43 std::unique_ptr<KeyStorageLinux> key_storage; |
| 44 if (used_desktop_env == base::nix::DESKTOP_ENVIRONMENT_GNOME || |
| 45 used_desktop_env == base::nix::DESKTOP_ENVIRONMENT_UNITY || |
| 46 used_desktop_env == base::nix::DESKTOP_ENVIRONMENT_XFCE) { |
| 47 #if defined(USE_LIBSECRET) |
| 48 key_storage.reset(new KeyStorageLibsecret()); |
| 49 if (key_storage->Init()) { |
| 50 VLOG(1) << "OSCrypt using Libsecret as backend."; |
| 51 return key_storage; |
| 52 } |
| 53 #endif |
| 54 } |
15 | 55 |
| 56 // The appropriate store was not available. |
| 57 VLOG(1) << "OSCrypt could not initialize a backend."; |
16 return nullptr; | 58 return nullptr; |
17 } | 59 } |
OLD | NEW |