Chromium Code Reviews| Index: base/metrics/persistent_metrics_file_util.cc |
| diff --git a/base/metrics/persistent_metrics_file_util.cc b/base/metrics/persistent_metrics_file_util.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..beaf0a5f06e3090a58c0d996919a4f52b032ef29 |
| --- /dev/null |
| +++ b/base/metrics/persistent_metrics_file_util.cc |
| @@ -0,0 +1,66 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/metrics/persistent_metrics_file_util.h" |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/metrics/persistent_histogram_allocator.h" |
| +#include "base/metrics/persistent_memory_allocator.h" |
| + |
| +namespace base { |
| + |
| +void InitializeGlobalPersistentMetricsStorage(const base::FilePath& dir, |
| + const char* metrics_name, |
| + size_t size) { |
| + // Metrics maps one file that we're going to use during this run (the |
| + // "-active" one), and provides the previous one to the metrics system for for |
| + // association with the previous run. On startup, we move the current -active |
| + // to the base name, and then start this session on the -active one. Normally, |
| + // when the browser starts it will read and delete the base name one, but in |
| + // the case that something goes wrong, we will overwrite/delete any old one. |
| + base::FilePath metrics_file = |
| + dir.AppendASCII(metrics_name) |
| + .AddExtension(PersistentMemoryAllocator::kFileExtension); |
| + base::FilePath active_file = |
| + dir.AppendASCII(metrics_name + std::string("-active")) |
| + .AddExtension(PersistentMemoryAllocator::kFileExtension); |
| + |
| + if (!base::ReplaceFile(active_file, metrics_file, nullptr)) |
| + base::DeleteFile(metrics_file, /*recursive=*/false); |
| + |
| + base::GlobalHistogramAllocator::CreateWithFile(active_file, size, 0, |
| + metrics_name); |
| + |
| + // Get the allocator that was just created and report result. Exit if the |
| + // allocator could not be created. |
| + base::GlobalHistogramAllocator* allocator = |
| + base::GlobalHistogramAllocator::Get(); |
| + if (!allocator) |
| + return; |
| + |
| + // Create tracking histograms for the allocator and record storage file. |
| + allocator->CreateTrackingHistograms(metrics_name); |
|
bcwhite
2016/09/14 13:16:12
This is the only line cannot be moved into CreateW
scottmg
2016/09/14 20:37:01
Done.
|
| + allocator->SetPersistentLocation(active_file); |
|
bcwhite
2016/09/14 13:16:12
I think this could be part of CreateWithFile().
scottmg
2016/09/14 20:37:01
Done. I removed it from chrome/browser/chrome_brow
|
| +} |
| + |
| +void CleanUpGlobalPersistentHistogramStorage() { |
| + base::GlobalHistogramAllocator* allocator = |
| + base::GlobalHistogramAllocator::Get(); |
| + if (!allocator) |
| + return; |
| + |
| + const base::FilePath& path = allocator->GetPersistentLocation(); |
| + if (path.empty()) |
| + return; |
| + |
| + // Open (with delete) and then immediately close the file by going out of |
| + // scope. This is the only cross-platform safe way to delete a file that may |
| + // be open elsewhere. Open handles will continue to operate normally but |
| + // new opens will not be possible. |
| + base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ | |
| + base::File::FLAG_DELETE_ON_CLOSE); |
| +} |
| + |
| +} // namespace base |