| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 "chrome/browser/feedback/system_logs/system_logs_fetcher.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 | |
| 14 using content::BrowserThread; | |
| 15 | |
| 16 namespace system_logs { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // List of keys in the SystemLogsResponse map whose corresponding values will | |
| 21 // not be anonymized. | |
| 22 constexpr const char* const kWhitelistedKeysOfUUIDs[] = { | |
| 23 "CHROMEOS_BOARD_APPID", | |
| 24 "CHROMEOS_CANARY_APPID", | |
| 25 "CHROMEOS_RELEASE_APPID", | |
| 26 "CLIENT_ID", | |
| 27 }; | |
| 28 | |
| 29 // Returns true if the given |key| is anonymizer-whitelisted and whose | |
| 30 // corresponding value should not be anonymized. | |
| 31 bool IsKeyWhitelisted(const std::string& key) { | |
| 32 for (auto* const whitelisted_key : kWhitelistedKeysOfUUIDs) { | |
| 33 if (key == whitelisted_key) | |
| 34 return true; | |
| 35 } | |
| 36 return false; | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 SystemLogsFetcher::SystemLogsFetcher(bool scrub_data) | |
| 42 : response_(base::MakeUnique<SystemLogsResponse>()), | |
| 43 num_pending_requests_(0), | |
| 44 weak_ptr_factory_(this) { | |
| 45 if (scrub_data) | |
| 46 anonymizer_ = base::MakeUnique<feedback::AnonymizerTool>(); | |
| 47 } | |
| 48 | |
| 49 SystemLogsFetcher::~SystemLogsFetcher() {} | |
| 50 | |
| 51 void SystemLogsFetcher::AddSource(std::unique_ptr<SystemLogsSource> source) { | |
| 52 data_sources_.emplace_back(std::move(source)); | |
| 53 num_pending_requests_++; | |
| 54 } | |
| 55 | |
| 56 void SystemLogsFetcher::Fetch(const SysLogsFetcherCallback& callback) { | |
| 57 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 58 DCHECK(callback_.is_null()); | |
| 59 DCHECK(!callback.is_null()); | |
| 60 | |
| 61 callback_ = callback; | |
| 62 for (size_t i = 0; i < data_sources_.size(); ++i) { | |
| 63 VLOG(1) << "Fetching SystemLogSource: " << data_sources_[i]->source_name(); | |
| 64 data_sources_[i]->Fetch(base::Bind(&SystemLogsFetcher::OnFetched, | |
| 65 weak_ptr_factory_.GetWeakPtr(), | |
| 66 data_sources_[i]->source_name())); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 void SystemLogsFetcher::OnFetched(const std::string& source_name, | |
| 71 SystemLogsResponse* response) { | |
| 72 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 73 | |
| 74 VLOG(1) << "Received SystemLogSource: " << source_name; | |
| 75 | |
| 76 if (anonymizer_) { | |
| 77 for (auto& element : *response) { | |
| 78 if (!IsKeyWhitelisted(element.first)) | |
| 79 element.second = anonymizer_->Anonymize(element.second); | |
| 80 } | |
| 81 } | |
| 82 AddResponse(source_name, response); | |
| 83 } | |
| 84 | |
| 85 void SystemLogsFetcher::AddResponse(const std::string& source_name, | |
| 86 SystemLogsResponse* response) { | |
| 87 for (const auto& it : *response) { | |
| 88 // An element with a duplicate key would not be successfully inserted. | |
| 89 bool ok = response_->emplace(it).second; | |
| 90 DCHECK(ok) << "Duplicate key found: " << it.first; | |
| 91 } | |
| 92 | |
| 93 --num_pending_requests_; | |
| 94 if (num_pending_requests_ > 0) | |
| 95 return; | |
| 96 | |
| 97 callback_.Run(std::move(response_)); | |
| 98 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); | |
| 99 } | |
| 100 | |
| 101 } // namespace system_logs | |
| OLD | NEW |