diff options
author | xiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-17 08:39:51 +0000 |
---|---|---|
committer | xiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-17 08:39:51 +0000 |
commit | ed54318708245efdfcf7454d3aaebfd419f4448c (patch) | |
tree | 8e233da9e3ca2c2cb8b57f04d75dde3d1cf1f704 /chrome/browser/web_applications/web_app.cc | |
parent | 80cf356e08919b18f502f20864d6f6b72b48eb60 (diff) | |
download | chromium_src-ed54318708245efdfcf7454d3aaebfd419f4448c.zip chromium_src-ed54318708245efdfcf7454d3aaebfd419f4448c.tar.gz chromium_src-ed54318708245efdfcf7454d3aaebfd419f4448c.tar.bz2 |
Implement web app shortcuts natively per issue 25528
- Implement a CreateApplicationShortcutView similar to the current Gears-based one;
- Add a few profile prefs to persist user's last checked shortcut locations
- Implement a web_app::CreateShortcut that stores icon under "<profile>/Web Applications"
in a similar layout as gears (i.e. <host>/<scheme_port>/<web_app_title>.ico) and calls
file_util code to creates shortcut on Windows;
- Add Win7 taskbar pin/unpin support function to file_util;
- Update TabContents to replace gears with new code;
Note:
- Gears dialog is modaless but this one is a modal dialog.
- Gear's icon store is not migrated because gears icons
could still be used by shortcuts created by gears and
thus we could not delete them even after migration. And we
are not using the local ico files in the dialog even if
they exists.
- New CreateApplicationShortcutView is included when
TOOLKIT_VIEW is defined. However on platforms other
than Windows, the actual CreateShortCut code is not
implemented. Right now it calls ShellIntegration's
CreateDesktopShort if OS_LINUX is defined and
NOTREACHED() for other platforms.
BUG=25528
TEST=Verify the new UI provides the same functionality as gears under XP/Vista and support pinning on Win 7.
Review URL: http://codereview.chromium.org/372012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@32165 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/web_applications/web_app.cc')
-rw-r--r-- | chrome/browser/web_applications/web_app.cc | 301 |
1 files changed, 301 insertions, 0 deletions
diff --git a/chrome/browser/web_applications/web_app.cc b/chrome/browser/web_applications/web_app.cc new file mode 100644 index 0000000..1c55c4b --- /dev/null +++ b/chrome/browser/web_applications/web_app.cc @@ -0,0 +1,301 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/web_applications/web_app.h" + +#include "base/file_util.h" +#include "base/message_loop.h" +#include "base/path_service.h" +#include "base/thread.h" +#include "base/string_util.h" +#include "chrome/browser/browser_process.h" +#include "chrome/common/chrome_paths.h" +#include "chrome/common/chrome_plugin_util.h" + +#if defined(OS_WIN) +#include "app/gfx/icon_util.h" +#include "base/win_util.h" +#endif // defined(OS_WIN) + +namespace { + +// Returns true if |ch| is in visible ASCII range and not one of +// "/ \ : * ? " < > | ; ,". +bool IsValidFilePathChar(char16 c) { + if (c < 32) + return false; + + switch (c) { + case '/': + case '\\': + case ':': + case '*': + case '?': + case '"': + case '<': + case '>': + case '|': + case ';': + case ',': + return false; + }; + + return true; +} + +// Returns sanitized name that could be used as a file name +FilePath GetSanitizedFileName(const string16& name) { + string16 file_name; + + for (size_t i = 0; i < name.length(); ++i) { + char16 c = name[i]; + if (!IsValidFilePathChar(c)) + c = '_'; + + file_name += c; + } + +#if defined(OS_WIN) + return FilePath(file_name); +#elif defined(OS_POSIX) + return FilePath(UTF16ToUTF8(file_name)); +#endif +} + +// Returns relative directory of given web app url. +FilePath GetWebAppDir(const GURL& url) { + FilePath::StringType host; + FilePath::StringType scheme_port; + +#if defined(OS_WIN) + host = UTF8ToWide(url.host()); + scheme_port = (url.has_scheme() ? UTF8ToWide(url.scheme()) : L"http") + + FILE_PATH_LITERAL("_") + + (url.has_port() ? UTF8ToWide(url.port()) : L"80"); +#elif defined(OS_POSIX) + host = url.host(); + scheme_port = url.scheme() + FILE_PATH_LITERAL("_") + url.port(); +#endif + + return FilePath(host).Append(scheme_port); +} + +// Returns data directory for given web app url +FilePath GetWebAppDataDirectory(const FilePath& root_dir, + const GURL& url) { + return root_dir.Append(GetWebAppDir(url)); +} + +// Represents a task that creates web application shortcut. This runs on +// file thread and schedules the callback (if any) on the calling thread +// when finished (either success or failure). +class CreateShortcutTask : public Task { + public: + CreateShortcutTask(const FilePath& root_dir, + const ShellIntegration::ShortcutInfo& shortcut_info, + web_app::CreateShortcutCallback* callback); + + private: + class CreateShortcutCallbackTask : public Task { + public: + CreateShortcutCallbackTask(web_app::CreateShortcutCallback* callback, + bool success) + : callback_(callback), + success_(success) { + } + + // Overridden from Task: + virtual void Run() { + callback_->Run(success_); + } + + private: + web_app::CreateShortcutCallback* callback_; + bool success_; + }; + + // Overridden from Task: + virtual void Run(); + + // Returns true if shortcut is created successfully. + bool CreateShortcut(); + + // Path to store persisted data for web_app. + FilePath web_app_path_; + + // Our copy of short cut data. + ShellIntegration::ShortcutInfo shortcut_info_; + + // Callback when task is finished. + web_app::CreateShortcutCallback* callback_; + MessageLoop* message_loop_; +}; + +CreateShortcutTask::CreateShortcutTask( + const FilePath& root_dir, + const ShellIntegration::ShortcutInfo& shortcut_info, + web_app::CreateShortcutCallback* callback) + : web_app_path_(GetWebAppDataDirectory(root_dir, shortcut_info.url)), + shortcut_info_(shortcut_info), + callback_(callback), + message_loop_(MessageLoop::current()) { + DCHECK(message_loop_ != NULL); +} + +void CreateShortcutTask::Run() { + bool success = CreateShortcut(); + + if (callback_ != NULL) + message_loop_->PostTask(FROM_HERE, + new CreateShortcutCallbackTask(callback_, success)); +} + +bool CreateShortcutTask::CreateShortcut() { +#if defined(OS_LINUX) + ShellIntegration::CreateDesktopShortcut(shortcut_info_); + return true; // assuming always success. +#elif defined(OS_WIN) + // Shortcut paths under which to create shortcuts. + std::vector<FilePath> shortcut_paths; + + // Locations to add to shortcut_paths. + struct { + const bool& use_this_location; + int location_id; + const wchar_t* sub_dir; + } locations[] = { + { + shortcut_info_.create_on_desktop, + chrome::DIR_USER_DESKTOP, + NULL + }, { + shortcut_info_.create_in_applications_menu, + base::DIR_START_MENU, + NULL + }, { + shortcut_info_.create_in_quick_launch_bar, + // For Win7, create_in_quick_launch_bar means pining to taskbar. Use + // base::PATH_START as a flag for this case. + (win_util::GetWinVersion() >= win_util::WINVERSION_WIN7) ? + base::PATH_START : base::DIR_APP_DATA, + (win_util::GetWinVersion() >= win_util::WINVERSION_WIN7) ? + NULL : L"Microsoft\\Internet Explorer\\Quick Launch" + } + }; + + // Populate shortcut_paths. + for (int i = 0; i < arraysize(locations); ++i) { + if (locations[i].use_this_location) { + FilePath path; + + // Skip the Win7 case. + if (locations[i].location_id == base::PATH_START) + continue; + + if (!PathService::Get(locations[i].location_id, &path)) { + NOTREACHED(); + return false; + } + + if (locations[i].sub_dir != NULL) + path = path.Append(locations[i].sub_dir); + + shortcut_paths.push_back(path); + } + } + + bool pin_to_taskbar = + shortcut_info_.create_in_quick_launch_bar && + (win_util::GetWinVersion() >= win_util::WINVERSION_WIN7); + + // For Win7's pinning support, any shortcut could be used. So we only create + // the shortcut file when there is no shortcut file will be created. That is, + // user only selects "Pin to taskbar". + if (pin_to_taskbar && shortcut_paths.empty()) { + // Creates the shortcut in web_app_path_ in this case. + shortcut_paths.push_back(web_app_path_); + } + + if (shortcut_paths.empty()) { + NOTREACHED(); + return false; + } + + // Ensure web_app_path_ exists. + if (!file_util::PathExists(web_app_path_) && + !file_util::CreateDirectory(web_app_path_)) { + NOTREACHED(); + return false; + } + + // Generates file name to use with persisted ico and shortcut file. + FilePath file_name = GetSanitizedFileName(shortcut_info_.title); + + // Creates an ico file to use with shortcut. + FilePath icon_file = web_app_path_.Append(file_name).ReplaceExtension( + FILE_PATH_LITERAL(".ico")); + if (!IconUtil::CreateIconFileFromSkBitmap(shortcut_info_.favicon, + icon_file.value())) { + NOTREACHED(); + return false; + } + + std::wstring chrome_exe; + if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { + NOTREACHED(); + return false; + } + + // Working directory. + std::wstring chrome_folder = file_util::GetDirectoryFromPath(chrome_exe); + + // Gets the command line switches. + std::string switches; + if (CPB_GetCommandLineArgumentsCommon(shortcut_info_.url.spec().c_str(), + &switches) != CPERR_SUCCESS) { + NOTREACHED(); + return false; + } + std::wstring wide_switchs(UTF8ToWide(switches)); + + bool success = true; + for (size_t i = 0; i < shortcut_paths.size(); ++i) { + FilePath shortcut_file = shortcut_paths[i].Append(file_name). + ReplaceExtension(FILE_PATH_LITERAL(".lnk")); + success &= file_util::CreateShortcutLink(chrome_exe.c_str(), + shortcut_file.value().c_str(), + chrome_folder.c_str(), + wide_switchs.c_str(), + shortcut_info_.description.c_str(), + icon_file.value().c_str(), + 0); + } + + if (success && pin_to_taskbar) { + // Any shortcut would work for the pinning. We use the first one. + FilePath shortcut_file = shortcut_paths[0].Append(file_name). + ReplaceExtension(FILE_PATH_LITERAL(".lnk")); + success &= file_util::TaskbarPinShortcutLink(shortcut_file.value().c_str()); + } + + return success; +#else + NOTIMPLEMENTED(); + return false; +#endif +} + +}; // namespace + +namespace web_app { + +void CreateShortcut( + const FilePath& data_dir, + const ShellIntegration::ShortcutInfo& shortcut_info, + CreateShortcutCallback* callback) { + g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE, + new CreateShortcutTask(data_dir, shortcut_info, callback)); +} + +}; // namespace web_app |