blob: d84489885287c9f653a5c31e70de746dbc870fa1 [file] [log] [blame]
ahemery1d5239a52017-05-18 13:19:331// Copyright 2017 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_ANDROID_BACKGROUND_TAB_MANAGER_H_
6#define CHROME_BROWSER_ANDROID_BACKGROUND_TAB_MANAGER_H_
7
8#include <memory>
9#include <vector>
10
11#include "base/memory/singleton.h"
12#include "content/public/browser/web_contents_observer.h"
13
14class Profile;
15
16namespace content {
17class WebContents;
18}
19
20namespace history {
21struct HistoryAddPageArgs;
22class HistoryService;
23}
24
25namespace chrome {
26namespace android {
27
28class BackgroundTabManager;
29
30class WebContentsDestroyedObserver : public content::WebContentsObserver {
31 public:
32 WebContentsDestroyedObserver(BackgroundTabManager* owner,
33 content::WebContents* watched_contents);
34 ~WebContentsDestroyedObserver() override;
35
36 // WebContentsObserver:
37 void WebContentsDestroyed() override;
38
39 private:
40 BackgroundTabManager* owner_;
41
42 DISALLOW_COPY_AND_ASSIGN(WebContentsDestroyedObserver);
43};
44
45// BackgroundTabManager is responsible for storing state for the current
46// background tab if any. To uniquely identify a tab we use a pointer to its
47// WebContents. State managed include :
48// - The Profile that is associated with the content.
49// - Browser History which is cached when the tab is hidden and committed when
50// shown.
51// This class is a global singleton.
52// All methods should be called on the UI thread.
53class BackgroundTabManager {
54 public:
55 BackgroundTabManager();
56
57 ~BackgroundTabManager();
58
59 // Return wether this WebContents is currently identified as being part of a
60 // background tab.
61 bool IsBackgroundTab(content::WebContents* web_contents) const;
62
63 // Register the WebContents as the content for the current background tab.
64 // At most one tab can be registered as a background tab.
65 void RegisterBackgroundTab(content::WebContents* web_contents,
66 Profile* profile);
67
68 // Manually unregister a WebContents. Called automatically when the registered
69 // WebContents is destroyed. Clear all state.
70 void UnregisterBackgroundTab();
71
72 // Retrieves the profile that was stored during background tab registration.
73 Profile* GetProfile() const;
74
75 // Cache a single history item, to be either used by CommitHistory() or
76 // discarded by UnregisterBackgroundTab().
77 void CacheHistory(const history::HistoryAddPageArgs& history_item);
78
79 // Commit the history that was previously cached for this tab. Committing
80 // history clears it from the local cache.
81 void CommitHistory(history::HistoryService* history_service);
82
83 static BackgroundTabManager* GetInstance();
84
85 private:
86 friend struct base::DefaultSingletonTraits<BackgroundTabManager>;
87
88 content::WebContents* web_contents_;
89 Profile* profile_;
90 std::vector<history::HistoryAddPageArgs> cached_history_;
91 std::unique_ptr<WebContentsDestroyedObserver> web_contents_observer_;
92};
93
94} // namespace android
95} // namespace chrome
96
97#endif // CHROME_BROWSER_ANDROID_BACKGROUND_TAB_MANAGER_H_