OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 <vector> | |
6 | |
7 #include "base/json/json_reader.h" | |
8 #include "base/json/json_writer.h" | |
9 #include "base/values.h" | |
10 #include "chrome/common/guid.h" | |
11 #include "chrome/common/net/url_request_context_getter.h" | |
12 #include "chrome/service/net/service_url_request_context.h" | |
13 #include "chrome/service/remoting/remoting_directory_service.h" | |
14 #include "net/base/net_util.h" | |
15 #include "net/http/http_request_headers.h" | |
16 #include "remoting/host/host_key_pair.h" | |
17 | |
18 static const char kRemotingDirectoryUrl[] = | |
19 "https://www.googleapis.com/chromoting/v1/@me/hosts"; | |
20 | |
21 RemotingDirectoryService::RemotingDirectoryService(Client* client) | |
22 : client_(client) { | |
23 } | |
24 | |
25 RemotingDirectoryService::~RemotingDirectoryService() { | |
26 DCHECK(!fetcher_.get()) << "URLFetcher not destroyed."; | |
27 } | |
28 | |
29 void RemotingDirectoryService::AddHost(const std::string& token) { | |
30 // TODO(hclam): This is a time consuming operation so we should run it on | |
31 // a separate thread. | |
32 host_key_pair_.reset(new remoting::HostKeyPair()); | |
33 host_key_pair_->Generate(); | |
34 | |
35 // Get a host name and generate a UUID for the request. | |
36 host_id_ = guid::GenerateGUID(); | |
37 host_name_ = net::GetHostName(); | |
38 | |
39 // Prepare the parameters for the request. | |
40 DictionaryValue data; | |
41 data.SetString("hostId", host_id_); | |
42 data.SetString("hostName", host_name_); | |
43 data.SetString("publicKey", host_key_pair_->GetPublicKey()); | |
44 | |
45 // Generate the final json query. | |
46 DictionaryValue args; | |
47 args.Set("data", data.DeepCopy()); | |
48 std::string request_content; | |
49 base::JSONWriter::Write(&args, false, &request_content); | |
50 | |
51 // Prepare the HTTP header for authentication. | |
52 net::HttpRequestHeaders headers; | |
53 headers.SetHeader("Authorization", "GoogleLogin auth=" + token); | |
54 fetcher_.reset( | |
55 new URLFetcher(GURL(kRemotingDirectoryUrl), URLFetcher::POST, this)); | |
56 fetcher_->set_request_context(new ServiceURLRequestContextGetter()); | |
57 fetcher_->set_upload_data("application/json", request_content); | |
58 fetcher_->set_extra_request_headers(headers.ToString()); | |
59 | |
60 // And then start the request. | |
61 fetcher_->Start(); | |
62 } | |
63 | |
64 void RemotingDirectoryService::CancelRequest() { | |
65 fetcher_.reset(); | |
66 } | |
67 | |
68 void RemotingDirectoryService::OnURLFetchComplete( | |
69 const URLFetcher* source, | |
70 const GURL& url, | |
71 const URLRequestStatus& status, | |
72 int response_code, | |
73 const ResponseCookies& cookies, | |
74 const std::string& data) { | |
75 // Destroy the fetcher after the response has been received. | |
76 fetcher_.reset(); | |
77 | |
78 // TODO(hclam): Simply checking 200 status is not enough. | |
79 if (response_code == 200) { | |
80 client_->OnRemotingHostAdded(); | |
81 } else { | |
82 client_->OnRemotingDirectoryError(); | |
83 } | |
84 } | |
OLD | NEW |