#include <shlobj.h>
#include <shobjidl.h>
#include "content/browser/safe_util_win.h"
#include "base/file_path.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "base/win/scoped_comptr.h"
#include "ui/base/win/shell.h"
namespace {
static const GUID kClientID = { 0x2676a9a2, 0xd919, 0x4fee,
{ 0x91, 0x87, 0x15, 0x21, 0x0, 0x39, 0x3a, 0xb2 } };
bool SetInternetZoneIdentifierDirectly(const FilePath& full_path) {
const DWORD kShare = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
std::wstring path = full_path.value() + L":Zone.Identifier";
HANDLE file = CreateFile(path.c_str(), GENERIC_WRITE, kShare, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == file)
return false;
static const char kIdentifier[] = "[ZoneTransfer]\r\nZoneId=3\r\n";
static const DWORD kIdentifierSize = arraysize(kIdentifier) - 1;
DWORD written = 0;
BOOL result = WriteFile(file, kIdentifier, kIdentifierSize, &written,
NULL);
BOOL flush_result = FlushFileBuffers(file);
CloseHandle(file);
if (!result || !flush_result || written != kIdentifierSize) {
NOTREACHED();
return false;
}
return true;
}
}
namespace win_util {
bool SaferOpenItemViaShell(HWND hwnd, const std::wstring& window_title,
const FilePath& full_path,
const std::wstring& source_url) {
base::win::ScopedComPtr<IAttachmentExecute> attachment_services;
HRESULT hr = attachment_services.CreateInstance(CLSID_AttachmentServices);
if (FAILED(hr)) {
if (hr == CO_E_NOTINITIALIZED) {
NOTREACHED();
return false;
}
return ui::win::OpenItemViaShell(full_path);
}
attachment_services->SetClientGuid(kClientID);
if (!window_title.empty())
attachment_services->SetClientTitle(window_title.c_str());
hr = attachment_services->SetLocalPath(full_path.value().c_str());
if (FAILED(hr))
return false;
hr = attachment_services->SetSource(source_url.c_str());
if (FAILED(hr))
return false;
if (attachment_services->CheckPolicy() != S_OK) {
ATTACHMENT_ACTION action;
hr = attachment_services->Prompt(hwnd, ATTACHMENT_PROMPT_EXEC, &action);
if (FAILED(hr) || (ATTACHMENT_ACTION_CANCEL == action)) {
return false;
}
}
return ui::win::OpenItemViaShellNoZoneCheck(full_path);
}
bool SetInternetZoneIdentifier(const FilePath& full_path,
const std::wstring& source_url) {
base::win::ScopedComPtr<IAttachmentExecute> attachment_services;
HRESULT hr = attachment_services.CreateInstance(CLSID_AttachmentServices);
if (FAILED(hr)) {
if (hr == CO_E_NOTINITIALIZED) {
NOTREACHED();
return false;
}
return SetInternetZoneIdentifierDirectly(full_path);
}
hr = attachment_services->SetClientGuid(kClientID);
if (FAILED(hr))
return false;
hr = attachment_services->SetLocalPath(full_path.value().c_str());
if (FAILED(hr))
return false;
hr = attachment_services->SetSource(source_url.c_str());
if (FAILED(hr))
return false;
hr = attachment_services->Save();
if (FAILED(hr))
return false;
return true;
}
}