Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / feedback / system_logs / system_logs_fetcher_base.cc
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
15 SystemLogsSource::SystemLogsSource(const std::string& source_name)
16     : source_name_(source_name) {
17 }
18
19 SystemLogsSource::~SystemLogsSource() {
20 }
21
22 SystemLogsFetcherBase::SystemLogsFetcherBase()
23     : response_(new SystemLogsResponse),
24       num_pending_requests_(0) {
25 }
26
27 SystemLogsFetcherBase::~SystemLogsFetcherBase() {}
28
29 void SystemLogsFetcherBase::Fetch(const SysLogsFetcherCallback& callback) {
30   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
31   DCHECK(callback_.is_null());
32   DCHECK(!callback.is_null());
33
34   callback_ = callback;
35   for (size_t i = 0; i < data_sources_.size(); ++i) {
36     VLOG(1) << "Fetching SystemLogSource: " << data_sources_[i]->source_name();
37     data_sources_[i]->Fetch(base::Bind(&SystemLogsFetcherBase::AddResponse,
38                                        AsWeakPtr(),
39                                        data_sources_[i]->source_name()));
40   }
41 }
42
43 void SystemLogsFetcherBase::AddResponse(const std::string& source_name,
44                                         SystemLogsResponse* response) {
45   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
46
47   VLOG(1) << "Received SystemLogSource: " << source_name;
48
49   for (SystemLogsResponse::const_iterator it = response->begin();
50        it != response->end();
51        ++it) {
52     // It is an error to insert an element with a pre-existing key.
53     bool ok = response_->insert(*it).second;
54     DCHECK(ok) << "Duplicate key found: " << it->first;
55   }
56
57   --num_pending_requests_;
58   if (num_pending_requests_ > 0)
59     return;
60
61   callback_.Run(response_.Pass());
62   BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this);
63 }
64
65 }  // namespace system_logs