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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
// Copyright (c) 2012 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/ui/ash/screenshot_taker.h"
#include <climits>
#include <string>
#include "ash/shell.h"
#include "base/bind.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/memory/ref_counted_memory.h"
#include "base/stringprintf.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/webui/screenshot_source.h"
#include "chrome/browser/ui/window_snapshot/window_snapshot.h"
#include "content/public/browser/browser_thread.h"
#include "grit/ash_strings.h"
#include "grit/theme_resources.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/drive/drive_file_system_util.h"
#include "chrome/browser/chromeos/extensions/file_manager/file_manager_util.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#endif
namespace {
// The minimum interval between two screenshot commands. It has to be
// more than 1000 to prevent the conflict of filenames.
const int kScreenshotMinimumIntervalInMS = 1000;
const char kNotificationId[] = "screenshot";
const char kNotificationOriginUrl[] = "chrome://screenshot";
// Delegate for a notification. This class has two roles: to implement callback
// methods for notification, and to provide an identity of the associated
// notification.
class ScreenshotTakerNotificationDelegate : public NotificationDelegate {
public:
ScreenshotTakerNotificationDelegate(bool success,
const base::FilePath& screenshot_path)
: success_(success),
screenshot_path_(screenshot_path) {
}
// Overridden from NotificationDelegate:
virtual void Display() OVERRIDE {}
virtual void Error() OVERRIDE {}
virtual void Close(bool by_user) OVERRIDE {}
virtual void Click() OVERRIDE {
if (!success_)
return;
#if defined(OS_CHROMEOS)
file_manager_util::ShowFileInFolder(screenshot_path_);
#else
// TODO(sschmitz): perhaps add similar action for Windows.
#endif
}
virtual std::string id() const OVERRIDE {
return std::string(kNotificationId);
}
virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
return NULL;
}
private:
virtual ~ScreenshotTakerNotificationDelegate() {}
const bool success_;
const base::FilePath screenshot_path_;
DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate);
};
typedef base::Callback<
void(ScreenshotTakerObserver::Result screenshot_result,
const base::FilePath& screenshot_path)> ShowNotificationCallback;
void SaveScreenshotInternal(const ShowNotificationCallback& callback,
const base::FilePath& screenshot_path,
const base::FilePath& local_path,
scoped_refptr<base::RefCountedBytes> png_data) {
DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
DCHECK(!local_path.empty());
ScreenshotTakerObserver::Result result =
ScreenshotTakerObserver::SCREENSHOT_SUCCESS;
if (static_cast<size_t>(file_util::WriteFile(
local_path,
reinterpret_cast<char*>(&(png_data->data()[0])),
png_data->size())) != png_data->size()) {
LOG(ERROR) << "Failed to save to " << local_path.value();
result = ScreenshotTakerObserver::SCREENSHOT_WRITE_FILE_FAILED;
}
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(callback, result, screenshot_path));
}
void SaveScreenshot(const ShowNotificationCallback& callback,
const base::FilePath& screenshot_path,
scoped_refptr<base::RefCountedBytes> png_data) {
DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
DCHECK(!screenshot_path.empty());
if (!file_util::CreateDirectory(screenshot_path.DirName())) {
LOG(ERROR) << "Failed to ensure the existence of "
<< screenshot_path.DirName().value();
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(callback,
ScreenshotTakerObserver::SCREENSHOT_CREATE_DIR_FAILED,
screenshot_path));
return;
}
SaveScreenshotInternal(callback, screenshot_path, screenshot_path, png_data);
}
// TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch.
#if defined(OS_CHROMEOS)
void SaveScreenshotToDrive(const ShowNotificationCallback& callback,
const base::FilePath& screenshot_path,
scoped_refptr<base::RefCountedBytes> png_data,
drive::FileError error,
const base::FilePath& local_path) {
// |screenshot_path| is used in the notification callback.
// |local_path| is a temporary file in a hidden cache directory used for
// internal work generated by drive::util::PrepareWritableFileAndRun.
if (error != drive::FILE_ERROR_OK) {
LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error;
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(callback,
ScreenshotTakerObserver::SCREENSHOT_CREATE_FILE_FAILED,
screenshot_path));
return;
}
SaveScreenshotInternal(callback, screenshot_path, local_path, png_data);
}
void EnsureDirectoryExistsCallback(
const ShowNotificationCallback& callback,
Profile* profile,
const base::FilePath& screenshot_path,
scoped_refptr<base::RefCountedBytes> png_data,
drive::FileError error) {
// It is okay to fail with FILE_ERROR_EXISTS since anyway the directory
// of the target file exists.
if (error == drive::FILE_ERROR_OK ||
error == drive::FILE_ERROR_EXISTS) {
drive::util::PrepareWritableFileAndRun(
profile,
screenshot_path,
base::Bind(&SaveScreenshotToDrive,
callback,
screenshot_path,
png_data));
} else {
LOG(ERROR) << "Failed to ensure the existence of the specified directory "
<< "in Google Drive: " << error;
callback.Run(ScreenshotTakerObserver::SCREENSHOT_CHECK_DIR_FAILED,
screenshot_path);
}
}
void PostSaveScreenshotTask(const ShowNotificationCallback& callback,
Profile* profile,
const base::FilePath& screenshot_path,
scoped_refptr<base::RefCountedBytes> png_data) {
if (drive::util::IsUnderDriveMountPoint(screenshot_path)) {
drive::util::EnsureDirectoryExists(
profile,
screenshot_path.DirName(),
base::Bind(&EnsureDirectoryExistsCallback,
callback,
profile,
screenshot_path,
png_data));
} else {
content::BrowserThread::GetBlockingPool()->PostTask(
FROM_HERE, base::Bind(&SaveScreenshot,
callback,
screenshot_path,
png_data));
}
}
#else
void PostSaveScreenshotTask(const ShowNotificationCallback& callback,
Profile* profile,
const base::FilePath& screenshot_path,
scoped_refptr<base::RefCountedBytes> png_data) {
content::BrowserThread::GetBlockingPool()->PostTask(
FROM_HERE, base::Bind(&SaveScreenshot,
callback,
screenshot_path,
png_data));
}
#endif
bool GrabWindowSnapshot(aura::Window* window,
const gfx::Rect& snapshot_bounds,
std::vector<unsigned char>* png_data) {
#if defined(OS_LINUX)
// chrome::GrabWindowSnapshotForUser checks this too, but
// RootWindow::GrabSnapshot does not.
if (ScreenshotSource::AreScreenshotsDisabled())
return false;
// We use XGetImage() for Linux/ChromeOS for performance reasons.
// See crbug.com/119492
// TODO(mukai): remove this when the performance issue has been fixed.
if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data))
return true;
#endif // OS_LINUX
return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds);
}
} // namespace
ScreenshotTaker::ScreenshotTaker()
: ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)),
profile_for_test_(NULL) {
}
ScreenshotTaker::~ScreenshotTaker() {
}
void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() {
base::FilePath screenshot_directory;
if (!screenshot_directory_for_test_.empty()) {
screenshot_directory = screenshot_directory_for_test_;
} else if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) {
ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED,
base::FilePath());
return;
}
std::string screenshot_basename = !screenshot_basename_for_test_.empty() ?
screenshot_basename_for_test_ :
ScreenshotSource::GetScreenshotBaseFilename();
ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows();
// Reorder root_windows to take the primary root window's snapshot at first.
aura::RootWindow* primary_root = ash::Shell::GetPrimaryRootWindow();
if (*(root_windows.begin()) != primary_root) {
root_windows.erase(std::find(
root_windows.begin(), root_windows.end(), primary_root));
root_windows.insert(root_windows.begin(), primary_root);
}
for (size_t i = 0; i < root_windows.size(); ++i) {
aura::RootWindow* root_window = root_windows[i];
scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes);
std::string basename = screenshot_basename;
gfx::Rect rect = root_window->bounds();
if (root_windows.size() > 1)
basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1));
base::FilePath screenshot_path =
screenshot_directory.AppendASCII(basename + ".png");
if (GrabWindowSnapshot(root_window, rect, &png_data->data())) {
PostSaveScreenshotTask(
base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()),
GetProfile(),
screenshot_path,
png_data);
} else {
LOG(ERROR) << "Failed to grab the window screenshot for " << i;
ShowNotification(
ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED,
screenshot_path);
}
}
last_screenshot_timestamp_ = base::Time::Now();
}
void ScreenshotTaker::HandleTakePartialScreenshot(
aura::Window* window, const gfx::Rect& rect) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
base::FilePath screenshot_directory;
if (!screenshot_directory_for_test_.empty()) {
screenshot_directory = screenshot_directory_for_test_;
} else if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) {
ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED,
base::FilePath());
return;
}
scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes);
std::string screenshot_basename = !screenshot_basename_for_test_.empty() ?
screenshot_basename_for_test_ :
ScreenshotSource::GetScreenshotBaseFilename();
base::FilePath screenshot_path =
screenshot_directory.AppendASCII(screenshot_basename + ".png");
if (GrabWindowSnapshot(window, rect, &png_data->data())) {
last_screenshot_timestamp_ = base::Time::Now();
PostSaveScreenshotTask(
base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()),
GetProfile(),
screenshot_path,
png_data);
} else {
LOG(ERROR) << "Failed to grab the window screenshot";
ShowNotification(
ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED,
screenshot_path);
}
}
bool ScreenshotTaker::CanTakeScreenshot() {
return last_screenshot_timestamp_.is_null() ||
base::Time::Now() - last_screenshot_timestamp_ >
base::TimeDelta::FromMilliseconds(
kScreenshotMinimumIntervalInMS);
}
void ScreenshotTaker::ShowNotification(
ScreenshotTakerObserver::Result screenshot_result,
const base::FilePath& screenshot_path) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
#if defined(OS_CHROMEOS)
// TODO(sschmitz): make this work for Windows.
const std::string notification_id(kNotificationId);
// We cancel a previous screenshot notification, if any, to ensure we get
// a fresh notification pop-up.
g_browser_process->notification_ui_manager()->CancelById(notification_id);
const string16 replace_id(UTF8ToUTF16(notification_id));
bool success =
(screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS);
Notification notification(
GURL(kNotificationOriginUrl),
ui::ResourceBundle::GetSharedInstance().GetImageNamed(
IDR_SCREENSHOT_NOTIFICATION_ICON),
l10n_util::GetStringUTF16(
success ?
IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS :
IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL),
l10n_util::GetStringUTF16(
success ?
IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS :
IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL),
WebKit::WebTextDirectionDefault,
string16(),
replace_id,
new ScreenshotTakerNotificationDelegate(success, screenshot_path));
g_browser_process->notification_ui_manager()->Add(notification,
GetProfile());
#endif
FOR_EACH_OBSERVER(ScreenshotTakerObserver, observers_,
OnScreenshotCompleted(screenshot_result, screenshot_path));
}
void ScreenshotTaker::AddObserver(ScreenshotTakerObserver* observer) {
observers_.AddObserver(observer);
}
void ScreenshotTaker::RemoveObserver(ScreenshotTakerObserver* observer) {
observers_.RemoveObserver(observer);
}
bool ScreenshotTaker::HasObserver(ScreenshotTakerObserver* observer) const {
return observers_.HasObserver(observer);
}
Profile* ScreenshotTaker::GetProfile() {
if (profile_for_test_)
return profile_for_test_;
return ProfileManager::GetDefaultProfileOrOffTheRecord();
}
void ScreenshotTaker::SetScreenshotDirectoryForTest(
const base::FilePath& directory) {
screenshot_directory_for_test_ = directory;
}
void ScreenshotTaker::SetScreenshotBasenameForTest(
const std::string& basename) {
screenshot_basename_for_test_ = basename;
}
void ScreenshotTaker::SetScreenshotProfileForTest(Profile* profile) {
profile_for_test_ = profile;
}
|