nsEdgeMigrationUtils.cpp (1797B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "nsEdgeMigrationUtils.h" 6 7 #include "mozilla/dom/Promise.h" 8 #include "nsCOMPtr.h" 9 #include "nsIEventTarget.h" 10 11 #include <windows.h> 12 13 namespace mozilla { 14 15 NS_IMPL_ISUPPORTS(nsEdgeMigrationUtils, nsIEdgeMigrationUtils) 16 17 NS_IMETHODIMP 18 nsEdgeMigrationUtils::IsDbLocked(nsIFile* aFile, JSContext* aCx, 19 dom::Promise** aPromise) { 20 NS_ENSURE_ARG_POINTER(aFile); 21 22 nsString path; 23 nsresult rv = aFile->GetPath(path); 24 NS_ENSURE_SUCCESS(rv, rv); 25 26 ErrorResult err; 27 RefPtr<dom::Promise> promise = 28 dom::Promise::Create(xpc::CurrentNativeGlobal(aCx), err); 29 30 if (MOZ_UNLIKELY(err.Failed())) { 31 return err.StealNSResult(); 32 } 33 34 nsMainThreadPtrHandle<dom::Promise> promiseHolder( 35 new nsMainThreadPtrHolder<dom::Promise>("nsEdgeMigrationUtils Promise", 36 promise)); 37 38 NS_DispatchBackgroundTask(NS_NewRunnableFunction( 39 __func__, 40 [promiseHolder = std::move(promiseHolder), path = std::move(path)]() { 41 HANDLE file = ::CreateFileW(path.get(), GENERIC_READ, FILE_SHARE_READ, 42 nullptr, OPEN_EXISTING, 0, nullptr); 43 44 bool locked = true; 45 if (file != INVALID_HANDLE_VALUE) { 46 locked = false; 47 ::CloseHandle(file); 48 } 49 50 NS_DispatchToMainThread(NS_NewRunnableFunction( 51 __func__, [promiseHolder = std::move(promiseHolder), locked]() { 52 promiseHolder.get()->MaybeResolve(locked); 53 })); 54 })); 55 56 promise.forget(aPromise); 57 58 return NS_OK; 59 } 60 61 } // namespace mozilla