summaryrefslogtreecommitdiffstats
path: root/content/browser/frame_host/navigation_entry_screenshot_manager.h
blob: 072d6fd4a7fe19a1111acdb6b2e5c6c470f92285 (plain)
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
// Copyright 2013 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 CONTENT_BROWSER_FRAME_HOST_NAVIGATION_ENTRY_SCREENSHOT_MANAGER_H_
#define CONTENT_BROWSER_FRAME_HOST_NAVIGATION_ENTRY_SCREENSHOT_MANAGER_H_

#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "content/common/content_export.h"

class SkBitmap;

namespace content {

class NavigationControllerImpl;
class NavigationEntryImpl;
class RenderViewHost;
class ScreenshotData;

// NavigationEntryScreenshotManager takes care of taking image-captures for the
// current navigation entry of a NavigationControllerImpl, and managing these
// captured images. These image-captures are used for history navigation using
// overscroll gestures.
class CONTENT_EXPORT NavigationEntryScreenshotManager {
 public:
  explicit NavigationEntryScreenshotManager(
      NavigationControllerImpl* controller);
  virtual ~NavigationEntryScreenshotManager();

  // Takes a screenshot of the last-committed entry of the controller.
  void TakeScreenshot();

  // Clears screenshots of all navigation entries.
  void ClearAllScreenshots();

 protected:
  virtual void TakeScreenshotImpl(RenderViewHost* host,
                                  NavigationEntryImpl* entry);

  // Called after a screenshot has been set on an NavigationEntryImpl.
  // Overridden in tests to get notified of when a screenshot is set.
  virtual void OnScreenshotSet(NavigationEntryImpl* entry);

  NavigationControllerImpl* owner() { return owner_; }

  void SetMinScreenshotIntervalMS(int interval_ms);

  // The callback invoked when taking the screenshot of the page is complete.
  // This sets the screenshot on the navigation entry.
  void OnScreenshotTaken(int unique_id,
                         bool success,
                         const SkBitmap& bitmap);

  // Returns the number of entries with screenshots.
  int GetScreenshotCount() const;

 private:
  // This is called when the screenshot data has beene encoded to PNG in a
  // worker thread.
  void OnScreenshotEncodeComplete(int unique_id,
                                  scoped_refptr<ScreenshotData> data);

  // Removes the screenshot for the entry, returning true if the entry had a
  // screenshot.
  bool ClearScreenshot(NavigationEntryImpl* entry);

  // The screenshots in the NavigationEntryImpls can accumulate and consume a
  // large amount of memory. This function makes sure that the memory
  // consumption is within a certain limit.
  void PurgeScreenshotsIfNecessary();

  // The navigation controller that owns this screenshot-manager.
  NavigationControllerImpl* owner_;

  // Taking a screenshot and encoding them can be async. So use a weakptr for
  // the callback to make sure that the screenshot/encoding completion callback
  // does not trigger on a destroyed NavigationEntryScreenshotManager.
  base::WeakPtrFactory<NavigationEntryScreenshotManager> screenshot_factory_;

  base::Time last_screenshot_time_;
  int min_screenshot_interval_ms_;

  DISALLOW_COPY_AND_ASSIGN(NavigationEntryScreenshotManager);
};

}  // namespace content

#endif  // CONTENT_BROWSER_FRAME_HOST_NAVIGATION_ENTRY_SCREENSHOT_MANAGER_H_