OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/browser_watcher/stability_paths.h" | 5 #include "components/browser_watcher/stability_paths.h" |
6 | 6 |
| 7 #if defined(OS_WIN) |
| 8 #include <windows.h> |
| 9 #endif // defined(OS_WIN) |
| 10 |
7 #include <string> | 11 #include <string> |
8 | 12 |
9 #include "base/feature_list.h" | 13 #include "base/feature_list.h" |
10 #include "base/files/file.h" | 14 #include "base/files/file.h" |
11 #include "base/metrics/persistent_memory_allocator.h" | 15 #include "base/metrics/persistent_memory_allocator.h" |
12 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
13 #include "base/time/time.h" | 17 #include "base/time/time.h" |
14 #include "components/browser_watcher/features.h" | 18 #include "components/browser_watcher/features.h" |
15 | 19 |
16 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
17 | 21 |
| 22 #include "third_party/crashpad/crashpad/util/win/time.h" |
| 23 |
18 namespace browser_watcher { | 24 namespace browser_watcher { |
19 namespace { | 25 namespace { |
20 | 26 |
21 bool GetCreationTime(const base::Process& process, base::Time* time) { | 27 bool GetCreationTime(const base::Process& process, FILETIME* creation_time) { |
22 DCHECK(time); | 28 FILETIME ignore; |
23 | 29 return ::GetProcessTimes(process.Handle(), creation_time, &ignore, &ignore, |
24 FILETIME creation_time = {}; | 30 &ignore) != 0; |
25 FILETIME ignore1 = {}; | |
26 FILETIME ignore2 = {}; | |
27 FILETIME ignore3 = {}; | |
28 if (!::GetProcessTimes(process.Handle(), &creation_time, &ignore1, &ignore2, | |
29 &ignore3)) { | |
30 return false; | |
31 } | |
32 *time = base::Time::FromFileTime(creation_time); | |
33 return true; | |
34 } | 31 } |
35 | 32 |
36 } // namespace | 33 } // namespace |
37 | 34 |
38 base::FilePath GetStabilityDir(const base::FilePath& user_data_dir) { | 35 base::FilePath GetStabilityDir(const base::FilePath& user_data_dir) { |
39 return user_data_dir.AppendASCII("Stability"); | 36 return user_data_dir.AppendASCII("Stability"); |
40 } | 37 } |
41 | 38 |
| 39 base::FilePath GetStabilityFileForProcess(base::ProcessId pid, |
| 40 timeval creation_time, |
| 41 const base::FilePath& user_data_dir) { |
| 42 base::FilePath stability_dir = GetStabilityDir(user_data_dir); |
| 43 |
| 44 constexpr uint64_t kMicrosecondsPerSecond = static_cast<uint64_t>(1E6); |
| 45 int64_t creation_time_us = |
| 46 creation_time.tv_sec * kMicrosecondsPerSecond + creation_time.tv_usec; |
| 47 |
| 48 std::string file_name = base::StringPrintf("%u-%lld", pid, creation_time_us); |
| 49 return stability_dir.AppendASCII(file_name).AddExtension( |
| 50 base::PersistentMemoryAllocator::kFileExtension); |
| 51 } |
| 52 |
42 bool GetStabilityFileForProcess(const base::Process& process, | 53 bool GetStabilityFileForProcess(const base::Process& process, |
43 const base::FilePath& user_data_dir, | 54 const base::FilePath& user_data_dir, |
44 base::FilePath* file_path) { | 55 base::FilePath* file_path) { |
45 DCHECK(file_path); | 56 DCHECK(file_path); |
46 base::FilePath stability_dir = GetStabilityDir(user_data_dir); | |
47 | 57 |
48 // Build the name using the pid and creation time. On windows, this is unique | 58 FILETIME creation_time; |
49 // even after the process exits. | |
50 base::Time creation_time; | |
51 if (!GetCreationTime(process, &creation_time)) | 59 if (!GetCreationTime(process, &creation_time)) |
52 return false; | 60 return false; |
53 | 61 |
54 std::string file_name = | 62 // We rely on Crashpad's conversion to ensure the resulting filename is the |
55 base::StringPrintf("%u-%llu", process.Pid(), creation_time.ToJavaTime()); | 63 // same as on crash, when the creation time is obtained via Crashpad. |
56 *file_path = stability_dir.AppendASCII(file_name).AddExtension( | 64 *file_path = GetStabilityFileForProcess( |
57 base::PersistentMemoryAllocator::kFileExtension); | 65 process.Pid(), crashpad::FiletimeToTimevalEpoch(creation_time), |
| 66 user_data_dir); |
58 return true; | 67 return true; |
59 } | 68 } |
60 | 69 |
61 base::FilePath::StringType GetStabilityFilePattern() { | 70 base::FilePath::StringType GetStabilityFilePattern() { |
62 return base::FilePath::StringType(FILE_PATH_LITERAL("*-*")) + | 71 return base::FilePath::StringType(FILE_PATH_LITERAL("*-*")) + |
63 base::PersistentMemoryAllocator::kFileExtension; | 72 base::PersistentMemoryAllocator::kFileExtension; |
64 } | 73 } |
65 | 74 |
66 void MarkStabilityFileForDeletion(const base::FilePath& user_data_dir) { | 75 void MarkStabilityFileForDeletion(const base::FilePath& user_data_dir) { |
67 if (!base::FeatureList::IsEnabled( | 76 if (!base::FeatureList::IsEnabled( |
(...skipping 12 matching lines...) Expand all Loading... |
80 // scope. This should cause the stability debugging file to be deleted prior | 89 // scope. This should cause the stability debugging file to be deleted prior |
81 // to the next execution. | 90 // to the next execution. |
82 base::File file(stability_file, base::File::FLAG_OPEN | | 91 base::File file(stability_file, base::File::FLAG_OPEN | |
83 base::File::FLAG_READ | | 92 base::File::FLAG_READ | |
84 base::File::FLAG_DELETE_ON_CLOSE); | 93 base::File::FLAG_DELETE_ON_CLOSE); |
85 } | 94 } |
86 | 95 |
87 } // namespace browser_watcher | 96 } // namespace browser_watcher |
88 | 97 |
89 #endif // defined(OS_WIN) | 98 #endif // defined(OS_WIN) |
OLD | NEW |