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

Unified 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, 7 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 side-by-side diff with in-line comments
Download patch
Index: remoting/host/pairing_registry_delegate_linux.cc
diff --git a/remoting/host/pairing_registry_delegate_linux.cc b/remoting/host/pairing_registry_delegate_linux.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cdeef48bbf35820bf69f02c57736fa816a715ab7
--- /dev/null
+++ b/remoting/host/pairing_registry_delegate_linux.cc
@@ -0,0 +1,146 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "remoting/host/pairing_registry_delegate_linux.h"
+
+#include "base/bind.h"
+#include "base/environment.h"
+#include "base/file_util.h"
+#include "base/json/json_file_value_serializer.h"
+#include "base/location.h"
+#include "base/task_runner.h"
+#include "base/values.h"
+
+namespace {
+const char kConfigDir[] = ".config/chrome-remote-desktop";
+const char kRegistryFilename[] = "paired-clients";
+const char kCreatedTimeLowKey[] = "created-time-low";
+const char kCreatedTimeHighKey[] = "created-time-high";
+const char kClientIdKey[] = "client-id";
+const char kClientNameKey[] = "client-name";
+const char kSharedSecretKey[] = "shared-secret";
+} // namespace
+
+
+namespace remoting {
+namespace protocol {
+
+PairingRegistryDelegateLinux::PairingRegistryDelegateLinux(
+ scoped_refptr<base::TaskRunner> task_runner)
+ : task_runner_(task_runner),
+ weak_factory_(this) {
+}
+
+void PairingRegistryDelegateLinux::Save(
+ const PairingRegistry::PairedClients& paired_clients) {
+ task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&PairingRegistryDelegateLinux::SaveOnCurrentThread,
+ weak_factory_.GetWeakPtr(), paired_clients));
+}
+
+void PairingRegistryDelegateLinux::SaveOnCurrentThread(
+ const PairingRegistry::PairedClients& paired_clients) {
+ base::FilePath registry_path;
+ if (!GetRegistryFilePath(&registry_path)) {
+ return;
+ }
+
+ base::ListValue root;
+ for (PairingRegistry::PairedClients::const_iterator i =
+ paired_clients.begin(); i != paired_clients.end(); ++i) {
+ base::DictionaryValue* pairing = new base::DictionaryValue();
+ int64 created_time = i->second.created_time.ToInternalValue();
+ pairing->SetInteger(kCreatedTimeLowKey, created_time & 0xffffffff);
+ pairing->SetInteger(kCreatedTimeHighKey, created_time >> 32);
+ pairing->SetString(kClientNameKey, i->second.client_name);
+ pairing->SetString(kClientIdKey, i->second.client_id);
+ pairing->SetString(kSharedSecretKey, i->second.shared_secret);
+ root.Append(pairing);
+ }
+
+ JSONFileValueSerializer registry(registry_path);
+ registry.Serialize(root);
+}
+
+PairingRegistry::PairedClients
+PairingRegistryDelegateLinux::LoadOnCurrentThread() {
+ PairingRegistry::PairedClients result;
+ base::FilePath registry_path;
+ if (!GetRegistryFilePath(&registry_path)) {
+ return result;
+ }
+
+ JSONFileValueSerializer registry(registry_path);
+ int error_code;
+ std::string error_message;
+ scoped_ptr<base::Value> root(
+ registry.Deserialize(&error_code, &error_message));
+ if (!root) {
+ LOG(ERROR) << "Failed to load paired clients: " << error_message
+ << " (" << error_code << ").";
+ return result;
+ }
+
+ base::ListValue* root_list = NULL;
+ if (!root->GetAsList(&root_list)) {
+ LOG(ERROR) << "Failed to load paired clients: root node is not a list.";
+ return result;
+ }
+
+ for (size_t i = 0; i < root_list->GetSize(); ++i) {
+ base::DictionaryValue* pairing = NULL;
+ std::string client_id, client_name, shared_secret;
+ int created_time_low, created_time_high;
+ if (root_list->GetDictionary(i, &pairing) &&
+ pairing->GetInteger(kCreatedTimeLowKey, &created_time_low) &&
+ pairing->GetInteger(kCreatedTimeHighKey, &created_time_high) &&
+ pairing->GetString(kClientIdKey, &client_id) &&
+ pairing->GetString(kClientNameKey, &client_name) &&
+ pairing->GetString(kSharedSecretKey, &shared_secret)) {
+ PairingRegistry::Pairing pairing_struct;
+ pairing_struct.created_time = base::Time::FromInternalValue(
+ (static_cast<int64>(created_time_high) << 32) + created_time_low);
+ pairing_struct.client_id = client_id;
+ pairing_struct.client_name = client_name;
+ pairing_struct.shared_secret = shared_secret;
+ result[client_id] = pairing_struct;
+ } else {
+ LOG(ERROR) << "Paired client " << i << " has unexpected format.";
+ }
+ }
+
+ return result;
+}
+
+bool PairingRegistryDelegateLinux::GetRegistryFilePath(base::FilePath* path) {
+ if (!filename_for_testing_.empty()) {
+ *path = filename_for_testing_;
+ return true;
+ }
+
+ base::FilePath home = file_util::GetHomeDir();
+ if (home.empty()) {
+ LOG(ERROR) << "Could not locate user's home directory.";
+ return false;
+ }
+
+ *path = home.Append(kConfigDir).Append(kRegistryFilename);
+ return true;
+}
+
+void PairingRegistryDelegateLinux::SetFilenameForTesting(
+ const base::FilePath &filename) {
+ filename_for_testing_ = filename;
+}
+
+
+scoped_ptr<PairingRegistry::Delegate> PairingRegistry::CreateDelegate(
+ scoped_refptr<base::TaskRunner> task_runner) {
+ return scoped_ptr<PairingRegistry::Delegate>(
+ new PairingRegistryDelegateLinux(task_runner));
+}
+
+} // namespace protocol
+} // namespace remoting

Powered by Google App Engine
This is Rietveld 408576698