summaryrefslogtreecommitdiffstats
path: root/remoting/host/pairing_registry_delegate_linux.cc
blob: 8bd25de77f6a35d65cb420363dd7de7feb5aa10c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 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/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/files/important_file_writer.h"
#include "base/json/json_file_value_serializer.h"
#include "base/json/json_string_value_serializer.h"
#include "base/location.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "remoting/host/branding.h"

namespace {

// The pairing registry path relative to the configuration directory.
const char kRegistryDirectory[] = "paired-clients";

const char kPairingFilenameFormat[] = "%s.json";
const char kPairingFilenamePattern[] = "*.json";

}  // namespace

namespace remoting {

using protocol::PairingRegistry;

PairingRegistryDelegateLinux::PairingRegistryDelegateLinux() {
}

PairingRegistryDelegateLinux::~PairingRegistryDelegateLinux() {
}

scoped_ptr<base::ListValue> PairingRegistryDelegateLinux::LoadAll() {
  scoped_ptr<base::ListValue> pairings(new base::ListValue());

  // Enumerate all pairing files in the pairing registry.
  base::FilePath registry_path = GetRegistryPath();
  base::FileEnumerator enumerator(registry_path, false,
                                  base::FileEnumerator::FILES,
                                  kPairingFilenamePattern);
  for (base::FilePath pairing_file = enumerator.Next(); !pairing_file.empty();
       pairing_file = enumerator.Next()) {
    // Read the JSON containing pairing data.
    JSONFileValueDeserializer deserializer(pairing_file);
    int error_code;
    std::string error_message;
    scoped_ptr<base::Value> pairing_json =
        deserializer.Deserialize(&error_code, &error_message);
    if (!pairing_json) {
      LOG(WARNING) << "Failed to load '" << pairing_file.value() << "' ("
                   << error_code << ").";
      continue;
    }

    pairings->Append(pairing_json.release());
  }

  return pairings;
}

bool PairingRegistryDelegateLinux::DeleteAll() {
  // Delete all pairing files in the pairing registry.
  base::FilePath registry_path = GetRegistryPath();
  base::FileEnumerator enumerator(registry_path, false,
                                  base::FileEnumerator::FILES,
                                  kPairingFilenamePattern);

  bool success = true;
  for (base::FilePath pairing_file = enumerator.Next(); !pairing_file.empty();
       pairing_file = enumerator.Next()) {
    success = success && base::DeleteFile(pairing_file, false);
  }

  return success;
}

PairingRegistry::Pairing PairingRegistryDelegateLinux::Load(
    const std::string& client_id) {
  base::FilePath registry_path = GetRegistryPath();
  base::FilePath pairing_file = registry_path.Append(
      base::StringPrintf(kPairingFilenameFormat, client_id.c_str()));

  JSONFileValueDeserializer deserializer(pairing_file);
  int error_code;
  std::string error_message;
  scoped_ptr<base::Value> pairing =
      deserializer.Deserialize(&error_code, &error_message);
  if (!pairing) {
    LOG(WARNING) << "Failed to load pairing information: " << error_message
                 << " (" << error_code << ").";
    return PairingRegistry::Pairing();
  }

  base::DictionaryValue* pairing_dictionary;
  if (!pairing->GetAsDictionary(&pairing_dictionary)) {
    LOG(WARNING) << "Failed to parse pairing information: not a dictionary.";
    return PairingRegistry::Pairing();
  }

  return PairingRegistry::Pairing::CreateFromValue(*pairing_dictionary);
}

bool PairingRegistryDelegateLinux::Save(
    const PairingRegistry::Pairing& pairing) {
  base::FilePath registry_path = GetRegistryPath();
  base::File::Error error;
  if (!base::CreateDirectoryAndGetError(registry_path, &error)) {
    LOG(ERROR) << "Could not create pairing registry directory: " << error;
    return false;
  }

  std::string pairing_json;
  JSONStringValueSerializer serializer(&pairing_json);
  if (!serializer.Serialize(*pairing.ToValue())) {
    LOG(ERROR) << "Failed to serialize pairing data for "
               << pairing.client_id();
    return false;
  }

  base::FilePath pairing_file = registry_path.Append(
      base::StringPrintf(kPairingFilenameFormat, pairing.client_id().c_str()));
  if (!base::ImportantFileWriter::WriteFileAtomically(pairing_file,
                                                      pairing_json)) {
    LOG(ERROR) << "Could not save pairing data for " << pairing.client_id();
    return false;
  }

  return true;
}

bool PairingRegistryDelegateLinux::Delete(const std::string& client_id) {
  base::FilePath registry_path = GetRegistryPath();
  base::FilePath pairing_file = registry_path.Append(
      base::StringPrintf(kPairingFilenameFormat, client_id.c_str()));

  return base::DeleteFile(pairing_file, false);
}

base::FilePath PairingRegistryDelegateLinux::GetRegistryPath() {
  if (!registry_path_for_testing_.empty()) {
    return registry_path_for_testing_;
  }

  base::FilePath config_dir = remoting::GetConfigDir();
  return config_dir.Append(kRegistryDirectory);
}

void PairingRegistryDelegateLinux::SetRegistryPathForTesting(
    const base::FilePath& registry_path) {
  registry_path_for_testing_ = registry_path;
}


scoped_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate() {
  return make_scoped_ptr(new PairingRegistryDelegateLinux());
}

}  // namespace remoting