OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium 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 "remoting/host/pairing_registry_delegate_linux.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/files/important_file_writer.h" |
| 10 #include "base/location.h" |
| 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "remoting/host/branding.h" |
| 14 |
| 15 namespace { |
| 16 const char kRegistryFilename[] = "paired-clients.json"; |
| 17 } // namespace |
| 18 |
| 19 namespace remoting { |
| 20 |
| 21 using protocol::PairingRegistry; |
| 22 |
| 23 PairingRegistryDelegateLinux::PairingRegistryDelegateLinux( |
| 24 scoped_refptr<base::TaskRunner> task_runner) |
| 25 : task_runner_(task_runner), |
| 26 weak_factory_(this) { |
| 27 } |
| 28 |
| 29 PairingRegistryDelegateLinux::~PairingRegistryDelegateLinux() { |
| 30 } |
| 31 |
| 32 void PairingRegistryDelegateLinux::Save( |
| 33 const std::string& pairings_json, |
| 34 const PairingRegistry::SaveCallback& callback) { |
| 35 // If a callback was supplied, wrap it in a helper function that will |
| 36 // run it on this thread. |
| 37 PairingRegistry::SaveCallback run_callback_on_this_thread; |
| 38 if (!callback.is_null()) { |
| 39 run_callback_on_this_thread = |
| 40 base::Bind(&PairingRegistryDelegateLinux::RunSaveCallbackOnThread, |
| 41 base::ThreadTaskRunnerHandle::Get(), |
| 42 callback); |
| 43 } |
| 44 task_runner_->PostTask( |
| 45 FROM_HERE, |
| 46 base::Bind(&PairingRegistryDelegateLinux::DoSave, |
| 47 weak_factory_.GetWeakPtr(), |
| 48 pairings_json, |
| 49 run_callback_on_this_thread)); |
| 50 } |
| 51 |
| 52 void PairingRegistryDelegateLinux::Load( |
| 53 const PairingRegistry::LoadCallback& callback) { |
| 54 // Wrap the callback in a helper function that will run it on this thread. |
| 55 // Note that, unlike AddPairing, the GetPairing callback is mandatory. |
| 56 PairingRegistry::LoadCallback run_callback_on_this_thread = |
| 57 base::Bind(&PairingRegistryDelegateLinux::RunLoadCallbackOnThread, |
| 58 base::ThreadTaskRunnerHandle::Get(), |
| 59 callback); |
| 60 task_runner_->PostTask( |
| 61 FROM_HERE, |
| 62 base::Bind(&PairingRegistryDelegateLinux::DoLoad, |
| 63 weak_factory_.GetWeakPtr(), |
| 64 run_callback_on_this_thread)); |
| 65 } |
| 66 |
| 67 void PairingRegistryDelegateLinux::RunSaveCallbackOnThread( |
| 68 scoped_refptr<base::TaskRunner> task_runner, |
| 69 const PairingRegistry::SaveCallback& callback, |
| 70 bool success) { |
| 71 task_runner->PostTask(FROM_HERE, base::Bind(callback, success)); |
| 72 } |
| 73 |
| 74 void PairingRegistryDelegateLinux::RunLoadCallbackOnThread( |
| 75 scoped_refptr<base::TaskRunner> task_runner, |
| 76 const PairingRegistry::LoadCallback& callback, |
| 77 const std::string& pairings_json) { |
| 78 task_runner->PostTask(FROM_HERE, base::Bind(callback, pairings_json)); |
| 79 } |
| 80 |
| 81 void PairingRegistryDelegateLinux::DoSave( |
| 82 const std::string& pairings_json, |
| 83 const PairingRegistry::SaveCallback& callback) { |
| 84 base::FilePath registry_path = GetRegistryFilePath(); |
| 85 base::FilePath parent_directory = registry_path.DirName(); |
| 86 base::PlatformFileError error; |
| 87 if (!file_util::CreateDirectoryAndGetError(parent_directory, &error)) { |
| 88 LOG(ERROR) << "Could not create pairing registry directory: " << error; |
| 89 return; |
| 90 } |
| 91 if (!base::ImportantFileWriter::WriteFileAtomically(registry_path, |
| 92 pairings_json)) { |
| 93 LOG(ERROR) << "Could not save pairing registry."; |
| 94 } |
| 95 |
| 96 if (!callback.is_null()) { |
| 97 callback.Run(true); |
| 98 } |
| 99 } |
| 100 |
| 101 void PairingRegistryDelegateLinux::DoLoad( |
| 102 const PairingRegistry::LoadCallback& callback) { |
| 103 base::FilePath registry_path = GetRegistryFilePath(); |
| 104 std::string result; |
| 105 if (!file_util::ReadFileToString(registry_path, &result)) { |
| 106 LOG(ERROR) << "Load failed."; |
| 107 } |
| 108 callback.Run(result); |
| 109 } |
| 110 |
| 111 base::FilePath PairingRegistryDelegateLinux::GetRegistryFilePath() { |
| 112 if (!filename_for_testing_.empty()) { |
| 113 return filename_for_testing_; |
| 114 } |
| 115 |
| 116 base::FilePath config_dir = remoting::GetConfigDir(); |
| 117 return config_dir.Append(kRegistryFilename); |
| 118 } |
| 119 |
| 120 void PairingRegistryDelegateLinux::SetFilenameForTesting( |
| 121 const base::FilePath &filename) { |
| 122 filename_for_testing_ = filename; |
| 123 } |
| 124 |
| 125 |
| 126 scoped_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate( |
| 127 scoped_refptr<base::TaskRunner> task_runner) { |
| 128 return scoped_ptr<PairingRegistry::Delegate>( |
| 129 new PairingRegistryDelegateLinux(task_runner)); |
| 130 } |
| 131 |
| 132 } // namespace remoting |
OLD | NEW |