Chromium Code Reviews| 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"; |
|
Sergey Ulanov
2013/05/31 18:45:46
There is remoting::GetConfigDir() that returns pat
Jamie
2013/06/19 17:56:44
Done.
|
| +const char kRegistryFilename[] = "paired-clients"; |
|
Sergey Ulanov
2013/05/31 18:45:46
Maybe paired-clients.json?
For host config we add
Jamie
2013/06/19 17:56:44
Done.
|
| +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( |
|
Sergey Ulanov
2013/05/31 18:45:46
Why is it "OnCurrentThread"? Should it be somethin
Jamie
2013/06/19 17:56:44
The new interface has different functions, but I'v
|
| + const PairingRegistry::PairedClients& paired_clients) { |
| + base::FilePath registry_path; |
| + if (!GetRegistryFilePath(®istry_path)) { |
| + return; |
| + } |
| + |
| + base::ListValue root; |
| + for (PairingRegistry::PairedClients::const_iterator i = |
|
Sergey Ulanov
2013/05/31 18:45:46
It looks to me that most of the code in this class
Lambros
2013/05/31 18:51:59
This serialization code looks like it would be the
Jamie
2013/06/19 17:56:44
I was concerned about permissions. For example, on
Jamie
2013/06/19 17:56:44
I don't think all platforms will use JSON. On Wind
Sergey Ulanov
2013/06/19 18:23:50
What are the benefits of using registry instead of
|
| + 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); |
|
Sergey Ulanov
2013/05/31 18:45:46
It's better to use base::ImportantFileWriter - it
Jamie
2013/06/19 17:56:44
Done.
|
| + registry.Serialize(root); |
|
Lambros
2013/05/31 18:51:59
I don't think JSONFileValueSerializer creates the
Jamie
2013/06/19 17:56:44
Done.
|
| +} |
| + |
| +PairingRegistry::PairedClients |
| +PairingRegistryDelegateLinux::LoadOnCurrentThread() { |
| + PairingRegistry::PairedClients result; |
| + base::FilePath registry_path; |
| + if (!GetRegistryFilePath(®istry_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); |
|
Sergey Ulanov
2013/05/31 18:45:46
I'm not sure this will always work correctly. Part
Lambros
2013/05/31 19:55:26
Ah yes, I missed this! I think created_time_low/hi
Jamie
2013/06/19 17:56:44
Done.
Jamie
2013/06/19 17:56:44
I've followed Lambros's suggestion of using string
|
| + 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 |