create user data dir on thread that allows IO

This commit is contained in:
deepak1556 2017-12-09 22:54:37 +05:30 committed by Cheng Zhao
parent 249bd41e11
commit 5c25855ac5
2 changed files with 43 additions and 7 deletions

View file

@ -14,11 +14,31 @@
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/thread_task_runner_handle.h"
#include "brightray/browser/brightray_paths.h"
#include "content/public/browser/browser_thread.h"
namespace atom {
namespace {
bool CreateUserDataDirectory() {
// Make sure the userData directory is created.
base::FilePath user_data;
bool success = false;
if (PathService::Get(brightray::DIR_USER_DATA, &user_data))
success = base::CreateDirectoryAndGetError(user_data, nullptr);
if (!success) {
NOTREACHED() << "Failed to create directory '" << user_data.value() << "'";
}
return success;
}
} // namespace
Browser::Browser()
: is_quiting_(false),
is_exiting_(false),
@ -150,14 +170,25 @@ void Browser::WillFinishLaunching() {
}
void Browser::DidFinishLaunching(const base::DictionaryValue& launch_info) {
// Make sure the userData directory is created.
base::FilePath user_data;
if (PathService::Get(brightray::DIR_USER_DATA, &user_data))
base::CreateDirectoryAndGetError(user_data, nullptr);
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
is_ready_ = true;
for (BrowserObserver& observer : observers_)
observer.OnFinishLaunching(launch_info);
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_BLOCKING},
base::Bind(&CreateUserDataDirectory),
base::Bind(&Browser::OnUserDataDirectoryCreated, base::Unretained(this),
launch_info));
}
void Browser::OnUserDataDirectoryCreated(
const base::DictionaryValue& launch_info,
bool success) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (success) {
is_ready_ = true;
for (BrowserObserver& observer : observers_)
observer.OnFinishLaunching(launch_info);
}
}
void Browser::OnAccessibilitySupportChanged() {

View file

@ -258,6 +258,11 @@ class Browser : public WindowListObserver {
void OnWindowCloseCancelled(NativeWindow* window) override;
void OnWindowAllClosed() override;
// Called after attempting to create the user data directory
// for the app.
void OnUserDataDirectoryCreated(const base::DictionaryValue& launch_info,
bool success);
// Observers of the browser.
base::ObserverList<BrowserObserver> observers_;