1 // Copyright 2020 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 IOS_CHROME_BROWSER_SNAPSHOTS_SNAPSHOT_BROWSER_AGENT_H_
6 #define IOS_CHROME_BROWSER_SNAPSHOTS_SNAPSHOT_BROWSER_AGENT_H_
7 
8 #import <Foundation/Foundation.h>
9 
10 #include "base/macros.h"
11 #import "ios/chrome/browser/main/browser_observer.h"
12 #import "ios/chrome/browser/main/browser_user_data.h"
13 #import "ios/chrome/browser/web_state_list/web_state_list_observer.h"
14 
15 namespace base {
16 class FilePath;
17 }
18 @class SnapshotCache;
19 
20 // Associates a SnapshotCache to a Browser.
21 class SnapshotBrowserAgent : BrowserObserver,
22                              public WebStateListObserver,
23                              public BrowserUserData<SnapshotBrowserAgent> {
24  public:
25   SnapshotBrowserAgent();
26   ~SnapshotBrowserAgent() override;
27 
28   // Set a session identification string that will be used to locate the
29   // snapshots directory. Setting this more than once on the same agent is
30   // probably a programming error.
31   void SetSessionID(const std::string& session_identifier);
32 
33   // Maintains the snapshots storage including purging unused images and
34   // performing any necessary migrations.
35   void PerformStorageMaintenance();
36 
37   // Permanently removes all snapshots.
38   void RemoveAllSnapshots();
39 
snapshot_cache()40   SnapshotCache* snapshot_cache() { return snapshot_cache_; }
41 
42  private:
43   explicit SnapshotBrowserAgent(Browser* browser);
44   friend class BrowserUserData<SnapshotBrowserAgent>;
45   BROWSER_USER_DATA_KEY_DECL();
46 
47   // BrowserObserver methods
48   void BrowserDestroyed(Browser* browser) override;
49 
50   // WebStateListObserver methods
51   void WebStateInsertedAt(WebStateList* web_state_list,
52                           web::WebState* web_state,
53                           int index,
54                           bool activating) override;
55   void WebStateReplacedAt(WebStateList* web_state_list,
56                           web::WebState* old_web_state,
57                           web::WebState* new_web_state,
58                           int index) override;
59   void WebStateDetachedAt(WebStateList* web_state_list,
60                           web::WebState* web_state,
61                           int index) override;
62   void WillBeginBatchOperation(WebStateList* web_state_list) override;
63   void BatchOperationEnded(WebStateList* web_state_list) override;
64 
65   // Migrates the snapshot storage if a folder exists in the old snapshots
66   // storage location.
67   void MigrateStorageIfNecessary();
68 
69   // Purges the snapshots folder of unused snapshots.
70   void PurgeUnusedSnapshots();
71 
72   // Returns the storage folder for snapshots.
73   base::FilePath GetStoragePath();
74 
75   // Returns the Tab IDs of all the WebStates in the Browser.
76   NSSet<NSString*>* GetTabIDs();
77 
78   Browser* browser_;               // unowned.
79   SnapshotCache* snapshot_cache_;  // strong, owned.
80 
81   // Session identifier for this agent.
82   std::string session_identifier_;
83 };
84 
85 #endif  // IOS_CHROME_BROWSER_SNAPSHOTS_SNAPSHOT_BROWSER_AGENT_H_
86