1 // Copyright (c) 2011 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.
8 #include "content/browser/safe_util_win.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/path_service.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/win/scoped_comptr.h"
16 #include "ui/base/win/shell.h"
22 // Sets the Zone Identifier on the file to "Internet" (3). Returns true if the
23 // function succeeds, false otherwise. A failure is expected on system where
24 // the Zone Identifier is not supported, like a machine with a FAT32 filesystem.
25 // This function does not invoke Windows Attachment Execution Services.
27 // |full_path| is the path to the downloaded file.
28 bool SetInternetZoneIdentifierDirectly(const base::FilePath
& full_path
) {
29 const DWORD kShare
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
30 std::wstring path
= full_path
.value() + L
":Zone.Identifier";
31 HANDLE file
= CreateFile(path
.c_str(), GENERIC_WRITE
, kShare
, NULL
,
32 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
33 if (INVALID_HANDLE_VALUE
== file
)
36 static const char kIdentifier
[] = "[ZoneTransfer]\r\nZoneId=3\r\n";
37 // Don't include trailing null in data written.
38 static const DWORD kIdentifierSize
= arraysize(kIdentifier
) - 1;
40 BOOL result
= WriteFile(file
, kIdentifier
, kIdentifierSize
, &written
, NULL
);
41 BOOL flush_result
= FlushFileBuffers(file
);
44 if (!result
|| !flush_result
|| written
!= kIdentifierSize
) {
54 HRESULT
AVScanFile(const base::FilePath
& full_path
,
55 const std::string
& source_url
,
56 const GUID
& client_guid
) {
57 base::win::ScopedComPtr
<IAttachmentExecute
> attachment_services
;
58 HRESULT hr
= attachment_services
.CreateInstance(CLSID_AttachmentServices
);
61 // The thread must have COM initialized.
62 DCHECK_NE(CO_E_NOTINITIALIZED
, hr
);
64 // We don't have Attachment Execution Services, it must be a pre-XP.SP2
65 // Windows installation, or the thread does not have COM initialized. Try to
66 // set the zone information directly. Failure is not considered an error.
67 SetInternetZoneIdentifierDirectly(full_path
);
71 if (!IsEqualGUID(client_guid
, GUID_NULL
)) {
72 hr
= attachment_services
->SetClientGuid(client_guid
);
77 hr
= attachment_services
->SetLocalPath(full_path
.value().c_str());
81 // Note: SetSource looks like it needs to be called, even if empty.
82 // Docs say it is optional, but it appears not calling it at all sets
83 // a zone that is too restrictive.
84 hr
= attachment_services
->SetSource(base::UTF8ToWide(source_url
).c_str());
88 // A failure in the Save() call below could result in the downloaded file
90 return attachment_services
->Save();
93 } // namespace content