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
|
// Copyright (c) 2010 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 <vector>
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/values.h"
#include "chrome/common/net/url_request_context_getter.h"
#include "chrome/service/net/service_url_request_context.h"
#include "chrome/service/remoting/remoting_directory_service.h"
#include "net/base/net_util.h"
#include "net/http/http_request_headers.h"
#include "remoting/host/host_key_pair.h"
static const char kRemotingDirectoryUrl[] =
"http://www-googleapis-test.sandbox.google.com/chromoting/v1/@me/hosts";
RemotingDirectoryService::RemotingDirectoryService(Client* client)
: client_(client) {
}
RemotingDirectoryService::~RemotingDirectoryService() {
DCHECK(!fetcher_.get()) << "URLFetcher not destroyed.";
}
void RemotingDirectoryService::AddHost(const std::string& token) {
// TODO(hclam): This is a time consuming operation so we should run it on
// a separate thread.
host_key_pair_.reset(new remoting::HostKeyPair());
host_key_pair_->Generate();
// Use the host address as ID and host name.
std::string hostname = net::GetHostName();
host_id_ = hostname;
host_name_ = hostname;
// Prepare the parameters for the request.
DictionaryValue data;
data.SetString("hostId", hostname);
data.SetString("hostName", hostname);
data.SetString("publicKey", host_key_pair_->GetPublicKey());
// Generate the final json query.
DictionaryValue args;
args.Set("data", data.DeepCopy());
std::string request_content;
base::JSONWriter::Write(&args, false, &request_content);
// Prepare the HTTP header for authentication.
net::HttpRequestHeaders headers;
headers.SetHeader("Authorization", "GoogleLogin auth=" + token);
fetcher_.reset(
new URLFetcher(GURL(kRemotingDirectoryUrl), URLFetcher::POST, this));
fetcher_->set_request_context(new ServiceURLRequestContextGetter());
fetcher_->set_upload_data("application/json", request_content);
fetcher_->set_extra_request_headers(headers.ToString());
// And then start the request.
fetcher_->Start();
}
void RemotingDirectoryService::CancelRequest() {
fetcher_.reset();
}
void RemotingDirectoryService::OnURLFetchComplete(
const URLFetcher* source,
const GURL& url,
const URLRequestStatus& status,
int response_code,
const ResponseCookies& cookies,
const std::string& data) {
// Destroy the fetcher after the response has been received.
fetcher_.reset();
// TODO(hclam): Simply checking 200 status is not enough.
if (response_code == 200) {
client_->OnRemotingHostAdded();
} else {
client_->OnRemotingDirectoryError();
}
}
|