Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
|
jochen (gone - plz use gerrit)
2014/08/04 09:13:41
why this file? Where's the header and the gyp/gn c
erikwright (departed)
2014/08/12 18:34:31
Sorry, this was somehow left over from an earlier
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/utility/sandboxed_shell_handler_win.h" | |
| 6 | |
| 7 #include <commdlg.h> | |
| 8 | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "chrome/common/chrome_utility_messages.h" | |
| 12 #include "content/public/utility/utility_thread.h" | |
| 13 #include "ui/base/win/shell.h" | |
| 14 | |
| 15 SandboxedShellHandler::SandboxedShellHandler() {} | |
| 16 SandboxedShellHandler::~SandboxedShellHandler() {} | |
| 17 | |
| 18 bool SandboxedShellHandler::OnMessageReceived(const IPC::Message& message) { | |
| 19 bool handled = true; | |
| 20 IPC_BEGIN_MESSAGE_MAP(SandboxedShellHandler, message) | |
| 21 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_OpenItemViaShell, | |
| 22 OnOpenItemViaShell) | |
| 23 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetOpenFileName, | |
| 24 OnGetOpenFileName) | |
| 25 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 26 IPC_END_MESSAGE_MAP() | |
| 27 return handled; | |
| 28 } | |
| 29 | |
| 30 void SandboxedShellHandler::OnOpenItemViaShell( | |
| 31 const base::FilePath& full_path) { | |
| 32 ui::win::OpenItemViaShell(full_path); | |
| 33 } | |
| 34 | |
| 35 void SandboxedShellHandler::OnGetOpenFileName( | |
| 36 HWND owner, | |
| 37 DWORD flags, | |
| 38 const GetOpenFileNameFilter& filter, | |
| 39 const base::FilePath& initial_directory, | |
| 40 const base::FilePath& filename) { | |
| 41 OPENFILENAME ofn; | |
| 42 // We must do this otherwise the ofn's FlagsEx may be initialized to random | |
| 43 // junk in release builds which can cause the Places Bar not to show up! | |
| 44 ZeroMemory(&ofn, sizeof(ofn)); | |
| 45 ofn.lStructSize = sizeof(ofn); | |
| 46 ofn.hwndOwner = owner; | |
| 47 scoped_ptr<wchar_t[]> filename_buffer(new wchar_t[UNICODE_STRING_MAX_CHARS]); | |
| 48 // According to http://support.microsoft.com/?scid=kb;en-us;222003&x=8&y=12, | |
| 49 // The lpstrFile Buffer MUST be NULL Terminated. | |
| 50 filename_buffer[0] = 0; | |
| 51 if (!initial_directory.empty()) { | |
| 52 ofn.lpstrInitialDir = initial_directory.value().c_str(); | |
| 53 if (!filename.empty()) { | |
| 54 base::wcslcpy(filename_buffer.get(), | |
| 55 filename.value().c_str(), | |
| 56 UNICODE_STRING_MAX_CHARS); | |
| 57 } | |
| 58 } | |
| 59 ofn.lpstrFile = filename_buffer.get(); | |
| 60 ofn.nMaxFile = UNICODE_STRING_MAX_CHARS; | |
| 61 ofn.Flags = flags; | |
| 62 base::string16 filter_buffer; | |
| 63 if (!filter.empty()) { | |
| 64 for (GetOpenFileNameFilter::const_iterator it = filter.begin(); | |
| 65 it != filter.end(); | |
| 66 ++it) { | |
| 67 filter_buffer.append(it->a); | |
| 68 filter_buffer.push_back(0); | |
| 69 filter_buffer.append(it->b); | |
| 70 filter_buffer.push_back(0); | |
| 71 } | |
| 72 filter_buffer.push_back(0); | |
| 73 ofn.lpstrFilter = filter_buffer.c_str(); | |
| 74 } | |
| 75 | |
| 76 base::FilePath directory; | |
| 77 std::vector<base::FilePath> filenames; | |
| 78 | |
| 79 if (::GetOpenFileName(&ofn)) { | |
| 80 const wchar_t* selection = ofn.lpstrFile; | |
| 81 while (*selection) { // Empty string indicates end of list. | |
| 82 filenames.push_back(base::FilePath(selection)); | |
| 83 // Skip over filename and null-terminator. | |
| 84 selection += filenames.back().value().length() + 1; | |
| 85 } | |
| 86 if (filenames.size() == 1) { | |
| 87 // When there is one file, it contains the path and filename. | |
| 88 directory = filenames[0].DirName(); | |
| 89 filenames[0] = filenames[0].BaseName(); | |
| 90 } else if (filenames.size() > 1) { | |
| 91 // Otherwise, the first string is the path, and the remainder are | |
| 92 // filenames. | |
| 93 directory = filenames[0]; | |
| 94 filenames.erase(filenames.begin()); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 if (filenames.size()) { | |
| 99 content::UtilityThread::Get()->Send( | |
| 100 new ChromeUtilityHostMsg_GetOpenFileName_Result(directory, filenames)); | |
| 101 } else { | |
| 102 content::UtilityThread::Get()->Send( | |
| 103 new ChromeUtilityHostMsg_GetOpenFileName_Failed()); | |
| 104 } | |
| 105 } | |
| OLD | NEW |