Cleanup OSCrypt after initialisation

OSCrypt only needs to read from the backend during its initialisation.
Instances created for this initialisation can be cleaned up afterwards.

To support this change, the mocking of OSCrypt can no longer be based
on a singleton. Tests using the mocking mechanism are updated.

Bug: 709096
Change-Id: Iba5b37ad45ada11b9a217dc2c388313f2371f647
Reviewed-on: https://chromium-review.googlesource.com/571221
Reviewed-by: Roger Tawa <[email protected]>
Reviewed-by: Mathieu Perreault <[email protected]>
Reviewed-by: Martin Šrámek <[email protected]>
Reviewed-by: Pavel Yatsuk <[email protected]>
Reviewed-by: Scott Violet <[email protected]>
Reviewed-by: Vasilii Sukhanov <[email protected]>
Commit-Queue: Christos Froussios <[email protected]>
Cr-Commit-Position: refs/heads/master@{#488675}
diff --git a/components/os_crypt/os_crypt_mocker_linux.cc b/components/os_crypt/os_crypt_mocker_linux.cc
index 899616c..f8f0504 100644
--- a/components/os_crypt/os_crypt_mocker_linux.cc
+++ b/components/os_crypt/os_crypt_mocker_linux.cc
@@ -15,45 +15,24 @@
 
 namespace {
 
-static base::LazyInstance<OSCryptMockerLinux>::Leaky g_mocker =
-    LAZY_INSTANCE_INITIALIZER;
-
-KeyStorageLinux* GetKeyStorage() {
-  return OSCryptMockerLinux::GetInstance();
+std::unique_ptr<KeyStorageLinux> CreateNewMock() {
+  return base::MakeUnique<OSCryptMockerLinux>();
 }
 
-std::string* GetPassword() {
-  return OSCryptMockerLinux::GetInstance()->GetKeyPtr();
 }
 
-}  // namespace
-
 std::string OSCryptMockerLinux::GetKey() {
-  if (key_.empty())
-    base::Base64Encode(base::RandBytesAsString(16), &key_);
   return key_;
 }
 
-bool OSCryptMockerLinux::Init() {
-  return true;
-}
-
-void OSCryptMockerLinux::ResetTo(base::StringPiece new_key) {
-  key_ = new_key.as_string();
-}
-
 std::string* OSCryptMockerLinux::GetKeyPtr() {
   return &key_;
 }
 
 // static
-OSCryptMockerLinux* OSCryptMockerLinux::GetInstance() {
-  return g_mocker.Pointer();
-}
-
-// static
-void OSCryptMockerLinux::SetUpWithSingleton() {
-  UseMockKeyStorageForTesting(&GetKeyStorage, &GetPassword);
+void OSCryptMockerLinux::SetUp() {
+  UseMockKeyStorageForTesting(
+      &CreateNewMock, nullptr /* get the key from the provider above */);
   OSCrypt::SetConfig(base::MakeUnique<os_crypt::Config>());
 }
 
@@ -62,3 +41,8 @@
   UseMockKeyStorageForTesting(nullptr, nullptr);
   ClearCacheForTesting();
 }
+
+bool OSCryptMockerLinux::Init() {
+  key_ = "the_encryption_key";
+  return true;
+}