|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=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 "IDBFileHandle.h" |
|
8 |
|
9 #include "mozilla/dom/file/File.h" |
|
10 #include "mozilla/dom/IDBFileHandleBinding.h" |
|
11 #include "mozilla/dom/quota/FileStreams.h" |
|
12 |
|
13 #include "IDBDatabase.h" |
|
14 |
|
15 USING_INDEXEDDB_NAMESPACE |
|
16 USING_QUOTA_NAMESPACE |
|
17 |
|
18 namespace { |
|
19 |
|
20 inline |
|
21 already_AddRefed<nsIFile> |
|
22 GetFileFor(FileInfo* aFileInfo) |
|
23 |
|
24 { |
|
25 FileManager* fileManager = aFileInfo->Manager(); |
|
26 nsCOMPtr<nsIFile> directory = fileManager->GetDirectory(); |
|
27 NS_ENSURE_TRUE(directory, nullptr); |
|
28 |
|
29 nsCOMPtr<nsIFile> file = fileManager->GetFileForId(directory, |
|
30 aFileInfo->Id()); |
|
31 NS_ENSURE_TRUE(file, nullptr); |
|
32 |
|
33 return file.forget(); |
|
34 } |
|
35 |
|
36 } // anonymous namespace |
|
37 |
|
38 IDBFileHandle::IDBFileHandle(IDBDatabase* aOwner) |
|
39 : FileHandle(aOwner) |
|
40 { |
|
41 } |
|
42 |
|
43 // static |
|
44 already_AddRefed<IDBFileHandle> |
|
45 IDBFileHandle::Create(IDBDatabase* aDatabase, |
|
46 const nsAString& aName, |
|
47 const nsAString& aType, |
|
48 already_AddRefed<FileInfo> aFileInfo) |
|
49 { |
|
50 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
|
51 |
|
52 nsRefPtr<FileInfo> fileInfo(aFileInfo); |
|
53 NS_ASSERTION(fileInfo, "Null pointer!"); |
|
54 |
|
55 nsRefPtr<IDBFileHandle> newFile = new IDBFileHandle(aDatabase); |
|
56 |
|
57 newFile->mFileStorage = aDatabase; |
|
58 newFile->mName = aName; |
|
59 newFile->mType = aType; |
|
60 |
|
61 newFile->mFile = GetFileFor(fileInfo); |
|
62 NS_ENSURE_TRUE(newFile->mFile, nullptr); |
|
63 newFile->mFileName.AppendInt(fileInfo->Id()); |
|
64 |
|
65 fileInfo.swap(newFile->mFileInfo); |
|
66 |
|
67 return newFile.forget(); |
|
68 } |
|
69 |
|
70 already_AddRefed<nsISupports> |
|
71 IDBFileHandle::CreateStream(nsIFile* aFile, bool aReadOnly) |
|
72 { |
|
73 nsCOMPtr<nsIOfflineStorage> storage = do_QueryInterface(mFileStorage); |
|
74 NS_ASSERTION(storage, "This should always succeed!"); |
|
75 |
|
76 PersistenceType persistenceType = storage->Type(); |
|
77 const nsACString& group = storage->Group(); |
|
78 const nsACString& origin = storage->Origin(); |
|
79 |
|
80 nsCOMPtr<nsISupports> result; |
|
81 |
|
82 if (aReadOnly) { |
|
83 nsRefPtr<FileInputStream> stream = |
|
84 FileInputStream::Create(persistenceType, group, origin, aFile, -1, -1, |
|
85 nsIFileInputStream::DEFER_OPEN); |
|
86 result = NS_ISUPPORTS_CAST(nsIFileInputStream*, stream); |
|
87 } |
|
88 else { |
|
89 nsRefPtr<FileStream> stream = |
|
90 FileStream::Create(persistenceType, group, origin, aFile, -1, -1, |
|
91 nsIFileStream::DEFER_OPEN); |
|
92 result = NS_ISUPPORTS_CAST(nsIFileStream*, stream); |
|
93 } |
|
94 NS_ENSURE_TRUE(result, nullptr); |
|
95 |
|
96 return result.forget(); |
|
97 } |
|
98 |
|
99 already_AddRefed<nsIDOMFile> |
|
100 IDBFileHandle::CreateFileObject(mozilla::dom::file::LockedFile* aLockedFile, |
|
101 uint32_t aFileSize) |
|
102 { |
|
103 nsCOMPtr<nsIDOMFile> file = new mozilla::dom::file::File( |
|
104 mName, mType, aFileSize, mFile, aLockedFile, mFileInfo); |
|
105 |
|
106 return file.forget(); |
|
107 } |
|
108 |
|
109 // virtual |
|
110 JSObject* |
|
111 IDBFileHandle::WrapObject(JSContext* aCx) |
|
112 { |
|
113 return IDBFileHandleBinding::Wrap(aCx, this); |
|
114 } |
|
115 |
|
116 IDBDatabase* |
|
117 IDBFileHandle::Database() |
|
118 { |
|
119 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
|
120 |
|
121 IDBDatabase* database = static_cast<IDBDatabase*>(mFileStorage.get()); |
|
122 MOZ_ASSERT(database); |
|
123 |
|
124 return database; |
|
125 } |