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
|
// Copyright 2014 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 "ui/snapshot/screenshot_grabber.h"
#include <climits>
#include <string>
#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/task_runner.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/time/time.h"
#include "ui/gfx/image/image.h"
#include "ui/snapshot/snapshot.h"
#if defined(USE_AURA)
#include "ui/aura/window.h"
#endif
namespace ui {
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;
using ShowNotificationCallback =
base::Callback<void(ScreenshotGrabberObserver::Result screenshot_result,
const base::FilePath& screenshot_path)>;
void SaveScreenshot(scoped_refptr<base::TaskRunner> ui_task_runner,
const ShowNotificationCallback& callback,
const base::FilePath& screenshot_path,
scoped_refptr<base::RefCountedBytes> png_data,
ScreenshotGrabberDelegate::FileResult result,
const base::FilePath& local_path) {
DCHECK(!base::MessageLoopForUI::IsCurrent());
DCHECK(!screenshot_path.empty());
// Convert FileResult into ScreenshotGrabberObserver::Result.
ScreenshotGrabberObserver::Result screenshot_result =
ScreenshotGrabberObserver::SCREENSHOT_SUCCESS;
switch (result) {
case ScreenshotGrabberDelegate::FILE_SUCCESS:
// Successfully got a local file to write to, write png data.
DCHECK_GT(static_cast<int>(png_data->size()), 0);
if (static_cast<size_t>(base::WriteFile(
local_path, reinterpret_cast<char*>(&(png_data->data()[0])),
static_cast<int>(png_data->size()))) != png_data->size()) {
LOG(ERROR) << "Failed to save to " << local_path.value();
screenshot_result =
ScreenshotGrabberObserver::SCREENSHOT_WRITE_FILE_FAILED;
}
break;
case ScreenshotGrabberDelegate::FILE_CHECK_DIR_FAILED:
screenshot_result =
ScreenshotGrabberObserver::SCREENSHOT_CHECK_DIR_FAILED;
break;
case ScreenshotGrabberDelegate::FILE_CREATE_DIR_FAILED:
screenshot_result =
ScreenshotGrabberObserver::SCREENSHOT_CREATE_DIR_FAILED;
break;
case ScreenshotGrabberDelegate::FILE_CREATE_FAILED:
screenshot_result =
ScreenshotGrabberObserver::SCREENSHOT_CREATE_FILE_FAILED;
break;
}
// Report the result on the UI thread.
ui_task_runner->PostTask(
FROM_HERE, base::Bind(callback, screenshot_result, screenshot_path));
}
void EnsureLocalDirectoryExists(
const base::FilePath& path,
ScreenshotGrabberDelegate::FileCallback callback) {
DCHECK(!base::MessageLoopForUI::IsCurrent());
DCHECK(!path.empty());
if (!base::CreateDirectory(path.DirName())) {
LOG(ERROR) << "Failed to ensure the existence of "
<< path.DirName().value();
callback.Run(ScreenshotGrabberDelegate::FILE_CREATE_DIR_FAILED, path);
return;
}
callback.Run(ScreenshotGrabberDelegate::FILE_SUCCESS, path);
}
} // namespace
void ScreenshotGrabberDelegate::PrepareFileAndRunOnBlockingPool(
const base::FilePath& path,
scoped_refptr<base::TaskRunner> blocking_task_runner,
const FileCallback& callback_on_blocking_pool) {
blocking_task_runner->PostTask(
FROM_HERE,
base::Bind(EnsureLocalDirectoryExists, path, callback_on_blocking_pool));
}
ScreenshotGrabber::ScreenshotGrabber(
ScreenshotGrabberDelegate* client,
scoped_refptr<base::TaskRunner> blocking_task_runner)
: client_(client),
blocking_task_runner_(blocking_task_runner),
factory_(this) {
}
ScreenshotGrabber::~ScreenshotGrabber() {
}
void ScreenshotGrabber::TakeScreenshot(gfx::NativeWindow window,
const gfx::Rect& rect,
const base::FilePath& screenshot_path) {
DCHECK(base::MessageLoopForUI::IsCurrent());
last_screenshot_timestamp_ = base::TimeTicks::Now();
bool is_partial = true;
// Window identifier is used to log a message on failure to capture a full
// screen (i.e. non partial) screenshot. The only time is_partial can be
// false, we will also have an identification string for the window.
std::string window_identifier;
#if defined(USE_AURA)
aura::Window* aura_window = static_cast<aura::Window*>(window);
is_partial = rect.size() != aura_window->bounds().size();
window_identifier = aura_window->GetBoundsInScreen().ToString();
#endif
ui::GrabWindowSnapshotAsync(
window, rect, blocking_task_runner_,
base::Bind(&ScreenshotGrabber::GrabWindowSnapshotAsyncCallback,
factory_.GetWeakPtr(), window_identifier, screenshot_path,
is_partial));
}
bool ScreenshotGrabber::CanTakeScreenshot() {
return last_screenshot_timestamp_.is_null() ||
base::TimeTicks::Now() - last_screenshot_timestamp_ >
base::TimeDelta::FromMilliseconds(kScreenshotMinimumIntervalInMS);
}
void ScreenshotGrabber::NotifyScreenshotCompleted(
ScreenshotGrabberObserver::Result screenshot_result,
const base::FilePath& screenshot_path) {
DCHECK(base::MessageLoopForUI::IsCurrent());
FOR_EACH_OBSERVER(ScreenshotGrabberObserver, observers_,
OnScreenshotCompleted(screenshot_result, screenshot_path));
}
void ScreenshotGrabber::AddObserver(ScreenshotGrabberObserver* observer) {
observers_.AddObserver(observer);
}
void ScreenshotGrabber::RemoveObserver(ScreenshotGrabberObserver* observer) {
observers_.RemoveObserver(observer);
}
bool ScreenshotGrabber::HasObserver(
const ScreenshotGrabberObserver* observer) const {
return observers_.HasObserver(observer);
}
void ScreenshotGrabber::GrabWindowSnapshotAsyncCallback(
const std::string& window_identifier,
base::FilePath screenshot_path,
bool is_partial,
scoped_refptr<base::RefCountedBytes> png_data) {
DCHECK(base::MessageLoopForUI::IsCurrent());
if (!png_data.get()) {
if (is_partial) {
LOG(ERROR) << "Failed to grab the window screenshot";
NotifyScreenshotCompleted(
ScreenshotGrabberObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED,
screenshot_path);
} else {
LOG(ERROR) << "Failed to grab the window screenshot for "
<< window_identifier;
NotifyScreenshotCompleted(
ScreenshotGrabberObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED,
screenshot_path);
}
return;
}
ShowNotificationCallback notification_callback(base::Bind(
&ScreenshotGrabber::NotifyScreenshotCompleted, factory_.GetWeakPtr()));
client_->PrepareFileAndRunOnBlockingPool(
screenshot_path, blocking_task_runner_,
base::Bind(&SaveScreenshot, base::MessageLoop::current()->task_runner(),
notification_callback, screenshot_path, png_data));
}
} // namespace ui
|