summaryrefslogtreecommitdiff
path: root/dom/indexedDB/IDBFileHandle.cpp
blob: 8b88e1722a45b72940500fab6b56b1fbe18763f9 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "IDBFileHandle.h"

#include "IDBEvents.h"
#include "IDBMutableFile.h"
#include "mozilla/Assertions.h"
#include "mozilla/dom/IDBFileHandleBinding.h"
#include "mozilla/dom/filehandle/ActorsChild.h"
#include "mozilla/EventDispatcher.h"
#include "nsServiceManagerUtils.h"
#include "nsWidgetsCID.h"

namespace mozilla {
namespace dom {

using namespace mozilla::dom::indexedDB;

IDBFileHandle::IDBFileHandle(FileMode aMode,
                             IDBMutableFile* aMutableFile)
  : FileHandleBase(DEBUGONLY(aMutableFile->OwningThread(),)
                   aMode)
  , mMutableFile(aMutableFile)
{
  AssertIsOnOwningThread();
}

IDBFileHandle::~IDBFileHandle()
{
  AssertIsOnOwningThread();

  mMutableFile->UnregisterFileHandle(this);
}

// static
already_AddRefed<IDBFileHandle>
IDBFileHandle::Create(IDBMutableFile* aMutableFile,
                      FileMode aMode)
{
  MOZ_ASSERT(aMutableFile);
  aMutableFile->AssertIsOnOwningThread();
  MOZ_ASSERT(aMode == FileMode::Readonly || aMode == FileMode::Readwrite);

  RefPtr<IDBFileHandle> fileHandle =
    new IDBFileHandle(aMode, aMutableFile);

  fileHandle->BindToOwner(aMutableFile);

  // XXX Fix!
  MOZ_ASSERT(NS_IsMainThread(), "This won't work on non-main threads!");

  nsCOMPtr<nsIRunnable> runnable = do_QueryObject(fileHandle);
  nsContentUtils::RunInMetastableState(runnable.forget());

  fileHandle->SetCreating();

  aMutableFile->RegisterFileHandle(fileHandle);

  return fileHandle.forget();
}

already_AddRefed<IDBFileRequest>
IDBFileHandle::GetMetadata(const IDBFileMetadataParameters& aParameters,
                           ErrorResult& aRv)
{
  AssertIsOnOwningThread();

  // Common state checking
  if (!CheckState(aRv)) {
    return nullptr;
  }

  // Argument checking for get metadata.
  if (!aParameters.mSize && !aParameters.mLastModified) {
    aRv.ThrowTypeError<MSG_METADATA_NOT_CONFIGURED>();
    return nullptr;
  }

 // Do nothing if the window is closed
  if (!CheckWindow()) {
    return nullptr;
  }

  FileRequestGetMetadataParams params;
  params.size() = aParameters.mSize;
  params.lastModified() = aParameters.mLastModified;

  RefPtr<FileRequestBase> fileRequest = GenerateFileRequest();

  StartRequest(fileRequest, params);

  return fileRequest.forget().downcast<IDBFileRequest>();
}

NS_IMPL_ADDREF_INHERITED(IDBFileHandle, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(IDBFileHandle, DOMEventTargetHelper)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBFileHandle)
  NS_INTERFACE_MAP_ENTRY(nsIRunnable)
  NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)

NS_IMPL_CYCLE_COLLECTION_CLASS(IDBFileHandle)

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(IDBFileHandle,
                                                  DOMEventTargetHelper)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMutableFile)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(IDBFileHandle,
                                                DOMEventTargetHelper)
  // Don't unlink mMutableFile!
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMETHODIMP
IDBFileHandle::Run()
{
  AssertIsOnOwningThread();

  OnReturnToEventLoop();

  return NS_OK;
}

nsresult
IDBFileHandle::PreHandleEvent(EventChainPreVisitor& aVisitor)
{
  AssertIsOnOwningThread();

  aVisitor.mCanHandle = true;
  aVisitor.mParentTarget = mMutableFile;
  return NS_OK;
}

// virtual
JSObject*
IDBFileHandle::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
  AssertIsOnOwningThread();

  return IDBFileHandleBinding::Wrap(aCx, this, aGivenProto);
}

mozilla::dom::MutableFileBase*
IDBFileHandle::MutableFile() const
{
  AssertIsOnOwningThread();

  return mMutableFile;
}

void
IDBFileHandle::HandleCompleteOrAbort(bool aAborted)
{
  AssertIsOnOwningThread();

  FileHandleBase::HandleCompleteOrAbort(aAborted);

  nsCOMPtr<nsIDOMEvent> event;
  if (aAborted) {
    event = CreateGenericEvent(this, nsDependentString(kAbortEventType),
                               eDoesBubble, eNotCancelable);
  } else {
    event = CreateGenericEvent(this, nsDependentString(kCompleteEventType),
                               eDoesNotBubble, eNotCancelable);
  }
  if (NS_WARN_IF(!event)) {
    return;
  }

  bool dummy;
  if (NS_FAILED(DispatchEvent(event, &dummy))) {
    NS_WARNING("DispatchEvent failed!");
  }
}

bool
IDBFileHandle::CheckWindow()
{
  AssertIsOnOwningThread();

  return GetOwner();
}

already_AddRefed<mozilla::dom::FileRequestBase>
IDBFileHandle::GenerateFileRequest()
{
  AssertIsOnOwningThread();

  return IDBFileRequest::Create(GetOwner(), this,
                                /* aWrapAsDOMRequest */ false);
}

} // namespace dom
} // namespace mozilla