1 // Copyright 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.
5 #ifndef IOS_CHROME_BROWSER_SNAPSHOTS_SNAPSHOT_CACHE_H_
6 #define IOS_CHROME_BROWSER_SNAPSHOTS_SNAPSHOT_CACHE_H_
8 #import <UIKit/UIKit.h>
10 #include "base/mac/objc_property_releaser.h"
11 #include "base/mac/scoped_nsobject.h"
12 #include "base/time/time.h"
14 typedef void (^GreyBlock
)(UIImage
*);
16 // A singleton providing an in-memory and on-disk cache of tab snapshots.
17 // A snapshot is a full-screen image of the contents of the page at the current
18 // scroll offset and zoom level, used to stand in for the UIWebView if it has
19 // been purged from memory or when quickly switching tabs.
20 // Persists to disk on a background thread each time a snapshot changes.
21 @interface SnapshotCache
: NSObject
{
23 // Dictionary to hold color snapshots in memory. n.b. Color snapshots are not
24 // kept in memory on tablets.
25 base::scoped_nsobject
<NSMutableDictionary
> imageDictionary_
;
26 // Temporary dictionary to hold grey snapshots for tablet side swipe. This
27 // will be nil before -createGreyCache is called and after -removeGreyCache
29 base::scoped_nsobject
<NSMutableDictionary
> greyImageDictionary_
;
32 // Session ID of most recent pending grey snapshot request.
33 base::scoped_nsobject
<NSString
> mostRecentGreySessionId_
;
34 // Block used by pending request for a grey snapshot.
35 base::scoped_nsprotocol
<GreyBlock
> mostRecentGreyBlock_
;
37 // Session ID and correspoinding UIImage for the snapshot that will likely
38 // be requested to be saved to disk when the application is backgrounded.
39 base::scoped_nsobject
<NSString
> backgroundingImageSessionId_
;
40 base::scoped_nsobject
<UIImage
> backgroundingColorImage_
;
42 base::mac::ObjCPropertyReleaser propertyReleaser_SnapshotCache_
;
45 // Track session IDs to not release on low memory and to reload on
46 // |UIApplicationDidBecomeActiveNotification|.
47 @
property(nonatomic
, retain
) NSSet
* pinnedIDs
;
49 + (SnapshotCache
*)sharedInstance
;
51 // The scale that should be used for snapshots.
52 + (CGFloat
)snapshotScaleForDevice
;
54 // Retrieve a cached snapshot for the |sessionID| and return it via the callback
55 // if it exists. The callback is guaranteed to be called synchronously if the
56 // image is in memory. It will be called asynchronously if the image is on disk
57 // or with nil if the image is not present at all.
58 - (void)retrieveImageForSessionID
:(NSString
*)sessionID
59 callback
:(void (^)(UIImage
*))callback
;
61 // Request the session's grey snapshot. If the image is already loaded in
62 // memory, this will immediately call back on |callback|.
63 - (void)retrieveGreyImageForSessionID
:(NSString
*)sessionID
64 callback
:(void (^)(UIImage
*))callback
;
66 - (void)setImage
:(UIImage
*)img withSessionID
:(NSString
*)sessionID
;
67 - (void)removeImageWithSessionID
:(NSString
*)sessionID
;
68 // Purge the cache of snapshots that are older than |date|. The snapshots for
69 // the sessions given in |liveSessionIds| will be kept. This will be done
70 // asynchronously on a background thread.
71 - (void)purgeCacheOlderThan
:(const base::Time
&)date
72 keeping
:(NSSet
*)liveSessionIds
;
73 // Hint that the snapshot for |sessionID| will likely be saved to disk when the
74 // application is backgrounded. The snapshot is then saved in memory, so it
75 // does not need to be read off disk.
76 - (void)willBeSavedGreyWhenBackgrounding
:(NSString
*)sessionID
;
78 // Create temporary cache of grey images for tablet side swipe.
79 - (void)createGreyCache
:(NSArray
*)sessionIDs
;
80 // Release all images in grey cache.
81 - (void)removeGreyCache
;
82 // Request the session's grey snapshot. If the image is already loaded this will
83 // immediately call back on |callback|. Otherwise, only use |callback| for the
84 // most recent caller. The callback is not guaranteed to be called.
85 - (void)greyImageForSessionID
:(NSString
*)sessionID
86 callback
:(void (^)(UIImage
*))callback
;
88 // Write a grey copy of the snapshot for |sessionID| to disk, but if and only if
89 // a color version of the snapshot already exists in memory or on disk.
90 - (void)saveGreyInBackgroundForSessionID
:(NSString
*)sessionID
;
93 #endif // IOS_CHROME_BROWSER_SNAPSHOTS_SNAPSHOT_CACHE_H_