| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/common/platform_util.h" | 5 #include "chrome/common/platform_util.h" |
| 6 | 6 |
| 7 #include <atlbase.h> | 7 #include <atlbase.h> |
| 8 #include <atlapp.h> | 8 #include <atlapp.h> |
| 9 #include <commdlg.h> | 9 #include <commdlg.h> |
| 10 #include <dwmapi.h> | 10 #include <dwmapi.h> |
| 11 #include <shellapi.h> | 11 #include <shellapi.h> |
| 12 #include <shlobj.h> | 12 #include <shlobj.h> |
| 13 | 13 |
| 14 #include "app/win_util.h" | 14 #include "app/win_util.h" |
| 15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
| 16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 17 #include "base/gfx/native_widget_types.h" | 17 #include "base/gfx/native_widget_types.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/registry.h" |
| 19 #include "base/string_util.h" | 20 #include "base/string_util.h" |
| 21 #include "googleurl/src/gurl.h" |
| 20 | 22 |
| 21 namespace platform_util { | 23 namespace platform_util { |
| 22 | 24 |
| 23 void ShowItemInFolder(const FilePath& full_path) { | 25 void ShowItemInFolder(const FilePath& full_path) { |
| 24 FilePath dir = full_path.DirName(); | 26 FilePath dir = full_path.DirName(); |
| 25 // ParseDisplayName will fail if the directory is "C:", it must be "C:\\". | 27 // ParseDisplayName will fail if the directory is "C:", it must be "C:\\". |
| 26 if (dir.value() == L"" || !file_util::EnsureEndsWithSeparator(&dir)) | 28 if (dir.value() == L"" || !file_util::EnsureEndsWithSeparator(&dir)) |
| 27 return; | 29 return; |
| 28 | 30 |
| 29 typedef HRESULT (WINAPI *SHOpenFolderAndSelectItemsFuncPtr)( | 31 typedef HRESULT (WINAPI *SHOpenFolderAndSelectItemsFuncPtr)( |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 {file_item}, | 81 {file_item}, |
| 80 }; | 82 }; |
| 81 (*open_folder_and_select_itemsPtr)(dir_item, arraysize(highlight), | 83 (*open_folder_and_select_itemsPtr)(dir_item, arraysize(highlight), |
| 82 highlight, NULL); | 84 highlight, NULL); |
| 83 } | 85 } |
| 84 | 86 |
| 85 void OpenItem(const FilePath& full_path) { | 87 void OpenItem(const FilePath& full_path) { |
| 86 win_util::OpenItemViaShell(full_path); | 88 win_util::OpenItemViaShell(full_path); |
| 87 } | 89 } |
| 88 | 90 |
| 91 void OpenExternal(const GURL& url) { |
| 92 // Quote the input scheme to be sure that the command does not have |
| 93 // parameters unexpected by the external program. This url should already |
| 94 // have been escaped. |
| 95 std::string escaped_url = url.spec(); |
| 96 escaped_url.insert(0, "\""); |
| 97 escaped_url += "\""; |
| 98 |
| 99 // According to Mozilla in uriloader/exthandler/win/nsOSHelperAppService.cpp: |
| 100 // "Some versions of windows (Win2k before SP3, Win XP before SP1) crash in |
| 101 // ShellExecute on long URLs (bug 161357 on bugzilla.mozilla.org). IE 5 and 6 |
| 102 // support URLS of 2083 chars in length, 2K is safe." |
| 103 const size_t kMaxUrlLength = 2048; |
| 104 if (escaped_url.length() > kMaxUrlLength) { |
| 105 NOTREACHED(); |
| 106 return; |
| 107 } |
| 108 |
| 109 RegKey key; |
| 110 std::wstring registry_path = ASCIIToWide(url.scheme()) + |
| 111 L"\\shell\\open\\command"; |
| 112 key.Open(HKEY_CLASSES_ROOT, registry_path.c_str()); |
| 113 if (key.Valid()) { |
| 114 DWORD size = 0; |
| 115 key.ReadValue(NULL, NULL, &size); |
| 116 if (size <= 2) { |
| 117 // ShellExecute crashes the process when the command is empty. |
| 118 // We check for "2" because it always returns the trailing NULL. |
| 119 // TODO(nsylvain): we should also add a dialog to warn on errors. See |
| 120 // bug 1136923. |
| 121 return; |
| 122 } |
| 123 } |
| 124 |
| 125 if (reinterpret_cast<ULONG_PTR>(ShellExecuteA(NULL, "open", |
| 126 escaped_url.c_str(), NULL, NULL, |
| 127 SW_SHOWNORMAL)) <= 32) { |
| 128 // We fail to execute the call. We could display a message to the user. |
| 129 // TODO(nsylvain): we should also add a dialog to warn on errors. See |
| 130 // bug 1136923. |
| 131 return; |
| 132 } |
| 133 } |
| 134 |
| 89 gfx::NativeWindow GetTopLevel(gfx::NativeView view) { | 135 gfx::NativeWindow GetTopLevel(gfx::NativeView view) { |
| 90 return GetAncestor(view, GA_ROOT); | 136 return GetAncestor(view, GA_ROOT); |
| 91 } | 137 } |
| 92 | 138 |
| 93 string16 GetWindowTitle(gfx::NativeWindow window_handle) { | 139 string16 GetWindowTitle(gfx::NativeWindow window_handle) { |
| 94 std::wstring result; | 140 std::wstring result; |
| 95 int length = ::GetWindowTextLength(window_handle) + 1; | 141 int length = ::GetWindowTextLength(window_handle) + 1; |
| 96 ::GetWindowText(window_handle, WriteInto(&result, length), length); | 142 ::GetWindowText(window_handle, WriteInto(&result, length), length); |
| 97 return WideToUTF16(result); | 143 return WideToUTF16(result); |
| 98 } | 144 } |
| 99 | 145 |
| 100 bool IsWindowActive(gfx::NativeWindow window) { | 146 bool IsWindowActive(gfx::NativeWindow window) { |
| 101 return ::GetForegroundWindow() == window; | 147 return ::GetForegroundWindow() == window; |
| 102 } | 148 } |
| 103 | 149 |
| 104 bool IsVisible(gfx::NativeView view) { | 150 bool IsVisible(gfx::NativeView view) { |
| 105 // MSVC complains if we don't include != 0. | 151 // MSVC complains if we don't include != 0. |
| 106 return ::IsWindowVisible(view) != 0; | 152 return ::IsWindowVisible(view) != 0; |
| 107 } | 153 } |
| 108 | 154 |
| 109 } // namespace platform_util | 155 } // namespace platform_util |
| OLD | NEW |