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"
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
{
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
);
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.
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_