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_
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"
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
{
39 typedef base::Callback
<void(const std::string
& /*token*/)> TokenCallback
;
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
,
57 const std::string
& token
,
59 net::URLFetcher::RequestType request_type
,
60 net::URLRequestContextGetter
* request_context
,
62 virtual ~PrivetURLFetcher();
64 void DoNotRetryOnTransientError();
66 void AllowEmptyPrivetToken();
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(); }
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_
;
90 net::URLFetcher::RequestType request_type_
;
91 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
94 bool do_not_retry_on_transient_error_
;
95 bool allow_empty_privet_token_
;
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
{
109 explicit PrivetURLFetcherFactory(
110 net::URLRequestContextGetter
* request_context
);
111 ~PrivetURLFetcherFactory();
113 scoped_ptr
<PrivetURLFetcher
> CreateURLFetcher(
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_
; }
122 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
125 DISALLOW_COPY_AND_ASSIGN(PrivetURLFetcherFactory
);
128 } // namespace local_discovery
130 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_URL_FETCHER_H_