1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
// 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"
#include "chrome/common/url_constants.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,
web_app::GenerateApplicationNameFromURL(shortcut_info_.url).c_str());
}
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 {
std::wstring GenerateApplicationNameFromURL(const GURL& url) {
std::string t;
t.append(url.host());
t.append("_");
t.append(url.path());
return UTF8ToWide(t);
}
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));
}
bool IsValidUrl(const GURL& url) {
static const char* const kValidUrlSchemes[] = {
chrome::kFileScheme,
chrome::kFtpScheme,
chrome::kHttpScheme,
chrome::kHttpsScheme,
};
for (size_t i = 0; i < arraysize(kValidUrlSchemes); ++i) {
if (url.SchemeIs(kValidUrlSchemes[i]))
return true;
}
return false;
}
}; // namespace web_app
|