Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /** |
michael@0 | 6 | * Native implementation of some OS.File operations. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsString.h" |
michael@0 | 10 | #include "nsNetCID.h" |
michael@0 | 11 | #include "nsThreadUtils.h" |
michael@0 | 12 | #include "nsXPCOMCID.h" |
michael@0 | 13 | #include "nsCycleCollectionParticipant.h" |
michael@0 | 14 | #include "nsServiceManagerUtils.h" |
michael@0 | 15 | #include "nsProxyRelease.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "nsINativeOSFileInternals.h" |
michael@0 | 18 | #include "NativeOSFileInternals.h" |
michael@0 | 19 | #include "mozilla/dom/NativeOSFileInternalsBinding.h" |
michael@0 | 20 | |
michael@0 | 21 | #include "nsIUnicodeDecoder.h" |
michael@0 | 22 | #include "nsIEventTarget.h" |
michael@0 | 23 | |
michael@0 | 24 | #include "mozilla/dom/EncodingUtils.h" |
michael@0 | 25 | #include "mozilla/DebugOnly.h" |
michael@0 | 26 | #include "mozilla/Scoped.h" |
michael@0 | 27 | #include "mozilla/HoldDropJSObjects.h" |
michael@0 | 28 | #include "mozilla/TimeStamp.h" |
michael@0 | 29 | |
michael@0 | 30 | #include "prio.h" |
michael@0 | 31 | #include "prerror.h" |
michael@0 | 32 | #include "private/pprio.h" |
michael@0 | 33 | |
michael@0 | 34 | #include "jsapi.h" |
michael@0 | 35 | #include "jsfriendapi.h" |
michael@0 | 36 | #include "js/Utility.h" |
michael@0 | 37 | #include "xpcpublic.h" |
michael@0 | 38 | |
michael@0 | 39 | #include <algorithm> |
michael@0 | 40 | #if defined(XP_UNIX) |
michael@0 | 41 | #include <unistd.h> |
michael@0 | 42 | #include <errno.h> |
michael@0 | 43 | #include <fcntl.h> |
michael@0 | 44 | #include <sys/stat.h> |
michael@0 | 45 | #include <sys/uio.h> |
michael@0 | 46 | #endif // defined (XP_UNIX) |
michael@0 | 47 | |
michael@0 | 48 | #if defined(XP_WIN) |
michael@0 | 49 | #include <windows.h> |
michael@0 | 50 | #endif // defined (XP_WIN) |
michael@0 | 51 | |
michael@0 | 52 | namespace mozilla { |
michael@0 | 53 | |
michael@0 | 54 | MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(ScopedPRFileDesc, PRFileDesc, PR_Close) |
michael@0 | 55 | |
michael@0 | 56 | namespace { |
michael@0 | 57 | |
michael@0 | 58 | // Utilities for safely manipulating ArrayBuffer contents even in the |
michael@0 | 59 | // absence of a JSContext. |
michael@0 | 60 | |
michael@0 | 61 | /** |
michael@0 | 62 | * The C buffer underlying to an ArrayBuffer. Throughout the code, we manipulate |
michael@0 | 63 | * this instead of a void* buffer, as this lets us transfer data across threads |
michael@0 | 64 | * and into JavaScript without copy. |
michael@0 | 65 | */ |
michael@0 | 66 | struct ArrayBufferContents { |
michael@0 | 67 | /** |
michael@0 | 68 | * The data of the ArrayBuffer. This is the pointer manipulated to |
michael@0 | 69 | * read/write the contents of the buffer. |
michael@0 | 70 | */ |
michael@0 | 71 | uint8_t* data; |
michael@0 | 72 | /** |
michael@0 | 73 | * The number of bytes in the ArrayBuffer. |
michael@0 | 74 | */ |
michael@0 | 75 | size_t nbytes; |
michael@0 | 76 | }; |
michael@0 | 77 | |
michael@0 | 78 | /** |
michael@0 | 79 | * RAII for ArrayBufferContents. |
michael@0 | 80 | */ |
michael@0 | 81 | struct ScopedArrayBufferContentsTraits { |
michael@0 | 82 | typedef ArrayBufferContents type; |
michael@0 | 83 | const static type empty() { |
michael@0 | 84 | type result = {0, 0}; |
michael@0 | 85 | return result; |
michael@0 | 86 | } |
michael@0 | 87 | const static void release(type ptr) { |
michael@0 | 88 | js_free(ptr.data); |
michael@0 | 89 | ptr.data = nullptr; |
michael@0 | 90 | ptr.nbytes = 0; |
michael@0 | 91 | } |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | struct ScopedArrayBufferContents: public Scoped<ScopedArrayBufferContentsTraits> { |
michael@0 | 95 | ScopedArrayBufferContents(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM): |
michael@0 | 96 | Scoped<ScopedArrayBufferContentsTraits>(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT) |
michael@0 | 97 | { } |
michael@0 | 98 | ScopedArrayBufferContents(const ArrayBufferContents& v |
michael@0 | 99 | MOZ_GUARD_OBJECT_NOTIFIER_PARAM): |
michael@0 | 100 | Scoped<ScopedArrayBufferContentsTraits>(v MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) |
michael@0 | 101 | { } |
michael@0 | 102 | ScopedArrayBufferContents& operator=(ArrayBufferContents ptr) { |
michael@0 | 103 | Scoped<ScopedArrayBufferContentsTraits>::operator=(ptr); |
michael@0 | 104 | return *this; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | /** |
michael@0 | 108 | * Request memory for this ArrayBufferContent. This memory may later |
michael@0 | 109 | * be used to create an ArrayBuffer object (possibly on another |
michael@0 | 110 | * thread) without copy. |
michael@0 | 111 | * |
michael@0 | 112 | * @return true In case of success, false otherwise. |
michael@0 | 113 | */ |
michael@0 | 114 | bool Allocate(uint32_t length) { |
michael@0 | 115 | dispose(); |
michael@0 | 116 | ArrayBufferContents& value = rwget(); |
michael@0 | 117 | void *ptr = JS_AllocateArrayBufferContents(/*no context available*/nullptr, length); |
michael@0 | 118 | if (ptr) { |
michael@0 | 119 | value.data = (uint8_t *) ptr; |
michael@0 | 120 | value.nbytes = length; |
michael@0 | 121 | return true; |
michael@0 | 122 | } |
michael@0 | 123 | return false; |
michael@0 | 124 | } |
michael@0 | 125 | private: |
michael@0 | 126 | explicit ScopedArrayBufferContents(ScopedArrayBufferContents& source) MOZ_DELETE; |
michael@0 | 127 | ScopedArrayBufferContents& operator=(ScopedArrayBufferContents& source) MOZ_DELETE; |
michael@0 | 128 | }; |
michael@0 | 129 | |
michael@0 | 130 | ///////// Cross-platform issues |
michael@0 | 131 | |
michael@0 | 132 | // Platform specific constants. As OS.File always uses OS-level |
michael@0 | 133 | // errors, we need to map a few high-level errors to OS-level |
michael@0 | 134 | // constants. |
michael@0 | 135 | #if defined(XP_UNIX) |
michael@0 | 136 | #define OS_ERROR_NOMEM ENOMEM |
michael@0 | 137 | #define OS_ERROR_INVAL EINVAL |
michael@0 | 138 | #define OS_ERROR_TOO_LARGE EFBIG |
michael@0 | 139 | #define OS_ERROR_RACE EIO |
michael@0 | 140 | #elif defined(XP_WIN) |
michael@0 | 141 | #define OS_ERROR_NOMEM ERROR_NOT_ENOUGH_MEMORY |
michael@0 | 142 | #define OS_ERROR_INVAL ERROR_BAD_ARGUMENTS |
michael@0 | 143 | #define OS_ERROR_TOO_LARGE ERROR_FILE_TOO_LARGE |
michael@0 | 144 | #define OS_ERROR_RACE ERROR_SHARING_VIOLATION |
michael@0 | 145 | #else |
michael@0 | 146 | #error "We do not have platform-specific constants for this platform" |
michael@0 | 147 | #endif |
michael@0 | 148 | |
michael@0 | 149 | ///////// Results of OS.File operations |
michael@0 | 150 | |
michael@0 | 151 | /** |
michael@0 | 152 | * Base class for results passed to the callbacks. |
michael@0 | 153 | * |
michael@0 | 154 | * This base class implements caching of JS values returned to the client. |
michael@0 | 155 | * We make use of this caching in derived classes e.g. to avoid accidents |
michael@0 | 156 | * when we transfer data allocated on another thread into JS. Note that |
michael@0 | 157 | * this caching can lead to cycles (e.g. if a client adds a back-reference |
michael@0 | 158 | * in the JS value), so we implement all Cycle Collector primitives in |
michael@0 | 159 | * AbstractResult. |
michael@0 | 160 | */ |
michael@0 | 161 | class AbstractResult: public nsINativeOSFileResult { |
michael@0 | 162 | public: |
michael@0 | 163 | NS_DECL_NSINATIVEOSFILERESULT |
michael@0 | 164 | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
michael@0 | 165 | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AbstractResult) |
michael@0 | 166 | |
michael@0 | 167 | /** |
michael@0 | 168 | * Construct the result object. Must be called on the main thread |
michael@0 | 169 | * as the AbstractResult is cycle-collected. |
michael@0 | 170 | * |
michael@0 | 171 | * @param aStartDate The instant at which the operation was |
michael@0 | 172 | * requested. Used to collect Telemetry statistics. |
michael@0 | 173 | */ |
michael@0 | 174 | AbstractResult(TimeStamp aStartDate) |
michael@0 | 175 | : mStartDate(aStartDate) |
michael@0 | 176 | { |
michael@0 | 177 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 178 | mozilla::HoldJSObjects(this); |
michael@0 | 179 | } |
michael@0 | 180 | virtual ~AbstractResult() { |
michael@0 | 181 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 182 | DropJSData(); |
michael@0 | 183 | mozilla::DropJSObjects(this); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | /** |
michael@0 | 187 | * Setup the AbstractResult once data is available. |
michael@0 | 188 | * |
michael@0 | 189 | * @param aDispatchDate The instant at which the IO thread received |
michael@0 | 190 | * the operation request. Used to collect Telemetry statistics. |
michael@0 | 191 | * @param aExecutionDuration The duration of the operation on the |
michael@0 | 192 | * IO thread. |
michael@0 | 193 | */ |
michael@0 | 194 | void Init(TimeStamp aDispatchDate, |
michael@0 | 195 | TimeDuration aExecutionDuration) { |
michael@0 | 196 | MOZ_ASSERT(!NS_IsMainThread()); |
michael@0 | 197 | |
michael@0 | 198 | mDispatchDuration = (aDispatchDate - mStartDate); |
michael@0 | 199 | mExecutionDuration = aExecutionDuration; |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | /** |
michael@0 | 203 | * Drop any data that could lead to a cycle. |
michael@0 | 204 | */ |
michael@0 | 205 | void DropJSData() { |
michael@0 | 206 | mCachedResult = JS::UndefinedValue(); |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | protected: |
michael@0 | 210 | virtual nsresult GetCacheableResult(JSContext *cx, JS::MutableHandleValue aResult) = 0; |
michael@0 | 211 | |
michael@0 | 212 | private: |
michael@0 | 213 | TimeStamp mStartDate; |
michael@0 | 214 | TimeDuration mDispatchDuration; |
michael@0 | 215 | TimeDuration mExecutionDuration; |
michael@0 | 216 | JS::Heap<JS::Value> mCachedResult; |
michael@0 | 217 | }; |
michael@0 | 218 | |
michael@0 | 219 | NS_IMPL_CYCLE_COLLECTING_ADDREF(AbstractResult) |
michael@0 | 220 | NS_IMPL_CYCLE_COLLECTING_RELEASE(AbstractResult) |
michael@0 | 221 | |
michael@0 | 222 | NS_IMPL_CYCLE_COLLECTION_CLASS(AbstractResult) |
michael@0 | 223 | |
michael@0 | 224 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AbstractResult) |
michael@0 | 225 | NS_INTERFACE_MAP_ENTRY(nsINativeOSFileResult) |
michael@0 | 226 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 227 | NS_INTERFACE_MAP_END |
michael@0 | 228 | |
michael@0 | 229 | NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(AbstractResult) |
michael@0 | 230 | NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mCachedResult) |
michael@0 | 231 | NS_IMPL_CYCLE_COLLECTION_TRACE_END |
michael@0 | 232 | |
michael@0 | 233 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AbstractResult) |
michael@0 | 234 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS |
michael@0 | 235 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
michael@0 | 236 | |
michael@0 | 237 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AbstractResult) |
michael@0 | 238 | tmp->DropJSData(); |
michael@0 | 239 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
michael@0 | 240 | |
michael@0 | 241 | NS_IMETHODIMP |
michael@0 | 242 | AbstractResult::GetDispatchDurationMS(double *aDispatchDuration) |
michael@0 | 243 | { |
michael@0 | 244 | *aDispatchDuration = mDispatchDuration.ToMilliseconds(); |
michael@0 | 245 | return NS_OK; |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | NS_IMETHODIMP |
michael@0 | 249 | AbstractResult::GetExecutionDurationMS(double *aExecutionDuration) |
michael@0 | 250 | { |
michael@0 | 251 | *aExecutionDuration = mExecutionDuration.ToMilliseconds(); |
michael@0 | 252 | return NS_OK; |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | NS_IMETHODIMP |
michael@0 | 256 | AbstractResult::GetResult(JSContext *cx, JS::MutableHandleValue aResult) |
michael@0 | 257 | { |
michael@0 | 258 | if (mCachedResult.isUndefined()) { |
michael@0 | 259 | nsresult rv = GetCacheableResult(cx, aResult); |
michael@0 | 260 | if (NS_FAILED(rv)) { |
michael@0 | 261 | return rv; |
michael@0 | 262 | } |
michael@0 | 263 | mCachedResult = aResult; |
michael@0 | 264 | return NS_OK; |
michael@0 | 265 | } |
michael@0 | 266 | aResult.set(mCachedResult); |
michael@0 | 267 | return NS_OK; |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | /** |
michael@0 | 271 | * Return a result as a string. |
michael@0 | 272 | * |
michael@0 | 273 | * In this implementation, attribute |result| is a string. Strings are |
michael@0 | 274 | * passed to JS without copy. |
michael@0 | 275 | */ |
michael@0 | 276 | class StringResult MOZ_FINAL : public AbstractResult |
michael@0 | 277 | { |
michael@0 | 278 | public: |
michael@0 | 279 | StringResult(TimeStamp aStartDate) |
michael@0 | 280 | : AbstractResult(aStartDate) |
michael@0 | 281 | { |
michael@0 | 282 | } |
michael@0 | 283 | |
michael@0 | 284 | /** |
michael@0 | 285 | * Initialize the object once the contents of the result as available. |
michael@0 | 286 | * |
michael@0 | 287 | * @param aContents The string to pass to JavaScript. Ownership of the |
michael@0 | 288 | * string and its contents is passed to StringResult. The string must |
michael@0 | 289 | * be valid UTF-16. |
michael@0 | 290 | */ |
michael@0 | 291 | void Init(TimeStamp aDispatchDate, |
michael@0 | 292 | TimeDuration aExecutionDuration, |
michael@0 | 293 | nsString& aContents) { |
michael@0 | 294 | AbstractResult::Init(aDispatchDate, aExecutionDuration); |
michael@0 | 295 | mContents = aContents; |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | protected: |
michael@0 | 299 | nsresult GetCacheableResult(JSContext* cx, JS::MutableHandleValue aResult) MOZ_OVERRIDE; |
michael@0 | 300 | |
michael@0 | 301 | private: |
michael@0 | 302 | nsString mContents; |
michael@0 | 303 | }; |
michael@0 | 304 | |
michael@0 | 305 | nsresult |
michael@0 | 306 | StringResult::GetCacheableResult(JSContext* cx, JS::MutableHandleValue aResult) |
michael@0 | 307 | { |
michael@0 | 308 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 309 | MOZ_ASSERT(mContents.get()); |
michael@0 | 310 | |
michael@0 | 311 | // Convert mContents to a js string without copy. Note that this |
michael@0 | 312 | // may have the side-effect of stealing the contents of the string |
michael@0 | 313 | // from XPCOM and into JS. |
michael@0 | 314 | if (!xpc::StringToJsval(cx, mContents, aResult)) { |
michael@0 | 315 | return NS_ERROR_FAILURE; |
michael@0 | 316 | } |
michael@0 | 317 | return NS_OK; |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | |
michael@0 | 321 | /** |
michael@0 | 322 | * Return a result as a Uint8Array. |
michael@0 | 323 | * |
michael@0 | 324 | * In this implementation, attribute |result| is a Uint8Array. The array |
michael@0 | 325 | * is passed to JS without memory copy. |
michael@0 | 326 | */ |
michael@0 | 327 | class TypedArrayResult MOZ_FINAL : public AbstractResult |
michael@0 | 328 | { |
michael@0 | 329 | public: |
michael@0 | 330 | TypedArrayResult(TimeStamp aStartDate) |
michael@0 | 331 | : AbstractResult(aStartDate) |
michael@0 | 332 | { |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | /** |
michael@0 | 336 | * @param aContents The contents to pass to JS. Calling this method. |
michael@0 | 337 | * transmits ownership of the ArrayBufferContents to the TypedArrayResult. |
michael@0 | 338 | * Do not reuse this value anywhere else. |
michael@0 | 339 | */ |
michael@0 | 340 | void Init(TimeStamp aDispatchDate, |
michael@0 | 341 | TimeDuration aExecutionDuration, |
michael@0 | 342 | ArrayBufferContents aContents) { |
michael@0 | 343 | AbstractResult::Init(aDispatchDate, aExecutionDuration); |
michael@0 | 344 | mContents = aContents; |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | protected: |
michael@0 | 348 | nsresult GetCacheableResult(JSContext* cx, JS::MutableHandleValue aResult) MOZ_OVERRIDE; |
michael@0 | 349 | private: |
michael@0 | 350 | ScopedArrayBufferContents mContents; |
michael@0 | 351 | }; |
michael@0 | 352 | |
michael@0 | 353 | nsresult |
michael@0 | 354 | TypedArrayResult::GetCacheableResult(JSContext* cx, JS::MutableHandle<JS::Value> aResult) |
michael@0 | 355 | { |
michael@0 | 356 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 357 | // We cannot simply construct a typed array using contents.data as |
michael@0 | 358 | // this would allow us to have several otherwise unrelated |
michael@0 | 359 | // ArrayBuffers with the same underlying C buffer. As this would be |
michael@0 | 360 | // very unsafe, we need to cache the result once we have it. |
michael@0 | 361 | |
michael@0 | 362 | const ArrayBufferContents& contents = mContents.get(); |
michael@0 | 363 | MOZ_ASSERT(contents.data); |
michael@0 | 364 | |
michael@0 | 365 | JS::Rooted<JSObject*> |
michael@0 | 366 | arrayBuffer(cx, JS_NewArrayBufferWithContents(cx, contents.nbytes, contents.data)); |
michael@0 | 367 | if (!arrayBuffer) { |
michael@0 | 368 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | JS::Rooted<JSObject*> |
michael@0 | 372 | result(cx, JS_NewUint8ArrayWithBuffer(cx, arrayBuffer, |
michael@0 | 373 | 0, contents.nbytes)); |
michael@0 | 374 | if (!result) { |
michael@0 | 375 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 376 | } |
michael@0 | 377 | // The memory of contents has been allocated on a thread that |
michael@0 | 378 | // doesn't have a JSRuntime, hence without a context. Now that we |
michael@0 | 379 | // have a context, attach the memory to where it belongs. |
michael@0 | 380 | JS_updateMallocCounter(cx, contents.nbytes); |
michael@0 | 381 | mContents.forget(); |
michael@0 | 382 | |
michael@0 | 383 | aResult.setObject(*result); |
michael@0 | 384 | return NS_OK; |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | //////// Callback events |
michael@0 | 388 | |
michael@0 | 389 | /** |
michael@0 | 390 | * An event used to notify asynchronously of an error. |
michael@0 | 391 | */ |
michael@0 | 392 | class ErrorEvent MOZ_FINAL : public nsRunnable { |
michael@0 | 393 | public: |
michael@0 | 394 | /** |
michael@0 | 395 | * @param aOnSuccess The success callback. |
michael@0 | 396 | * @param aOnError The error callback. |
michael@0 | 397 | * @param aDiscardedResult The discarded result. |
michael@0 | 398 | * @param aOperation The name of the operation, used for error reporting. |
michael@0 | 399 | * @param aOSError The OS error of the operation, as returned by errno/ |
michael@0 | 400 | * GetLastError(). |
michael@0 | 401 | * |
michael@0 | 402 | * Note that we pass both the success callback and the error |
michael@0 | 403 | * callback, as well as the discarded result to ensure that they are |
michael@0 | 404 | * all released on the main thread, rather than on the IO thread |
michael@0 | 405 | * (which would hopefully segfault). Also, we pass the callbacks as |
michael@0 | 406 | * alread_AddRefed to ensure that we do not manipulate main-thread |
michael@0 | 407 | * only refcounters off the main thread. |
michael@0 | 408 | */ |
michael@0 | 409 | ErrorEvent(already_AddRefed<nsINativeOSFileSuccessCallback>&& aOnSuccess, |
michael@0 | 410 | already_AddRefed<nsINativeOSFileErrorCallback>&& aOnError, |
michael@0 | 411 | already_AddRefed<AbstractResult>& aDiscardedResult, |
michael@0 | 412 | const nsACString& aOperation, |
michael@0 | 413 | int32_t aOSError) |
michael@0 | 414 | : mOnSuccess(aOnSuccess) |
michael@0 | 415 | , mOnError(aOnError) |
michael@0 | 416 | , mDiscardedResult(aDiscardedResult) |
michael@0 | 417 | , mOSError(aOSError) |
michael@0 | 418 | , mOperation(aOperation) |
michael@0 | 419 | { |
michael@0 | 420 | MOZ_ASSERT(!NS_IsMainThread()); |
michael@0 | 421 | } |
michael@0 | 422 | |
michael@0 | 423 | NS_METHOD Run() { |
michael@0 | 424 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 425 | (void)mOnError->Complete(mOperation, mOSError); |
michael@0 | 426 | |
michael@0 | 427 | // Ensure that the callbacks are released on the main thread. |
michael@0 | 428 | mOnSuccess = nullptr; |
michael@0 | 429 | mOnError = nullptr; |
michael@0 | 430 | mDiscardedResult = nullptr; |
michael@0 | 431 | |
michael@0 | 432 | return NS_OK; |
michael@0 | 433 | } |
michael@0 | 434 | private: |
michael@0 | 435 | // The callbacks. Maintained as nsRefPtr as they are generally |
michael@0 | 436 | // xpconnect values, which cannot be manipulated with nsCOMPtr off |
michael@0 | 437 | // the main thread. We store both the success callback and the |
michael@0 | 438 | // error callback to ensure that they are safely released on the |
michael@0 | 439 | // main thread. |
michael@0 | 440 | nsRefPtr<nsINativeOSFileSuccessCallback> mOnSuccess; |
michael@0 | 441 | nsRefPtr<nsINativeOSFileErrorCallback> mOnError; |
michael@0 | 442 | nsRefPtr<AbstractResult> mDiscardedResult; |
michael@0 | 443 | int32_t mOSError; |
michael@0 | 444 | nsCString mOperation; |
michael@0 | 445 | }; |
michael@0 | 446 | |
michael@0 | 447 | /** |
michael@0 | 448 | * An event used to notify of a success. |
michael@0 | 449 | */ |
michael@0 | 450 | class SuccessEvent MOZ_FINAL : public nsRunnable { |
michael@0 | 451 | public: |
michael@0 | 452 | /** |
michael@0 | 453 | * @param aOnSuccess The success callback. |
michael@0 | 454 | * @param aOnError The error callback. |
michael@0 | 455 | * |
michael@0 | 456 | * Note that we pass both the success callback and the error |
michael@0 | 457 | * callback to ensure that they are both released on the main |
michael@0 | 458 | * thread, rather than on the IO thread (which would hopefully |
michael@0 | 459 | * segfault). Also, we pass them as alread_AddRefed to ensure that |
michael@0 | 460 | * we do not manipulate xpconnect refcounters off the main thread |
michael@0 | 461 | * (which is illegal). |
michael@0 | 462 | */ |
michael@0 | 463 | SuccessEvent(already_AddRefed<nsINativeOSFileSuccessCallback>&& aOnSuccess, |
michael@0 | 464 | already_AddRefed<nsINativeOSFileErrorCallback>&& aOnError, |
michael@0 | 465 | already_AddRefed<nsINativeOSFileResult>& aResult) |
michael@0 | 466 | : mOnSuccess(aOnSuccess) |
michael@0 | 467 | , mOnError(aOnError) |
michael@0 | 468 | , mResult(aResult) |
michael@0 | 469 | { |
michael@0 | 470 | MOZ_ASSERT(!NS_IsMainThread()); |
michael@0 | 471 | } |
michael@0 | 472 | |
michael@0 | 473 | NS_METHOD Run() { |
michael@0 | 474 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 475 | (void)mOnSuccess->Complete(mResult); |
michael@0 | 476 | |
michael@0 | 477 | // Ensure that the callbacks are released on the main thread. |
michael@0 | 478 | mOnSuccess = nullptr; |
michael@0 | 479 | mOnError = nullptr; |
michael@0 | 480 | mResult = nullptr; |
michael@0 | 481 | |
michael@0 | 482 | return NS_OK; |
michael@0 | 483 | } |
michael@0 | 484 | private: |
michael@0 | 485 | // The callbacks. Maintained as nsRefPtr as they are generally |
michael@0 | 486 | // xpconnect values, which cannot be manipulated with nsCOMPtr off |
michael@0 | 487 | // the main thread. We store both the success callback and the |
michael@0 | 488 | // error callback to ensure that they are safely released on the |
michael@0 | 489 | // main thread. |
michael@0 | 490 | nsRefPtr<nsINativeOSFileSuccessCallback> mOnSuccess; |
michael@0 | 491 | nsRefPtr<nsINativeOSFileErrorCallback> mOnError; |
michael@0 | 492 | nsRefPtr<nsINativeOSFileResult> mResult; |
michael@0 | 493 | }; |
michael@0 | 494 | |
michael@0 | 495 | |
michael@0 | 496 | //////// Action events |
michael@0 | 497 | |
michael@0 | 498 | /** |
michael@0 | 499 | * Base class shared by actions. |
michael@0 | 500 | */ |
michael@0 | 501 | class AbstractDoEvent: public nsRunnable { |
michael@0 | 502 | public: |
michael@0 | 503 | AbstractDoEvent(already_AddRefed<nsINativeOSFileSuccessCallback>& aOnSuccess, |
michael@0 | 504 | already_AddRefed<nsINativeOSFileErrorCallback>& aOnError) |
michael@0 | 505 | : mOnSuccess(aOnSuccess) |
michael@0 | 506 | , mOnError(aOnError) |
michael@0 | 507 | #if defined(DEBUG) |
michael@0 | 508 | , mResolved(false) |
michael@0 | 509 | #endif // defined(DEBUG) |
michael@0 | 510 | { |
michael@0 | 511 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 512 | } |
michael@0 | 513 | |
michael@0 | 514 | /** |
michael@0 | 515 | * Fail, asynchronously. |
michael@0 | 516 | */ |
michael@0 | 517 | void Fail(const nsACString& aOperation, |
michael@0 | 518 | already_AddRefed<AbstractResult>&& aDiscardedResult, |
michael@0 | 519 | int32_t aOSError = 0) { |
michael@0 | 520 | Resolve(); |
michael@0 | 521 | nsRefPtr<ErrorEvent> event = new ErrorEvent(mOnSuccess.forget(), |
michael@0 | 522 | mOnError.forget(), |
michael@0 | 523 | aDiscardedResult, |
michael@0 | 524 | aOperation, |
michael@0 | 525 | aOSError); |
michael@0 | 526 | nsresult rv = NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); |
michael@0 | 527 | if (NS_FAILED(rv)) { |
michael@0 | 528 | // Last ditch attempt to release on the main thread - some of |
michael@0 | 529 | // the members of event are not thread-safe, so letting the |
michael@0 | 530 | // pointer go out of scope would cause a crash. |
michael@0 | 531 | nsCOMPtr<nsIThread> main = do_GetMainThread(); |
michael@0 | 532 | NS_ProxyRelease(main, event); |
michael@0 | 533 | } |
michael@0 | 534 | } |
michael@0 | 535 | |
michael@0 | 536 | /** |
michael@0 | 537 | * Succeed, asynchronously. |
michael@0 | 538 | */ |
michael@0 | 539 | void Succeed(already_AddRefed<nsINativeOSFileResult>&& aResult) { |
michael@0 | 540 | Resolve(); |
michael@0 | 541 | nsRefPtr<SuccessEvent> event = new SuccessEvent(mOnSuccess.forget(), |
michael@0 | 542 | mOnError.forget(), |
michael@0 | 543 | aResult); |
michael@0 | 544 | nsresult rv = NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); |
michael@0 | 545 | if (NS_FAILED(rv)) { |
michael@0 | 546 | // Last ditch attempt to release on the main thread - some of |
michael@0 | 547 | // the members of event are not thread-safe, so letting the |
michael@0 | 548 | // pointer go out of scope would cause a crash. |
michael@0 | 549 | nsCOMPtr<nsIThread> main = do_GetMainThread(); |
michael@0 | 550 | NS_ProxyRelease(main, event); |
michael@0 | 551 | } |
michael@0 | 552 | |
michael@0 | 553 | } |
michael@0 | 554 | |
michael@0 | 555 | private: |
michael@0 | 556 | |
michael@0 | 557 | /** |
michael@0 | 558 | * Mark the event as complete, for debugging purposes. |
michael@0 | 559 | */ |
michael@0 | 560 | void Resolve() { |
michael@0 | 561 | #if defined(DEBUG) |
michael@0 | 562 | MOZ_ASSERT(!mResolved); |
michael@0 | 563 | mResolved = true; |
michael@0 | 564 | #endif // defined(DEBUG) |
michael@0 | 565 | } |
michael@0 | 566 | |
michael@0 | 567 | private: |
michael@0 | 568 | nsRefPtr<nsINativeOSFileSuccessCallback> mOnSuccess; |
michael@0 | 569 | nsRefPtr<nsINativeOSFileErrorCallback> mOnError; |
michael@0 | 570 | #if defined(DEBUG) |
michael@0 | 571 | // |true| once the action is complete |
michael@0 | 572 | bool mResolved; |
michael@0 | 573 | #endif // defined(DEBUG) |
michael@0 | 574 | }; |
michael@0 | 575 | |
michael@0 | 576 | /** |
michael@0 | 577 | * An abstract event implementing reading from a file. |
michael@0 | 578 | * |
michael@0 | 579 | * Concrete subclasses are responsible for handling the |
michael@0 | 580 | * data obtained from the file and possibly post-processing it. |
michael@0 | 581 | */ |
michael@0 | 582 | class AbstractReadEvent: public AbstractDoEvent { |
michael@0 | 583 | public: |
michael@0 | 584 | /** |
michael@0 | 585 | * @param aPath The path of the file. |
michael@0 | 586 | */ |
michael@0 | 587 | AbstractReadEvent(const nsAString& aPath, |
michael@0 | 588 | const uint64_t aBytes, |
michael@0 | 589 | already_AddRefed<nsINativeOSFileSuccessCallback>& aOnSuccess, |
michael@0 | 590 | already_AddRefed<nsINativeOSFileErrorCallback>& aOnError) |
michael@0 | 591 | : AbstractDoEvent(aOnSuccess, aOnError) |
michael@0 | 592 | , mPath(aPath) |
michael@0 | 593 | , mBytes(aBytes) |
michael@0 | 594 | { |
michael@0 | 595 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 596 | } |
michael@0 | 597 | |
michael@0 | 598 | NS_METHOD Run() MOZ_OVERRIDE { |
michael@0 | 599 | MOZ_ASSERT(!NS_IsMainThread()); |
michael@0 | 600 | TimeStamp dispatchDate = TimeStamp::Now(); |
michael@0 | 601 | |
michael@0 | 602 | nsresult rv = BeforeRead(); |
michael@0 | 603 | if (NS_FAILED(rv)) { |
michael@0 | 604 | // Error reporting is handled by BeforeRead(); |
michael@0 | 605 | return NS_OK; |
michael@0 | 606 | } |
michael@0 | 607 | |
michael@0 | 608 | ScopedArrayBufferContents buffer; |
michael@0 | 609 | rv = Read(buffer); |
michael@0 | 610 | if (NS_FAILED(rv)) { |
michael@0 | 611 | // Error reporting is handled by Read(); |
michael@0 | 612 | return NS_OK; |
michael@0 | 613 | } |
michael@0 | 614 | |
michael@0 | 615 | AfterRead(dispatchDate, buffer); |
michael@0 | 616 | return NS_OK; |
michael@0 | 617 | } |
michael@0 | 618 | |
michael@0 | 619 | private: |
michael@0 | 620 | /** |
michael@0 | 621 | * Read synchronously. |
michael@0 | 622 | * |
michael@0 | 623 | * Must be called off the main thread. |
michael@0 | 624 | * |
michael@0 | 625 | * @param aBuffer The destination buffer. |
michael@0 | 626 | */ |
michael@0 | 627 | nsresult Read(ScopedArrayBufferContents& aBuffer) |
michael@0 | 628 | { |
michael@0 | 629 | MOZ_ASSERT(!NS_IsMainThread()); |
michael@0 | 630 | |
michael@0 | 631 | ScopedPRFileDesc file; |
michael@0 | 632 | #if defined(XP_WIN) |
michael@0 | 633 | // On Windows, we can't use PR_OpenFile because it doesn't |
michael@0 | 634 | // handle UTF-16 encoding, which is pretty bad. In addition, |
michael@0 | 635 | // PR_OpenFile opens files without sharing, which is not the |
michael@0 | 636 | // general semantics of OS.File. |
michael@0 | 637 | HANDLE handle = |
michael@0 | 638 | ::CreateFileW(mPath.get(), |
michael@0 | 639 | GENERIC_READ, |
michael@0 | 640 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
michael@0 | 641 | /*Security attributes*/nullptr, |
michael@0 | 642 | OPEN_EXISTING, |
michael@0 | 643 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, |
michael@0 | 644 | /*Template file*/ nullptr); |
michael@0 | 645 | |
michael@0 | 646 | if (handle == INVALID_HANDLE_VALUE) { |
michael@0 | 647 | Fail(NS_LITERAL_CSTRING("open"), nullptr, ::GetLastError()); |
michael@0 | 648 | return NS_ERROR_FAILURE; |
michael@0 | 649 | } |
michael@0 | 650 | |
michael@0 | 651 | file = PR_ImportFile((PROsfd)handle); |
michael@0 | 652 | if (!file) { |
michael@0 | 653 | // |file| is closed by PR_ImportFile |
michael@0 | 654 | Fail(NS_LITERAL_CSTRING("ImportFile"), nullptr, PR_GetOSError()); |
michael@0 | 655 | return NS_ERROR_FAILURE; |
michael@0 | 656 | } |
michael@0 | 657 | |
michael@0 | 658 | #else |
michael@0 | 659 | // On other platforms, PR_OpenFile will do. |
michael@0 | 660 | NS_ConvertUTF16toUTF8 path(mPath); |
michael@0 | 661 | file = PR_OpenFile(path.get(), PR_RDONLY, 0); |
michael@0 | 662 | if (!file) { |
michael@0 | 663 | Fail(NS_LITERAL_CSTRING("open"), nullptr, PR_GetOSError()); |
michael@0 | 664 | return NS_ERROR_FAILURE; |
michael@0 | 665 | } |
michael@0 | 666 | |
michael@0 | 667 | #endif // defined(XP_XIN) |
michael@0 | 668 | |
michael@0 | 669 | PRFileInfo64 stat; |
michael@0 | 670 | if (PR_GetOpenFileInfo64(file, &stat) != PR_SUCCESS) { |
michael@0 | 671 | Fail(NS_LITERAL_CSTRING("stat"), nullptr, PR_GetOSError()); |
michael@0 | 672 | return NS_ERROR_FAILURE; |
michael@0 | 673 | } |
michael@0 | 674 | |
michael@0 | 675 | uint64_t bytes = std::min((uint64_t)stat.size, mBytes); |
michael@0 | 676 | if (bytes > UINT32_MAX) { |
michael@0 | 677 | Fail(NS_LITERAL_CSTRING("Arithmetics"), nullptr, OS_ERROR_INVAL); |
michael@0 | 678 | return NS_ERROR_FAILURE; |
michael@0 | 679 | } |
michael@0 | 680 | |
michael@0 | 681 | if (!aBuffer.Allocate(bytes)) { |
michael@0 | 682 | Fail(NS_LITERAL_CSTRING("allocate"), nullptr, OS_ERROR_NOMEM); |
michael@0 | 683 | return NS_ERROR_FAILURE; |
michael@0 | 684 | } |
michael@0 | 685 | |
michael@0 | 686 | uint64_t total_read = 0; |
michael@0 | 687 | int32_t just_read = 0; |
michael@0 | 688 | char* dest_chars = reinterpret_cast<char*>(aBuffer.rwget().data); |
michael@0 | 689 | do { |
michael@0 | 690 | just_read = PR_Read(file, dest_chars + total_read, |
michael@0 | 691 | std::min(uint64_t(PR_INT32_MAX), bytes - total_read)); |
michael@0 | 692 | if (just_read == -1) { |
michael@0 | 693 | Fail(NS_LITERAL_CSTRING("read"), nullptr, PR_GetOSError()); |
michael@0 | 694 | return NS_ERROR_FAILURE; |
michael@0 | 695 | } |
michael@0 | 696 | total_read += just_read; |
michael@0 | 697 | } while (just_read != 0 && total_read < bytes); |
michael@0 | 698 | if (total_read != bytes) { |
michael@0 | 699 | // We seem to have a race condition here. |
michael@0 | 700 | Fail(NS_LITERAL_CSTRING("read"), nullptr, OS_ERROR_RACE); |
michael@0 | 701 | return NS_ERROR_FAILURE; |
michael@0 | 702 | } |
michael@0 | 703 | |
michael@0 | 704 | return NS_OK; |
michael@0 | 705 | } |
michael@0 | 706 | |
michael@0 | 707 | protected: |
michael@0 | 708 | /** |
michael@0 | 709 | * Any steps that need to be taken before reading. |
michael@0 | 710 | * |
michael@0 | 711 | * In case of error, this method should call Fail() and return |
michael@0 | 712 | * a failure code. |
michael@0 | 713 | */ |
michael@0 | 714 | virtual |
michael@0 | 715 | nsresult BeforeRead() { |
michael@0 | 716 | return NS_OK; |
michael@0 | 717 | } |
michael@0 | 718 | |
michael@0 | 719 | /** |
michael@0 | 720 | * Proceed after reading. |
michael@0 | 721 | */ |
michael@0 | 722 | virtual |
michael@0 | 723 | void AfterRead(TimeStamp aDispatchDate, ScopedArrayBufferContents& aBuffer) = 0; |
michael@0 | 724 | |
michael@0 | 725 | protected: |
michael@0 | 726 | const nsString mPath; |
michael@0 | 727 | const uint64_t mBytes; |
michael@0 | 728 | }; |
michael@0 | 729 | |
michael@0 | 730 | /** |
michael@0 | 731 | * An implementation of a Read event that provides the data |
michael@0 | 732 | * as a TypedArray. |
michael@0 | 733 | */ |
michael@0 | 734 | class DoReadToTypedArrayEvent MOZ_FINAL : public AbstractReadEvent { |
michael@0 | 735 | public: |
michael@0 | 736 | DoReadToTypedArrayEvent(const nsAString& aPath, |
michael@0 | 737 | const uint32_t aBytes, |
michael@0 | 738 | already_AddRefed<nsINativeOSFileSuccessCallback>&& aOnSuccess, |
michael@0 | 739 | already_AddRefed<nsINativeOSFileErrorCallback>&& aOnError) |
michael@0 | 740 | : AbstractReadEvent(aPath, aBytes, |
michael@0 | 741 | aOnSuccess, aOnError) |
michael@0 | 742 | , mResult(new TypedArrayResult(TimeStamp::Now())) |
michael@0 | 743 | { } |
michael@0 | 744 | |
michael@0 | 745 | ~DoReadToTypedArrayEvent() { |
michael@0 | 746 | // If AbstractReadEvent::Run() has bailed out, we may need to cleanup |
michael@0 | 747 | // mResult, which is main-thread only data |
michael@0 | 748 | if (!mResult) { |
michael@0 | 749 | return; |
michael@0 | 750 | } |
michael@0 | 751 | nsCOMPtr<nsIThread> main = do_GetMainThread(); |
michael@0 | 752 | (void)NS_ProxyRelease(main, mResult); |
michael@0 | 753 | } |
michael@0 | 754 | |
michael@0 | 755 | protected: |
michael@0 | 756 | void AfterRead(TimeStamp aDispatchDate, |
michael@0 | 757 | ScopedArrayBufferContents& aBuffer) MOZ_OVERRIDE { |
michael@0 | 758 | MOZ_ASSERT(!NS_IsMainThread()); |
michael@0 | 759 | mResult->Init(aDispatchDate, TimeStamp::Now() - aDispatchDate, aBuffer.forget()); |
michael@0 | 760 | Succeed(mResult.forget()); |
michael@0 | 761 | } |
michael@0 | 762 | |
michael@0 | 763 | private: |
michael@0 | 764 | nsRefPtr<TypedArrayResult> mResult; |
michael@0 | 765 | }; |
michael@0 | 766 | |
michael@0 | 767 | /** |
michael@0 | 768 | * An implementation of a Read event that provides the data |
michael@0 | 769 | * as a JavaScript string. |
michael@0 | 770 | */ |
michael@0 | 771 | class DoReadToStringEvent MOZ_FINAL : public AbstractReadEvent { |
michael@0 | 772 | public: |
michael@0 | 773 | DoReadToStringEvent(const nsAString& aPath, |
michael@0 | 774 | const nsACString& aEncoding, |
michael@0 | 775 | const uint32_t aBytes, |
michael@0 | 776 | already_AddRefed<nsINativeOSFileSuccessCallback>&& aOnSuccess, |
michael@0 | 777 | already_AddRefed<nsINativeOSFileErrorCallback>&& aOnError) |
michael@0 | 778 | : AbstractReadEvent(aPath, aBytes, aOnSuccess, aOnError) |
michael@0 | 779 | , mEncoding(aEncoding) |
michael@0 | 780 | , mResult(new StringResult(TimeStamp::Now())) |
michael@0 | 781 | { } |
michael@0 | 782 | |
michael@0 | 783 | ~DoReadToStringEvent() { |
michael@0 | 784 | // If AbstraactReadEvent::Run() has bailed out, we may need to cleanup |
michael@0 | 785 | // mResult, which is main-thread only data |
michael@0 | 786 | if (!mResult) { |
michael@0 | 787 | return; |
michael@0 | 788 | } |
michael@0 | 789 | nsCOMPtr<nsIThread> main = do_GetMainThread(); |
michael@0 | 790 | (void)NS_ProxyRelease(main, mResult); |
michael@0 | 791 | } |
michael@0 | 792 | |
michael@0 | 793 | protected: |
michael@0 | 794 | nsresult BeforeRead() MOZ_OVERRIDE { |
michael@0 | 795 | // Obtain the decoder. We do this before reading to avoid doing |
michael@0 | 796 | // any unnecessary I/O in case the name of the encoding is incorrect. |
michael@0 | 797 | MOZ_ASSERT(!NS_IsMainThread()); |
michael@0 | 798 | nsAutoCString encodingName; |
michael@0 | 799 | if (!dom::EncodingUtils::FindEncodingForLabel(mEncoding, encodingName)) { |
michael@0 | 800 | Fail(NS_LITERAL_CSTRING("Decode"), mResult.forget(), OS_ERROR_INVAL); |
michael@0 | 801 | return NS_ERROR_FAILURE; |
michael@0 | 802 | } |
michael@0 | 803 | mDecoder = dom::EncodingUtils::DecoderForEncoding(encodingName); |
michael@0 | 804 | if (!mDecoder) { |
michael@0 | 805 | Fail(NS_LITERAL_CSTRING("DecoderForEncoding"), mResult.forget(), OS_ERROR_INVAL); |
michael@0 | 806 | return NS_ERROR_FAILURE; |
michael@0 | 807 | } |
michael@0 | 808 | |
michael@0 | 809 | return NS_OK; |
michael@0 | 810 | } |
michael@0 | 811 | |
michael@0 | 812 | void AfterRead(TimeStamp aDispatchDate, |
michael@0 | 813 | ScopedArrayBufferContents& aBuffer) MOZ_OVERRIDE { |
michael@0 | 814 | MOZ_ASSERT(!NS_IsMainThread()); |
michael@0 | 815 | |
michael@0 | 816 | int32_t maxChars; |
michael@0 | 817 | const char* sourceChars = reinterpret_cast<const char*>(aBuffer.get().data); |
michael@0 | 818 | int32_t sourceBytes = aBuffer.get().nbytes; |
michael@0 | 819 | if (sourceBytes < 0) { |
michael@0 | 820 | Fail(NS_LITERAL_CSTRING("arithmetics"), mResult.forget(), OS_ERROR_TOO_LARGE); |
michael@0 | 821 | return; |
michael@0 | 822 | } |
michael@0 | 823 | |
michael@0 | 824 | nsresult rv = mDecoder->GetMaxLength(sourceChars, sourceBytes, &maxChars); |
michael@0 | 825 | if (NS_FAILED(rv)) { |
michael@0 | 826 | Fail(NS_LITERAL_CSTRING("GetMaxLength"), mResult.forget(), OS_ERROR_INVAL); |
michael@0 | 827 | return; |
michael@0 | 828 | } |
michael@0 | 829 | |
michael@0 | 830 | if (maxChars < 0) { |
michael@0 | 831 | Fail(NS_LITERAL_CSTRING("arithmetics"), mResult.forget(), OS_ERROR_TOO_LARGE); |
michael@0 | 832 | return; |
michael@0 | 833 | } |
michael@0 | 834 | |
michael@0 | 835 | nsString resultString; |
michael@0 | 836 | resultString.SetLength(maxChars); |
michael@0 | 837 | if (resultString.Length() != (nsString::size_type)maxChars) { |
michael@0 | 838 | Fail(NS_LITERAL_CSTRING("allocation"), mResult.forget(), OS_ERROR_TOO_LARGE); |
michael@0 | 839 | return; |
michael@0 | 840 | } |
michael@0 | 841 | |
michael@0 | 842 | |
michael@0 | 843 | rv = mDecoder->Convert(sourceChars, &sourceBytes, |
michael@0 | 844 | resultString.BeginWriting(), &maxChars); |
michael@0 | 845 | MOZ_ASSERT(NS_SUCCEEDED(rv)); |
michael@0 | 846 | resultString.SetLength(maxChars); |
michael@0 | 847 | |
michael@0 | 848 | mResult->Init(aDispatchDate, TimeStamp::Now() - aDispatchDate, resultString); |
michael@0 | 849 | Succeed(mResult.forget()); |
michael@0 | 850 | } |
michael@0 | 851 | |
michael@0 | 852 | private: |
michael@0 | 853 | nsCString mEncoding; |
michael@0 | 854 | nsCOMPtr<nsIUnicodeDecoder> mDecoder; |
michael@0 | 855 | nsRefPtr<StringResult> mResult; |
michael@0 | 856 | }; |
michael@0 | 857 | |
michael@0 | 858 | } // osfile |
michael@0 | 859 | |
michael@0 | 860 | // The OS.File service |
michael@0 | 861 | |
michael@0 | 862 | NS_IMPL_ISUPPORTS(NativeOSFileInternalsService, nsINativeOSFileInternalsService); |
michael@0 | 863 | |
michael@0 | 864 | NS_IMETHODIMP |
michael@0 | 865 | NativeOSFileInternalsService::Read(const nsAString& aPath, |
michael@0 | 866 | JS::HandleValue aOptions, |
michael@0 | 867 | nsINativeOSFileSuccessCallback *aOnSuccess, |
michael@0 | 868 | nsINativeOSFileErrorCallback *aOnError, |
michael@0 | 869 | JSContext* cx) |
michael@0 | 870 | { |
michael@0 | 871 | // Extract options |
michael@0 | 872 | nsCString encoding; |
michael@0 | 873 | uint64_t bytes = UINT64_MAX; |
michael@0 | 874 | |
michael@0 | 875 | if (aOptions.isObject()) { |
michael@0 | 876 | dom::NativeOSFileReadOptions dict; |
michael@0 | 877 | if (!dict.Init(cx, aOptions)) { |
michael@0 | 878 | return NS_ERROR_INVALID_ARG; |
michael@0 | 879 | } |
michael@0 | 880 | |
michael@0 | 881 | if (dict.mEncoding.WasPassed()) { |
michael@0 | 882 | CopyUTF16toUTF8(dict.mEncoding.Value(), encoding); |
michael@0 | 883 | } |
michael@0 | 884 | |
michael@0 | 885 | if (dict.mBytes.WasPassed() && !dict.mBytes.Value().IsNull()) { |
michael@0 | 886 | bytes = dict.mBytes.Value().Value(); |
michael@0 | 887 | } |
michael@0 | 888 | } |
michael@0 | 889 | |
michael@0 | 890 | // Prepare the off main thread event and dispatch it |
michael@0 | 891 | nsCOMPtr<nsINativeOSFileSuccessCallback> onSuccess(aOnSuccess); |
michael@0 | 892 | nsCOMPtr<nsINativeOSFileErrorCallback> onError(aOnError); |
michael@0 | 893 | |
michael@0 | 894 | nsRefPtr<AbstractDoEvent> event; |
michael@0 | 895 | if (encoding.IsEmpty()) { |
michael@0 | 896 | event = new DoReadToTypedArrayEvent(aPath, bytes, |
michael@0 | 897 | onSuccess.forget(), |
michael@0 | 898 | onError.forget()); |
michael@0 | 899 | } else { |
michael@0 | 900 | event = new DoReadToStringEvent(aPath, encoding, bytes, |
michael@0 | 901 | onSuccess.forget(), |
michael@0 | 902 | onError.forget()); |
michael@0 | 903 | } |
michael@0 | 904 | |
michael@0 | 905 | nsresult rv; |
michael@0 | 906 | nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv); |
michael@0 | 907 | |
michael@0 | 908 | if (NS_FAILED(rv)) { |
michael@0 | 909 | return rv; |
michael@0 | 910 | } |
michael@0 | 911 | return target->Dispatch(event, NS_DISPATCH_NORMAL); |
michael@0 | 912 | } |
michael@0 | 913 | |
michael@0 | 914 | } // namespace mozilla |
michael@0 | 915 |