blob: e41b061b2f08911a771c72fc733346c1a07e21b3 [file] [log] [blame]
tonychune3fe9842015-07-10 18:45:301// 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
dcheng0765c492016-04-06 22:41:538#include <memory>
tonychune3fe9842015-07-10 18:45:309#include <string>
10#include <vector>
11
12#include "base/callback.h"
avi5a080f012015-12-22 23:15:4313#include "base/macros.h"
tonychune3fe9842015-07-10 18:45:3014#include "base/memory/ref_counted.h"
tonychune3fe9842015-07-10 18:45:3015#include "net/url_request/url_fetcher_delegate.h"
16#include "remoting/test/host_info.h"
17
tonychune3fe9842015-07-10 18:45:3018namespace remoting {
19class URLRequestContextGetter;
20}
21
22namespace remoting {
23namespace 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.
27const char kHostListProdRequestUrl[] = "https://www.googleapis.com/"
28 "chromoting/v1/@me/hosts";
joedow910989d2016-12-02 21:15:3229const char kHostListTestRequestUrl[] =
30 "https://www-googleapis-test.sandbox.google.com/chromoting/v1/@me/hosts";
tonychune3fe9842015-07-10 18:45:3031
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.
37class 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,
joedow910989d2016-12-02 21:15:3250 const std::string& target_url,
tonychune3fe9842015-07-10 18:45:3051 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.
dcheng0765c492016-04-06 22:41:5361 std::unique_ptr<net::URLFetcher> request_;
tonychune3fe9842015-07-10 18:45:3062
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_