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