summaryrefslogtreecommitdiffstats
path: root/chrome/browser/safe_browsing/last_download_finder.cc
blob: 05e7c425ffb6dc5ede5203bbba6a18927b2c7051 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/safe_browsing/last_download_finder.h"

#include <algorithm>
#include <functional>
#include <utility>

#include "base/bind.h"
#include "base/macros.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/safe_browsing/csd.pb.h"
#include "chrome/common/safe_browsing/download_protection_util.h"
#include "content/public/browser/download_item.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"

namespace safe_browsing {

namespace {

// Returns true if |first| is more recent than |second|, preferring opened over
// non-opened for downloads that completed at the same time (extraordinarily
// unlikely). Only files that look like some kind of executable are considered.
bool IsMoreInterestingThan(const history::DownloadRow& first,
                           const history::DownloadRow& second) {
  if (first.end_time < second.end_time)
    return false;
  // TODO(grt): Peek into archives to see if they contain binaries;
  // http://crbug.com/386915.
  if (!download_protection_util::IsBinaryFile(first.target_path) ||
      download_protection_util::IsArchiveFile(first.target_path)) {
    return false;
  }
  return (first.end_time != second.end_time ||
          (first.opened && !second.opened));
}

// Returns a pointer to the most interesting completed download in |downloads|.
const history::DownloadRow* FindMostInteresting(
    const std::vector<history::DownloadRow>& downloads) {
  const history::DownloadRow* most_recent_row = NULL;
  for (size_t i = 0; i < downloads.size(); ++i) {
    const history::DownloadRow& row = downloads[i];
    // Ignore incomplete downloads.
    if (row.state != content::DownloadItem::COMPLETE)
      continue;
    if (!most_recent_row || IsMoreInterestingThan(row, *most_recent_row))
      most_recent_row = &row;
  }
  return most_recent_row;
}

// Populates the |details| protobuf with information pertaining to |download|.
void PopulateDetails(const history::DownloadRow& download,
                     ClientIncidentReport_DownloadDetails* details) {
  ClientDownloadRequest* download_request = details->mutable_download();
  download_request->set_url(download.url_chain.back().spec());
  // digests is a required field, so force it to exist.
  // TODO(grt): Include digests in reports; http://crbug.com/389123.
  ignore_result(download_request->mutable_digests());
  download_request->set_length(download.received_bytes);
  for (size_t i = 0; i < download.url_chain.size(); ++i) {
    const GURL& url = download.url_chain[i];
    ClientDownloadRequest_Resource* resource =
        download_request->add_resources();
    resource->set_url(url.spec());
    if (i != download.url_chain.size() - 1) {  // An intermediate redirect.
      resource->set_type(ClientDownloadRequest::DOWNLOAD_REDIRECT);
    } else {  // The final download URL.
      resource->set_type(ClientDownloadRequest::DOWNLOAD_URL);
      if (!download.referrer_url.is_empty())
        resource->set_referrer(download.referrer_url.spec());
    }
  }
  download_request->set_file_basename(
      download.target_path.BaseName().AsUTF8Unsafe());
  download_request->set_download_type(
      download_protection_util::GetDownloadType(download.target_path));
  download_request->set_locale(
      g_browser_process->local_state()->GetString(prefs::kApplicationLocale));

  details->set_download_time_msec(download.end_time.ToJavaTime());
  // Opened time is unknown for now, so use the download time if the file was
  // opened in Chrome.
  if (download.opened)
    details->set_open_time_msec(download.end_time.ToJavaTime());
}

}  // namespace

LastDownloadFinder::~LastDownloadFinder() {
}

// static
scoped_ptr<LastDownloadFinder> LastDownloadFinder::Create(
    const LastDownloadCallback& callback) {
  scoped_ptr<LastDownloadFinder> finder(make_scoped_ptr(new LastDownloadFinder(
      g_browser_process->profile_manager()->GetLoadedProfiles(), callback)));
  // Return NULL if there is no work to do.
  if (finder->profiles_.empty())
    return scoped_ptr<LastDownloadFinder>();
  return finder.Pass();
}

LastDownloadFinder::LastDownloadFinder() : weak_ptr_factory_(this) {
}

LastDownloadFinder::LastDownloadFinder(const std::vector<Profile*>& profiles,
                                       const LastDownloadCallback& callback)
    : callback_(callback), weak_ptr_factory_(this) {
  // Observe profile lifecycle events so that the finder can begin or abandon
  // the search in profiles while it is running.
  notification_registrar_.Add(this,
                              chrome::NOTIFICATION_PROFILE_ADDED,
                              content::NotificationService::AllSources());
  notification_registrar_.Add(this,
                              chrome::NOTIFICATION_HISTORY_LOADED,
                              content::NotificationService::AllSources());
  notification_registrar_.Add(this,
                              chrome::NOTIFICATION_PROFILE_DESTROYED,
                              content::NotificationService::AllSources());

  // Begin the seach for all given profiles.
  std::for_each(
      profiles.begin(),
      profiles.end(),
      std::bind1st(std::mem_fun(&LastDownloadFinder::SearchInProfile), this));
}

void LastDownloadFinder::SearchInProfile(Profile* profile) {
  // Do not look in OTR profiles or in profiles that do not participate in
  // safe browsing.
  if (profile->IsOffTheRecord() ||
      !profile->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) {
    return;
  }

  HistoryService* history_service =
      HistoryServiceFactory::GetForProfile(profile, Profile::IMPLICIT_ACCESS);
  // No history service is returned for profiles that do not save history.
  if (!history_service)
    return;

  profiles_.push_back(profile);
  if (history_service->BackendLoaded()) {
    history_service->QueryDownloads(
        base::Bind(&LastDownloadFinder::OnDownloadQuery,
                   weak_ptr_factory_.GetWeakPtr(),
                   profile));
  }  // else wait until history is loaded.
}

void LastDownloadFinder::OnProfileHistoryLoaded(
    Profile* profile,
    HistoryService* history_service) {
  if (std::find(profiles_.begin(), profiles_.end(), profile) !=
      profiles_.end()) {
    history_service->QueryDownloads(
        base::Bind(&LastDownloadFinder::OnDownloadQuery,
                   weak_ptr_factory_.GetWeakPtr(),
                   profile));
  }
}

void LastDownloadFinder::AbandonSearchInProfile(Profile* profile) {
  // |profile| may not be present in the set of profiles.
  std::vector<Profile*>::iterator it =
      std::find(profiles_.begin(), profiles_.end(), profile);
  if (it != profiles_.end())
    RemoveProfileAndReportIfDone(it);
}

void LastDownloadFinder::OnDownloadQuery(
    Profile* profile,
    scoped_ptr<std::vector<history::DownloadRow> > downloads) {
  // Early-exit if the history search for this profile was abandoned.
  std::vector<Profile*>::iterator it =
      std::find(profiles_.begin(), profiles_.end(), profile);
  if (it == profiles_.end())
    return;

  // Find the most recent from this profile and use it if it's better than
  // anything else found so far.
  const history::DownloadRow* profile_best = FindMostInteresting(*downloads);
  if (profile_best && IsMoreInterestingThan(*profile_best, most_recent_row_))
    most_recent_row_ = *profile_best;

  RemoveProfileAndReportIfDone(it);
}

void LastDownloadFinder::RemoveProfileAndReportIfDone(
    std::vector<Profile*>::iterator it) {
  DCHECK(it != profiles_.end());

  *it = profiles_.back();
  profiles_.resize(profiles_.size() - 1);

  // Finish processing if all results are in.
  if (profiles_.empty())
    ReportResults();
  // Do not touch this instance after reporting results.
}

void LastDownloadFinder::ReportResults() {
  DCHECK(profiles_.empty());
  if (most_recent_row_.end_time.is_null()) {
    callback_.Run(scoped_ptr<ClientIncidentReport_DownloadDetails>());
    // Do not touch this instance after running the callback, since it may have
    // been deleted.
  } else {
    scoped_ptr<ClientIncidentReport_DownloadDetails> details(
        new ClientIncidentReport_DownloadDetails());
    PopulateDetails(most_recent_row_, details.get());
    callback_.Run(details.Pass());
    // Do not touch this instance after running the callback, since it may have
    // been deleted.
  }
}

void LastDownloadFinder::Observe(int type,
                                 const content::NotificationSource& source,
                                 const content::NotificationDetails& details) {
  switch (type) {
    case chrome::NOTIFICATION_PROFILE_ADDED:
      SearchInProfile(content::Source<Profile>(source).ptr());
      break;
    case chrome::NOTIFICATION_HISTORY_LOADED:
      OnProfileHistoryLoaded(content::Source<Profile>(source).ptr(),
                             content::Details<HistoryService>(details).ptr());
      break;
    case chrome::NOTIFICATION_PROFILE_DESTROYED:
      AbandonSearchInProfile(content::Source<Profile>(source).ptr());
      break;
    default:
      break;
  }
}

}  // namespace safe_browsing