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_base.h" 6 7 #include "base/bind.h" 8 #include "base/bind_helpers.h" 9 #include "content/public/browser/browser_thread.h" 10 11 using content::BrowserThread; 12 13 namespace system_logs { 14 SystemLogsFetcherBase()15SystemLogsFetcherBase::SystemLogsFetcherBase() 16 : response_(new SystemLogsResponse), 17 num_pending_requests_(0) { 18 } 19 ~SystemLogsFetcherBase()20SystemLogsFetcherBase::~SystemLogsFetcherBase() {} 21 Fetch(const SysLogsFetcherCallback & callback)22void SystemLogsFetcherBase::Fetch(const SysLogsFetcherCallback& callback) { 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 24 DCHECK(callback_.is_null()); 25 DCHECK(!callback.is_null()); 26 27 callback_ = callback; 28 for (size_t i = 0; i < data_sources_.size(); ++i) { 29 data_sources_[i]->Fetch(base::Bind(&SystemLogsFetcherBase::AddResponse, 30 AsWeakPtr())); 31 } 32 } 33 AddResponse(SystemLogsResponse * response)34void SystemLogsFetcherBase::AddResponse(SystemLogsResponse* response) { 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 36 37 for (SystemLogsResponse::const_iterator it = response->begin(); 38 it != response->end(); 39 ++it) { 40 // It is an error to insert an element with a pre-existing key. 41 bool ok = response_->insert(*it).second; 42 DCHECK(ok) << "Duplicate key found: " << it->first; 43 } 44 45 --num_pending_requests_; 46 if (num_pending_requests_ > 0) 47 return; 48 49 callback_.Run(response_.Pass()); 50 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); 51 } 52 53 } // namespace system_logs 54