blob: 3eb4e47b2affb3eac0e7f5a1995dcb4781810dbe [file] [log] [blame]
manzagopf2322662016-09-27 11:39:591// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/browser_watcher/stability_debugging_win.h"
6
7#include <string>
8
manzagop14aff5d2016-11-23 17:27:009#include "base/feature_list.h"
10#include "base/files/file.h"
manzagopf2322662016-09-27 11:39:5911#include "base/metrics/persistent_memory_allocator.h"
12#include "base/path_service.h"
manzagop14aff5d2016-11-23 17:27:0013#include "base/process/process.h"
manzagopf2322662016-09-27 11:39:5914#include "base/strings/stringprintf.h"
15#include "base/time/time.h"
manzagop14aff5d2016-11-23 17:27:0016#include "components/browser_watcher/features.h"
manzagopf2322662016-09-27 11:39:5917
18namespace browser_watcher {
19
20namespace {
21
22bool GetCreationTime(const base::Process& process, base::Time* time) {
23 DCHECK(time);
24
25 FILETIME creation_time = {};
26 FILETIME ignore1 = {};
27 FILETIME ignore2 = {};
28 FILETIME ignore3 = {};
29 if (!::GetProcessTimes(process.Handle(), &creation_time, &ignore1, &ignore2,
30 &ignore3)) {
31 return false;
32 }
33 *time = base::Time::FromFileTime(creation_time);
34 return true;
35}
36
37} // namespace
38
39base::FilePath GetStabilityDir(const base::FilePath& user_data_dir) {
40 return user_data_dir.AppendASCII("Stability");
41}
42
43bool GetStabilityFileForProcess(const base::Process& process,
44 const base::FilePath& user_data_dir,
45 base::FilePath* file_path) {
46 DCHECK(file_path);
47 base::FilePath stability_dir = GetStabilityDir(user_data_dir);
48
49 // Build the name using the pid and creation time. On windows, this is unique
50 // even after the process exits.
51 base::Time creation_time;
52 if (!GetCreationTime(process, &creation_time))
53 return false;
54
55 std::string file_name =
56 base::StringPrintf("%u-%llu", process.Pid(), creation_time.ToJavaTime());
57 *file_path = stability_dir.AppendASCII(file_name).AddExtension(
58 base::PersistentMemoryAllocator::kFileExtension);
59 return true;
60}
61
62base::FilePath::StringType GetStabilityFilePattern() {
63 return base::FilePath::StringType(FILE_PATH_LITERAL("*-*")) +
64 base::PersistentMemoryAllocator::kFileExtension;
65}
66
manzagop14aff5d2016-11-23 17:27:0067void MarkStabilityFileForDeletion(const base::FilePath& user_data_dir) {
68 if (!base::FeatureList::IsEnabled(
69 browser_watcher::kStabilityDebuggingFeature)) {
70 return;
71 }
72
73 base::FilePath stability_file;
74 if (!GetStabilityFileForProcess(base::Process::Current(), user_data_dir,
75 &stability_file)) {
76 // TODO(manzagop): add a metric for this.
77 return;
78 }
79
80 // Open (with delete) and then immediately close the file by going out of
81 // scope. This should cause the stability debugging file to be deleted prior
82 // to the next execution.
83 base::File file(stability_file, base::File::FLAG_OPEN |
84 base::File::FLAG_READ |
85 base::File::FLAG_DELETE_ON_CLOSE);
86}
87
manzagopf2322662016-09-27 11:39:5988} // namespace browser_watcher