Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / components / sync_driver / sync_stopped_reporter.h
blobc4bd8cfc37e1d32f73f3d80371788c25e8824c8d
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.
5 #ifndef COMPONENTS_SYNC_DRIVER_SYNC_STOPPED_REPORTER_H_
6 #define COMPONENTS_SYNC_DRIVER_SYNC_STOPPED_REPORTER_H_
8 #include "base/callback.h"
9 #include "base/timer/timer.h"
10 #include "net/url_request/url_fetcher.h"
11 #include "net/url_request/url_fetcher_delegate.h"
12 #include "net/url_request/url_request_context_getter.h"
13 #include "url/gurl.h"
15 namespace browser_sync {
17 // Manages informing the sync server that sync has been disabled.
18 // An implementation of URLFetcherDelegate was needed in order to
19 // clean up the fetcher_ pointer when the request completes.
20 class SyncStoppedReporter : public net::URLFetcherDelegate {
21 public:
22 enum Result {
23 RESULT_SUCCESS,
24 RESULT_ERROR,
25 RESULT_TIMEOUT
28 typedef base::Callback<void(const Result&)> ResultCallback;
30 SyncStoppedReporter(const GURL& sync_service_url,
31 const std::string& user_agent,
32 const scoped_refptr<net::URLRequestContextGetter>& request_context,
33 const ResultCallback& callback);
34 ~SyncStoppedReporter() override;
36 // Inform the sync server that sync was stopped on this device.
37 // |access_token|, |cache_guid|, and |birthday| must not be empty.
38 void ReportSyncStopped(const std::string& access_token,
39 const std::string& cache_guid,
40 const std::string& birthday);
42 // net::URLFetcherDelegate implementation.
43 void OnURLFetchComplete(const net::URLFetcher* source) override;
45 // Override the timer's task runner so it can be triggered in tests.
46 void SetTimerTaskRunnerForTest(
47 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
49 private:
50 // Convert the base sync URL into the sync event URL.
51 static GURL GetSyncEventURL(const GURL& sync_service_url);
53 // Callback for a request timing out.
54 void OnTimeout();
56 // Handles timing out requests.
57 base::OneShotTimer<SyncStoppedReporter> timer_;
59 // The URL for the sync server's event RPC.
60 const GURL sync_event_url_;
62 // The user agent for the browser.
63 const std::string user_agent_;
65 // Stored to simplify the API; needed for URLFetcher::Create().
66 scoped_refptr<net::URLRequestContextGetter> request_context_;
68 // The current URLFetcher. Null unless a request is in progress.
69 scoped_ptr<net::URLFetcher> fetcher_;
71 // A callback for request completion or timeout.
72 ResultCallback callback_;
74 DISALLOW_COPY_AND_ASSIGN(SyncStoppedReporter);
77 } // namespace browser_sync
79 #endif // COMPONENTS_SYNC_DRIVER_SYNC_STOPPED_REPORTER_H_