[sessions]: Componentize TabRestore code
[chromium-blink-merge.git] / chrome / browser / extensions / api / sessions / sessions_api.h
blob87939246043d053b61d3fa3aa3500fc490534df9
1 // Copyright 2013 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 CHROME_BROWSER_EXTENSIONS_API_SESSIONS_SESSIONS_API_H__
6 #define CHROME_BROWSER_EXTENSIONS_API_SESSIONS_SESSIONS_API_H__
8 #include <string>
10 #include "chrome/browser/extensions/chrome_extension_function.h"
11 #include "chrome/common/extensions/api/sessions.h"
12 #include "chrome/common/extensions/api/tabs.h"
13 #include "chrome/common/extensions/api/windows.h"
14 #include "components/sessions/core/tab_restore_service.h"
15 #include "components/sessions/core/tab_restore_service_observer.h"
16 #include "extensions/browser/browser_context_keyed_api_factory.h"
17 #include "extensions/browser/event_router.h"
19 class Profile;
21 namespace sync_driver {
22 struct SyncedSession;
25 namespace extensions {
27 class SessionId;
29 class SessionsGetRecentlyClosedFunction : public ChromeSyncExtensionFunction {
30 protected:
31 ~SessionsGetRecentlyClosedFunction() override {}
32 bool RunSync() override;
33 DECLARE_EXTENSION_FUNCTION("sessions.getRecentlyClosed",
34 SESSIONS_GETRECENTLYCLOSED)
36 private:
37 scoped_ptr<api::tabs::Tab> CreateTabModel(const TabRestoreService::Tab& tab,
38 int session_id,
39 int selected_index);
40 scoped_ptr<api::windows::Window> CreateWindowModel(
41 const TabRestoreService::Window& window,
42 int session_id);
43 scoped_ptr<api::sessions::Session> CreateSessionModel(
44 const TabRestoreService::Entry* entry);
47 class SessionsGetDevicesFunction : public ChromeSyncExtensionFunction {
48 protected:
49 ~SessionsGetDevicesFunction() override {}
50 bool RunSync() override;
51 DECLARE_EXTENSION_FUNCTION("sessions.getDevices", SESSIONS_GETDEVICES)
53 private:
54 scoped_ptr<api::tabs::Tab> CreateTabModel(const std::string& session_tag,
55 const sessions::SessionTab& tab,
56 int tab_index,
57 int selected_index);
58 scoped_ptr<api::windows::Window> CreateWindowModel(
59 const sessions::SessionWindow& window,
60 const std::string& session_tag);
61 scoped_ptr<api::sessions::Session> CreateSessionModel(
62 const sessions::SessionWindow& window,
63 const std::string& session_tag);
64 scoped_ptr<api::sessions::Device> CreateDeviceModel(
65 const sync_driver::SyncedSession* session);
68 class SessionsRestoreFunction : public ChromeSyncExtensionFunction {
69 protected:
70 ~SessionsRestoreFunction() override {}
71 bool RunSync() override;
72 DECLARE_EXTENSION_FUNCTION("sessions.restore", SESSIONS_RESTORE)
74 private:
75 void SetInvalidIdError(const std::string& invalid_id);
76 void SetResultRestoredTab(content::WebContents* contents);
77 bool SetResultRestoredWindow(int window_id);
78 bool RestoreMostRecentlyClosed(Browser* browser);
79 bool RestoreLocalSession(const SessionId& session_id, Browser* browser);
80 bool RestoreForeignSession(const SessionId& session_id,
81 Browser* browser);
84 class SessionsEventRouter : public TabRestoreServiceObserver {
85 public:
86 explicit SessionsEventRouter(Profile* profile);
87 ~SessionsEventRouter() override;
89 // Observer callback for TabRestoreServiceObserver. Sends data on
90 // recently closed tabs to the javascript side of this page to
91 // display to the user.
92 void TabRestoreServiceChanged(TabRestoreService* service) override;
94 // Observer callback to notice when our associated TabRestoreService
95 // is destroyed.
96 void TabRestoreServiceDestroyed(TabRestoreService* service) override;
98 private:
99 Profile* profile_;
101 // TabRestoreService that we are observing.
102 TabRestoreService* tab_restore_service_;
104 DISALLOW_COPY_AND_ASSIGN(SessionsEventRouter);
107 class SessionsAPI : public BrowserContextKeyedAPI,
108 public extensions::EventRouter::Observer {
109 public:
110 explicit SessionsAPI(content::BrowserContext* context);
111 ~SessionsAPI() override;
113 // BrowserContextKeyedService implementation.
114 void Shutdown() override;
116 // BrowserContextKeyedAPI implementation.
117 static BrowserContextKeyedAPIFactory<SessionsAPI>* GetFactoryInstance();
119 // EventRouter::Observer implementation.
120 void OnListenerAdded(const extensions::EventListenerInfo& details) override;
122 private:
123 friend class BrowserContextKeyedAPIFactory<SessionsAPI>;
125 content::BrowserContext* browser_context_;
127 // BrowserContextKeyedAPI implementation.
128 static const char* service_name() {
129 return "SessionsAPI";
131 static const bool kServiceIsNULLWhileTesting = true;
133 // Created lazily upon OnListenerAdded.
134 scoped_ptr<SessionsEventRouter> sessions_event_router_;
136 DISALLOW_COPY_AND_ASSIGN(SessionsAPI);
139 } // namespace extensions
141 #endif // CHROME_BROWSER_EXTENSIONS_API_SESSIONS_SESSIONS_API_H__