tonychun | e3fe984 | 2015-07-10 18:45:30 | [diff] [blame] | 1 | // Copyright 2015 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 REMOTING_TEST_HOST_LIST_FETCHER_H_ |
| 6 | #define REMOTING_TEST_HOST_LIST_FETCHER_H_ |
| 7 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 8 | #include <memory> |
tonychun | e3fe984 | 2015-07-10 18:45:30 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/callback.h" |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 13 | #include "base/macros.h" |
tonychun | e3fe984 | 2015-07-10 18:45:30 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
tonychun | e3fe984 | 2015-07-10 18:45:30 | [diff] [blame] | 15 | #include "net/url_request/url_fetcher_delegate.h" |
| 16 | #include "remoting/test/host_info.h" |
| 17 | |
tonychun | e3fe984 | 2015-07-10 18:45:30 | [diff] [blame] | 18 | namespace remoting { |
| 19 | class URLRequestContextGetter; |
| 20 | } |
| 21 | |
| 22 | namespace remoting { |
| 23 | namespace test { |
| 24 | |
| 25 | // Used by the HostlistFetcher to make HTTP requests and also by the |
| 26 | // unittests for this class to set fake response data for these URLs. |
| 27 | const char kHostListProdRequestUrl[] = "https://www.googleapis.com/" |
| 28 | "chromoting/v1/@me/hosts"; |
joedow | 910989d | 2016-12-02 21:15:32 | [diff] [blame] | 29 | const char kHostListTestRequestUrl[] = |
| 30 | "https://www-googleapis-test.sandbox.google.com/chromoting/v1/@me/hosts"; |
tonychun | e3fe984 | 2015-07-10 18:45:30 | [diff] [blame] | 31 | |
| 32 | // Requests a host list from the directory service for an access token. |
| 33 | // Destroying the RemoteHostInfoFetcher while a request is outstanding |
| 34 | // will cancel the request. It is safe to delete the fetcher from within a |
| 35 | // completion callback. Must be used from a thread running a message loop. |
| 36 | // The public method is virtual to allow for mocking and fakes. |
| 37 | class HostListFetcher : public net::URLFetcherDelegate { |
| 38 | public: |
| 39 | HostListFetcher(); |
| 40 | ~HostListFetcher() override; |
| 41 | |
| 42 | // Supplied by the client for each hostlist request and returns a valid, |
| 43 | // initialized Hostlist object on success. |
| 44 | typedef base::Callback<void(const std::vector<HostInfo>& hostlist)> |
| 45 | HostlistCallback; |
| 46 | |
| 47 | // Makes a service call to retrieve a hostlist. The |
| 48 | // callback will be called once the HTTP request has completed. |
| 49 | virtual void RetrieveHostlist(const std::string& access_token, |
joedow | 910989d | 2016-12-02 21:15:32 | [diff] [blame] | 50 | const std::string& target_url, |
tonychun | e3fe984 | 2015-07-10 18:45:30 | [diff] [blame] | 51 | const HostlistCallback& callback); |
| 52 | |
| 53 | private: |
| 54 | // Processes the response from the directory service. |
| 55 | bool ProcessResponse(std::vector<HostInfo>* hostlist); |
| 56 | |
| 57 | // net::URLFetcherDelegate interface. |
| 58 | void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 59 | |
| 60 | // Holds the URLFetcher for the Host List request. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 61 | std::unique_ptr<net::URLFetcher> request_; |
tonychun | e3fe984 | 2015-07-10 18:45:30 | [diff] [blame] | 62 | |
| 63 | // Provides application-specific context for the network request. |
| 64 | scoped_refptr<remoting::URLRequestContextGetter> request_context_getter_; |
| 65 | |
| 66 | // Caller-supplied callback used to return hostlist on success. |
| 67 | HostlistCallback hostlist_callback_; |
| 68 | |
| 69 | DISALLOW_COPY_AND_ASSIGN(HostListFetcher); |
| 70 | }; |
| 71 | |
| 72 | } // namespace test |
| 73 | } // namespace remoting |
| 74 | |
| 75 | #endif // REMOTING_TEST_HOST_LIST_FETCHER_H_ |