summaryrefslogtreecommitdiffstats
path: root/sync/test/accounts_client/test_accounts_client.cc
blob: 8257691cdaeea6d6c9db38a38a76d86176370ba4 (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
// 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 "sync/test/accounts_client/test_accounts_client.h"

#include <algorithm>
#include <string>
#include <vector>

#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/message_loop.h"
#include "base/run_loop.h"
#include "base/stringprintf.h"
#include "base/time.h"
#include "base/values.h"
#include "net/base/url_util.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context_getter.h"
#include "sync/test/accounts_client/url_request_context_getter.h"

using std::string;
using std::vector;

static const int kMaxSessionLifetimeSeconds = 30 * 60;
static const string kClaimPath = "claim";
static const string kReleasePath = "release";
static const base::TimeDelta kRequestTimeout = base::TimeDelta::FromSeconds(10);

AccountSession::AccountSession() {}
AccountSession::~AccountSession() {}

class AccountsRequestDelegate : public net::URLFetcherDelegate {
 public:
  AccountsRequestDelegate(base::RunLoop* run_loop) : response_(""),
      success_(false), run_loop_(run_loop) {}

  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE {
    string url = source->GetURL().spec();
    source->GetResponseAsString(&response_);

    if (!source->GetStatus().is_success()) {
      int error = source->GetStatus().error();
      DVLOG(0) << "The request failed with error code " << error << "."
               << "\nRequested URL: " << url << ".";
    } else if (source->GetResponseCode() != net::HTTP_OK) {
      DVLOG(0) << "The request failed with response code "
               << source->GetResponseCode() << "."
               << "\nRequested URL: " << url
               << "\nResponse body: \"" << response_ << "\"";
    } else {
      success_ = true;
    }

    run_loop_->Quit();
  }

  string response() const { return response_; }
  bool success() const { return success_; }

 private:
  string response_;
  bool success_;
  base::RunLoop* run_loop_;
};

TestAccountsClient::TestAccountsClient(const string& server,
                                       const string& account_space,
                                       const vector<string>& usernames)
    : server_(server), account_space_(account_space), usernames_(usernames) {
}

TestAccountsClient::~TestAccountsClient() {}

bool TestAccountsClient::ClaimAccount(AccountSession* session) {
  GURL url = CreateGURLWithPath(kClaimPath);
  url = net::AppendQueryParameter(url, "account_space", account_space_);
  string max_lifetime_seconds = base::StringPrintf("%d",
                                                   kMaxSessionLifetimeSeconds);
  url = net::AppendQueryParameter(url, "max_lifetime_seconds",
                                  max_lifetime_seconds);

  // TODO(pvalenzuela): Select N random usernames instead of all usernames.
  for (vector<string>::iterator it = usernames_.begin();
       it != usernames_.end(); ++it) {
    url = net::AppendQueryParameter(url, "username", *it);
  }

  string response;
  if (!SendRequest(url, &response)) {
    return false;
  }

  scoped_ptr<Value> value(base::JSONReader::Read(response));
  base::DictionaryValue* dict_value;
  if (value != NULL && value->GetAsDictionary(&dict_value) &&
      dict_value != NULL) {
    dict_value->GetString("username", &session->username);
    dict_value->GetString("account_space", &session->account_space);
    dict_value->GetString("session_id", &session->session_id);
    dict_value->GetString("expiration_time", &session->expiration_time);
  } else {
    return false;
  }

  return true;
}

void TestAccountsClient::ReleaseAccount(const AccountSession& session) {
  // The expiration_time field is ignored since it isn't passed as part of the
  // release request.
  if (session.username.empty() || session.account_space.empty() ||
      account_space_.compare(session.account_space) != 0 ||
      session.session_id.empty()) {
    return;
  }

  GURL url = CreateGURLWithPath(kReleasePath);
  url = net::AppendQueryParameter(url, "account_space", session.account_space);
  url = net::AppendQueryParameter(url, "username", session.username);
  url = net::AppendQueryParameter(url, "session_id", session.session_id);

  // This operation is best effort, so don't send any errors back to the caller.
  string response;
  SendRequest(url, &response);
}

GURL TestAccountsClient::CreateGURLWithPath(const string& path) {
  return GURL(base::StringPrintf("%s/%s", server_.c_str(), path.c_str()));
}


bool TestAccountsClient::SendRequest(const GURL& url, string* response) {
  base::MessageLoop* loop = base::MessageLoop::current();
  scoped_refptr<URLRequestContextGetter> context_getter(
      new URLRequestContextGetter(loop->message_loop_proxy()));

  base::RunLoop run_loop;

  AccountsRequestDelegate delegate(&run_loop);
  scoped_ptr<net::URLFetcher> fetcher(net::URLFetcher::Create(
          url, net::URLFetcher::POST, &delegate));
  fetcher->SetRequestContext(context_getter.get());
  fetcher->SetUploadData("application/json", "");
  fetcher->Start();

  base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
    run_loop.QuitClosure(),
    kRequestTimeout);
  run_loop.Run();

  if (delegate.success()) {
    *response = delegate.response();
  }

  return delegate.success();
}