Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(651)

Side by Side Diff: remoting/host/pairing_registry_delegate_linux.cc

Issue 15709005: Linux pairing registry delegate implementation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/environment.h"
9 #include "base/file_util.h"
10 #include "base/json/json_file_value_serializer.h"
11 #include "base/location.h"
12 #include "base/task_runner.h"
13 #include "base/values.h"
14
15 namespace {
16 const char kConfigDir[] = ".config/chrome-remote-desktop";
17 const char kRegistryFilename[] = "paired-clients";
18 const char kCreatedTimeLowKey[] = "created-time-low";
19 const char kCreatedTimeHighKey[] = "created-time-high";
20 const char kClientIdKey[] = "client-id";
21 const char kClientNameKey[] = "client-name";
22 const char kSharedSecretKey[] = "shared-secret";
23 } // namespace
24
25
26 namespace remoting {
27 namespace protocol {
28
29 PairingRegistryDelegateLinux::PairingRegistryDelegateLinux(
30 scoped_refptr<base::TaskRunner> task_runner)
31 : task_runner_(task_runner),
32 weak_factory_(this) {
33 }
34
35 void PairingRegistryDelegateLinux::Save(
36 const PairingRegistry::PairedClients& paired_clients) {
37 task_runner_->PostTask(
38 FROM_HERE,
39 base::Bind(&PairingRegistryDelegateLinux::SaveOnCurrentThread,
40 weak_factory_.GetWeakPtr(), paired_clients));
41 }
42
43 void PairingRegistryDelegateLinux::SaveOnCurrentThread(
44 const PairingRegistry::PairedClients& paired_clients) {
45 base::FilePath registry_path;
46 if (!GetRegistryFilePath(&registry_path)) {
47 return;
48 }
49
50 base::ListValue root;
51 for (PairingRegistry::PairedClients::const_iterator i =
52 paired_clients.begin(); i != paired_clients.end(); ++i) {
53 base::DictionaryValue* pairing = new base::DictionaryValue();
54 int64 created_time = i->second.created_time.ToInternalValue();
55 pairing->SetInteger(kCreatedTimeLowKey, created_time & 0xffffffff);
56 pairing->SetInteger(kCreatedTimeHighKey, created_time >> 32);
57 pairing->SetString(kClientNameKey, i->second.client_name);
58 pairing->SetString(kClientIdKey, i->second.client_id);
59 pairing->SetString(kSharedSecretKey, i->second.shared_secret);
60 root.Append(pairing);
61 }
62
63 JSONFileValueSerializer registry(registry_path);
64 registry.Serialize(root);
65 }
66
67 PairingRegistry::PairedClients
68 PairingRegistryDelegateLinux::LoadOnCurrentThread() {
69 PairingRegistry::PairedClients result;
70 base::FilePath registry_path;
71 if (!GetRegistryFilePath(&registry_path)) {
72 return result;
73 }
74
75 JSONFileValueSerializer registry(registry_path);
76 int error_code;
77 std::string error_message;
78 scoped_ptr<base::Value> root(
79 registry.Deserialize(&error_code, &error_message));
80 if (!root) {
81 LOG(ERROR) << "Failed to load paired clients: " << error_message
82 << " (" << error_code << ").";
83 return result;
84 }
85
86 base::ListValue* root_list = NULL;
87 if (!root->GetAsList(&root_list)) {
88 LOG(ERROR) << "Failed to load paired clients: root node is not a list.";
89 return result;
90 }
91
92 for (size_t i = 0; i < root_list->GetSize(); ++i) {
93 base::DictionaryValue* pairing = NULL;
94 std::string client_id, client_name, shared_secret;
95 int created_time_low, created_time_high;
96 if (root_list->GetDictionary(i, &pairing) &&
97 pairing->GetInteger(kCreatedTimeLowKey, &created_time_low) &&
98 pairing->GetInteger(kCreatedTimeHighKey, &created_time_high) &&
99 pairing->GetString(kClientIdKey, &client_id) &&
100 pairing->GetString(kClientNameKey, &client_name) &&
101 pairing->GetString(kSharedSecretKey, &shared_secret)) {
102 PairingRegistry::Pairing pairing_struct;
103 pairing_struct.created_time = base::Time::FromInternalValue(
104 (static_cast<int64>(created_time_high) << 32) + created_time_low);
105 pairing_struct.client_id = client_id;
106 pairing_struct.client_name = client_name;
107 pairing_struct.shared_secret = shared_secret;
108 result[client_id] = pairing_struct;
109 } else {
110 LOG(ERROR) << "Paired client " << i << " has unexpected format.";
111 }
112 }
113
114 return result;
115 }
116
117 bool PairingRegistryDelegateLinux::GetRegistryFilePath(base::FilePath* path) {
118 if (!filename_for_testing_.empty()) {
119 *path = filename_for_testing_;
120 return true;
121 }
122
123 base::FilePath home = file_util::GetHomeDir();
124 if (home.empty()) {
125 LOG(ERROR) << "Could not locate user's home directory.";
126 return false;
127 }
128
129 *path = home.Append(kConfigDir).Append(kRegistryFilename);
130 return true;
131 }
132
133 void PairingRegistryDelegateLinux::SetFilenameForTesting(
134 const base::FilePath &filename) {
135 filename_for_testing_ = filename;
136 }
137
138
139 scoped_ptr<PairingRegistry::Delegate> PairingRegistry::CreateDelegate(
140 scoped_refptr<base::TaskRunner> task_runner) {
141 return scoped_ptr<PairingRegistry::Delegate>(
142 new PairingRegistryDelegateLinux(task_runner));
143 }
144
145 } // namespace protocol
146 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698