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

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: [win] Verify that the Zone.Identifier stream has the correct contents. Created 4 years, 3 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_macros.h" 16 #include "base/metrics/histogram_macros.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 #include "net/log/net_log_event_type.h" 22 #include "net/log/net_log_event_type.h"
24 23
25 namespace content { 24 namespace content {
26 namespace { 25 namespace {
27 26
28 const int kAllSpecialShFileOperationCodes[] = { 27 const int kAllSpecialShFileOperationCodes[] = {
29 // Should be kept in sync with the case statement below. 28 // Should be kept in sync with the case statement below.
30 ERROR_ACCESS_DENIED, 29 ERROR_ACCESS_DENIED,
31 ERROR_SHARING_VIOLATION, 30 ERROR_SHARING_VIOLATION,
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 } 280 }
282 281
283 if (result != DOWNLOAD_INTERRUPT_REASON_NONE) 282 if (result != DOWNLOAD_INTERRUPT_REASON_NONE)
284 return result; 283 return result;
285 284
286 // If not one of the above codes, it should be a standard Windows error code. 285 // If not one of the above codes, it should be a standard Windows error code.
287 return ConvertFileErrorToInterruptReason( 286 return ConvertFileErrorToInterruptReason(
288 base::File::OSErrorToFileError(code)); 287 base::File::OSErrorToFileError(code));
289 } 288 }
290 289
291 // Maps a return code from ScanAndSaveDownloadedFile() to a
292 // DownloadInterruptReason. The return code in |result| is usually from the
293 // final IAttachmentExecute::Save() call.
294 DownloadInterruptReason MapScanAndSaveErrorCodeToInterruptReason(
295 HRESULT result) {
296 if (SUCCEEDED(result))
297 return DOWNLOAD_INTERRUPT_REASON_NONE;
298
299 switch (result) {
300 case INET_E_SECURITY_PROBLEM: // 0x800c000e
301 // This is returned if the download was blocked due to security
302 // restrictions. E.g. if the source URL was in the Restricted Sites zone
303 // and downloads are blocked on that zone, then the download would be
304 // deleted and this error code is returned.
305 return DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED;
306
307 case E_FAIL: // 0x80004005
308 // Returned if an anti-virus product reports an infection in the
309 // downloaded file during IAE::Save().
310 return DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED;
311
312 default:
313 // Any other error that occurs during IAttachmentExecute::Save() likely
314 // indicates a problem with the security check, but not necessarily the
315 // download. See http://crbug.com/153212.
316 return DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED;
317 }
318 }
319
320 } // namespace 290 } // namespace
321 291
322 // Renames a file using the SHFileOperation API to ensure that the target file 292 // Renames a file using the SHFileOperation API to ensure that the target file
323 // gets the correct default security descriptor in the new path. 293 // gets the correct default security descriptor in the new path.
324 // Returns a network error, or net::OK for success. 294 // Returns a network error, or net::OK for success.
325 DownloadInterruptReason BaseFile::MoveFileAndAdjustPermissions( 295 DownloadInterruptReason BaseFile::MoveFileAndAdjustPermissions(
326 const base::FilePath& new_path) { 296 const base::FilePath& new_path) {
327 base::ThreadRestrictions::AssertIOAllowed(); 297 base::ThreadRestrictions::AssertIOAllowed();
328 298
329 // The parameters to SHFileOperation must be terminated with 2 NULL chars. 299 // The parameters to SHFileOperation must be terminated with 2 NULL chars.
(...skipping 16 matching lines...) Expand all
346 if (result == 0 && move_info.fAnyOperationsAborted) 316 if (result == 0 && move_info.fAnyOperationsAborted)
347 interrupt_reason = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED; 317 interrupt_reason = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
348 else if (result != 0) 318 else if (result != 0)
349 interrupt_reason = MapShFileOperationCodes(result); 319 interrupt_reason = MapShFileOperationCodes(result);
350 320
351 if (interrupt_reason != DOWNLOAD_INTERRUPT_REASON_NONE) 321 if (interrupt_reason != DOWNLOAD_INTERRUPT_REASON_NONE)
352 return LogInterruptReason("SHFileOperation", result, interrupt_reason); 322 return LogInterruptReason("SHFileOperation", result, interrupt_reason);
353 return interrupt_reason; 323 return interrupt_reason;
354 } 324 }
355 325
356 DownloadInterruptReason BaseFile::AnnotateWithSourceInformation(
357 const std::string& client_guid,
358 const GURL& source_url,
359 const GURL& referrer_url) {
360 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
361 DCHECK(!detached_);
362
363 bound_net_log_.BeginEvent(net::NetLogEventType::DOWNLOAD_FILE_ANNOTATED);
364 DownloadInterruptReason result = DOWNLOAD_INTERRUPT_REASON_NONE;
365 std::string braces_guid = "{" + client_guid + "}";
366 GUID guid = GUID_NULL;
367 if (base::IsValidGUID(client_guid)) {
368 HRESULT hr = CLSIDFromString(
369 base::UTF8ToUTF16(braces_guid).c_str(), &guid);
370 if (FAILED(hr))
371 guid = GUID_NULL;
372 }
373
374 HRESULT hr = AVScanFile(full_path_, source_url.spec(), guid);
375
376 // If the download file is missing after the call, then treat this as an
377 // interrupted download.
378 //
379 // If the ScanAndSaveDownloadedFile() call failed, but the downloaded file is
380 // still around, then don't interrupt the download. Attachment Execution
381 // Services deletes the submitted file if the downloaded file is blocked by
382 // policy or if it was found to be infected.
383 //
384 // If the file is still there, then the error could be due to AES not being
385 // available or some other error during the AES invocation. In either case,
386 // we don't surface the error to the user.
387 if (!base::PathExists(full_path_)) {
388 DCHECK(FAILED(hr));
389 result = MapScanAndSaveErrorCodeToInterruptReason(hr);
390 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) {
391 RecordDownloadCount(FILE_MISSING_AFTER_SUCCESSFUL_SCAN_COUNT);
392 result = DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED;
393 }
394 LogInterruptReason("ScanAndSaveDownloadedFile", hr, result);
395 }
396 bound_net_log_.EndEvent(net::NetLogEventType::DOWNLOAD_FILE_ANNOTATED);
397 return result;
398 }
399
400 } // namespace content 326 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698