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 COMPONENTS_BROWSER_SYNC_SIGNIN_CONFIRMATION_HELPER_H_
6 #define COMPONENTS_BROWSER_SYNC_SIGNIN_CONFIRMATION_HELPER_H_
7 
8 #include <stddef.h>
9 
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/task/cancelable_task_tracker.h"
13 
14 namespace base {
15 class SequencedTaskRunner;
16 }
17 
18 namespace history {
19 class HistoryService;
20 class QueryResults;
21 }
22 
23 namespace browser_sync {
24 
25 // Helper class for sync signin to check some asynchronous conditions. Call
26 // either CheckHasHistory or CheckHasTypedUrls or both, and |return_result|
27 // will be called with true if either returns true, otherwise false.
28 class SigninConfirmationHelper {
29  public:
30   SigninConfirmationHelper(history::HistoryService* history_service,
31                            base::OnceCallback<void(bool)> return_result);
32 
33   // This helper checks if there are history entries in the history service.
34   void CheckHasHistory(int max_entries);
35 
36   // This helper checks if there are typed URLs in the history service.
37   void CheckHasTypedURLs();
38 
39  private:
40   // Deletes itself.
41   ~SigninConfirmationHelper();
42 
43   // Callback helper function for CheckHasHistory.
44   void OnHistoryQueryResults(size_t max_entries, history::QueryResults results);
45 
46   // Posts the given result to the origin sequence.
47   void PostResult(bool result);
48 
49   // Calls |return_result_| if |result| == true or if it's the result of the
50   // last pending check.
51   void ReturnResult(bool result);
52 
53   // The task runner for the sequence this object was constructed on.
54   const scoped_refptr<base::SequencedTaskRunner> origin_sequence_;
55 
56   // Pointer to the history service.
57   history::HistoryService* history_service_;
58 
59   // Used for async tasks.
60   base::CancelableTaskTracker task_tracker_;
61 
62   // Keep track of how many async requests are pending.
63   int pending_requests_;
64 
65   // Callback to pass the result back to the caller.
66   base::OnceCallback<void(bool)> return_result_;
67 
68   DISALLOW_COPY_AND_ASSIGN(SigninConfirmationHelper);
69 };
70 
71 }  // namespace browser_sync
72 
73 #endif  // COMPONENTS_BROWSER_SYNC_SIGNIN_CONFIRMATION_HELPER_H_
74