privet_url_fetcher.cc revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
1// Copyright 2013 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 "chrome/browser/local_discovery/privet_url_fetcher.h"
6
7#include "base/json/json_reader.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/local_discovery/privet_constants.h"
10#include "net/http/http_status_code.h"
11#include "net/url_request/url_request_status.h"
12
13namespace local_discovery {
14
15namespace {
16const char kXPrivetTokenHeaderPrefix[] = "X-Privet-Token: ";
17}
18
19PrivetURLFetcher::PrivetURLFetcher(
20    const std::string& token,
21    const GURL& url,
22    net::URLFetcher::RequestType request_type,
23    net::URLRequestContextGetter* request_context,
24    PrivetURLFetcher::Delegate* delegate)
25    : privet_access_token_(token), delegate_(delegate) {
26  url_fetcher_.reset(net::URLFetcher::Create(url, request_type, this));
27  url_fetcher_->SetRequestContext(request_context);
28  url_fetcher_->AddExtraRequestHeader(std::string(kXPrivetTokenHeaderPrefix) +
29                                      token);
30
31  // URLFetcher requires us to set upload data for POST requests.
32  if (request_type == net::URLFetcher::POST)
33      url_fetcher_->SetUploadData(std::string(), std::string());
34}
35
36PrivetURLFetcher::~PrivetURLFetcher() {
37}
38
39void PrivetURLFetcher::Start() {
40  url_fetcher_->Start();
41}
42
43void PrivetURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
44  if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS) {
45    delegate_->OnError(this, URL_FETCH_ERROR);
46    return;
47  }
48
49  if (source->GetResponseCode() != net::HTTP_OK) {
50    delegate_->OnError(this, RESPONSE_CODE_ERROR);
51    return;
52  }
53
54  std::string response_str;
55
56  if (!source->GetResponseAsString(&response_str)) {
57    delegate_->OnError(this, URL_FETCH_ERROR);
58    return;
59  }
60
61  base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS);
62  scoped_ptr<base::Value> value;
63
64  value.reset(json_reader.ReadToValue(response_str));
65
66  if (!value) {
67    delegate_->OnError(this, JSON_PARSE_ERROR);
68    return;
69  }
70
71  const base::DictionaryValue* dictionary_value;
72
73  if (!value->GetAsDictionary(&dictionary_value)) {
74    delegate_->OnError(this, JSON_PARSE_ERROR);
75    return;
76  }
77
78  delegate_->OnParsedJson(this, dictionary_value,
79                          dictionary_value->HasKey(kPrivetKeyError));
80}
81
82PrivetURLFetcherFactory::PrivetURLFetcherFactory(
83    net::URLRequestContextGetter* request_context)
84    : request_context_(request_context) {
85}
86
87PrivetURLFetcherFactory::~PrivetURLFetcherFactory() {
88}
89
90scoped_ptr<PrivetURLFetcher> PrivetURLFetcherFactory::CreateURLFetcher(
91    const GURL& url, net::URLFetcher::RequestType request_type,
92    PrivetURLFetcher::Delegate* delegate) const {
93  return scoped_ptr<PrivetURLFetcher>(
94      new PrivetURLFetcher(token_, url, request_type,
95                           request_context_.get(), delegate));
96}
97
98}  // namespace local_discovery
99