privet_url_fetcher.h 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#ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_
7
8#include <string>
9
10#include "base/values.h"
11#include "net/url_request/url_fetcher.h"
12#include "net/url_request/url_fetcher_delegate.h"
13#include "net/url_request/url_request_context_getter.h"
14
15namespace local_discovery {
16
17const int kPrivetHTTPCodeInternalFailure = -1;
18
19// Privet-specific URLFetcher adapter. Currently supports only the subset
20// of HTTP features required by Privet for GCP 1.5
21// (/privet/info and /privet/register).
22class PrivetURLFetcher : public net::URLFetcherDelegate {
23 public:
24  enum ErrorType {
25    JSON_PARSE_ERROR,
26    URL_FETCH_ERROR,
27    RESPONSE_CODE_ERROR
28  };
29
30  class Delegate {
31   public:
32    virtual ~Delegate() {}
33
34    virtual void OnError(PrivetURLFetcher* fetcher, ErrorType error) = 0;
35    virtual void OnParsedJson(PrivetURLFetcher* fetcher,
36                              const base::DictionaryValue* value,
37                              bool has_error) = 0;
38  };
39
40  PrivetURLFetcher(
41      const std::string& token,
42      const GURL& url,
43      net::URLFetcher::RequestType request_type,
44      net::URLRequestContextGetter* request_context,
45      Delegate* delegate);
46  virtual ~PrivetURLFetcher();
47
48  void Start();
49
50  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
51
52  const GURL& url() const { return url_fetcher_->GetOriginalURL(); }
53  int response_code() const { return url_fetcher_->GetResponseCode(); }
54
55 private:
56  scoped_ptr<net::URLFetcher> url_fetcher_;
57  Delegate* delegate_;
58
59  DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcher);
60};
61
62class PrivetURLFetcherFactory {
63 public:
64  explicit PrivetURLFetcherFactory(
65      net::URLRequestContextGetter* request_context);
66  ~PrivetURLFetcherFactory();
67
68  scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
69      const GURL& url, net::URLFetcher::RequestType request_type,
70      PrivetURLFetcher::Delegate* delegate) const;
71
72  void set_token(const std::string& token) { token_ = token; }
73  const std::string& get_token() const { return token_; }
74
75 private:
76  scoped_refptr<net::URLRequestContextGetter> request_context_;
77  std::string token_;
78
79  DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcherFactory);
80};
81
82}  // namespace local_discovery
83
84#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_
85