| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 <shlobj.h> | 5 #include <shlobj.h> |
| 6 #include <shobjidl.h> | 6 #include <shobjidl.h> |
| 7 | 7 |
| 8 #include "content/browser/safe_util_win.h" | 8 #include "content/browser/safe_util_win.h" |
| 9 | 9 |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/utf_string_conversions.h" |
| 14 #include "base/win/scoped_comptr.h" | 15 #include "base/win/scoped_comptr.h" |
| 16 #include "googleurl/src/gurl.h" |
| 15 #include "ui/base/win/shell.h" | 17 #include "ui/base/win/shell.h" |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 // This GUID is associated with any 'don't ask me again' settings that the | 21 // This GUID is associated with any 'don't ask me again' settings that the |
| 20 // user can select for different file types. | 22 // user can select for different file types. |
| 21 // {2676A9A2-D919-4fee-9187-152100393AB2} | 23 // {2676A9A2-D919-4fee-9187-152100393AB2} |
| 22 static const GUID kClientID = { 0x2676a9a2, 0xd919, 0x4fee, | 24 static const GUID kClientID = { 0x2676a9a2, 0xd919, 0x4fee, |
| 23 { 0x91, 0x87, 0x15, 0x21, 0x0, 0x39, 0x3a, 0xb2 } }; | 25 { 0x91, 0x87, 0x15, 0x21, 0x0, 0x39, 0x3a, 0xb2 } }; |
| 24 | 26 |
| 25 // Directly writes the ZoneIdentifier stream, without using the | 27 // Sets the Zone Identifier on the file to "Internet" (3). Returns true if the |
| 26 // IAttachmentExecute service. | 28 // function succeeds, false otherwise. A failure is expected on system where |
| 29 // the Zone Identifier is not supported, like a machine with a FAT32 filesystem. |
| 30 // This function does not invoke Windows Attachment Execution Services. |
| 31 // |
| 32 // |full_path| is the path to the downloaded file. |
| 27 bool SetInternetZoneIdentifierDirectly(const FilePath& full_path) { | 33 bool SetInternetZoneIdentifierDirectly(const FilePath& full_path) { |
| 28 const DWORD kShare = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 34 const DWORD kShare = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
| 29 std::wstring path = full_path.value() + L":Zone.Identifier"; | 35 std::wstring path = full_path.value() + L":Zone.Identifier"; |
| 30 HANDLE file = CreateFile(path.c_str(), GENERIC_WRITE, kShare, NULL, | 36 HANDLE file = CreateFile(path.c_str(), GENERIC_WRITE, kShare, NULL, |
| 31 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | 37 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 32 if (INVALID_HANDLE_VALUE == file) | 38 if (INVALID_HANDLE_VALUE == file) |
| 33 return false; | 39 return false; |
| 34 | 40 |
| 35 static const char kIdentifier[] = "[ZoneTransfer]\r\nZoneId=3\r\n"; | 41 static const char kIdentifier[] = "[ZoneTransfer]\r\nZoneId=3\r\n"; |
| 36 // Don't include trailing null in data written. | 42 // Don't include trailing null in data written. |
| 37 static const DWORD kIdentifierSize = arraysize(kIdentifier) - 1; | 43 static const DWORD kIdentifierSize = arraysize(kIdentifier) - 1; |
| 38 DWORD written = 0; | 44 DWORD written = 0; |
| 39 BOOL result = WriteFile(file, kIdentifier, kIdentifierSize, &written, | 45 BOOL result = WriteFile(file, kIdentifier, kIdentifierSize, &written, NULL); |
| 40 NULL); | |
| 41 BOOL flush_result = FlushFileBuffers(file); | 46 BOOL flush_result = FlushFileBuffers(file); |
| 42 CloseHandle(file); | 47 CloseHandle(file); |
| 43 | 48 |
| 44 if (!result || !flush_result || written != kIdentifierSize) { | 49 if (!result || !flush_result || written != kIdentifierSize) { |
| 45 NOTREACHED(); | 50 NOTREACHED(); |
| 46 return false; | 51 return false; |
| 47 } | 52 } |
| 48 | 53 |
| 49 return true; | 54 return true; |
| 50 } | 55 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 // decode and show the publisher and the certificate. | 113 // decode and show the publisher and the certificate. |
| 109 hr = attachment_services->Prompt(hwnd, ATTACHMENT_PROMPT_EXEC, &action); | 114 hr = attachment_services->Prompt(hwnd, ATTACHMENT_PROMPT_EXEC, &action); |
| 110 if (FAILED(hr) || (ATTACHMENT_ACTION_CANCEL == action)) { | 115 if (FAILED(hr) || (ATTACHMENT_ACTION_CANCEL == action)) { |
| 111 // The user has declined opening the item. | 116 // The user has declined opening the item. |
| 112 return false; | 117 return false; |
| 113 } | 118 } |
| 114 } | 119 } |
| 115 return ui::win::OpenItemViaShellNoZoneCheck(full_path); | 120 return ui::win::OpenItemViaShellNoZoneCheck(full_path); |
| 116 } | 121 } |
| 117 | 122 |
| 118 bool SetInternetZoneIdentifier(const FilePath& full_path, | 123 HRESULT ScanAndSaveDownloadedFile(const FilePath& full_path, |
| 119 const std::wstring& source_url) { | 124 const GURL& source_url) { |
| 120 base::win::ScopedComPtr<IAttachmentExecute> attachment_services; | 125 base::win::ScopedComPtr<IAttachmentExecute> attachment_services; |
| 121 HRESULT hr = attachment_services.CreateInstance(CLSID_AttachmentServices); | 126 HRESULT hr = attachment_services.CreateInstance(CLSID_AttachmentServices); |
| 122 | 127 |
| 123 if (FAILED(hr)) { | 128 if (FAILED(hr)) { |
| 129 // The thread must have COM initialized. |
| 130 DCHECK_NE(CO_E_NOTINITIALIZED, hr); |
| 131 |
| 124 // We don't have Attachment Execution Services, it must be a pre-XP.SP2 | 132 // We don't have Attachment Execution Services, it must be a pre-XP.SP2 |
| 125 // Windows installation, or the thread does not have COM initialized. | 133 // Windows installation, or the thread does not have COM initialized. Try to |
| 126 if (hr == CO_E_NOTINITIALIZED) { | 134 // set the zone information directly. Failure is not considered an error. |
| 127 NOTREACHED(); | 135 SetInternetZoneIdentifierDirectly(full_path); |
| 128 return false; | 136 return hr; |
| 129 } | |
| 130 | |
| 131 // Write the ZoneIdentifier file directly. | |
| 132 return SetInternetZoneIdentifierDirectly(full_path); | |
| 133 } | 137 } |
| 134 | 138 |
| 135 hr = attachment_services->SetClientGuid(kClientID); | 139 hr = attachment_services->SetClientGuid(kClientID); |
| 136 if (FAILED(hr)) | 140 if (FAILED(hr)) |
| 137 return false; | 141 return hr; |
| 138 | 142 |
| 139 hr = attachment_services->SetLocalPath(full_path.value().c_str()); | 143 hr = attachment_services->SetLocalPath(full_path.value().c_str()); |
| 140 if (FAILED(hr)) | 144 if (FAILED(hr)) |
| 141 return false; | 145 return hr; |
| 142 | 146 |
| 143 // Source is necessary for files ending in ".tmp" to avoid error 0x800c000e. | 147 hr = attachment_services->SetSource(UTF8ToWide(source_url.spec()).c_str()); |
| 144 hr = attachment_services->SetSource(source_url.c_str()); | |
| 145 if (FAILED(hr)) | 148 if (FAILED(hr)) |
| 146 return false; | 149 return hr; |
| 147 | 150 |
| 148 hr = attachment_services->Save(); | 151 // A failure in the Save() call below could result in the downloaded file |
| 149 if (FAILED(hr)) | 152 // being deleted. |
| 150 return false; | 153 return attachment_services->Save(); |
| 151 | |
| 152 return true; | |
| 153 } | 154 } |
| 154 | 155 |
| 155 } // namespace win_util | 156 } // namespace win_util |
| OLD | NEW |