Home | History | Annotate | Download | only in ash
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_UI_ASH_SCREENSHOT_TAKER_H_
      6 #define CHROME_BROWSER_UI_ASH_SCREENSHOT_TAKER_H_
      7 
      8 #include "ash/screenshot_delegate.h"
      9 #include "base/basictypes.h"
     10 #include "base/compiler_specific.h"
     11 #include "base/files/file_path.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/observer_list.h"
     15 #include "base/time/time.h"
     16 #include "chrome/browser/notifications/notification.h"
     17 
     18 class Profile;
     19 
     20 namespace ash {
     21 namespace test {
     22 class ScreenshotTakerTest;
     23 }
     24 }
     25 namespace aura {
     26 class Window;
     27 }
     28 
     29 class ScreenshotTakerObserver {
     30  public:
     31   enum Result {
     32     SCREENSHOT_SUCCESS = 0,
     33     SCREENSHOT_GRABWINDOW_PARTIAL_FAILED,
     34     SCREENSHOT_GRABWINDOW_FULL_FAILED,
     35     SCREENSHOT_CREATE_DIR_FAILED,
     36     SCREENSHOT_GET_DIR_FAILED,
     37     SCREENSHOT_CHECK_DIR_FAILED,
     38     SCREENSHOT_CREATE_FILE_FAILED,
     39     SCREENSHOT_WRITE_FILE_FAILED,
     40     SCREENSHOTS_DISABLED,
     41     SCREENSHOT_RESULT_COUNT
     42   };
     43 
     44   virtual void OnScreenshotCompleted(
     45       Result screenshot_result,
     46       const base::FilePath& screenshot_path) = 0;
     47 
     48  protected:
     49   virtual ~ScreenshotTakerObserver() {}
     50 };
     51 
     52 class ScreenshotTaker : public ash::ScreenshotDelegate {
     53  public:
     54   ScreenshotTaker();
     55 
     56   virtual ~ScreenshotTaker();
     57 
     58   // Overridden from ash::ScreenshotDelegate:
     59   virtual void HandleTakeScreenshotForAllRootWindows() OVERRIDE;
     60   virtual void HandleTakePartialScreenshot(aura::Window* window,
     61                                            const gfx::Rect& rect) OVERRIDE;
     62   virtual bool CanTakeScreenshot() OVERRIDE;
     63 
     64   void ShowNotification(
     65       ScreenshotTakerObserver::Result screenshot_result,
     66       const base::FilePath& screenshot_path);
     67 
     68   void AddObserver(ScreenshotTakerObserver* observer);
     69   void RemoveObserver(ScreenshotTakerObserver* observer);
     70   bool HasObserver(ScreenshotTakerObserver* observer) const;
     71 
     72  private:
     73   friend class ash::test::ScreenshotTakerTest;
     74 
     75   Profile* GetProfile();
     76   void SetScreenshotDirectoryForTest(const base::FilePath& directory);
     77   void SetScreenshotBasenameForTest(const std::string& basename);
     78   void SetScreenshotProfileForTest(Profile* profile);
     79 
     80   Notification* CreateNotification(
     81       ScreenshotTakerObserver::Result screenshot_result,
     82       const base::FilePath& screenshot_path);
     83 
     84   base::WeakPtrFactory<ScreenshotTaker> factory_;
     85 
     86   // The timestamp when the screenshot task was issued last time.
     87   base::Time last_screenshot_timestamp_;
     88 
     89   ObserverList<ScreenshotTakerObserver> observers_;
     90 
     91   base::FilePath screenshot_directory_for_test_;
     92   std::string screenshot_basename_for_test_;
     93   Profile* profile_for_test_;
     94 
     95   DISALLOW_COPY_AND_ASSIGN(ScreenshotTaker);
     96 };
     97 
     98 #endif  // CHROME_BROWSER_UI_ASH_SCREENSHOT_TAKER_H_
     99