| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ | 5 #ifndef COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ |
| 6 #define COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ | 6 #define COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/metrics/statistics_recorder.h" | 16 #include "base/metrics/statistics_recorder.h" |
| 17 #include "base/threading/thread_checker.h" | 17 #include "base/sequence_checker.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "components/metrics/metrics_provider.h" | 19 #include "components/metrics/metrics_provider.h" |
| 20 | 20 |
| 21 class PrefRegistrySimple; | 21 class PrefRegistrySimple; |
| 22 class PrefService; | 22 class PrefService; |
| 23 | 23 |
| 24 namespace base { | 24 namespace base { |
| 25 class TaskRunner; | 25 class TaskRunner; |
| 26 } | 26 } |
| 27 | 27 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 ASSOCIATE_INTERNAL_PROFILE, | 90 ASSOCIATE_INTERNAL_PROFILE, |
| 91 | 91 |
| 92 // Like above but fall back to ASSOCIATE_PREVIOUS_RUN if there is no | 92 // Like above but fall back to ASSOCIATE_PREVIOUS_RUN if there is no |
| 93 // embedded profile. This has a small cost during startup as that is | 93 // embedded profile. This has a small cost during startup as that is |
| 94 // when previous-run metrics are sent so the file has be checked at | 94 // when previous-run metrics are sent so the file has be checked at |
| 95 // that time even though actual transfer will be delayed if an | 95 // that time even though actual transfer will be delayed if an |
| 96 // embedded profile is found. | 96 // embedded profile is found. |
| 97 ASSOCIATE_INTERNAL_PROFILE_OR_PREVIOUS_RUN, | 97 ASSOCIATE_INTERNAL_PROFILE_OR_PREVIOUS_RUN, |
| 98 }; | 98 }; |
| 99 | 99 |
| 100 FileMetricsProvider(const scoped_refptr<base::TaskRunner>& task_runner, | 100 explicit FileMetricsProvider(PrefService* local_state); |
| 101 PrefService* local_state); | |
| 102 ~FileMetricsProvider() override; | 101 ~FileMetricsProvider() override; |
| 103 | 102 |
| 104 // Indicates a file or directory to be monitored and how the file or files | 103 // Indicates a file or directory to be monitored and how the file or files |
| 105 // within that directory are used. Because some metadata may need to persist | 104 // within that directory are used. Because some metadata may need to persist |
| 106 // across process restarts, preferences entries are used based on the | 105 // across process restarts, preferences entries are used based on the |
| 107 // |prefs_key| name. Call RegisterPrefs() with the same name to create the | 106 // |prefs_key| name. Call RegisterPrefs() with the same name to create the |
| 108 // necessary keys in advance. Set |prefs_key| empty (nullptr will work) if | 107 // necessary keys in advance. Set |prefs_key| empty (nullptr will work) if |
| 109 // no persistence is required. ACTIVE files shouldn't have a pref key as | 108 // no persistence is required. ACTIVE files shouldn't have a pref key as |
| 110 // they update internal state about what has been previously sent. | 109 // they update internal state about what has been previously sent. |
| 111 void RegisterSource(const base::FilePath& path, | 110 void RegisterSource(const base::FilePath& path, |
| 112 SourceType type, | 111 SourceType type, |
| 113 SourceAssociation source_association, | 112 SourceAssociation source_association, |
| 114 const base::StringPiece prefs_key); | 113 const base::StringPiece prefs_key); |
| 115 | 114 |
| 116 // Registers all necessary preferences for maintaining persistent state | 115 // Registers all necessary preferences for maintaining persistent state |
| 117 // about a monitored file across process restarts. The |prefs_key| is | 116 // about a monitored file across process restarts. The |prefs_key| is |
| 118 // typically the filename. | 117 // typically the filename. |
| 119 static void RegisterPrefs(PrefRegistrySimple* prefs, | 118 static void RegisterPrefs(PrefRegistrySimple* prefs, |
| 120 const base::StringPiece prefs_key); | 119 const base::StringPiece prefs_key); |
| 121 | 120 |
| 121 // Sets the task runner to use for testing. |
| 122 static void SetTaskRunnerForTesting( |
| 123 const scoped_refptr<base::TaskRunner>& task_runner); |
| 124 |
| 122 private: | 125 private: |
| 123 friend class FileMetricsProviderTest; | 126 friend class FileMetricsProviderTest; |
| 124 | 127 |
| 125 // The different results that can occur accessing a file. | 128 // The different results that can occur accessing a file. |
| 126 enum AccessResult { | 129 enum AccessResult { |
| 127 // File was successfully mapped. | 130 // File was successfully mapped. |
| 128 ACCESS_RESULT_SUCCESS, | 131 ACCESS_RESULT_SUCCESS, |
| 129 | 132 |
| 130 // File does not exist. | 133 // File does not exist. |
| 131 ACCESS_RESULT_DOESNT_EXIST, | 134 ACCESS_RESULT_DOESNT_EXIST, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 SourceInfoList sources_with_profile_; | 221 SourceInfoList sources_with_profile_; |
| 219 | 222 |
| 220 // A list of sources for a previous run. These are held separately because | 223 // A list of sources for a previous run. These are held separately because |
| 221 // they are not subject to the periodic background checking that handles | 224 // they are not subject to the periodic background checking that handles |
| 222 // metrics for the current run. | 225 // metrics for the current run. |
| 223 SourceInfoList sources_for_previous_run_; | 226 SourceInfoList sources_for_previous_run_; |
| 224 | 227 |
| 225 // The preferences-service used to store persistent state about sources. | 228 // The preferences-service used to store persistent state about sources. |
| 226 PrefService* pref_service_; | 229 PrefService* pref_service_; |
| 227 | 230 |
| 228 base::ThreadChecker thread_checker_; | 231 SEQUENCE_CHECKER(sequence_checker_); |
| 229 base::WeakPtrFactory<FileMetricsProvider> weak_factory_; | 232 base::WeakPtrFactory<FileMetricsProvider> weak_factory_; |
| 230 | 233 |
| 231 DISALLOW_COPY_AND_ASSIGN(FileMetricsProvider); | 234 DISALLOW_COPY_AND_ASSIGN(FileMetricsProvider); |
| 232 }; | 235 }; |
| 233 | 236 |
| 234 } // namespace metrics | 237 } // namespace metrics |
| 235 | 238 |
| 236 #endif // COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ | 239 #endif // COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ |
| OLD | NEW |