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 | |
Sergey Ulanov
2013/06/21 23:27:55
nit: don't need this empty line.
Jamie
2013/06/21 23:33:25
Done.
| |
104 base::FilePath registry_path = GetRegistryFilePath(); | |
105 std::string result; | |
106 if (!file_util::ReadFileToString(registry_path, &result)) { | |
107 LOG(ERROR) << "Load failed."; | |
108 } | |
109 callback.Run(result); | |
110 } | |
111 | |
112 base::FilePath PairingRegistryDelegateLinux::GetRegistryFilePath() { | |
113 if (!filename_for_testing_.empty()) { | |
114 return filename_for_testing_; | |
115 } | |
116 | |
117 base::FilePath config_dir = remoting::GetConfigDir(); | |
118 return config_dir.Append(kRegistryFilename); | |
119 } | |
120 | |
121 void PairingRegistryDelegateLinux::SetFilenameForTesting( | |
122 const base::FilePath &filename) { | |
123 filename_for_testing_ = filename; | |
124 } | |
125 | |
126 | |
127 scoped_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate( | |
128 scoped_refptr<base::TaskRunner> task_runner) { | |
129 return scoped_ptr<PairingRegistry::Delegate>( | |
130 new PairingRegistryDelegateLinux(task_runner)); | |
131 } | |
132 | |
133 } // namespace remoting | |
OLD | NEW |