OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/i18n/file_util_icu.h" | 9 #include "base/i18n/file_util_icu.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/common/extensions/extension.h" |
13 #include "chrome/common/chrome_constants.h" | 14 #include "chrome/common/chrome_constants.h" |
14 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
16 | 17 |
17 using content::BrowserThread; | 18 using content::BrowserThread; |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 // Returns relative directory of given web app url. | |
22 FilePath GetWebAppDir(const ShellIntegration::ShortcutInfo& info) { | |
23 if (!info.extension_id.empty()) { | |
24 std::string app_name = | |
25 web_app::GenerateApplicationNameFromExtensionId(info.extension_id); | |
26 #if defined(OS_WIN) | |
27 return FilePath(UTF8ToUTF16(app_name)); | |
28 #elif defined(OS_POSIX) | |
29 return FilePath(app_name); | |
30 #endif | |
31 } else { | |
32 FilePath::StringType host; | |
33 FilePath::StringType scheme_port; | |
34 | |
35 #if defined(OS_WIN) | |
36 host = UTF8ToUTF16(info.url.host()); | |
37 scheme_port = (info.url.has_scheme() ? UTF8ToUTF16(info.url.scheme()) | |
38 : L"http") + FILE_PATH_LITERAL("_") + | |
39 (info.url.has_port() ? UTF8ToUTF16(info.url.port()) : L"80"); | |
40 #elif defined(OS_POSIX) | |
41 host = info.url.host(); | |
42 scheme_port = info.url.scheme() + FILE_PATH_LITERAL("_") + info.url.port(); | |
43 #endif | |
44 | |
45 return FilePath(host).Append(scheme_port); | |
46 } | |
47 } | |
48 | |
49 #if defined(TOOLKIT_VIEWS) | 22 #if defined(TOOLKIT_VIEWS) |
50 // Predicator for sorting images from largest to smallest. | 23 // Predicator for sorting images from largest to smallest. |
51 bool IconPrecedes(const WebApplicationInfo::IconInfo& left, | 24 bool IconPrecedes(const WebApplicationInfo::IconInfo& left, |
52 const WebApplicationInfo::IconInfo& right) { | 25 const WebApplicationInfo::IconInfo& right) { |
53 return left.width < right.width; | 26 return left.width < right.width; |
54 } | 27 } |
55 #endif | 28 #endif |
56 | 29 |
57 } // namespace | 30 } // namespace |
58 | 31 |
59 namespace web_app { | 32 namespace web_app { |
60 | 33 |
61 // The following string is used to build the directory name for | 34 // The following string is used to build the directory name for |
62 // shortcuts to chrome applications (the kind which are installed | 35 // shortcuts to chrome applications (the kind which are installed |
63 // from a CRX). Application shortcuts to URLs use the {host}_{path} | 36 // from a CRX). Application shortcuts to URLs use the {host}_{path} |
64 // for the name of this directory. Hosts can't include an underscore. | 37 // for the name of this directory. Hosts can't include an underscore. |
65 // By starting this string with an underscore, we ensure that there | 38 // By starting this string with an underscore, we ensure that there |
66 // are no naming conflicts. | 39 // are no naming conflicts. |
67 static const char* kCrxAppPrefix = "_crx_"; | 40 static const char* kCrxAppPrefix = "_crx_"; |
68 | 41 |
69 namespace internals { | 42 namespace internals { |
70 | 43 |
71 // Returns data directory for given web app url | |
72 FilePath GetWebAppDataDirectory(const FilePath& root_dir, | |
73 const ShellIntegration::ShortcutInfo& info) { | |
74 return root_dir.Append(GetWebAppDir(info)); | |
75 } | |
76 | |
77 FilePath GetSanitizedFileName(const string16& name) { | 44 FilePath GetSanitizedFileName(const string16& name) { |
78 #if defined(OS_WIN) | 45 #if defined(OS_WIN) |
79 string16 file_name = name; | 46 string16 file_name = name; |
80 #else | 47 #else |
81 std::string file_name = UTF16ToUTF8(name); | 48 std::string file_name = UTF16ToUTF8(name); |
82 #endif | 49 #endif |
83 file_util::ReplaceIllegalCharactersInPath(&file_name, '_'); | 50 file_util::ReplaceIllegalCharactersInPath(&file_name, '_'); |
84 return FilePath(file_name); | 51 return FilePath(file_name); |
85 } | 52 } |
86 | 53 |
87 } // namespace internals | 54 } // namespace internals |
88 | 55 |
| 56 FilePath GetWebAppDataDirectory(const FilePath& profile_path, |
| 57 const std::string& extension_id, |
| 58 const GURL& url) { |
| 59 FilePath app_data_dir(profile_path.Append(chrome::kWebAppDirname)); |
| 60 |
| 61 if (!extension_id.empty()) { |
| 62 return app_data_dir.AppendASCII( |
| 63 GenerateApplicationNameFromExtensionId(extension_id)); |
| 64 } |
| 65 |
| 66 std::string host(url.host()); |
| 67 std::string scheme(url.has_scheme() ? url.scheme() : "http"); |
| 68 std::string port(url.has_port() ? url.port() : "80"); |
| 69 std::string scheme_port(scheme + "_" + port); |
| 70 |
| 71 #if defined(OS_WIN) |
| 72 FilePath::StringType host_path(UTF8ToUTF16(host)); |
| 73 FilePath::StringType scheme_port_path(UTF8ToUTF16(scheme_port)); |
| 74 #elif defined(OS_POSIX) |
| 75 FilePath::StringType host_path(host); |
| 76 FilePath::StringType scheme_port_path(scheme_port); |
| 77 #endif |
| 78 |
| 79 return app_data_dir.Append(host_path).Append(scheme_port_path); |
| 80 } |
| 81 |
| 82 FilePath GetWebAppDataDirectory(const FilePath& profile_path, |
| 83 const Extension& extension) { |
| 84 return GetWebAppDataDirectory( |
| 85 profile_path, extension.id(), GURL(extension.launch_web_url())); |
| 86 } |
| 87 |
89 std::string GenerateApplicationNameFromInfo( | 88 std::string GenerateApplicationNameFromInfo( |
90 const ShellIntegration::ShortcutInfo& shortcut_info) { | 89 const ShellIntegration::ShortcutInfo& shortcut_info) { |
91 if (!shortcut_info.extension_id.empty()) { | 90 if (!shortcut_info.extension_id.empty()) { |
92 return web_app::GenerateApplicationNameFromExtensionId( | 91 return web_app::GenerateApplicationNameFromExtensionId( |
93 shortcut_info.extension_id); | 92 shortcut_info.extension_id); |
94 } else { | 93 } else { |
95 return web_app::GenerateApplicationNameFromURL( | 94 return web_app::GenerateApplicationNameFromURL( |
96 shortcut_info.url); | 95 shortcut_info.url); |
97 } | 96 } |
98 } | 97 } |
(...skipping 19 matching lines...) Expand all Loading... |
118 return app_name.substr(prefix.length()); | 117 return app_name.substr(prefix.length()); |
119 } | 118 } |
120 | 119 |
121 void CreateShortcut( | 120 void CreateShortcut( |
122 const FilePath& data_dir, | 121 const FilePath& data_dir, |
123 const ShellIntegration::ShortcutInfo& shortcut_info) { | 122 const ShellIntegration::ShortcutInfo& shortcut_info) { |
124 BrowserThread::PostTask( | 123 BrowserThread::PostTask( |
125 BrowserThread::FILE, | 124 BrowserThread::FILE, |
126 FROM_HERE, | 125 FROM_HERE, |
127 base::Bind(&internals::CreateShortcutTask, | 126 base::Bind(&internals::CreateShortcutTask, |
128 web_app::internals::GetWebAppDataDirectory( | 127 GetWebAppDataDirectory( |
129 web_app::GetDataDir(data_dir), | 128 data_dir, |
130 shortcut_info), | 129 shortcut_info.extension_id, |
| 130 shortcut_info.url), |
131 data_dir, | 131 data_dir, |
132 shortcut_info)); | 132 shortcut_info)); |
133 } | 133 } |
134 | 134 |
135 bool IsValidUrl(const GURL& url) { | 135 bool IsValidUrl(const GURL& url) { |
136 static const char* const kValidUrlSchemes[] = { | 136 static const char* const kValidUrlSchemes[] = { |
137 chrome::kFileScheme, | 137 chrome::kFileScheme, |
138 chrome::kFtpScheme, | 138 chrome::kFtpScheme, |
139 chrome::kHttpScheme, | 139 chrome::kHttpScheme, |
140 chrome::kHttpsScheme, | 140 chrome::kHttpsScheme, |
141 chrome::kExtensionScheme, | 141 chrome::kExtensionScheme, |
142 }; | 142 }; |
143 | 143 |
144 for (size_t i = 0; i < arraysize(kValidUrlSchemes); ++i) { | 144 for (size_t i = 0; i < arraysize(kValidUrlSchemes); ++i) { |
145 if (url.SchemeIs(kValidUrlSchemes[i])) | 145 if (url.SchemeIs(kValidUrlSchemes[i])) |
146 return true; | 146 return true; |
147 } | 147 } |
148 | 148 |
149 return false; | 149 return false; |
150 } | 150 } |
151 | 151 |
152 FilePath GetDataDir(const FilePath& profile_path) { | |
153 return profile_path.Append(chrome::kWebAppDirname); | |
154 } | |
155 | |
156 #if defined(TOOLKIT_VIEWS) | 152 #if defined(TOOLKIT_VIEWS) |
157 void GetIconsInfo(const WebApplicationInfo& app_info, | 153 void GetIconsInfo(const WebApplicationInfo& app_info, |
158 IconInfoList* icons) { | 154 IconInfoList* icons) { |
159 DCHECK(icons); | 155 DCHECK(icons); |
160 | 156 |
161 icons->clear(); | 157 icons->clear(); |
162 for (size_t i = 0; i < app_info.icons.size(); ++i) { | 158 for (size_t i = 0; i < app_info.icons.size(); ++i) { |
163 // We only take square shaped icons (i.e. width == height). | 159 // We only take square shaped icons (i.e. width == height). |
164 if (app_info.icons[i].width == app_info.icons[i].height) { | 160 if (app_info.icons[i].width == app_info.icons[i].height) { |
165 icons->push_back(app_info.icons[i]); | 161 icons->push_back(app_info.icons[i]); |
166 } | 162 } |
167 } | 163 } |
168 | 164 |
169 std::sort(icons->begin(), icons->end(), &IconPrecedes); | 165 std::sort(icons->begin(), icons->end(), &IconPrecedes); |
170 } | 166 } |
171 #endif | 167 #endif |
172 | 168 |
173 #if defined(TOOLKIT_USES_GTK) | 169 #if defined(TOOLKIT_USES_GTK) |
174 std::string GetWMClassFromAppName(std::string app_name) { | 170 std::string GetWMClassFromAppName(std::string app_name) { |
175 file_util::ReplaceIllegalCharactersInPath(&app_name, '_'); | 171 file_util::ReplaceIllegalCharactersInPath(&app_name, '_'); |
176 TrimString(app_name, "_", &app_name); | 172 TrimString(app_name, "_", &app_name); |
177 return app_name; | 173 return app_name; |
178 } | 174 } |
179 #endif | 175 #endif |
180 | 176 |
181 } // namespace web_app | 177 } // namespace web_app |
OLD | NEW |