Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / sessions / sessions_api.h
blob876faae50bb5b2c582880e77bea2e6bfcc725086
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(
38 const sessions::TabRestoreService::Tab& tab,
39 int session_id,
40 int selected_index);
41 scoped_ptr<api::windows::Window> CreateWindowModel(
42 const sessions::TabRestoreService::Window& window,
43 int session_id);
44 scoped_ptr<api::sessions::Session> CreateSessionModel(
45 const sessions::TabRestoreService::Entry* entry);
48 class SessionsGetDevicesFunction : public ChromeSyncExtensionFunction {
49 protected:
50 ~SessionsGetDevicesFunction() override {}
51 bool RunSync() override;
52 DECLARE_EXTENSION_FUNCTION("sessions.getDevices", SESSIONS_GETDEVICES)
54 private:
55 scoped_ptr<api::tabs::Tab> CreateTabModel(const std::string& session_tag,
56 const sessions::SessionTab& tab,
57 int tab_index,
58 int selected_index);
59 scoped_ptr<api::windows::Window> CreateWindowModel(
60 const sessions::SessionWindow& window,
61 const std::string& session_tag);
62 scoped_ptr<api::sessions::Session> CreateSessionModel(
63 const sessions::SessionWindow& window,
64 const std::string& session_tag);
65 scoped_ptr<api::sessions::Device> CreateDeviceModel(
66 const sync_driver::SyncedSession* session);
69 class SessionsRestoreFunction : public ChromeSyncExtensionFunction {
70 protected:
71 ~SessionsRestoreFunction() override {}
72 bool RunSync() override;
73 DECLARE_EXTENSION_FUNCTION("sessions.restore", SESSIONS_RESTORE)
75 private:
76 void SetInvalidIdError(const std::string& invalid_id);
77 void SetResultRestoredTab(content::WebContents* contents);
78 bool SetResultRestoredWindow(int window_id);
79 bool RestoreMostRecentlyClosed(Browser* browser);
80 bool RestoreLocalSession(const SessionId& session_id, Browser* browser);
81 bool RestoreForeignSession(const SessionId& session_id,
82 Browser* browser);
85 class SessionsEventRouter : public sessions::TabRestoreServiceObserver {
86 public:
87 explicit SessionsEventRouter(Profile* profile);
88 ~SessionsEventRouter() override;
90 // Observer callback for TabRestoreServiceObserver. Sends data on
91 // recently closed tabs to the javascript side of this page to
92 // display to the user.
93 void TabRestoreServiceChanged(sessions::TabRestoreService* service) override;
95 // Observer callback to notice when our associated TabRestoreService
96 // is destroyed.
97 void TabRestoreServiceDestroyed(
98 sessions::TabRestoreService* service) override;
100 private:
101 Profile* profile_;
103 // TabRestoreService that we are observing.
104 sessions::TabRestoreService* tab_restore_service_;
106 DISALLOW_COPY_AND_ASSIGN(SessionsEventRouter);
109 class SessionsAPI : public BrowserContextKeyedAPI,
110 public extensions::EventRouter::Observer {
111 public:
112 explicit SessionsAPI(content::BrowserContext* context);
113 ~SessionsAPI() override;
115 // BrowserContextKeyedService implementation.
116 void Shutdown() override;
118 // BrowserContextKeyedAPI implementation.
119 static BrowserContextKeyedAPIFactory<SessionsAPI>* GetFactoryInstance();
121 // EventRouter::Observer implementation.
122 void OnListenerAdded(const extensions::EventListenerInfo& details) override;
124 private:
125 friend class BrowserContextKeyedAPIFactory<SessionsAPI>;
127 content::BrowserContext* browser_context_;
129 // BrowserContextKeyedAPI implementation.
130 static const char* service_name() {
131 return "SessionsAPI";
133 static const bool kServiceIsNULLWhileTesting = true;
135 // Created lazily upon OnListenerAdded.
136 scoped_ptr<SessionsEventRouter> sessions_event_router_;
138 DISALLOW_COPY_AND_ASSIGN(SessionsAPI);
141 } // namespace extensions
143 #endif // CHROME_BROWSER_EXTENSIONS_API_SESSIONS_SESSIONS_API_H__