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

Side by Side Diff: content/child/background_sync/background_sync_provider_thread_proxy.h

Issue 1104283002: [Background Sync] Add renderer code to interface between JS API and back-end service (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bgsync-mojo
Patch Set: Make CallbackThreadAdapter own the wrapped callbacks Created 5 years, 7 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
OLDNEW
(Empty)
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 CONTENT_CHILD_BACKGROUND_SYNC_BACKGROUND_SYNC_PROVIDER_THREAD_PROXY_H_
6 #define CONTENT_CHILD_BACKGROUND_SYNC_BACKGROUND_SYNC_PROVIDER_THREAD_PROXY_H_
7
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "content/child/worker_task_runner.h"
13 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncProv ider.h"
14
15 namespace base {
16 class SingleThreadTaskRunner;
17 }
18
19 namespace content {
20
21 class BackgroundSyncProvider;
22
23 // BackgroundSyncProviderThreadProxy is a proxy to the BackgroundSyncProvider
24 // for callers running on a different thread than the main thread. There is one
25 // instance of that class per thread.
26 //
27 // This class handles all of the thread switching, jumping to the main thread to
28 // call the WebSyncProvider methods, and wrapping the callbacks passed in with
29 // code to switch back to the original calling thread.
30 class BackgroundSyncProviderThreadProxy :
31 public blink::WebSyncProvider,
32 public WorkerTaskRunner::Observer {
33
34 // CallbackThreadAdapater<S,T> is a wrapper for WebCallbacks<S,T> which
35 // switches to a specific thread before calling the wrapped callback's
36 // onSuccess or onError methods.
37 //
38 // Takes ownership of the WebCallbacks object which it wraps.
39 template<typename S, typename T>
40 class CallbackThreadAdapter : public blink::WebCallbacks<S,T> {
jkarlin 2015/05/11 11:06:27 Cool! Can this class be moved to the src file's an
iclelland 2015/05/11 18:57:01 Absolutely! Doesn't need to be exposed in headers.
41 public:
42 CallbackThreadAdapter(scoped_ptr<blink::WebCallbacks<S,T>> callbacks,
43 int worker_thread_id) :
44 callbacks_(callbacks), worker_thread_id_(worker_thread_id) {
45 }
46
47 virtual void onSuccess(S* results) {
48 // If the worker thread has been destroyed, then this task will be
49 // silently discarded.
50 WorkerTaskRunner::Instance()->PostTask(
51 worker_thread_id_,
52 base::Bind(&blink::WebCallbacks<S,T>::onSuccess,
53 base::Unretained(callbacks_.get()),
54 results));
55 }
56
57 virtual void onError(T* error) {
58 // If the worker thread has been destroyed, then this task will be
59 // silently discarded.
60 WorkerTaskRunner::Instance()->PostTask(
61 worker_thread_id_,
62 base::Bind(&blink::WebCallbacks<S,T>::onError,
63 base::Unretained(callbacks_.get()),
64 error));
65 }
66
67 private:
68 scoped_ptr<blink::WebCallbacks<S,T>> callbacks_;
69 int worker_thread_id_;
70 };
71
72 public:
73 static BackgroundSyncProviderThreadProxy* GetThreadInstance(
74 base::SingleThreadTaskRunner* main_thread_task_runner,
75 BackgroundSyncProvider* permissions_dispatcher);
76
77 // blink::WebSyncProvider implementation
78 void registerBackgroundSync(
79 const blink::WebSyncRegistration* options,
80 blink::WebServiceWorkerRegistration* service_worker_registration,
81 blink::WebSyncRegistrationCallbacks* callbacks);
82 void unregisterBackgroundSync(
83 blink::WebSyncRegistration::Periodicity periodicity,
84 int64_t id, const blink::WebString& tag,
85 blink::WebServiceWorkerRegistration* service_worker_registration,
86 blink::WebSyncUnregistrationCallbacks* callbacks);
87 void getRegistration(
88 blink::WebSyncRegistration::Periodicity,
89 const blink::WebString& tag,
90 blink::WebServiceWorkerRegistration* service_worker_registration,
91 blink::WebSyncRegistrationCallbacks* callbacks);
92 void getRegistrations(
93 blink::WebSyncRegistration::Periodicity periodicity,
94 blink::WebServiceWorkerRegistration* service_worker_registration,
95 blink::WebSyncGetRegistrationsCallbacks* callbacks);
96
97 // WorkerTaskRunner::Observer implementation.
98 void OnWorkerRunLoopStopped() override;
99
100 private:
101 BackgroundSyncProviderThreadProxy(
102 base::SingleThreadTaskRunner* main_thread_task_runner,
103 BackgroundSyncProvider* sync_provider);
104
105 virtual ~BackgroundSyncProviderThreadProxy();
106
107 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
108 // This belongs to the renderer main thread, (created by BlinkPlatformImpl)
109 // and so should outlive the BackgroundSyncProviderThreadProxy, which is
110 // created for a worker thread.
111 BackgroundSyncProvider* sync_provider_;
112
113 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncProviderThreadProxy);
114 };
115
116 } // namespace content
117
118 #endif // CONTENT_CHILD_BACKGROUND_SYNC_BACKGROUND_SYNC_PROVIDER_THREAD_PROXY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698