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