Chromium Code Reviews

Side by Side Diff: chrome/service/remoting/remoting_directory_service.cc

Issue 6036001: Move directory code from chrome/service to chrome/browser. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
(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/net/url_request_context_getter.h"
11 #include "chrome/service/net/service_url_request_context.h"
12 #include "chrome/service/remoting/remoting_directory_service.h"
13 #include "net/base/net_util.h"
14 #include "net/http/http_request_headers.h"
15 #include "remoting/host/host_key_pair.h"
16
17 static const char kRemotingDirectoryUrl[] =
18 "https://www.googleapis.com/chromoting/v1/@me/hosts";
19
20 RemotingDirectoryService::RemotingDirectoryService(Client* client)
21 : client_(client) {
22 }
23
24 RemotingDirectoryService::~RemotingDirectoryService() {
25 DCHECK(!fetcher_.get()) << "URLFetcher not destroyed.";
26 }
27
28 void RemotingDirectoryService::AddHost(const std::string& token) {
29 // TODO(hclam): This is a time consuming operation so we should run it on
30 // a separate thread.
31 host_key_pair_.reset(new remoting::HostKeyPair());
32 host_key_pair_->Generate();
33
34 // Use the host address as ID and host name.
35 std::string hostname = net::GetHostName();
36 host_id_ = hostname;
37 host_name_ = hostname;
38
39 // Prepare the parameters for the request.
40 DictionaryValue data;
41 data.SetString("hostId", hostname);
42 data.SetString("hostName", hostname);
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 }
OLDNEW

Powered by Google App Engine