manzagop | f232266 | 2016-09-27 11:39:59 | [diff] [blame] | 1 | // 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 | |
manzagop | 14aff5d | 2016-11-23 17:27:00 | [diff] [blame^] | 9 | #include "base/feature_list.h" |
| 10 | #include "base/files/file.h" |
manzagop | f232266 | 2016-09-27 11:39:59 | [diff] [blame] | 11 | #include "base/metrics/persistent_memory_allocator.h" |
| 12 | #include "base/path_service.h" |
manzagop | 14aff5d | 2016-11-23 17:27:00 | [diff] [blame^] | 13 | #include "base/process/process.h" |
manzagop | f232266 | 2016-09-27 11:39:59 | [diff] [blame] | 14 | #include "base/strings/stringprintf.h" |
| 15 | #include "base/time/time.h" |
manzagop | 14aff5d | 2016-11-23 17:27:00 | [diff] [blame^] | 16 | #include "components/browser_watcher/features.h" |
manzagop | f232266 | 2016-09-27 11:39:59 | [diff] [blame] | 17 | |
| 18 | namespace browser_watcher { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | bool 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 | |
| 39 | base::FilePath GetStabilityDir(const base::FilePath& user_data_dir) { |
| 40 | return user_data_dir.AppendASCII("Stability"); |
| 41 | } |
| 42 | |
| 43 | bool 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 | |
| 62 | base::FilePath::StringType GetStabilityFilePattern() { |
| 63 | return base::FilePath::StringType(FILE_PATH_LITERAL("*-*")) + |
| 64 | base::PersistentMemoryAllocator::kFileExtension; |
| 65 | } |
| 66 | |
manzagop | 14aff5d | 2016-11-23 17:27:00 | [diff] [blame^] | 67 | void 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 | |
manzagop | f232266 | 2016-09-27 11:39:59 | [diff] [blame] | 88 | } // namespace browser_watcher |