1// Copyright (c) 2013 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 CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_SCREENSHOT_MANAGER_H_
6#define CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_SCREENSHOT_MANAGER_H_
7
8#include "base/compiler_specific.h"
9#include "base/memory/weak_ptr.h"
10#include "base/time/time.h"
11#include "content/common/content_export.h"
12
13class SkBitmap;
14
15namespace content {
16
17class NavigationControllerImpl;
18class NavigationEntryImpl;
19class RenderViewHost;
20class ScreenshotData;
21
22// WebContentsScreenshotManager takes care of taking image-captures for the
23// current navigation entry of a NavigationControllerImpl, and managing these
24// captured images. These image-captures are used for history navigation using
25// overscroll gestures.
26class CONTENT_EXPORT WebContentsScreenshotManager {
27 public:
28  explicit WebContentsScreenshotManager(NavigationControllerImpl* controller);
29  virtual ~WebContentsScreenshotManager();
30
31  // Takes a screenshot of the last-committed entry of the controller.
32  void TakeScreenshot();
33
34  // Clears screenshots of all navigation entries.
35  void ClearAllScreenshots();
36
37 protected:
38  virtual void TakeScreenshotImpl(RenderViewHost* host,
39                                  NavigationEntryImpl* entry);
40
41  // Called after a screenshot has been set on an NavigationEntryImpl.
42  // Overridden in tests to get notified of when a screenshot is set.
43  virtual void OnScreenshotSet(NavigationEntryImpl* entry);
44
45  NavigationControllerImpl* owner() { return owner_; }
46
47  void SetMinScreenshotIntervalMS(int interval_ms);
48
49  // The callback invoked when taking the screenshot of the page is complete.
50  // This sets the screenshot on the navigation entry.
51  void OnScreenshotTaken(int unique_id,
52                         bool success,
53                         const SkBitmap& bitmap);
54
55  // Returns the number of entries with screenshots.
56  int GetScreenshotCount() const;
57
58 private:
59  // This is called when the screenshot data has beene encoded to PNG in a
60  // worker thread.
61  void OnScreenshotEncodeComplete(int unique_id,
62                                  scoped_refptr<ScreenshotData> data);
63
64  // Removes the screenshot for the entry, returning true if the entry had a
65  // screenshot.
66  bool ClearScreenshot(NavigationEntryImpl* entry);
67
68  // The screenshots in the NavigationEntryImpls can accumulate and consume a
69  // large amount of memory. This function makes sure that the memory
70  // consumption is within a certain limit.
71  void PurgeScreenshotsIfNecessary();
72
73  // The navigation controller that owns this screenshot-manager.
74  NavigationControllerImpl* owner_;
75
76  // Taking a screenshot and encoding them can be async. So use a weakptr for
77  // the callback to make sure that the screenshot/encoding completion callback
78  // does not trigger on a destroyed WebContentsScreenshotManager.
79  base::WeakPtrFactory<WebContentsScreenshotManager> screenshot_factory_;
80
81  base::Time last_screenshot_time_;
82  int min_screenshot_interval_ms_;
83
84  DISALLOW_COPY_AND_ASSIGN(WebContentsScreenshotManager);
85};
86
87}  // namespace content
88
89#endif  // CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_SCREENSHOT_MANAGER_H_
90