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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: content/child/background_sync/background_sync_provider_thread_proxy.h
diff --git a/content/child/background_sync/background_sync_provider_thread_proxy.h b/content/child/background_sync/background_sync_provider_thread_proxy.h
new file mode 100644
index 0000000000000000000000000000000000000000..8622a62acf830f899745fe5beebbc13c529df01d
--- /dev/null
+++ b/content/child/background_sync/background_sync_provider_thread_proxy.h
@@ -0,0 +1,118 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_CHILD_BACKGROUND_SYNC_BACKGROUND_SYNC_PROVIDER_THREAD_PROXY_H_
+#define CONTENT_CHILD_BACKGROUND_SYNC_BACKGROUND_SYNC_PROVIDER_THREAD_PROXY_H_
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/macros.h"
+#include "base/memory/scoped_ptr.h"
+#include "content/child/worker_task_runner.h"
+#include "third_party/WebKit/public/platform/modules/background_sync/WebSyncProvider.h"
+
+namespace base {
+class SingleThreadTaskRunner;
+}
+
+namespace content {
+
+class BackgroundSyncProvider;
+
+// BackgroundSyncProviderThreadProxy is a proxy to the BackgroundSyncProvider
+// for callers running on a different thread than the main thread. There is one
+// instance of that class per thread.
+//
+// This class handles all of the thread switching, jumping to the main thread to
+// call the WebSyncProvider methods, and wrapping the callbacks passed in with
+// code to switch back to the original calling thread.
+class BackgroundSyncProviderThreadProxy :
+ public blink::WebSyncProvider,
+ public WorkerTaskRunner::Observer {
+
+ // CallbackThreadAdapater<S,T> is a wrapper for WebCallbacks<S,T> which
+ // switches to a specific thread before calling the wrapped callback's
+ // onSuccess or onError methods.
+ //
+ // Takes ownership of the WebCallbacks object which it wraps.
+ template<typename S, typename T>
+ 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.
+ public:
+ CallbackThreadAdapter(scoped_ptr<blink::WebCallbacks<S,T>> callbacks,
+ int worker_thread_id) :
+ callbacks_(callbacks), worker_thread_id_(worker_thread_id) {
+ }
+
+ virtual void onSuccess(S* results) {
+ // If the worker thread has been destroyed, then this task will be
+ // silently discarded.
+ WorkerTaskRunner::Instance()->PostTask(
+ worker_thread_id_,
+ base::Bind(&blink::WebCallbacks<S,T>::onSuccess,
+ base::Unretained(callbacks_.get()),
+ results));
+ }
+
+ virtual void onError(T* error) {
+ // If the worker thread has been destroyed, then this task will be
+ // silently discarded.
+ WorkerTaskRunner::Instance()->PostTask(
+ worker_thread_id_,
+ base::Bind(&blink::WebCallbacks<S,T>::onError,
+ base::Unretained(callbacks_.get()),
+ error));
+ }
+
+ private:
+ scoped_ptr<blink::WebCallbacks<S,T>> callbacks_;
+ int worker_thread_id_;
+ };
+
+ public:
+ static BackgroundSyncProviderThreadProxy* GetThreadInstance(
+ base::SingleThreadTaskRunner* main_thread_task_runner,
+ BackgroundSyncProvider* permissions_dispatcher);
+
+ // blink::WebSyncProvider implementation
+ void registerBackgroundSync(
+ const blink::WebSyncRegistration* options,
+ blink::WebServiceWorkerRegistration* service_worker_registration,
+ blink::WebSyncRegistrationCallbacks* callbacks);
+ void unregisterBackgroundSync(
+ blink::WebSyncRegistration::Periodicity periodicity,
+ int64_t id, const blink::WebString& tag,
+ blink::WebServiceWorkerRegistration* service_worker_registration,
+ blink::WebSyncUnregistrationCallbacks* callbacks);
+ void getRegistration(
+ blink::WebSyncRegistration::Periodicity,
+ const blink::WebString& tag,
+ blink::WebServiceWorkerRegistration* service_worker_registration,
+ blink::WebSyncRegistrationCallbacks* callbacks);
+ void getRegistrations(
+ blink::WebSyncRegistration::Periodicity periodicity,
+ blink::WebServiceWorkerRegistration* service_worker_registration,
+ blink::WebSyncGetRegistrationsCallbacks* callbacks);
+
+ // WorkerTaskRunner::Observer implementation.
+ void OnWorkerRunLoopStopped() override;
+
+ private:
+ BackgroundSyncProviderThreadProxy(
+ base::SingleThreadTaskRunner* main_thread_task_runner,
+ BackgroundSyncProvider* sync_provider);
+
+ virtual ~BackgroundSyncProviderThreadProxy();
+
+ scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
+ // This belongs to the renderer main thread, (created by BlinkPlatformImpl)
+ // and so should outlive the BackgroundSyncProviderThreadProxy, which is
+ // created for a worker thread.
+ BackgroundSyncProvider* sync_provider_;
+
+ DISALLOW_COPY_AND_ASSIGN(BackgroundSyncProviderThreadProxy);
+};
+
+} // namespace content
+
+#endif // CONTENT_CHILD_BACKGROUND_SYNC_BACKGROUND_SYNC_PROVIDER_THREAD_PROXY_H_

Powered by Google App Engine
This is Rietveld 408576698