Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(766)

Side by Side Diff: chrome/browser/sync_file_system/drive_backend/callback_tracker.h

Issue 310383002: [SyncFS] Introduce CallbackTracker to handle callback abortion (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/sync_file_system/drive_backend/callback_tracker.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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 CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_TRACKER_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_TRACKER_H_
7
8 #include <map>
9
10 #include "base/bind.h"
11
12 namespace sync_file_system {
13 namespace drive_backend {
14
15 class CallbackTracker;
16
17 namespace internal {
18
19 template <typename>
20 struct InvokeAndInvalidateHelper;
21
22 class AbortHelper {
23 public:
24 explicit AbortHelper(CallbackTracker* tracker);
25 ~AbortHelper();
26 base::WeakPtr<AbortHelper> AsWeakPtr();
27
28 static scoped_ptr<AbortHelper> TakeOwnership(
29 const base::WeakPtr<AbortHelper>& abort_helper);
30
31 private:
32 CallbackTracker* tracker_; // Not owned.
33 base::WeakPtrFactory<AbortHelper> weak_ptr_factory_;
34
35 DISALLOW_COPY_AND_ASSIGN(AbortHelper);
36 };
37
38 } // namespace internal
39
40 // A helper class to ensure one-shot callback to be called exactly once.
41 //
42 // Usage:
43 // class Foo {
44 // private:
45 // CallbackTracker callback_tracker_;
46 // };
47 //
48 // void DoSomethingAsync(const SomeCallbackType& callback) {
49 // base::Closure abort_case_handler = base::Bind(callback, ABORT_ERROR);
50 //
51 // SomeCallbackType wrapped_callback =
52 // callback_tracker_.Register(
53 // abort_case_handler, callback);
54 //
55 // // The body of the operation goes here.
56 // }
57 class CallbackTracker {
58 public:
59 typedef std::map<internal::AbortHelper*, base::Closure> AbortClosureByHelper;
60
61 CallbackTracker();
62 ~CallbackTracker();
63
64 // Returns a wrapped callback.
65 // Upon AbortAll() call, CallbackTracker invokes |abort_closure| and voids all
66 // wrapped callbacks returned by Register().
67 // Invocation of the wrapped callback unregisters |callback| from
68 // CallbackTracker.
69 template <typename T>
70 base::Callback<T> Register(const base::Closure& abort_closure,
71 const base::Callback<T>& callback) {
72 internal::AbortHelper* helper = new internal::AbortHelper(this);
73 helpers_[helper] = abort_closure;
74 return base::Bind(&internal::InvokeAndInvalidateHelper<T>::Run,
75 helper->AsWeakPtr(), callback);
76 }
77
78 void AbortAll();
79
80 private:
81 friend class internal::AbortHelper;
82
83 scoped_ptr<internal::AbortHelper> PassAbortHelper(
84 internal::AbortHelper* helper);
85
86 AbortClosureByHelper helpers_; // Owns Wrapper.
87
88 DISALLOW_COPY_AND_ASSIGN(CallbackTracker);
89 };
90
91 } // namespace drive_backend
92 } // namespace sync_file_system
93
94 #include "chrome/browser/sync_file_system/drive_backend/callback_tracker_interna l.h"
peria 2014/06/04 07:39:21 Putting a header file on the bottom line seems sto
tzik 2014/06/04 08:15:56 Done.
95
96 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_CALLBACK_TRACKER_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/sync_file_system/drive_backend/callback_tracker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698