Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / local_discovery / privet_url_fetcher.h
blobf3b7405378d0cc73385b7162db9ac81ef0ef5479
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.
5 #ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_
8 #include <string>
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"
18 namespace base {
19 class FilePath;
22 namespace local_discovery {
24 const int kPrivetHTTPCodeInternalFailure = -1;
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
39 typedef base::Callback<void(const std::string& /*token*/)> TokenCallback;
41 class Delegate {
42 public:
43 virtual ~Delegate() {}
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;
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();
64 void DoNotRetryOnTransientError();
66 void AllowEmptyPrivetToken();
68 void Start();
70 void SetUploadData(const std::string& upload_content_type,
71 const std::string& upload_data);
73 void SetUploadFilePath(const std::string& upload_content_type,
74 const base::FilePath& upload_file_path);
76 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
78 const GURL& url() const { return url_fetcher_->GetOriginalURL(); }
79 int response_code() const { return url_fetcher_->GetResponseCode(); }
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);
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_;
94 bool do_not_retry_on_transient_error_;
95 bool allow_empty_privet_token_;
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_;
103 base::WeakPtrFactory<PrivetURLFetcher> weak_factory_;
104 DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcher);
107 class PrivetURLFetcherFactory {
108 public:
109 explicit PrivetURLFetcherFactory(
110 net::URLRequestContextGetter* request_context);
111 ~PrivetURLFetcherFactory();
113 scoped_ptr<PrivetURLFetcher> CreateURLFetcher(
114 const GURL& url,
115 net::URLFetcher::RequestType request_type,
116 PrivetURLFetcher::Delegate* delegate) const;
118 void set_token(const std::string& token) { token_ = token; }
119 const std::string& get_token() const { return token_; }
121 private:
122 scoped_refptr<net::URLRequestContextGetter> request_context_;
123 std::string token_;
125 DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcherFactory);
128 } // namespace local_discovery
130 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_