File: session_restore.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (174 lines) | stat: -rw-r--r-- 6,897 bytes parent folder | download | duplicates (3)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SESSIONS_SESSION_RESTORE_H_
#define CHROME_BROWSER_SESSIONS_SESSION_RESTORE_H_

#include <stdint.h>

#include <memory>
#include <vector>

#include "base/callback_list.h"
#include "base/gtest_prod_util.h"
#include "base/observer_list.h"
#include "chrome/browser/sessions/session_restore_observer.h"
#include "components/sessions/core/session_types.h"
#include "ui/base/window_open_disposition.h"

class Browser;
class Profile;
class SessionRestoreImpl;

namespace content {
class WebContents;
}

struct StartupTab;
using StartupTabs = std::vector<StartupTab>;

// SessionRestore handles restoring either the last or saved session. Session
// restore come in two variants, asynchronous or synchronous. The synchronous
// variety is meant for startup and blocks until restore is complete.
class SessionRestore {
 public:
  // Bitmask representing behaviors available when restoring a session. Populate
  // using the values below.
  using BehaviorBitmask = uint32_t;

  enum {
    // Indicates the active tab of the supplied browser should be closed.
    CLOBBER_CURRENT_TAB = 1 << 0,

    // Indicates that if there is a problem restoring the last session then a
    // new tabbed browser should be created.
    ALWAYS_CREATE_TABBED_BROWSER = 1 << 1,

    // Restore blocks until complete. This is intended for use during startup
    // when we want to block until restore is complete.
    SYNCHRONOUS = 1 << 2,

    // Restore apps as well.
    RESTORE_APPS = 1 << 3,

    // Restores normal browsers.
    RESTORE_BROWSER = 1 << 4,
  };

  // Notification callback list.
  using CallbackList = base::RepeatingCallbackList<void(Profile*, int)>;
  using RestoredCallback = base::RepeatingCallback<void(Profile*, int)>;

  SessionRestore(const SessionRestore&) = delete;
  SessionRestore& operator=(const SessionRestore&) = delete;

  // Restores the last session. |behavior| is a bitmask of Behaviors, see it
  // for details. If |browser| is non-null the tabs for the first window are
  // added to it. Returns the last active browser.
  //
  // If |startup_tabs| is non-empty, a tab is added for each of the URLs.
  static Browser* RestoreSession(Profile* profile,
                                 Browser* browser,
                                 BehaviorBitmask behavior,
                                 const StartupTabs& startup_tabs);

  // Restores the last session when the last session crashed. It's a wrapper
  // of function RestoreSession.
  static void RestoreSessionAfterCrash(Browser* browser);

  // Opens the startup pages when the last session crashed.
  static void OpenStartupPagesAfterCrash(Browser* browser);

  // Specifically used in the restoration of a foreign session.  This function
  // restores the given session windows to multiple browsers. Returns the
  // created Browsers.
  static std::vector<Browser*> RestoreForeignSessionWindows(
      Profile* profile,
      std::vector<const sessions::SessionWindow*>::const_iterator begin,
      std::vector<const sessions::SessionWindow*>::const_iterator end);

  // Specifically used in the restoration of a foreign session.  This method
  // restores the given session tab to the browser of |source_web_contents| if
  // the disposition is not NEW_WINDOW. Returns the WebContents corresponding
  // to the restored tab. If |disposition| is CURRENT_TAB, |source_web_contents|
  // may be destroyed. If |skip_renderer_creation| is true, depending on if
  // |disposition| is BACKGROUND_TAB, lazily initialize tabs without a renderer.
  static content::WebContents* RestoreForeignSessionTab(
      content::WebContents* source_web_contents,
      const sessions::SessionTab& tab,
      WindowOpenDisposition disposition,
      bool skip_renderer_creation = false);

  // Returns true if we're in the process of restoring |profile|.
  static bool IsRestoring(const Profile* profile);

  // Returns true if synchronously restoring a session.
  static bool IsRestoringSynchronously();

  // Registers a callback that is notified every time session restore completes.
  // Note that 'complete' means all the browsers and tabs have been created but
  // have not necessarily finished loading. The integer supplied to the callback
  // indicates the number of tabs that were created.
  static base::CallbackListSubscription RegisterOnSessionRestoredCallback(
      const RestoredCallback& callback);

  // Add/remove an observer to/from this session restore.
  static void AddObserver(SessionRestoreObserver* observer);
  static void RemoveObserver(SessionRestoreObserver* observer);

  // Get called when the tab loader finishes loading tabs in tab restore even
  // without session restore started.
  static void OnTabLoaderFinishedLoadingTabs();

  // Is called when windows are read from the last session restore file.
  static void OnGotSession(Profile* profile, bool for_apps, int window_count);

 private:
  friend class SessionRestoreImpl;
  FRIEND_TEST_ALL_PREFIXES(SessionRestoreObserverTest, SingleSessionRestore);
  FRIEND_TEST_ALL_PREFIXES(SessionRestoreObserverTest,
                           SequentialSessionRestores);
  FRIEND_TEST_ALL_PREFIXES(SessionRestoreObserverTest,
                           ConcurrentSessionRestores);
  FRIEND_TEST_ALL_PREFIXES(SessionRestoreObserverTest,
                           TabManagerShouldObserveSessionRestore);

  // Session restore observer list.
  using SessionRestoreObserverList =
      base::ObserverList<SessionRestoreObserver>::Unchecked;

  SessionRestore();

  // Accessor for |*on_session_restored_callbacks_|. Creates a new object the
  // first time so that it always returns a valid object.
  static CallbackList* on_session_restored_callbacks() {
    if (!on_session_restored_callbacks_)
      on_session_restored_callbacks_ = new CallbackList();
    return on_session_restored_callbacks_;
  }

  // Accessor for the observer list. Create the list the first time to always
  // return a valid reference.
  static SessionRestoreObserverList* observers() {
    if (!observers_)
      observers_ = new SessionRestoreObserverList();
    return observers_;
  }

  // Contains all registered callbacks for session restore notifications.
  static CallbackList* on_session_restored_callbacks_;

  // Notify SessionRestoreObservers session restore started. If there are
  // multiple concurrent session restores, observers get notified only once in
  // the first session restore.
  static void NotifySessionRestoreStartedLoadingTabs();

  // Contains all registered observers for session restore events.
  static SessionRestoreObserverList* observers_;

  // Whether session restore started or not.
  static bool session_restore_started_;
};

#endif  // CHROME_BROWSER_SESSIONS_SESSION_RESTORE_H_