OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/browser_watcher/stability_paths.h" |
| 6 |
| 7 #if defined(OS_WIN) |
| 8 #include <windows.h> |
| 9 #endif // defined(OS_WIN) |
| 10 |
| 11 #include <string> |
| 12 |
| 13 #include "base/feature_list.h" |
| 14 #include "base/files/file.h" |
| 15 #include "base/metrics/persistent_memory_allocator.h" |
| 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/time/time.h" |
| 18 #include "components/browser_watcher/features.h" |
| 19 |
| 20 #if defined(OS_WIN) |
| 21 |
| 22 #include "third_party/crashpad/crashpad/util/win/time.h" |
| 23 |
| 24 namespace browser_watcher { |
| 25 namespace { |
| 26 |
| 27 const uint64_t kMicrosecondsPerSecond = static_cast<uint64_t>(1E6); |
| 28 |
| 29 bool GetCreationTime(const base::Process& process, FILETIME* creation_time) { |
| 30 DCHECK(creation_time); |
| 31 |
| 32 FILETIME ignore1 = {}; |
| 33 FILETIME ignore2 = {}; |
| 34 FILETIME ignore3 = {}; |
| 35 return ::GetProcessTimes(process.Handle(), creation_time, &ignore1, &ignore2, |
| 36 &ignore3) != 0; |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 base::FilePath GetStabilityDir(const base::FilePath& user_data_dir) { |
| 42 return user_data_dir.AppendASCII("Stability"); |
| 43 } |
| 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 |
| 57 bool GetStabilityFileForProcess(const base::Process& process, |
| 58 const base::FilePath& user_data_dir, |
| 59 base::FilePath* file_path) { |
| 60 DCHECK(file_path); |
| 61 |
| 62 FILETIME creation_time; |
| 63 if (!GetCreationTime(process, &creation_time)) |
| 64 return false; |
| 65 |
| 66 // We rely on Crashpad's conversion to ensure the resulting filename is the |
| 67 // same as on crash, when the creation time is obtained via Crashpad. |
| 68 *file_path = GetStabilityFileForProcess( |
| 69 process.Pid(), crashpad::FiletimeToTimevalEpoch(creation_time), |
| 70 user_data_dir); |
| 71 return true; |
| 72 } |
| 73 |
| 74 base::FilePath::StringType GetStabilityFilePattern() { |
| 75 return base::FilePath::StringType(FILE_PATH_LITERAL("*-*")) + |
| 76 base::PersistentMemoryAllocator::kFileExtension; |
| 77 } |
| 78 |
| 79 void MarkStabilityFileForDeletion(const base::FilePath& user_data_dir) { |
| 80 if (!base::FeatureList::IsEnabled( |
| 81 browser_watcher::kStabilityDebuggingFeature)) { |
| 82 return; |
| 83 } |
| 84 |
| 85 base::FilePath stability_file; |
| 86 if (!GetStabilityFileForProcess(base::Process::Current(), user_data_dir, |
| 87 &stability_file)) { |
| 88 // TODO(manzagop): add a metric for this. |
| 89 return; |
| 90 } |
| 91 |
| 92 // Open (with delete) and then immediately close the file by going out of |
| 93 // scope. This should cause the stability debugging file to be deleted prior |
| 94 // to the next execution. |
| 95 base::File file(stability_file, base::File::FLAG_OPEN | |
| 96 base::File::FLAG_READ | |
| 97 base::File::FLAG_DELETE_ON_CLOSE); |
| 98 } |
| 99 |
| 100 } // namespace browser_watcher |
| 101 |
| 102 #endif // defined(OS_WIN) |
OLD | NEW |