| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/browser/web_applications/web_app.h" | 5 #include "chrome/browser/web_applications/web_app.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <shellapi.h> | 8 #include <shellapi.h> |
| 9 #include <shlobj.h> | 9 #include <shlobj.h> |
| 10 #endif // defined(OS_WIN) | 10 #endif // defined(OS_WIN) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "base/scoped_ptr.h" | 22 #include "base/scoped_ptr.h" |
| 23 #include "base/utf_string_conversions.h" | 23 #include "base/utf_string_conversions.h" |
| 24 #include "base/win/windows_version.h" | 24 #include "base/win/windows_version.h" |
| 25 #include "chrome/browser/browser_thread.h" | 25 #include "chrome/browser/browser_thread.h" |
| 26 #include "chrome/browser/download/download_util.h" | 26 #include "chrome/browser/download/download_util.h" |
| 27 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/tab_contents/tab_contents.h" | 28 #include "chrome/browser/tab_contents/tab_contents.h" |
| 29 #include "chrome/common/chrome_constants.h" | 29 #include "chrome/common/chrome_constants.h" |
| 30 #include "chrome/common/chrome_paths.h" | 30 #include "chrome/common/chrome_paths.h" |
| 31 #include "chrome/common/chrome_plugin_util.h" | 31 #include "chrome/common/chrome_plugin_util.h" |
| 32 #include "chrome/common/extensions/extension.h" |
| 32 #include "chrome/common/notification_registrar.h" | 33 #include "chrome/common/notification_registrar.h" |
| 33 #include "chrome/common/url_constants.h" | 34 #include "chrome/common/url_constants.h" |
| 34 #include "chrome/common/web_apps.h" | 35 #include "chrome/common/web_apps.h" |
| 35 | 36 |
| 36 #if defined(OS_LINUX) | 37 #if defined(OS_LINUX) |
| 37 #include "base/environment.h" | 38 #include "base/environment.h" |
| 38 #endif // defined(OS_LINUX) | 39 #endif // defined(OS_LINUX) |
| 39 | 40 |
| 40 #if defined(OS_WIN) | 41 #if defined(OS_WIN) |
| 41 #include "base/win_util.h" | 42 #include "base/win_util.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 c = '_'; | 84 c = '_'; |
| 84 | 85 |
| 85 file_name += c; | 86 file_name += c; |
| 86 } | 87 } |
| 87 | 88 |
| 88 return FilePath(file_name); | 89 return FilePath(file_name); |
| 89 } | 90 } |
| 90 #endif // defined(OS_WIN) | 91 #endif // defined(OS_WIN) |
| 91 | 92 |
| 92 // Returns relative directory of given web app url. | 93 // Returns relative directory of given web app url. |
| 93 FilePath GetWebAppDir(const GURL& url) { | 94 FilePath GetWebAppDir(const ShellIntegration::ShortcutInfo& info) { |
| 94 FilePath::StringType host; | 95 if (!info.extension_id.empty()) { |
| 95 FilePath::StringType scheme_port; | 96 std::string app_name = web_app::GenerateApplicationNameFromExtensionId( |
| 97 UTF16ToUTF8(info.extension_id)); |
| 98 #if defined(OS_WIN) |
| 99 return FilePath(UTF8ToWide(app_name)); |
| 100 #elif defined(OS_POSIX) |
| 101 return FilePath(app_name); |
| 102 #endif |
| 103 } else { |
| 104 FilePath::StringType host; |
| 105 FilePath::StringType scheme_port; |
| 96 | 106 |
| 97 #if defined(OS_WIN) | 107 #if defined(OS_WIN) |
| 98 host = UTF8ToWide(url.host()); | 108 host = UTF8ToWide(info.url.host()); |
| 99 scheme_port = (url.has_scheme() ? UTF8ToWide(url.scheme()) : L"http") + | 109 scheme_port = (info.url.has_scheme() ? UTF8ToWide(info.url.scheme()) |
| 100 FILE_PATH_LITERAL("_") + | 110 : L"http") + FILE_PATH_LITERAL("_") + |
| 101 (url.has_port() ? UTF8ToWide(url.port()) : L"80"); | 111 (info.url.has_port() ? UTF8ToWide(info.url.port()) : L"80"); |
| 102 #elif defined(OS_POSIX) | 112 #elif defined(OS_POSIX) |
| 103 host = url.host(); | 113 host = info.url.host(); |
| 104 scheme_port = url.scheme() + FILE_PATH_LITERAL("_") + url.port(); | 114 scheme_port = info.url.scheme() + FILE_PATH_LITERAL("_") + info.url.port(); |
| 105 #endif | 115 #endif |
| 106 | 116 |
| 107 return FilePath(host).Append(scheme_port); | 117 return FilePath(host).Append(scheme_port); |
| 118 } |
| 108 } | 119 } |
| 109 | 120 |
| 110 // Returns data directory for given web app url | 121 // Returns data directory for given web app url |
| 111 FilePath GetWebAppDataDirectory(const FilePath& root_dir, | 122 FilePath GetWebAppDataDirectory(const FilePath& root_dir, |
| 112 const GURL& url) { | 123 const ShellIntegration::ShortcutInfo& info) { |
| 113 return root_dir.Append(GetWebAppDir(url)); | 124 return root_dir.Append(GetWebAppDir(info)); |
| 114 } | 125 } |
| 115 | 126 |
| 116 #if defined(TOOLKIT_VIEWS) | 127 #if defined(TOOLKIT_VIEWS) |
| 117 // Predicator for sorting images from largest to smallest. | 128 // Predicator for sorting images from largest to smallest. |
| 118 bool IconPrecedes(const WebApplicationInfo::IconInfo& left, | 129 bool IconPrecedes(const WebApplicationInfo::IconInfo& left, |
| 119 const WebApplicationInfo::IconInfo& right) { | 130 const WebApplicationInfo::IconInfo& right) { |
| 120 return left.width < right.width; | 131 return left.width < right.width; |
| 121 } | 132 } |
| 122 #endif | 133 #endif |
| 123 | 134 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 web_app::CreateShortcutCallback* callback_; | 246 web_app::CreateShortcutCallback* callback_; |
| 236 MessageLoop* message_loop_; | 247 MessageLoop* message_loop_; |
| 237 | 248 |
| 238 DISALLOW_COPY_AND_ASSIGN(CreateShortcutTask); | 249 DISALLOW_COPY_AND_ASSIGN(CreateShortcutTask); |
| 239 }; | 250 }; |
| 240 | 251 |
| 241 CreateShortcutTask::CreateShortcutTask( | 252 CreateShortcutTask::CreateShortcutTask( |
| 242 const FilePath& profile_path, | 253 const FilePath& profile_path, |
| 243 const ShellIntegration::ShortcutInfo& shortcut_info, | 254 const ShellIntegration::ShortcutInfo& shortcut_info, |
| 244 web_app::CreateShortcutCallback* callback) | 255 web_app::CreateShortcutCallback* callback) |
| 245 : web_app_path_(GetWebAppDataDirectory(web_app::GetDataDir(profile_path), | 256 : web_app_path_(GetWebAppDataDirectory( |
| 246 shortcut_info.url)), | 257 web_app::GetDataDir(profile_path), |
| 258 shortcut_info)), |
| 247 profile_path_(profile_path), | 259 profile_path_(profile_path), |
| 248 shortcut_info_(shortcut_info), | 260 shortcut_info_(shortcut_info), |
| 249 callback_(callback), | 261 callback_(callback), |
| 250 message_loop_(MessageLoop::current()) { | 262 message_loop_(MessageLoop::current()) { |
| 251 DCHECK(message_loop_ != NULL); | 263 DCHECK(message_loop_ != NULL); |
| 252 } | 264 } |
| 253 | 265 |
| 254 void CreateShortcutTask::Run() { | 266 void CreateShortcutTask::Run() { |
| 255 bool success = CreateShortcut(); | 267 bool success = CreateShortcut(); |
| 256 | 268 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 std::string switches = | 381 std::string switches = |
| 370 ShellIntegration::GetCommandLineArgumentsCommon(shortcut_info_.url, | 382 ShellIntegration::GetCommandLineArgumentsCommon(shortcut_info_.url, |
| 371 shortcut_info_.extension_id); | 383 shortcut_info_.extension_id); |
| 372 std::wstring wide_switchs(UTF8ToWide(switches)); | 384 std::wstring wide_switchs(UTF8ToWide(switches)); |
| 373 | 385 |
| 374 // Sanitize description | 386 // Sanitize description |
| 375 if (shortcut_info_.description.length() >= MAX_PATH) | 387 if (shortcut_info_.description.length() >= MAX_PATH) |
| 376 shortcut_info_.description.resize(MAX_PATH - 1); | 388 shortcut_info_.description.resize(MAX_PATH - 1); |
| 377 | 389 |
| 378 // Generates app id from web app url and profile path. | 390 // Generates app id from web app url and profile path. |
| 391 std::string app_name; |
| 392 if (!shortcut_info_.extension_id.empty()) { |
| 393 app_name = web_app::GenerateApplicationNameFromExtensionId( |
| 394 UTF16ToUTF8(shortcut_info_.extension_id)); |
| 395 } else { |
| 396 app_name = web_app::GenerateApplicationNameFromURL( |
| 397 shortcut_info_.url); |
| 398 } |
| 379 std::wstring app_id = ShellIntegration::GetAppId( | 399 std::wstring app_id = ShellIntegration::GetAppId( |
| 380 UTF8ToWide(web_app::GenerateApplicationNameFromURL(shortcut_info_.url)), | 400 UTF8ToWide(app_name), profile_path_); |
| 381 profile_path_); | |
| 382 | 401 |
| 383 FilePath shortcut_to_pin; | 402 FilePath shortcut_to_pin; |
| 384 | 403 |
| 385 bool success = true; | 404 bool success = true; |
| 386 for (size_t i = 0; i < shortcut_paths.size(); ++i) { | 405 for (size_t i = 0; i < shortcut_paths.size(); ++i) { |
| 387 FilePath shortcut_file = shortcut_paths[i].Append(file_name). | 406 FilePath shortcut_file = shortcut_paths[i].Append(file_name). |
| 388 ReplaceExtension(FILE_PATH_LITERAL(".lnk")); | 407 ReplaceExtension(FILE_PATH_LITERAL(".lnk")); |
| 389 | 408 |
| 390 int unique_number = download_util::GetUniquePathNumber(shortcut_file); | 409 int unique_number = download_util::GetUniquePathNumber(shortcut_file); |
| 391 if (unique_number == -1) { | 410 if (unique_number == -1) { |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 void UpdateShortcutWorker::UpdateShortcuts() { | 627 void UpdateShortcutWorker::UpdateShortcuts() { |
| 609 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 628 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 610 NewRunnableMethod(this, | 629 NewRunnableMethod(this, |
| 611 &UpdateShortcutWorker::UpdateShortcutsOnFileThread)); | 630 &UpdateShortcutWorker::UpdateShortcutsOnFileThread)); |
| 612 } | 631 } |
| 613 | 632 |
| 614 void UpdateShortcutWorker::UpdateShortcutsOnFileThread() { | 633 void UpdateShortcutWorker::UpdateShortcutsOnFileThread() { |
| 615 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 634 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 616 | 635 |
| 617 FilePath web_app_path = GetWebAppDataDirectory( | 636 FilePath web_app_path = GetWebAppDataDirectory( |
| 618 web_app::GetDataDir(profile_path_), shortcut_info_.url); | 637 web_app::GetDataDir(profile_path_), shortcut_info_); |
| 619 | 638 |
| 620 // Ensure web_app_path exists. web_app_path could be missing for a legacy | 639 // Ensure web_app_path exists. web_app_path could be missing for a legacy |
| 621 // shortcut created by gears. | 640 // shortcut created by gears. |
| 622 if (!file_util::PathExists(web_app_path) && | 641 if (!file_util::PathExists(web_app_path) && |
| 623 !file_util::CreateDirectory(web_app_path)) { | 642 !file_util::CreateDirectory(web_app_path)) { |
| 624 NOTREACHED(); | 643 NOTREACHED(); |
| 625 return; | 644 return; |
| 626 } | 645 } |
| 627 | 646 |
| 628 FilePath icon_file = web_app_path.Append(file_name_).ReplaceExtension( | 647 FilePath icon_file = web_app_path.Append(file_name_).ReplaceExtension( |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 678 }; // namespace | 697 }; // namespace |
| 679 | 698 |
| 680 #if defined(OS_WIN) | 699 #if defined(OS_WIN) |
| 681 // Allows UpdateShortcutWorker without adding refcounting. UpdateShortcutWorker | 700 // Allows UpdateShortcutWorker without adding refcounting. UpdateShortcutWorker |
| 682 // manages its own life time and will delete itself when it's done. | 701 // manages its own life time and will delete itself when it's done. |
| 683 DISABLE_RUNNABLE_METHOD_REFCOUNT(UpdateShortcutWorker); | 702 DISABLE_RUNNABLE_METHOD_REFCOUNT(UpdateShortcutWorker); |
| 684 #endif // defined(OS_WIN) | 703 #endif // defined(OS_WIN) |
| 685 | 704 |
| 686 namespace web_app { | 705 namespace web_app { |
| 687 | 706 |
| 707 // The following string is used to build the directory name for |
| 708 // shortcuts to chrome applications (the kind which are installed |
| 709 // from a CRX). Application shortcuts to URLs use the {host}_{path} |
| 710 // for the name of this directory. Hosts can't include an underscore. |
| 711 // By starting this string with an underscore, we ensure that there |
| 712 // are no naming conflicts. |
| 713 static const char* kCrxAppPrefix = "_crx_"; |
| 714 |
| 688 std::string GenerateApplicationNameFromURL(const GURL& url) { | 715 std::string GenerateApplicationNameFromURL(const GURL& url) { |
| 689 std::string t; | 716 std::string t; |
| 690 t.append(url.host()); | 717 t.append(url.host()); |
| 691 t.append("_"); | 718 t.append("_"); |
| 692 t.append(url.path()); | 719 t.append(url.path()); |
| 693 return t; | 720 return t; |
| 694 } | 721 } |
| 695 | 722 |
| 723 std::string GenerateApplicationNameFromExtensionId(const std::string& id) { |
| 724 std::string t(web_app::kCrxAppPrefix); |
| 725 t.append(id); |
| 726 return t; |
| 727 } |
| 728 |
| 696 void CreateShortcut( | 729 void CreateShortcut( |
| 697 const FilePath& data_dir, | 730 const FilePath& data_dir, |
| 698 const ShellIntegration::ShortcutInfo& shortcut_info, | 731 const ShellIntegration::ShortcutInfo& shortcut_info, |
| 699 CreateShortcutCallback* callback) { | 732 CreateShortcutCallback* callback) { |
| 700 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 733 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 701 new CreateShortcutTask(data_dir, shortcut_info, callback)); | 734 new CreateShortcutTask(data_dir, shortcut_info, callback)); |
| 702 } | 735 } |
| 703 | 736 |
| 704 bool IsValidUrl(const GURL& url) { | 737 bool IsValidUrl(const GURL& url) { |
| 705 static const char* const kValidUrlSchemes[] = { | 738 static const char* const kValidUrlSchemes[] = { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 757 | 790 |
| 758 void UpdateShortcutForTabContents(TabContents* tab_contents) { | 791 void UpdateShortcutForTabContents(TabContents* tab_contents) { |
| 759 #if defined(OS_WIN) | 792 #if defined(OS_WIN) |
| 760 // UpdateShortcutWorker will delete itself when it's done. | 793 // UpdateShortcutWorker will delete itself when it's done. |
| 761 UpdateShortcutWorker* worker = new UpdateShortcutWorker(tab_contents); | 794 UpdateShortcutWorker* worker = new UpdateShortcutWorker(tab_contents); |
| 762 worker->Run(); | 795 worker->Run(); |
| 763 #endif // defined(OS_WIN) | 796 #endif // defined(OS_WIN) |
| 764 } | 797 } |
| 765 | 798 |
| 766 }; // namespace web_app | 799 }; // namespace web_app |
| OLD | NEW |