Chromium Code Reviews| Index: components/metrics/file_metrics_provider.h |
| diff --git a/components/metrics/file_metrics_provider.h b/components/metrics/file_metrics_provider.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..714872f520f23605fc7f56007b70100810b433d4 |
| --- /dev/null |
| +++ b/components/metrics/file_metrics_provider.h |
| @@ -0,0 +1,120 @@ |
| +// 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. |
| + |
| +#ifndef COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ |
| +#define COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ |
| + |
| +#include <list> |
| +#include <string> |
| + |
| +#include "base/callback.h" |
| +#include "base/files/file_path.h" |
| +#include "base/gtest_prod_util.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/synchronization/lock.h" |
| +#include "base/time/time.h" |
| +#include "components/metrics/metrics_provider.h" |
| + |
| +class PrefRegistrySimple; |
| +class PrefService; |
| + |
| +namespace base { |
| +class MemoryMappedFile; |
| +class PersistentMemoryAllocator; |
| +} |
| + |
| +namespace metrics { |
| + |
| +class MetricsService; |
| + |
| +// FileMetricsProvider gathers and logs histograms written to files on disk. |
| +// Any number of files can be registered and the will be polled once per |
|
grt (UTC plus 2)
2016/02/08 18:09:19
"and the will be polled"
bcwhite
2016/02/09 21:08:46
Done.
|
| +// upload cycle (at startup and about every 30 minutes thereafter) for data |
| +// to send. |
| +class FileMetricsProvider |
| + : public metrics::MetricsProvider { |
| + public: |
| + enum FileType { |
| + // "Atomic" files are a collection of histograms that are written |
| + // completely in a single atomic operation (typically a write followed |
| + // by an atomic rename) and the file is never updated again except to |
| + // be replaced by a completely new set of histograms. This is the only |
| + // option that can be used if the file is not writeable by *this* |
| + // process. |
| + FILE_HISTOGRAMS_ATOMIC, |
| + |
| + // "Active" files may be open by one or more other processes and updated |
| + // at any time with new samples or new histograms. Such files may also be |
| + // inactive for any period of time only to be opened again and have new |
| + // data written to it. Because the file will be actively watched by *this* |
|
grt (UTC plus 2)
2016/02/08 18:09:19
nit: it -> them since the subject is "Such files"
bcwhite
2016/02/09 21:08:46
Done.
|
| + // process, the underlying file cannot be deleted on operating systems |
| + // that do not support delete-while-open (e.g. Windows). |
|
grt (UTC plus 2)
2016/02/08 18:09:19
Windows does, sorta. If all openers use FLAG_SHARE
grt (UTC plus 2)
2016/02/15 15:42:39
Please update the comment here, as Windows does su
bcwhite
2016/02/15 19:22:10
Done, but differently since on reflection, the fil
|
| + // TODO(bcwhite): Enable when read/write mem-mapped files are supported. |
| + //FILE_HISTOGRAMS_ACTIVE, |
| + }; |
| + |
| + private: |
| + // This is fully defined in the header file (rather than just a forward |
| + // declaration) so it can be known to tests. |
| + struct FileInformation { |
| + FileInformation(); |
| + ~FileInformation(); |
| + |
| + base::FilePath path; |
| + FileType type; |
| + std::string prefs_key; |
| + base::Time last_seen; |
| + scoped_ptr<base::MemoryMappedFile> mapped; |
| + scoped_ptr<base::PersistentMemoryAllocator> allocator; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(FileInformation); |
| + }; |
| + |
| + public: |
| + typedef std::list<scoped_ptr<FileInformation>> FileInformationList; |
| + |
| + FileMetricsProvider(metrics::MetricsService* metrics_service, |
| + PrefService* local_state); |
| + ~FileMetricsProvider() override; |
| + |
| + // Indicate a file to be monitored and how the file is used. Because some |
| + // metadata must persist across process restarts, registry entries are used |
|
grt (UTC plus 2)
2016/02/08 18:09:19
regarding "registry entries", do you mean "prefere
bcwhite
2016/02/09 21:08:46
Done. Was thinking in terms of "PrefRegistry".
|
| + // based on the |prefs_key| name. Call RegisterPrefs() with the same name |
| + // to create the necessary keys in advance. |
| + void RegisterFile(const base::FilePath& path, FileType type, |
| + const base::StringPiece& prefs_key); |
| + |
| + static void RegisterPrefs(PrefRegistrySimple* prefs, |
| + const base::StringPiece& key); |
| + |
| + private: |
| + friend class FileMetricsProviderTest; |
| + FRIEND_TEST_ALL_PREFIXES(FileMetricsProviderTest, AccessMetrics); |
| + |
| + static void CheckAndMapNewMetricFilesOnBlockingPool( |
| + FileInformationList* files, |
| + base::Callback<void(FileInformationList*)> callback); |
| + static bool CheckAndMapNewMetrics(FileInformation* file); |
| + |
| + void ScheduleFilesCheck(); |
| + void RecordFileInformation(FileInformationList* files); |
| + void RecordFileAsSeen(FileInformation* file); |
| + |
| + // metrics::MetricsDataProvider: |
| + void OnDidCreateMetricsLog() override; |
| + void RecordHistogramSnapshots(base::HistogramSnapshotManager* hsm) override; |
| + |
| + base::Lock lock_; |
| + FileInformationList metrics_files_; |
| + FileInformationList metrics_found_; |
| + metrics::MetricsService* metrics_service_; |
| + PrefService* pref_service_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FileMetricsProvider); |
| +}; |
| + |
| +} // namespace metrics |
| + |
| +#endif // COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ |