summaryrefslogtreecommitdiffstats
path: root/content/child/background_sync/background_sync_provider.h
blob: 51ded1b63cdff7b5d5fa9d8b2b9b0bd14d78c962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// 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_H_
#define CONTENT_CHILD_BACKGROUND_SYNC_BACKGROUND_SYNC_PROVIDER_H_

#include <stdint.h>

#include <string>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "content/common/background_sync_service.mojom.h"
#include "content/public/child/worker_thread.h"
#include "third_party/WebKit/public/platform/modules/background_sync/WebSyncProvider.h"

namespace base {
class SingleThreadTaskRunner;
}

namespace content {

// The BackgroundSyncProvider is called by the SyncManager and SyncRegistration
// objects and communicates with the BackgroundSyncManager object in the browser
// process. Each thread will have its own instance (e.g. main thread, worker
// threads), instantiated as needed by BlinkPlatformImpl.  Each instance of
// the provider creates a new mojo connection to a new
// BackgroundSyncManagerImpl, which then talks to the BackgroundSyncManager
// object.
class BackgroundSyncProvider : public blink::WebSyncProvider,
                               public WorkerThread::Observer {
 public:
  // Constructor made public to allow BlinkPlatformImpl to own a copy for the
  // main thread. Everyone else should use GetOrCreateThreadSpecificInstance().
  explicit BackgroundSyncProvider(
      const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner);

  ~BackgroundSyncProvider() override;

  // Returns thread-specific instance (if exists).  Otherwise, a new instance
  // will be created for the thread, except when called for a worker thread that
  // has already been stopped.
  static BackgroundSyncProvider* GetOrCreateThreadSpecificInstance(
      base::SingleThreadTaskRunner* main_thread_task_runner);

  // blink::WebSyncProvider implementation
  void registerBackgroundSync(
      const blink::WebSyncRegistration* options,
      blink::WebServiceWorkerRegistration* service_worker_registration,
      bool requested_from_service_worker,
      blink::WebSyncRegistrationCallbacks* callbacks) override;
  void getRegistrations(
      blink::WebServiceWorkerRegistration* service_worker_registration,
      blink::WebSyncGetRegistrationsCallbacks* callbacks) override;

  // WorkerThread::Observer implementation.
  void WillStopCurrentWorkerThread() override;

 private:
  // Callback handlers
  void RegisterCallback(
      scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
      mojom::BackgroundSyncError error,
      const mojom::SyncRegistrationPtr& options);
  void GetRegistrationsCallback(
      scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks,
      mojom::BackgroundSyncError error,
      const mojo::Array<mojom::SyncRegistrationPtr>& registrations);

  // Helper method that returns an initialized BackgroundSyncServicePtr.
  mojom::BackgroundSyncServicePtr& GetBackgroundSyncServicePtr();

  mojom::BackgroundSyncServicePtr background_sync_service_;
  scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;

  DISALLOW_COPY_AND_ASSIGN(BackgroundSyncProvider);
};

}  // namespace content

#endif  // CONTENT_CHILD_BACKGROUND_SYNC_BACKGROUND_SYNC_PROVIDER_H_