Attachment #8763785: Part 1: WebIDL and DOM bindings for bug #1217544

View | Details | Raw Unified | Return to bug 1217544
Collapse All | Expand All

(-)a/dom/backgroundsync/BackgroundSync.cpp (+138 lines)
Line     Link Here 
Line 0    Link Here 
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "BackgroundSync.h"
8
9
#include "mozilla/dom/Promise.h"
10
#include "mozilla/dom/PromiseWorkerProxy.h"
11
#include "mozilla/dom/BackgroundSyncBinding.h"
12
#include "mozilla/ipc/BackgroundChild.h"
13
#include "mozilla/ipc/BackgroundUtils.h"
14
#include "mozilla/ipc/PBackgroundChild.h"
15
#include "mozilla/Preferences.h"
16
#include "nsIGlobalObject.h"
17
#include "WorkerPrivate.h"
18
19
namespace mozilla {
20
21
using namespace ipc;
22
23
namespace dom {
24
25
using namespace workers;
26
27
namespace backgroundsync {
28
29
// BackgroundSync
30
31
// static
32
already_AddRefed<BackgroundSync>
33
BackgroundSync::CreateOnMainThread(nsIGlobalObject* aGlobal,
34
                                   nsIPrincipal* aPrincipal,
35
                                   ErrorResult& aRv)
36
{
37
  MOZ_ASSERT(aGlobal);
38
  MOZ_ASSERT(aPrincipal);
39
  MOZ_ASSERT(NS_IsMainThread());
40
41
  PrincipalInfo principalInfo;
42
  nsresult rv = PrincipalToPrincipalInfo(aPrincipal, &principalInfo);
43
  if (NS_WARN_IF(NS_FAILED(rv))) {
44
    aRv.Throw(rv);
45
    return nullptr;
46
  }
47
48
  RefPtr<BackgroundSync> ref = new BackgroundSync(aGlobal, principalInfo);
49
  return ref.forget();
50
}
51
52
// static
53
already_AddRefed<BackgroundSync>
54
BackgroundSync::CreateOnWorker(WorkerPrivate* aWorkerPrivate)
55
{
56
  MOZ_ASSERT(aWorkerPrivate);
57
  aWorkerPrivate->AssertIsOnWorkerThread();
58
59
  const PrincipalInfo& principalInfo = aWorkerPrivate->GetPrincipalInfo();
60
61
  RefPtr<BackgroundSync> ref = new BackgroundSync(nullptr,
62
                                                  principalInfo);
63
  return ref.forget();
64
}
65
66
BackgroundSync::BackgroundSync(nsIGlobalObject* aGlobal,
67
                               const PrincipalInfo& aPrincipalInfo)
68
  : mGlobal(aGlobal)
69
  , mPrincipalInfo(new PrincipalInfo(aPrincipalInfo))
70
{}
71
72
BackgroundSync::~BackgroundSync()
73
{}
74
75
// Bindings methods.
76
77
JSObject*
78
BackgroundSync::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
79
{
80
  return mozilla::dom::BackgroundSyncBinding::Wrap(aCx, this, aGivenProto);
81
}
82
83
// static
84
bool
85
BackgroundSync::PrefEnabled(JSContext* aCx, JSObject* aObj)
86
{
87
  using mozilla::dom::workers::WorkerPrivate;
88
  using mozilla::dom::workers::GetWorkerPrivateFromContext;
89
90
  // If we are on the main thread, then check the pref directly.
91
  if (NS_IsMainThread()) {
92
    return Preferences::GetBool("dom.background.sync.enabled", false);
93
  }
94
95
  // Otherwise check the pref via the worker private helper.
96
  WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
97
  if (!workerPrivate) {
98
    return false;
99
  }
100
101
  return workerPrivate->BackgroundSyncEnabled();
102
}
103
104
// WebIDL interace methods.
105
106
already_AddRefed<Promise>
107
BackgroundSync::Register(const nsAString& aName, ErrorResult& aRv)
108
{
109
  RefPtr<Promise> p = Promise::Create(mGlobal, aRv);
110
  if (NS_WARN_IF(aRv.Failed())) {
111
    return nullptr;
112
  }
113
114
  return p.forget();
115
}
116
117
already_AddRefed<Promise>
118
BackgroundSync::GetTags(ErrorResult& aRv)
119
{
120
  RefPtr<Promise> p = Promise::Create(mGlobal, aRv);
121
  if (NS_WARN_IF(aRv.Failed())) {
122
    return nullptr;
123
  }
124
125
  return p.forget();
126
}
127
128
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(BackgroundSync, mGlobal)
129
NS_IMPL_CYCLE_COLLECTING_ADDREF(BackgroundSync)
130
NS_IMPL_CYCLE_COLLECTING_RELEASE(BackgroundSync)
131
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BackgroundSync)
132
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
133
  NS_INTERFACE_MAP_ENTRY(nsISupports)
134
NS_INTERFACE_MAP_END
135
136
} // namespace backgroundsync
137
} // namespace dom
138
} // namespace mozilla
(-)a/dom/backgroundsync/BackgroundSync.h (+87 lines)
Line     Link Here 
Line 0    Link Here 
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_dom_BackgroundSync_h
8
#define mozilla_dom_BackgroundSync_h
9
10
#include "jsapi.h"
11
#include "mozilla/AlreadyAddRefed.h"
12
#include "mozilla/ErrorResult.h"
13
#include "mozilla/dom/BindingDeclarations.h"
14
#include "nsCOMPtr.h"
15
#include "nsWrapperCache.h"
16
17
class nsIGlobalObject;
18
class nsIPrincipal;
19
20
namespace mozilla {
21
22
namespace ipc {
23
  class PrincipalInfo;
24
} // namespace ipc
25
26
namespace dom {
27
28
namespace workers {
29
  class WorkerPrivate;
30
} // namespace workers
31
32
class Promise;
33
34
namespace backgroundsync {
35
36
class BackgroundSync final : public nsISupports
37
                           , public nsWrapperCache
38
{
39
public:
40
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
41
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(BackgroundSync)
42
43
  static already_AddRefed<BackgroundSync>
44
  CreateOnMainThread(nsIGlobalObject* aGlobal,
45
                     nsIPrincipal* aPrincipal,
46
                     ErrorResult& aRv);
47
48
  static already_AddRefed<BackgroundSync>
49
  CreateOnWorker(workers::WorkerPrivate* aWorkerPrivate);
50
51
  // Binding methods.
52
53
  nsIGlobalObject*
54
  GetParentObject() const
55
  {
56
    return mGlobal;
57
  }
58
59
  JSObject*
60
  WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
61
62
  static bool PrefEnabled(JSContext* aCx, JSObject* aObj);
63
64
  // WebIDL interface methods.
65
66
  already_AddRefed<Promise>
67
  Register(const nsAString& aName, ErrorResult& aRv);
68
69
  already_AddRefed<Promise>
70
  GetTags(ErrorResult& aRv);
71
72
private:
73
  BackgroundSync(nsIGlobalObject* aGlobal,
74
                 const mozilla::ipc::PrincipalInfo& aPrincipalInfo);
75
76
  ~BackgroundSync();
77
78
  nsCOMPtr<nsIGlobalObject> mGlobal;
79
80
  nsAutoPtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo;
81
};
82
83
} // namespace backgroundsync
84
} // namespace dom
85
} // namespace mozilla
86
87
#endif // mozilla_dom_BackgroundSync_h
(-)a/dom/backgroundsync/moz.build (+20 lines)
Line     Link Here 
Line 0    Link Here 
1
# vim: set filetype=python:
2
# This Source Code Form is subject to the terms of the Mozilla Public
3
# License, v. 2.0. If a copy of the MPL was not distributed with this
4
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
6
EXPORTS.mozilla.dom.backgroundsync += [
7
    'BackgroundSync.h'
8
]
9
10
UNIFIED_SOURCES += [
11
    'BackgroundSync.cpp'
12
]
13
14
LOCAL_INCLUDES += [
15
    '/dom/workers'
16
]
17
18
include('/ipc/chromium/chromium-config.mozbuild')
19
20
FINAL_LIBRARY = 'xul'
(-)a/dom/bindings/Bindings.conf (+5 lines)
Line     Link Here 
 Lines 126-141   DOMInterfaces = { Link Here 
126
'AudioNode' : {
126
'AudioNode' : {
127
    'concrete': False,
127
    'concrete': False,
128
    'binaryNames': {
128
    'binaryNames': {
129
        'channelCountMode': 'channelCountModeValue',
129
        'channelCountMode': 'channelCountModeValue',
130
        'channelInterpretation': 'channelInterpretationValue',
130
        'channelInterpretation': 'channelInterpretationValue',
131
    },
131
    },
132
},
132
},
133
133
134
'BackgroundSync': {
135
    'nativeType': 'mozilla::dom::backgroundsync::BackgroundSync',
136
    'headerFile': 'mozilla/dom/backgroundsync/BackgroundSync.h',
137
},
138
134
'BarProp': {
139
'BarProp': {
135
    'headerFile': 'mozilla/dom/BarProps.h',
140
    'headerFile': 'mozilla/dom/BarProps.h',
136
},
141
},
137
142
138
'Blob': {
143
'Blob': {
139
    'headerFile': 'mozilla/dom/File.h',
144
    'headerFile': 'mozilla/dom/File.h',
140
},
145
},
141
146
(-)a/dom/moz.build (+1 lines)
Line     Link Here 
 Lines 38-53   if not CONFIG['MOZ_SIMPLEPUSH']: Link Here 
38
    interfaces += ['push']
38
    interfaces += ['push']
39
39
40
DIRS += ['interfaces/' + i for i in interfaces]
40
DIRS += ['interfaces/' + i for i in interfaces]
41
41
42
DIRS += [
42
DIRS += [
43
    'animation',
43
    'animation',
44
    'apps',
44
    'apps',
45
    'base',
45
    'base',
46
    'backgroundsync',
46
    'bluetooth',
47
    'bluetooth',
47
    'activities',
48
    'activities',
48
    'archivereader',
49
    'archivereader',
49
    'bindings',
50
    'bindings',
50
    'battery',
51
    'battery',
51
    'browser-element',
52
    'browser-element',
52
    'cache',
53
    'cache',
53
    'canvas',
54
    'canvas',
(-)a/dom/tests/mochitest/general/test_interfaces.html (+2 lines)
Line     Link Here 
 Lines 158-173   var interfaceNamesInGlobalScope = Link Here 
158
    "AudioParam",
158
    "AudioParam",
159
// IMPORTANT: Do not change this list without review from a DOM peer!
159
// IMPORTANT: Do not change this list without review from a DOM peer!
160
    "AudioProcessingEvent",
160
    "AudioProcessingEvent",
161
// IMPORTANT: Do not change this list without review from a DOM peer!
161
// IMPORTANT: Do not change this list without review from a DOM peer!
162
    "AudioStreamTrack",
162
    "AudioStreamTrack",
163
// IMPORTANT: Do not change this list without review from a DOM peer!
163
// IMPORTANT: Do not change this list without review from a DOM peer!
164
    {name: "AVInputPort", b2g: true, permission: ["inputport"]},
164
    {name: "AVInputPort", b2g: true, permission: ["inputport"]},
165
// IMPORTANT: Do not change this list without review from a DOM peer!
165
// IMPORTANT: Do not change this list without review from a DOM peer!
166
    {name: "BackgroundSync", b2g: true},
167
// IMPORTANT: Do not change this list without review from a DOM peer!
166
    "BarProp",
168
    "BarProp",
167
// IMPORTANT: Do not change this list without review from a DOM peer!
169
// IMPORTANT: Do not change this list without review from a DOM peer!
168
    "BatteryManager",
170
    "BatteryManager",
169
// IMPORTANT: Do not change this list without review from a DOM peer!
171
// IMPORTANT: Do not change this list without review from a DOM peer!
170
    {name: "BeforeAfterKeyboardEvent", b2g: true,
172
    {name: "BeforeAfterKeyboardEvent", b2g: true,
171
     permission: ["embed-apps", "before-after-keyboard-event"]},
173
     permission: ["embed-apps", "before-after-keyboard-event"]},
172
// IMPORTANT: Do not change this list without review from a DOM peer!
174
// IMPORTANT: Do not change this list without review from a DOM peer!
173
    "BeforeUnloadEvent",
175
    "BeforeUnloadEvent",
(-)a/dom/webidl/BackgroundSync.webidl (+17 lines)
Line     Link Here 
Line 0    Link Here 
1
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4
* You can obtain one at http://mozilla.org/MPL/2.0/.
5
*
6
* The origin of this IDL file is
7
* https://wicg.github.io/BackgroundSync/spec/
8
*/
9
10
[Exposed=(Window,Worker),
11
 Func="mozilla::dom::backgroundsync::BackgroundSync::PrefEnabled"]
12
interface BackgroundSync {
13
  [Throws]
14
  Promise<void> register(DOMString tag);
15
  [Throws]
16
  Promise<sequence<DOMString>> getTags();
17
};
(-)a/dom/webidl/moz.build (+1 lines)
Line     Link Here 
 Lines 45-60   WEBIDL_FILES = [ Link Here 
45
    'AudioNode.webidl',
45
    'AudioNode.webidl',
46
    'AudioParam.webidl',
46
    'AudioParam.webidl',
47
    'AudioProcessingEvent.webidl',
47
    'AudioProcessingEvent.webidl',
48
    'AudioStreamTrack.webidl',
48
    'AudioStreamTrack.webidl',
49
    'AudioTrack.webidl',
49
    'AudioTrack.webidl',
50
    'AudioTrackList.webidl',
50
    'AudioTrackList.webidl',
51
    'AutocompleteInfo.webidl',
51
    'AutocompleteInfo.webidl',
52
    'AVInputPort.webidl',
52
    'AVInputPort.webidl',
53
    'BackgroundSync.webidl',
53
    'BarProp.webidl',
54
    'BarProp.webidl',
54
    'BaseKeyframeTypes.webidl',
55
    'BaseKeyframeTypes.webidl',
55
    'BatteryManager.webidl',
56
    'BatteryManager.webidl',
56
    'BeforeAfterKeyboardEvent.webidl',
57
    'BeforeAfterKeyboardEvent.webidl',
57
    'BeforeUnloadEvent.webidl',
58
    'BeforeUnloadEvent.webidl',
58
    'BiquadFilterNode.webidl',
59
    'BiquadFilterNode.webidl',
59
    'Blob.webidl',
60
    'Blob.webidl',
60
    'BoxObject.webidl',
61
    'BoxObject.webidl',
(-)a/dom/workers/WorkerPrefs.h (+1 lines)
Line     Link Here 
 Lines 28-43   WORKER_SIMPLE_PREF("dom.caches.enabled", Link Here 
28
WORKER_SIMPLE_PREF("dom.caches.testing.enabled", DOMCachesTestingEnabled, DOM_CACHES_TESTING)
28
WORKER_SIMPLE_PREF("dom.caches.testing.enabled", DOMCachesTestingEnabled, DOM_CACHES_TESTING)
29
WORKER_SIMPLE_PREF("dom.performance.enable_user_timing_logging", PerformanceLoggingEnabled, PERFORMANCE_LOGGING_ENABLED)
29
WORKER_SIMPLE_PREF("dom.performance.enable_user_timing_logging", PerformanceLoggingEnabled, PERFORMANCE_LOGGING_ENABLED)
30
WORKER_SIMPLE_PREF("dom.webnotifications.enabled", DOMWorkerNotificationEnabled, DOM_WORKERNOTIFICATION)
30
WORKER_SIMPLE_PREF("dom.webnotifications.enabled", DOMWorkerNotificationEnabled, DOM_WORKERNOTIFICATION)
31
WORKER_SIMPLE_PREF("dom.webnotifications.serviceworker.enabled", DOMServiceWorkerNotificationEnabled, DOM_SERVICEWORKERNOTIFICATION)
31
WORKER_SIMPLE_PREF("dom.webnotifications.serviceworker.enabled", DOMServiceWorkerNotificationEnabled, DOM_SERVICEWORKERNOTIFICATION)
32
WORKER_SIMPLE_PREF("dom.serviceWorkers.enabled", ServiceWorkersEnabled, SERVICEWORKERS_ENABLED)
32
WORKER_SIMPLE_PREF("dom.serviceWorkers.enabled", ServiceWorkersEnabled, SERVICEWORKERS_ENABLED)
33
WORKER_SIMPLE_PREF("dom.serviceWorkers.testing.enabled", ServiceWorkersTestingEnabled, SERVICEWORKERS_TESTING_ENABLED)
33
WORKER_SIMPLE_PREF("dom.serviceWorkers.testing.enabled", ServiceWorkersTestingEnabled, SERVICEWORKERS_TESTING_ENABLED)
34
WORKER_SIMPLE_PREF("dom.serviceWorkers.openWindow.enabled", OpenWindowEnabled, OPEN_WINDOW_ENABLED)
34
WORKER_SIMPLE_PREF("dom.serviceWorkers.openWindow.enabled", OpenWindowEnabled, OPEN_WINDOW_ENABLED)
35
WORKER_SIMPLE_PREF("dom.push.enabled", PushEnabled, PUSH_ENABLED)
35
WORKER_SIMPLE_PREF("dom.push.enabled", PushEnabled, PUSH_ENABLED)
36
WORKER_SIMPLE_PREF("dom.background.sync.enabled", BackgroundSyncEnabled, BACKGROUND_SYNC_ENABLED)
36
WORKER_SIMPLE_PREF("dom.requestcontext.enabled", RequestContextEnabled, REQUESTCONTEXT_ENABLED)
37
WORKER_SIMPLE_PREF("dom.requestcontext.enabled", RequestContextEnabled, REQUESTCONTEXT_ENABLED)
37
WORKER_SIMPLE_PREF("gfx.offscreencanvas.enabled", OffscreenCanvasEnabled, OFFSCREENCANVAS_ENABLED)
38
WORKER_SIMPLE_PREF("gfx.offscreencanvas.enabled", OffscreenCanvasEnabled, OFFSCREENCANVAS_ENABLED)
38
WORKER_PREF("dom.workers.latestJSVersion", JSVersionChanged)
39
WORKER_PREF("dom.workers.latestJSVersion", JSVersionChanged)
39
WORKER_PREF("intl.accept_languages", PrefLanguagesChanged)
40
WORKER_PREF("intl.accept_languages", PrefLanguagesChanged)
40
WORKER_PREF("general.appname.override", AppNameOverrideChanged)
41
WORKER_PREF("general.appname.override", AppNameOverrideChanged)
41
WORKER_PREF("general.appversion.override", AppVersionOverrideChanged)
42
WORKER_PREF("general.appversion.override", AppVersionOverrideChanged)
42
WORKER_PREF("general.platform.override", PlatformOverrideChanged)
43
WORKER_PREF("general.platform.override", PlatformOverrideChanged)
43
#ifdef JS_GC_ZEAL
44
#ifdef JS_GC_ZEAL

Return to bug 1217544