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" | |
Mark Mentovai
2017/05/04 16:47:10
You’ll probably need to update a DEPS file to be a
manzagop (departed)
2017/05/04 19:52:41
components/browser_watcher/DEPS already has "+thir
| |
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 const uint64_t kMicrosecondsPerSecond = static_cast<uint64_t>(1E6); |
Mark Mentovai
2017/05/04 16:47:10
This is only used in one spot, you can move it to
manzagop (departed)
2017/05/04 19:52:41
Done.
| |
22 DCHECK(time); | |
23 | 28 |
24 FILETIME creation_time = {}; | 29 bool GetCreationTime(const base::Process& process, FILETIME* creation_time) { |
30 DCHECK(creation_time); | |
Mark Mentovai
2017/05/04 16:47:10
It’s OK to not do this and let GetProcessTimes() c
manzagop (departed)
2017/05/04 19:52:41
Done.
| |
31 | |
25 FILETIME ignore1 = {}; | 32 FILETIME ignore1 = {}; |
26 FILETIME ignore2 = {}; | 33 FILETIME ignore2 = {}; |
Mark Mentovai
2017/05/04 16:47:10
Is it OK to use the same “ignore” each time? We do
manzagop (departed)
2017/05/04 19:52:41
Looks like exit_code_watcher_win.cc does this too.
| |
27 FILETIME ignore3 = {}; | 34 FILETIME ignore3 = {}; |
Mark Mentovai
2017/05/04 16:47:10
And since you’re just gonna ignore it, there’s no
manzagop (departed)
2017/05/04 19:52:41
Done.
| |
28 if (!::GetProcessTimes(process.Handle(), &creation_time, &ignore1, &ignore2, | 35 return ::GetProcessTimes(process.Handle(), creation_time, &ignore1, &ignore2, |
29 &ignore3)) { | 36 &ignore3) != 0; |
30 return false; | |
31 } | |
32 *time = base::Time::FromFileTime(creation_time); | |
33 return true; | |
34 } | 37 } |
35 | 38 |
36 } // namespace | 39 } // namespace |
37 | 40 |
38 base::FilePath GetStabilityDir(const base::FilePath& user_data_dir) { | 41 base::FilePath GetStabilityDir(const base::FilePath& user_data_dir) { |
39 return user_data_dir.AppendASCII("Stability"); | 42 return user_data_dir.AppendASCII("Stability"); |
40 } | 43 } |
41 | 44 |
45 base::FilePath GetStabilityFileForProcess(base::ProcessId pid, | |
46 timeval creation_time, | |
47 const base::FilePath& user_data_dir) { | |
48 base::FilePath stability_dir = GetStabilityDir(user_data_dir); | |
49 | |
50 int64_t creation_time_us = | |
51 creation_time.tv_sec * kMicrosecondsPerSecond + creation_time.tv_usec; | |
52 std::string file_name = base::StringPrintf("%u-%llu", pid, creation_time_us); | |
53 return stability_dir.AppendASCII(file_name).AddExtension( | |
54 base::PersistentMemoryAllocator::kFileExtension); | |
55 } | |
56 | |
42 bool GetStabilityFileForProcess(const base::Process& process, | 57 bool GetStabilityFileForProcess(const base::Process& process, |
43 const base::FilePath& user_data_dir, | 58 const base::FilePath& user_data_dir, |
44 base::FilePath* file_path) { | 59 base::FilePath* file_path) { |
45 DCHECK(file_path); | 60 DCHECK(file_path); |
46 base::FilePath stability_dir = GetStabilityDir(user_data_dir); | |
47 | 61 |
48 // Build the name using the pid and creation time. On windows, this is unique | 62 FILETIME creation_time; |
49 // even after the process exits. | |
50 base::Time creation_time; | |
51 if (!GetCreationTime(process, &creation_time)) | 63 if (!GetCreationTime(process, &creation_time)) |
52 return false; | 64 return false; |
53 | 65 |
54 std::string file_name = | 66 // We rely on Crashpad's conversion to ensure the resulting filename is the |
55 base::StringPrintf("%u-%llu", process.Pid(), creation_time.ToJavaTime()); | 67 // same as on crash, when the creation time is obtained via Crashpad. |
56 *file_path = stability_dir.AppendASCII(file_name).AddExtension( | 68 *file_path = GetStabilityFileForProcess( |
57 base::PersistentMemoryAllocator::kFileExtension); | 69 process.Pid(), crashpad::FiletimeToTimevalEpoch(creation_time), |
70 user_data_dir); | |
58 return true; | 71 return true; |
59 } | 72 } |
60 | 73 |
61 base::FilePath::StringType GetStabilityFilePattern() { | 74 base::FilePath::StringType GetStabilityFilePattern() { |
62 return base::FilePath::StringType(FILE_PATH_LITERAL("*-*")) + | 75 return base::FilePath::StringType(FILE_PATH_LITERAL("*-*")) + |
63 base::PersistentMemoryAllocator::kFileExtension; | 76 base::PersistentMemoryAllocator::kFileExtension; |
64 } | 77 } |
65 | 78 |
66 void MarkStabilityFileForDeletion(const base::FilePath& user_data_dir) { | 79 void MarkStabilityFileForDeletion(const base::FilePath& user_data_dir) { |
67 if (!base::FeatureList::IsEnabled( | 80 if (!base::FeatureList::IsEnabled( |
(...skipping 12 matching lines...) Expand all Loading... | |
80 // scope. This should cause the stability debugging file to be deleted prior | 93 // scope. This should cause the stability debugging file to be deleted prior |
81 // to the next execution. | 94 // to the next execution. |
82 base::File file(stability_file, base::File::FLAG_OPEN | | 95 base::File file(stability_file, base::File::FLAG_OPEN | |
83 base::File::FLAG_READ | | 96 base::File::FLAG_READ | |
84 base::File::FLAG_DELETE_ON_CLOSE); | 97 base::File::FLAG_DELETE_ON_CLOSE); |
85 } | 98 } |
86 | 99 |
87 } // namespace browser_watcher | 100 } // namespace browser_watcher |
88 | 101 |
89 #endif // defined(OS_WIN) | 102 #endif // defined(OS_WIN) |
OLD | NEW |