• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/file_util.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/values.h"
13 #include "net/url_request/url_fetcher.h"
14 #include "net/url_request/url_fetcher_delegate.h"
15 #include "net/url_request/url_request_context_getter.h"
16 #include "url/gurl.h"
17 
18 namespace base {
19 class FilePath;
20 }
21 
22 namespace local_discovery {
23 
24 const int kPrivetHTTPCodeInternalFailure = -1;
25 
26 // Privet-specific URLFetcher adapter. Currently supports only the subset
27 // of HTTP features required by Privet for GCP 1.5
28 // (/privet/info and /privet/register).
29 class PrivetURLFetcher : public net::URLFetcherDelegate {
30  public:
31   enum ErrorType {
32     JSON_PARSE_ERROR,
33     URL_FETCH_ERROR,
34     RESPONSE_CODE_ERROR,
35     RETRY_ERROR,
36     TOKEN_ERROR
37   };
38 
39   typedef base::Callback<void(const std::string& /*token*/)> TokenCallback;
40 
41   class Delegate {
42    public:
~Delegate()43     virtual ~Delegate() {}
44 
45     // If you do not implement this method, you will always get a
46     // TOKEN_ERROR error when your token is invalid.
47     virtual void OnNeedPrivetToken(
48         PrivetURLFetcher* fetcher,
49         const TokenCallback& callback);
50     virtual void OnError(PrivetURLFetcher* fetcher, ErrorType error) = 0;
51     virtual void OnParsedJson(PrivetURLFetcher* fetcher,
52                               const base::DictionaryValue* value,
53                               bool has_error) = 0;
54   };
55 
56   PrivetURLFetcher(
57       const std::string& token,
58       const GURL& url,
59       net::URLFetcher::RequestType request_type,
60       net::URLRequestContextGetter* request_context,
61       Delegate* delegate);
62   virtual ~PrivetURLFetcher();
63 
64   void DoNotRetryOnTransientError();
65 
66   void AllowEmptyPrivetToken();
67 
68   void Start();
69 
70   void SetUploadData(const std::string& upload_content_type,
71                      const std::string& upload_data);
72 
73   void SetUploadFilePath(const std::string& upload_content_type,
74                          const base::FilePath& upload_file_path);
75 
76   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
77 
url()78   const GURL& url() const { return url_fetcher_->GetOriginalURL(); }
response_code()79   int response_code() const { return url_fetcher_->GetResponseCode(); }
80 
81  private:
82   void Try();
83   void ScheduleRetry(int timeout_seconds);
84   bool PrivetErrorTransient(const std::string& error);
85   void RequestTokenRefresh();
86   void RefreshToken(const std::string& token);
87 
88   std::string privet_access_token_;
89   GURL url_;
90   net::URLFetcher::RequestType request_type_;
91   scoped_refptr<net::URLRequestContextGetter> request_context_;
92   Delegate* delegate_;
93 
94   bool do_not_retry_on_transient_error_;
95   bool allow_empty_privet_token_;
96 
97   int tries_;
98   std::string upload_data_;
99   std::string upload_content_type_;
100   base::FilePath upload_file_path_;
101   scoped_ptr<net::URLFetcher> url_fetcher_;
102 
103   base::WeakPtrFactory<PrivetURLFetcher> weak_factory_;
104   DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcher);
105 };
106 
107 class PrivetURLFetcherFactory {
108  public:
109   explicit PrivetURLFetcherFactory(
110       net::URLRequestContextGetter* request_context);
111   ~PrivetURLFetcherFactory();
112 
113   scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
114       const GURL& url,
115       net::URLFetcher::RequestType request_type,
116       PrivetURLFetcher::Delegate* delegate) const;
117 
set_token(const std::string & token)118   void set_token(const std::string& token) { token_ = token; }
get_token()119   const std::string& get_token() const { return token_; }
120 
121  private:
122   scoped_refptr<net::URLRequestContextGetter> request_context_;
123   std::string token_;
124 
125   DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcherFactory);
126 };
127 
128 }  // namespace local_discovery
129 
130 #endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_
131