OLD | NEW |
---|---|
(Empty) | |
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 "base/metrics/persistent_metrics_file_util.h" | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "base/files/file_util.h" | |
9 #include "base/metrics/persistent_histogram_allocator.h" | |
10 #include "base/metrics/persistent_memory_allocator.h" | |
11 | |
12 namespace base { | |
13 | |
14 void InitializeGlobalPersistentMetricsStorage(const base::FilePath& dir, | |
15 const char* metrics_name, | |
16 size_t size) { | |
17 // Metrics maps one file that we're going to use during this run (the | |
18 // "-active" one), and provides the previous one to the metrics system for for | |
19 // association with the previous run. On startup, we move the current -active | |
20 // to the base name, and then start this session on the -active one. Normally, | |
21 // when the browser starts it will read and delete the base name one, but in | |
22 // the case that something goes wrong, we will overwrite/delete any old one. | |
23 base::FilePath metrics_file = | |
24 dir.AppendASCII(metrics_name) | |
25 .AddExtension(PersistentMemoryAllocator::kFileExtension); | |
26 base::FilePath active_file = | |
27 dir.AppendASCII(metrics_name + std::string("-active")) | |
28 .AddExtension(PersistentMemoryAllocator::kFileExtension); | |
29 | |
30 if (!base::ReplaceFile(active_file, metrics_file, nullptr)) | |
31 base::DeleteFile(metrics_file, /*recursive=*/false); | |
32 | |
33 base::GlobalHistogramAllocator::CreateWithFile(active_file, size, 0, | |
34 metrics_name); | |
35 | |
36 // Get the allocator that was just created and report result. Exit if the | |
37 // allocator could not be created. | |
38 base::GlobalHistogramAllocator* allocator = | |
39 base::GlobalHistogramAllocator::Get(); | |
40 if (!allocator) | |
41 return; | |
42 | |
43 // Create tracking histograms for the allocator and record storage file. | |
44 allocator->CreateTrackingHistograms(metrics_name); | |
45 allocator->SetPersistentLocation(active_file); | |
46 } | |
47 | |
48 void CleanUpGlobalPersistentHistogramStorage() { | |
49 base::GlobalHistogramAllocator* allocator = | |
50 base::GlobalHistogramAllocator::Get(); | |
51 if (!allocator) | |
52 return; | |
53 | |
54 const base::FilePath& path = allocator->GetPersistentLocation(); | |
55 if (path.empty()) | |
56 return; | |
57 | |
58 // Open (with delete) and then immediately close the file by going out of | |
59 // scope. This is the only cross-platform safe way to delete a file that may | |
60 // be open elsewhere. Open handles will continue to operate normally but | |
61 // new opens will not be possible. | |
62 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ | | |
Mark Mentovai
2016/09/13 21:34:06
Ha, there’s a river here at column 24!
scottmg
2016/09/13 21:40:50
Yeah, I was surprised by that too, but I guess cla
| |
63 base::File::FLAG_DELETE_ON_CLOSE); | |
64 } | |
65 | |
66 } // namespace base | |
OLD | NEW |