privet_url_fetcher.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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
32PrivetURLFetcher::~PrivetURLFetcher() {
33}
34
35void PrivetURLFetcher::Start() {
36  url_fetcher_->Start();
37}
38
39void PrivetURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
40  if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS) {
41    delegate_->OnError(this, URL_FETCH_ERROR);
42    return;
43  }
44
45  if (source->GetResponseCode() != net::HTTP_OK) {
46    delegate_->OnError(this, RESPONSE_CODE_ERROR);
47    return;
48  }
49
50  std::string response_str;
51
52  if (!source->GetResponseAsString(&response_str)) {
53    delegate_->OnError(this, URL_FETCH_ERROR);
54    return;
55  }
56
57  base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS);
58  scoped_ptr<base::Value> value;
59
60  value.reset(json_reader.ReadToValue(response_str));
61
62  if (!value) {
63    delegate_->OnError(this, JSON_PARSE_ERROR);
64    return;
65  }
66
67  const base::DictionaryValue* dictionary_value;
68
69  if (!value->GetAsDictionary(&dictionary_value)) {
70    delegate_->OnError(this, JSON_PARSE_ERROR);
71    return;
72  }
73
74  delegate_->OnParsedJson(this, dictionary_value,
75                          dictionary_value->HasKey(kPrivetKeyError));
76}
77
78PrivetURLFetcherFactory::PrivetURLFetcherFactory(
79    net::URLRequestContextGetter* request_context)
80    : request_context_(request_context) {
81}
82
83PrivetURLFetcherFactory::~PrivetURLFetcherFactory() {
84}
85
86scoped_ptr<PrivetURLFetcher> PrivetURLFetcherFactory::CreateURLFetcher(
87    const GURL& url, net::URLFetcher::RequestType request_type,
88    PrivetURLFetcher::Delegate* delegate) const {
89  return scoped_ptr<PrivetURLFetcher>(
90      new PrivetURLFetcher(token_, url, request_type,
91                           request_context_.get(), delegate));
92}
93
94}  // namespace local_discovery
95