blob: dabd9c872bcbb9c62ac27f9020dba0e76208bec3 [file] [log] [blame]
// 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.
#ifndef CHROME_BROWSER_UI_ASH_CHROME_SCREENSHOT_GRABBER_H_
#define CHROME_BROWSER_UI_ASH_CHROME_SCREENSHOT_GRABBER_H_
#include <memory>
#include "ash/screenshot_delegate.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "chrome/browser/notifications/notification.h"
#include "components/drive/chromeos/file_system_interface.h"
#include "components/drive/drive.pb.h"
#include "ui/gfx/image/image.h"
#include "ui/snapshot/screenshot_grabber.h"
class Profile;
namespace ash {
class ChromeScreenshotGrabberTest;
} // namespace ash
class ChromeScreenshotGrabber : public ash::ScreenshotDelegate,
public ui::ScreenshotGrabberDelegate,
public ui::ScreenshotGrabberObserver {
public:
ChromeScreenshotGrabber();
~ChromeScreenshotGrabber() override;
ui::ScreenshotGrabber* screenshot_grabber() {
return screenshot_grabber_.get();
}
// ash::ScreenshotDelegate:
void HandleTakeScreenshotForAllRootWindows() override;
void HandleTakePartialScreenshot(aura::Window* window,
const gfx::Rect& rect) override;
void HandleTakeWindowScreenshot(aura::Window* window) override;
bool CanTakeScreenshot() override;
// ui::ScreenshotGrabberDelegate:
void PrepareFileAndRunOnBlockingPool(
const base::FilePath& path,
const FileCallback& callback_on_blocking_pool) override;
// ui::ScreenshotGrabberObserver:
void OnScreenshotCompleted(ui::ScreenshotGrabberObserver::Result result,
const base::FilePath& screenshot_path) override;
private:
friend class ash::ChromeScreenshotGrabberTest;
// Callback method of FileSystemInterface::GetFile().
// Runs ReadScreenshotFileForPreviewLocal if successful. Otherwise, runs
// OnReadScreenshotFileForPreviewCompleted to show notification without
// screenshot preview.
// One way to try screenshot saved to Drive is to change the download
// directory from Settings > Downloads > Location.
//
// |screenshot_path| is screenshot path on Drive.
// Parameters after |screenshot_path| is set by GetFile().
// |screenshot_cache_path| is a local cache of remote |screenshot_path|.
void ReadScreenshotFileForPreviewDrive(
const base::FilePath& screenshot_path,
drive::FileError error,
const base::FilePath& screenshot_cache_path,
std::unique_ptr<drive::ResourceEntry> entry);
// Reads a screenshot file in |screenshot_cache_path|, and runs
// DecodeScreenshotFileForPreview.
//
// |screenshot_path| and |screenshot_cache_path| are different when the
// screenshot is saved to Drive. Otherwise, they should be same.
// |screenshot_path| can be a Drive path while |screenshot_cache_path| is
// always a local path.
void ReadScreenshotFileForPreviewLocal(
const base::FilePath& screenshot_path,
const base::FilePath& screenshot_cache_path);
// Decodes |image_data| and run OnScreenshotFileForPreviewDecoded.
// Although |image_data| is originally generated by the screenshot grabber,
// it is saved to local or remote storage, so we have to decode it in
// a sandboxed process.
//
// |screenshot_path| is a path that is opened by file manager when the
// notification is clicked.
void DecodeScreenshotFileForPreview(const base::FilePath& screenshot_path,
std::string image_data);
// Callback method of DecodeScreenshotFileForPreview.
// Converts SkBitmap to gfx::Image and calls
// OnReadScreenshotFileForPreviewCompleted.
//
// |screenshot_path| is a path that is opened by file manager when the
// notification is clicked.
void OnScreenshotFileForPreviewDecoded(const base::FilePath& screenshot_path,
const SkBitmap& decoded_image);
// Shows "Screenshot taken" notification.
//
// |screenshot_path| is a path that is opened by file manager when the
// notification is clicked.
// |image| is a preview image attached to the notification. It can be empty.
void OnReadScreenshotFileForPreviewCompleted(
ui::ScreenshotGrabberObserver::Result result,
const base::FilePath& screenshot_path,
gfx::Image image);
Notification* CreateNotification(
ui::ScreenshotGrabberObserver::Result screenshot_result,
const base::FilePath& screenshot_path,
gfx::Image image);
void SetProfileForTest(Profile* profile);
Profile* GetProfile();
std::unique_ptr<ui::ScreenshotGrabber> screenshot_grabber_;
Profile* profile_for_test_;
base::WeakPtrFactory<ChromeScreenshotGrabber> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ChromeScreenshotGrabber);
};
#endif // CHROME_BROWSER_UI_ASH_CHROME_SCREENSHOT_GRABBER_H_