Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(944)

Side by Side Diff: content/browser/download/base_file_win.cc

Issue 2123023002: [Downloads] Consolidate MOTW annotation APIs into a single API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@move-safe-util-to-downloads
Patch Set: . Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/download/base_file.h" 5 #include "content/browser/download/base_file.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <cguid.h> 8 #include <cguid.h>
9 #include <objbase.h> 9 #include <objbase.h>
10 #include <shellapi.h> 10 #include <shellapi.h>
11 11
12 #include "base/files/file.h" 12 #include "base/files/file.h"
13 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
14 #include "base/guid.h" 14 #include "base/guid.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/metrics/histogram.h" 16 #include "base/metrics/histogram.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread_restrictions.h" 18 #include "base/threading/thread_restrictions.h"
19 #include "content/browser/download/download_interrupt_reasons_impl.h" 19 #include "content/browser/download/download_interrupt_reasons_impl.h"
20 #include "content/browser/download/download_stats.h" 20 #include "content/browser/download/download_stats.h"
21 #include "content/browser/safe_util_win.h"
22 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
23 22
24 namespace content { 23 namespace content {
25 namespace { 24 namespace {
26 25
27 const int kAllSpecialShFileOperationCodes[] = { 26 const int kAllSpecialShFileOperationCodes[] = {
28 // Should be kept in sync with the case statement below. 27 // Should be kept in sync with the case statement below.
29 ERROR_ACCESS_DENIED, 28 ERROR_ACCESS_DENIED,
30 ERROR_SHARING_VIOLATION, 29 ERROR_SHARING_VIOLATION,
31 ERROR_INVALID_PARAMETER, 30 ERROR_INVALID_PARAMETER,
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 } 279 }
281 280
282 if (result != DOWNLOAD_INTERRUPT_REASON_NONE) 281 if (result != DOWNLOAD_INTERRUPT_REASON_NONE)
283 return result; 282 return result;
284 283
285 // If not one of the above codes, it should be a standard Windows error code. 284 // If not one of the above codes, it should be a standard Windows error code.
286 return ConvertFileErrorToInterruptReason( 285 return ConvertFileErrorToInterruptReason(
287 base::File::OSErrorToFileError(code)); 286 base::File::OSErrorToFileError(code));
288 } 287 }
289 288
290 // Maps a return code from ScanAndSaveDownloadedFile() to a
291 // DownloadInterruptReason. The return code in |result| is usually from the
292 // final IAttachmentExecute::Save() call.
293 DownloadInterruptReason MapScanAndSaveErrorCodeToInterruptReason(
294 HRESULT result) {
295 if (SUCCEEDED(result))
296 return DOWNLOAD_INTERRUPT_REASON_NONE;
297
298 switch (result) {
299 case INET_E_SECURITY_PROBLEM: // 0x800c000e
300 // This is returned if the download was blocked due to security
301 // restrictions. E.g. if the source URL was in the Restricted Sites zone
302 // and downloads are blocked on that zone, then the download would be
303 // deleted and this error code is returned.
304 return DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED;
305
306 case E_FAIL: // 0x80004005
307 // Returned if an anti-virus product reports an infection in the
308 // downloaded file during IAE::Save().
309 return DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED;
310
311 default:
312 // Any other error that occurs during IAttachmentExecute::Save() likely
313 // indicates a problem with the security check, but not necessarily the
314 // download. See http://crbug.com/153212.
315 return DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED;
316 }
317 }
318
319 } // namespace 289 } // namespace
320 290
321 // Renames a file using the SHFileOperation API to ensure that the target file 291 // Renames a file using the SHFileOperation API to ensure that the target file
322 // gets the correct default security descriptor in the new path. 292 // gets the correct default security descriptor in the new path.
323 // Returns a network error, or net::OK for success. 293 // Returns a network error, or net::OK for success.
324 DownloadInterruptReason BaseFile::MoveFileAndAdjustPermissions( 294 DownloadInterruptReason BaseFile::MoveFileAndAdjustPermissions(
325 const base::FilePath& new_path) { 295 const base::FilePath& new_path) {
326 base::ThreadRestrictions::AssertIOAllowed(); 296 base::ThreadRestrictions::AssertIOAllowed();
327 297
328 // The parameters to SHFileOperation must be terminated with 2 NULL chars. 298 // The parameters to SHFileOperation must be terminated with 2 NULL chars.
(...skipping 16 matching lines...) Expand all
345 if (result == 0 && move_info.fAnyOperationsAborted) 315 if (result == 0 && move_info.fAnyOperationsAborted)
346 interrupt_reason = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED; 316 interrupt_reason = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
347 else if (result != 0) 317 else if (result != 0)
348 interrupt_reason = MapShFileOperationCodes(result); 318 interrupt_reason = MapShFileOperationCodes(result);
349 319
350 if (interrupt_reason != DOWNLOAD_INTERRUPT_REASON_NONE) 320 if (interrupt_reason != DOWNLOAD_INTERRUPT_REASON_NONE)
351 return LogInterruptReason("SHFileOperation", result, interrupt_reason); 321 return LogInterruptReason("SHFileOperation", result, interrupt_reason);
352 return interrupt_reason; 322 return interrupt_reason;
353 } 323 }
354 324
355 DownloadInterruptReason BaseFile::AnnotateWithSourceInformation(
356 const std::string& client_guid,
357 const GURL& source_url,
358 const GURL& referrer_url) {
359 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
360 DCHECK(!detached_);
361
362 bound_net_log_.BeginEvent(net::NetLog::TYPE_DOWNLOAD_FILE_ANNOTATED);
363 DownloadInterruptReason result = DOWNLOAD_INTERRUPT_REASON_NONE;
364 std::string braces_guid = "{" + client_guid + "}";
365 GUID guid = GUID_NULL;
366 if (base::IsValidGUID(client_guid)) {
367 HRESULT hr = CLSIDFromString(
368 base::UTF8ToUTF16(braces_guid).c_str(), &guid);
369 if (FAILED(hr))
370 guid = GUID_NULL;
371 }
372
373 HRESULT hr = AVScanFile(full_path_, source_url.spec(), guid);
374
375 // If the download file is missing after the call, then treat this as an
376 // interrupted download.
377 //
378 // If the ScanAndSaveDownloadedFile() call failed, but the downloaded file is
379 // still around, then don't interrupt the download. Attachment Execution
380 // Services deletes the submitted file if the downloaded file is blocked by
381 // policy or if it was found to be infected.
382 //
383 // If the file is still there, then the error could be due to AES not being
384 // available or some other error during the AES invocation. In either case,
385 // we don't surface the error to the user.
386 if (!base::PathExists(full_path_)) {
387 DCHECK(FAILED(hr));
388 result = MapScanAndSaveErrorCodeToInterruptReason(hr);
389 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) {
390 RecordDownloadCount(FILE_MISSING_AFTER_SUCCESSFUL_SCAN_COUNT);
391 result = DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED;
392 }
393 LogInterruptReason("ScanAndSaveDownloadedFile", hr, result);
394 }
395 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_FILE_ANNOTATED);
396 return result;
397 }
398
399 } // namespace content 325 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698