diff options
author | xiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-19 09:18:50 +0000 |
---|---|---|
committer | xiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-19 09:18:50 +0000 |
commit | 86b5401aa4c221ea07185bb493bb9557d779be0e (patch) | |
tree | 7692a2c15897b02e99b3ece5a941abae6553c373 /base | |
parent | 4ea927b886af2654b5a193278c0bfb6c25b56fc7 (diff) | |
download | chromium_src-86b5401aa4c221ea07185bb493bb9557d779be0e.zip chromium_src-86b5401aa4c221ea07185bb493bb9557d779be0e.tar.gz chromium_src-86b5401aa4c221ea07185bb493bb9557d779be0e.tar.bz2 |
Set prop app id for chromium/application shortcut.
This is a follow up change after andrew's patch for win7 shortcut to
properly set app id for chromium/application shortcut.
- Move PKEY_AppUserModel_ID and code to set it from app/win_util.cc to
base/win_util.cc as SetAppIdForPropertyStore to share with file_util
shortcut code;
- Add an app_id args to file_util's CreateShortcutLink and
UpdateShortcutLink;
- Update code that calls the above two function in installer and
UserDataManager so that the chromium shortcuts are created with proper
app id (except the uninstall shortcut which is not tagged with any app
id).
- Move ComputeApplicationNameFromURL from Browser to web_app namespace
and use it as app id for application shortcut. This makes pinned
shortcut and browser window use the same app id and Win7 correctly
groups them;
- Rename ComputeApplicationNameFromURL to GenerateApplicationNameFromURL
per Ben's comments;
- Add a DCHECK in SetAppIdForPropertyStore to ensure app id is less than
128 chars and contains no space per msdn;
- Change default app id from IDS_PRODUCT_NAME to chrome::kBrowserAppName
BUG=28104
TEST=On Win7, pinned shortcut should no longer be separated from running instance of chrome for both chrome and web application.
Review URL: http://codereview.chromium.org/399045
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@32508 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/file_util.h | 10 | ||||
-rw-r--r-- | base/file_util_unittest.cc | 2 | ||||
-rw-r--r-- | base/file_util_win.cc | 129 | ||||
-rw-r--r-- | base/win_util.cc | 27 | ||||
-rw-r--r-- | base/win_util.h | 7 |
5 files changed, 89 insertions, 86 deletions
diff --git a/base/file_util.h b/base/file_util.h index 60e9793..915065e 100644 --- a/base/file_util.h +++ b/base/file_util.h @@ -218,24 +218,24 @@ bool ResolveShortcut(FilePath* path); // 'source' is the existing file, 'destination' is the new link file to be // created; for best results pass the filename with the .lnk extension. // The 'icon' can specify a dll or exe in which case the icon index is the -// resource id. +// resource id. 'app_id' is the app model id for the shortcut on Win7. // Note that if the shortcut exists it will overwrite it. bool CreateShortcutLink(const wchar_t *source, const wchar_t *destination, const wchar_t *working_dir, const wchar_t *arguments, const wchar_t *description, const wchar_t *icon, - int icon_index); + int icon_index, const wchar_t* app_id); // Update a Windows shortcut (.LNK file). This method assumes the shortcut // link already exists (otherwise false is returned). Ensure you have // initialized COM before calling into this function. Only 'destination' // parameter is required, everything else can be NULL (but if everything else // is NULL no changes are made to the shortcut). 'destination' is the link -// file to be updated. For best results pass the filename with the .lnk -// extension. +// file to be updated. 'app_id' is the app model id for the shortcut on Win7. +// For best results pass the filename with the .lnk extension. bool UpdateShortcutLink(const wchar_t *source, const wchar_t *destination, const wchar_t *working_dir, const wchar_t *arguments, const wchar_t *description, const wchar_t *icon, - int icon_index); + int icon_index, const wchar_t* app_id); // Pins a shortcut to the Windows 7 taskbar. The shortcut file must already // exist and be a shortcut that points to an executable. diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc index 40a4163..6d57e2c 100644 --- a/base/file_util_unittest.cc +++ b/base/file_util_unittest.cc @@ -989,7 +989,7 @@ TEST_F(FileUtilTest, CreateShortcutTest) { CoInitialize(NULL); EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(), link_file.value().c_str(), - NULL, NULL, NULL, NULL, 0)); + NULL, NULL, NULL, NULL, 0, NULL)); FilePath resolved_name = link_file; EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name)); std::wstring read_contents = ReadTextFile(resolved_name); diff --git a/base/file_util_win.cc b/base/file_util_win.cc index e050324..a320aca 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -5,6 +5,7 @@ #include "base/file_util.h" #include <windows.h> +#include <propvarutil.h> #include <shellapi.h> #include <shlobj.h> #include <time.h> @@ -12,6 +13,7 @@ #include "base/file_path.h" #include "base/logging.h" +#include "base/scoped_comptr_win.h" #include "base/scoped_handle.h" #include "base/string_util.h" #include "base/time.h" @@ -258,38 +260,32 @@ bool GetFileCreationLocalTime(const std::wstring& filename, bool ResolveShortcut(FilePath* path) { HRESULT result; - IShellLink *shell = NULL; + ScopedComPtr<IShellLink> i_shell_link; bool is_resolved = false; // Get pointer to the IShellLink interface - result = CoCreateInstance(CLSID_ShellLink, NULL, - CLSCTX_INPROC_SERVER, IID_IShellLink, - reinterpret_cast<LPVOID*>(&shell)); + result = i_shell_link.CreateInstance(CLSID_ShellLink, NULL, + CLSCTX_INPROC_SERVER); if (SUCCEEDED(result)) { - IPersistFile *persist = NULL; + ScopedComPtr<IPersistFile> persist; // Query IShellLink for the IPersistFile interface - result = shell->QueryInterface(IID_IPersistFile, - reinterpret_cast<LPVOID*>(&persist)); + result = persist.QueryFrom(i_shell_link); if (SUCCEEDED(result)) { WCHAR temp_path[MAX_PATH]; // Load the shell link result = persist->Load(path->value().c_str(), STGM_READ); if (SUCCEEDED(result)) { // Try to find the target of a shortcut - result = shell->Resolve(0, SLR_NO_UI); + result = i_shell_link->Resolve(0, SLR_NO_UI); if (SUCCEEDED(result)) { - result = shell->GetPath(temp_path, MAX_PATH, + result = i_shell_link->GetPath(temp_path, MAX_PATH, NULL, SLGP_UNCPRIORITY); *path = FilePath(temp_path); is_resolved = true; } } } - if (persist) - persist->Release(); } - if (shell) - shell->Release(); return is_resolved; } @@ -297,58 +293,46 @@ bool ResolveShortcut(FilePath* path) { bool CreateShortcutLink(const wchar_t *source, const wchar_t *destination, const wchar_t *working_dir, const wchar_t *arguments, const wchar_t *description, const wchar_t *icon, - int icon_index) { - IShellLink *i_shell_link = NULL; - IPersistFile *i_persist_file = NULL; + int icon_index, const wchar_t* app_id) { + ScopedComPtr<IShellLink> i_shell_link; + ScopedComPtr<IPersistFile> i_persist_file; // Get pointer to the IShellLink interface - HRESULT result = CoCreateInstance(CLSID_ShellLink, NULL, - CLSCTX_INPROC_SERVER, IID_IShellLink, - reinterpret_cast<LPVOID*>(&i_shell_link)); + HRESULT result = i_shell_link.CreateInstance(CLSID_ShellLink, NULL, + CLSCTX_INPROC_SERVER); if (FAILED(result)) return false; // Query IShellLink for the IPersistFile interface - result = i_shell_link->QueryInterface(IID_IPersistFile, - reinterpret_cast<LPVOID*>(&i_persist_file)); - if (FAILED(result)) { - i_shell_link->Release(); + result = i_persist_file.QueryFrom(i_shell_link); + if (FAILED(result)) return false; - } - if (FAILED(i_shell_link->SetPath(source))) { - i_persist_file->Release(); - i_shell_link->Release(); + if (FAILED(i_shell_link->SetPath(source))) return false; - } - if (working_dir && FAILED(i_shell_link->SetWorkingDirectory(working_dir))) { - i_persist_file->Release(); - i_shell_link->Release(); + if (working_dir && FAILED(i_shell_link->SetWorkingDirectory(working_dir))) return false; - } - if (arguments && FAILED(i_shell_link->SetArguments(arguments))) { - i_persist_file->Release(); - i_shell_link->Release(); + if (arguments && FAILED(i_shell_link->SetArguments(arguments))) return false; - } - if (description && FAILED(i_shell_link->SetDescription(description))) { - i_persist_file->Release(); - i_shell_link->Release(); + if (description && FAILED(i_shell_link->SetDescription(description))) return false; - } - if (icon && FAILED(i_shell_link->SetIconLocation(icon, icon_index))) { - i_persist_file->Release(); - i_shell_link->Release(); + if (icon && FAILED(i_shell_link->SetIconLocation(icon, icon_index))) return false; + + if (app_id && (win_util::GetWinVersion() >= win_util::WINVERSION_WIN7)) { + ScopedComPtr<IPropertyStore> property_store; + if (FAILED(property_store.QueryFrom(i_shell_link))) + return false; + + if (!win_util::SetAppIdForPropertyStore(property_store, app_id)) + return false; } result = i_persist_file->Save(destination, TRUE); - i_persist_file->Release(); - i_shell_link->Release(); return SUCCEEDED(result); } @@ -356,60 +340,45 @@ bool CreateShortcutLink(const wchar_t *source, const wchar_t *destination, bool UpdateShortcutLink(const wchar_t *source, const wchar_t *destination, const wchar_t *working_dir, const wchar_t *arguments, const wchar_t *description, const wchar_t *icon, - int icon_index) { + int icon_index, const wchar_t* app_id) { // Get pointer to the IPersistFile interface and load existing link - IShellLink *i_shell_link = NULL; - if (FAILED(CoCreateInstance(CLSID_ShellLink, NULL, - CLSCTX_INPROC_SERVER, IID_IShellLink, - reinterpret_cast<LPVOID*>(&i_shell_link)))) + ScopedComPtr<IShellLink> i_shell_link; + if (FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL, + CLSCTX_INPROC_SERVER))) return false; - IPersistFile *i_persist_file = NULL; - if (FAILED(i_shell_link->QueryInterface( - IID_IPersistFile, reinterpret_cast<LPVOID*>(&i_persist_file)))) { - i_shell_link->Release(); + ScopedComPtr<IPersistFile> i_persist_file; + if (FAILED(i_persist_file.QueryFrom(i_shell_link))) return false; - } - if (FAILED(i_persist_file->Load(destination, 0))) { - i_persist_file->Release(); - i_shell_link->Release(); + if (FAILED(i_persist_file->Load(destination, 0))) return false; - } - if (source && FAILED(i_shell_link->SetPath(source))) { - i_persist_file->Release(); - i_shell_link->Release(); + if (source && FAILED(i_shell_link->SetPath(source))) return false; - } - if (working_dir && FAILED(i_shell_link->SetWorkingDirectory(working_dir))) { - i_persist_file->Release(); - i_shell_link->Release(); + if (working_dir && FAILED(i_shell_link->SetWorkingDirectory(working_dir))) return false; - } - if (arguments && FAILED(i_shell_link->SetArguments(arguments))) { - i_persist_file->Release(); - i_shell_link->Release(); + if (arguments && FAILED(i_shell_link->SetArguments(arguments))) return false; - } - if (description && FAILED(i_shell_link->SetDescription(description))) { - i_persist_file->Release(); - i_shell_link->Release(); + if (description && FAILED(i_shell_link->SetDescription(description))) return false; - } - if (icon && FAILED(i_shell_link->SetIconLocation(icon, icon_index))) { - i_persist_file->Release(); - i_shell_link->Release(); + if (icon && FAILED(i_shell_link->SetIconLocation(icon, icon_index))) return false; + + if (app_id && win_util::GetWinVersion() >= win_util::WINVERSION_WIN7) { + ScopedComPtr<IPropertyStore> property_store; + if (FAILED(property_store.QueryFrom(i_shell_link))) + return false; + + if (!win_util::SetAppIdForPropertyStore(property_store, app_id)) + return false; } HRESULT result = i_persist_file->Save(destination, TRUE); - i_persist_file->Release(); - i_shell_link->Release(); return SUCCEEDED(result); } diff --git a/base/win_util.cc b/base/win_util.cc index e66dc1c..fa168d0 100644 --- a/base/win_util.cc +++ b/base/win_util.cc @@ -4,6 +4,7 @@ #include "base/win_util.h" +#include <propvarutil.h> #include <sddl.h> #include "base/logging.h" @@ -384,6 +385,32 @@ base::KeyboardCode WinToKeyboardCode(WORD keycode) { return static_cast<base::KeyboardCode>(keycode); } +bool SetAppIdForPropertyStore(IPropertyStore* property_store, + const wchar_t* app_id) { + DCHECK(property_store); + + // App id should be less than 128 chars and contain no space. And recommended + // format is CompanyName.ProductName[.SubProduct.ProductNumber]. + // See http://msdn.microsoft.com/en-us/library/dd378459%28VS.85%29.aspx + DCHECK(lstrlen(app_id) < 128 && wcschr(app_id, L' ') == NULL); + + static const PROPERTYKEY kPKEYAppUserModelID = + { { 0x9F4C2855, 0x9F79, 0x4B39, + { 0xA8, 0xD0, 0xE1, 0xD4, 0x2D, 0xE1, 0xD5, 0xF3, } }, 5 }; + + PROPVARIANT property_value; + if (FAILED(InitPropVariantFromString(app_id, &property_value))) + return false; + + HRESULT result = property_store->SetValue(kPKEYAppUserModelID, + property_value); + if (S_OK == result) + result = property_store->Commit(); + + PropVariantClear(&property_value); + return SUCCEEDED(result); +} + } // namespace win_util #ifdef _MSC_VER diff --git a/base/win_util.h b/base/win_util.h index 307eddb..bc10c38 100644 --- a/base/win_util.h +++ b/base/win_util.h @@ -7,6 +7,7 @@ #include <windows.h> #include <aclapi.h> +#include <shlobj.h> #include <string> @@ -110,6 +111,12 @@ std::wstring FormatLastWin32Error(); WORD KeyboardCodeToWin(base::KeyboardCode keycode); base::KeyboardCode WinToKeyboardCode(WORD keycode); +// Sets the application id in given IPropertyStore. The function is intended +// for tagging application/chromium shortcut, browser window and jump list for +// Win7. +bool SetAppIdForPropertyStore(IPropertyStore* property_store, + const wchar_t* app_id); + } // namespace win_util #endif // BASE_WIN_UTIL_H__ |