| 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 #include "components/metrics/file_metrics_provider.h" | 5 #include "components/metrics/file_metrics_provider.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 9 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/files/memory_mapped_file.h" | 11 #include "base/files/memory_mapped_file.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/metrics/histogram_base.h" | 14 #include "base/metrics/histogram_base.h" |
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/metrics/persistent_histogram_allocator.h" | 16 #include "base/metrics/persistent_histogram_allocator.h" |
| 17 #include "base/metrics/persistent_memory_allocator.h" | 17 #include "base/metrics/persistent_memory_allocator.h" |
| 18 #include "base/strings/string_piece.h" |
| 18 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
| 19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 20 #include "components/metrics/metrics_pref_names.h" | 21 #include "components/metrics/metrics_pref_names.h" |
| 21 #include "components/metrics/metrics_service.h" | 22 #include "components/metrics/metrics_service.h" |
| 22 #include "components/prefs/pref_registry_simple.h" | 23 #include "components/prefs/pref_registry_simple.h" |
| 23 #include "components/prefs/pref_service.h" | 24 #include "components/prefs/pref_service.h" |
| 24 | 25 |
| 25 | 26 |
| 26 namespace metrics { | 27 namespace metrics { |
| 27 | 28 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 source->mapped.reset(new base::MemoryMappedFile()); | 190 source->mapped.reset(new base::MemoryMappedFile()); |
| 190 if (!source->mapped->Initialize(std::move(file))) { | 191 if (!source->mapped->Initialize(std::move(file))) { |
| 191 source->mapped.reset(); | 192 source->mapped.reset(); |
| 192 return ACCESS_RESULT_SYSTEM_MAP_FAILURE; | 193 return ACCESS_RESULT_SYSTEM_MAP_FAILURE; |
| 193 } | 194 } |
| 194 | 195 |
| 195 // Ensure any problems below don't occur repeatedly. | 196 // Ensure any problems below don't occur repeatedly. |
| 196 source->last_seen = info.last_modified; | 197 source->last_seen = info.last_modified; |
| 197 | 198 |
| 198 // Test the validity of the file contents. | 199 // Test the validity of the file contents. |
| 199 if (!base::FilePersistentMemoryAllocator::IsFileAcceptable(*source->mapped)) { | 200 if (!base::FilePersistentMemoryAllocator::IsFileAcceptable(*source->mapped, |
| 201 true)) { |
| 200 source->mapped.reset(); | 202 source->mapped.reset(); |
| 201 return ACCESS_RESULT_INVALID_CONTENTS; | 203 return ACCESS_RESULT_INVALID_CONTENTS; |
| 202 } | 204 } |
| 203 | 205 |
| 204 switch (source->type) { | 206 switch (source->type) { |
| 205 case SOURCE_HISTOGRAMS_ATOMIC_FILE: | 207 case SOURCE_HISTOGRAMS_ATOMIC_FILE: |
| 206 case SOURCE_HISTOGRAMS_ATOMIC_DIR: | 208 case SOURCE_HISTOGRAMS_ATOMIC_DIR: |
| 207 // For an "atomic" file, copy the data into local memory but don't | 209 // For an "atomic" file, copy the data into local memory but don't |
| 208 // release the file so that it is held open to prevent access by other | 210 // release the file so that it is held open to prevent access by other |
| 209 // processes. The copy means all I/O is done on this thread instead of | 211 // processes. The copy means all I/O is done on this thread instead of |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 // File data was validated earlier. Files are not considered "untrusted" | 362 // File data was validated earlier. Files are not considered "untrusted" |
| 361 // as some processes might be (e.g. Renderer) so there's no need to check | 363 // as some processes might be (e.g. Renderer) so there's no need to check |
| 362 // again to try to thwart some malicious actor that may have modified the | 364 // again to try to thwart some malicious actor that may have modified the |
| 363 // data between then and now. | 365 // data between then and now. |
| 364 if (source->data.empty()) { | 366 if (source->data.empty()) { |
| 365 // Data hasn't been copied into memory. Create the allocator directly | 367 // Data hasn't been copied into memory. Create the allocator directly |
| 366 // on the memory-mapped file. | 368 // on the memory-mapped file. |
| 367 // TODO(bcwhite): Make this do read/write when supported for "active". | 369 // TODO(bcwhite): Make this do read/write when supported for "active". |
| 368 source->allocator.reset(new base::PersistentHistogramAllocator( | 370 source->allocator.reset(new base::PersistentHistogramAllocator( |
| 369 base::WrapUnique(new base::FilePersistentMemoryAllocator( | 371 base::WrapUnique(new base::FilePersistentMemoryAllocator( |
| 370 std::move(source->mapped), 0, "")))); | 372 std::move(source->mapped), 0, 0, base::StringPiece(), true)))); |
| 371 } else { | 373 } else { |
| 372 // Data was copied from the mapped file into memory. Create an allocator | 374 // Data was copied from the mapped file into memory. Create an allocator |
| 373 // on the copy thus eliminating disk I/O during data access. | 375 // on the copy thus eliminating disk I/O during data access. |
| 374 source->allocator.reset(new base::PersistentHistogramAllocator( | 376 source->allocator.reset(new base::PersistentHistogramAllocator( |
| 375 base::WrapUnique(new base::PersistentMemoryAllocator( | 377 base::WrapUnique(new base::PersistentMemoryAllocator( |
| 376 &source->data[0], source->data.size(), 0, 0, "", true)))); | 378 &source->data[0], source->data.size(), 0, 0, base::StringPiece(), |
| 379 true)))); |
| 377 } | 380 } |
| 378 } | 381 } |
| 379 | 382 |
| 380 void FileMetricsProvider::RecordSourcesChecked(SourceInfoList* checked) { | 383 void FileMetricsProvider::RecordSourcesChecked(SourceInfoList* checked) { |
| 381 DCHECK(thread_checker_.CalledOnValidThread()); | 384 DCHECK(thread_checker_.CalledOnValidThread()); |
| 382 | 385 |
| 383 // Move each processed source to either the "to-read" list (for processing) | 386 // Move each processed source to either the "to-read" list (for processing) |
| 384 // or the "to-check" list (for future checking). | 387 // or the "to-check" list (for future checking). |
| 385 for (auto iter = checked->begin(); iter != checked->end();) { | 388 for (auto iter = checked->begin(); iter != checked->end();) { |
| 386 auto temp = iter++; | 389 auto temp = iter++; |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 | 561 |
| 559 // Dump all histograms contained within the source to the snapshot-manager. | 562 // Dump all histograms contained within the source to the snapshot-manager. |
| 560 RecordHistogramSnapshotsFromSource(snapshot_manager, source.get()); | 563 RecordHistogramSnapshotsFromSource(snapshot_manager, source.get()); |
| 561 | 564 |
| 562 // Update the last-seen time so it isn't read again unless it changes. | 565 // Update the last-seen time so it isn't read again unless it changes. |
| 563 RecordSourceAsRead(source.get()); | 566 RecordSourceAsRead(source.get()); |
| 564 } | 567 } |
| 565 } | 568 } |
| 566 | 569 |
| 567 } // namespace metrics | 570 } // namespace metrics |
| OLD | NEW |