-#if defined(OS_CHROMEOS)
-// Small helper class used to create temporary log file and pass its
-// handle and error status to callback.
-// Use case:
-// DebugLogFileHelper* helper = new DebugLogFileHelper();
-// base::WorkerPool::PostTaskAndReply(FROM_HERE,
-// base::Bind(&DebugLogFileHelper::DoWork, base::Unretained(helper), ...),
-// base::Bind(&DebugLogFileHelper::Reply, base::Owned(helper), ...),
-// false);
-class DebugLogFileHelper {
- public:
- typedef base::Callback<void(PassPlatformFile pass_platform_file,
- bool created,
- PlatformFileError error,
- const base::FilePath& file_path)>
- DebugLogFileCallback;
-
- DebugLogFileHelper()
- : file_handle_(base::kInvalidPlatformFileValue),
- created_(false),
- error_(base::PLATFORM_FILE_OK) {
- }
-
- ~DebugLogFileHelper() {
- }
-
- void DoWork(const base::FilePath& fileshelf) {
- const base::FilePath::CharType kLogFileName[] =
- FILE_PATH_LITERAL("debug-log.tgz");
-
- file_path_ = fileshelf.Append(kLogFileName);
- file_path_ = logging::GenerateTimestampedName(file_path_,
- base::Time::Now());
-
- int flags =
- base::PLATFORM_FILE_CREATE_ALWAYS |
- base::PLATFORM_FILE_WRITE;
- file_handle_ = base::CreatePlatformFile(file_path_, flags,
- &created_, &error_);
- }
-
- void Reply(const DebugLogFileCallback& callback) {
- DCHECK(!callback.is_null());
- callback.Run(PassPlatformFile(&file_handle_), created_, error_, file_path_);
- }
-
- private:
- PlatformFile file_handle_;
- bool created_;
- PlatformFileError error_;
- base::FilePath file_path_;
-
- DISALLOW_COPY_AND_ASSIGN(DebugLogFileHelper);
-};
-
-// Following functions are used for getting debug logs. Logs are
-// fetched from /var/log/* and put on the fileshelf.
-
-// Called once StoreDebugLogs is complete. Takes two parameters:
-// - log_path: where the log file was saved in the case of success;
-// - succeeded: was the log file saved successfully.
-typedef base::Callback<void(const base::FilePath& log_path,
- bool succeded)> StoreDebugLogsCallback;
-
-// Closes file handle, so, should be called on the WorkerPool thread.
-void CloseDebugLogFile(PassPlatformFile pass_platform_file) {
- base::ClosePlatformFile(pass_platform_file.ReleaseValue());
-}
-
-// Closes file handle and deletes debug log file, so, should be called
-// on the WorkerPool thread.
-void CloseAndDeleteDebugLogFile(PassPlatformFile pass_platform_file,
- const base::FilePath& file_path) {
- CloseDebugLogFile(pass_platform_file);
- base::DeleteFile(file_path, false);
-}
-
-// Called upon completion of |WriteDebugLogToFile|. Closes file
-// descriptor, deletes log file in the case of failure and calls
-// |callback|.
-void WriteDebugLogToFileCompleted(const StoreDebugLogsCallback& callback,
- PassPlatformFile pass_platform_file,
- const base::FilePath& file_path,
- bool succeeded) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- if (!succeeded) {
- bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE,
- base::Bind(&CloseAndDeleteDebugLogFile, pass_platform_file, file_path),
- base::Bind(callback, file_path, false), false);
- DCHECK(posted);
- return;
- }
- bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE,
- base::Bind(&CloseDebugLogFile, pass_platform_file),
- base::Bind(callback, file_path, true), false);
- DCHECK(posted);
-}
-
-// Stores into |file_path| debug logs in the .tgz format. Calls
-// |callback| upon completion.
-void WriteDebugLogToFile(const StoreDebugLogsCallback& callback,
- PassPlatformFile pass_platform_file,
- bool created,
- PlatformFileError error,
- const base::FilePath& file_path) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- if (!created) {
- LOG(ERROR) <<
- "Can't create debug log file: " << file_path.AsUTF8Unsafe() << ", " <<
- "error: " << error;
- bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE,
- base::Bind(&CloseDebugLogFile, pass_platform_file),
- base::Bind(callback, file_path, false), false);
- DCHECK(posted);
- return;
- }
- PlatformFile platform_file = pass_platform_file.ReleaseValue();
- chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->GetDebugLogs(
- platform_file,
- base::Bind(&WriteDebugLogToFileCompleted,
- callback, PassPlatformFile(&platform_file), file_path));
-}
-
-// Stores debug logs in the .tgz archive on the |fileshelf|. The file
-// is created on the worker pool, then writing to it is triggered from
-// the UI thread, and finally it is closed (on success) or deleted (on
-// failure) on the worker pool, prior to calling |callback|.
-void StoreDebugLogs(const base::FilePath& fileshelf,
- const StoreDebugLogsCallback& callback) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- DCHECK(!callback.is_null());
- DebugLogFileHelper* helper = new DebugLogFileHelper();
- bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE,
- base::Bind(&DebugLogFileHelper::DoWork,
- base::Unretained(helper), fileshelf),
- base::Bind(&DebugLogFileHelper::Reply, base::Owned(helper),
- base::Bind(&WriteDebugLogToFile, callback)), false);
- DCHECK(posted);
-}
-#endif // defined(OS_CHROMEOS)
-