Home | History | Annotate | Download | only in local_discovery
      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 
     15 namespace local_discovery {
     16 
     17 const 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).
     22 class 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   std::string privet_access_token_;
     58   Delegate* delegate_;
     59 
     60   DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcher);
     61 };
     62 
     63 class PrivetURLFetcherFactory {
     64  public:
     65   explicit PrivetURLFetcherFactory(
     66       net::URLRequestContextGetter* request_context);
     67   ~PrivetURLFetcherFactory();
     68 
     69   scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
     70       const GURL& url, net::URLFetcher::RequestType request_type,
     71       PrivetURLFetcher::Delegate* delegate) const;
     72 
     73   void set_token(const std::string& token) { token_ = token; }
     74   const std::string& get_token() const { return token_; }
     75 
     76  private:
     77   scoped_refptr<net::URLRequestContextGetter> request_context_;
     78   std::string token_;
     79 
     80   DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcherFactory);
     81 };
     82 
     83 }  // namespace local_discovery
     84 
     85 #endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_
     86