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
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_API_SESSIONS_SESSIONS_API_H__
#define CHROME_BROWSER_EXTENSIONS_API_SESSIONS_SESSIONS_API_H__
#include <string>
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/browser/sessions/tab_restore_service.h"
#include "chrome/browser/sessions/tab_restore_service_observer.h"
#include "chrome/common/extensions/api/sessions.h"
#include "chrome/common/extensions/api/tabs.h"
#include "chrome/common/extensions/api/windows.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/event_router.h"
class Profile;
namespace browser_sync {
struct SyncedSession;
}
namespace extensions {
class SessionId;
class SessionsGetRecentlyClosedFunction : public ChromeSyncExtensionFunction {
protected:
virtual ~SessionsGetRecentlyClosedFunction() {}
virtual bool RunSync() override;
DECLARE_EXTENSION_FUNCTION("sessions.getRecentlyClosed",
SESSIONS_GETRECENTLYCLOSED)
private:
scoped_ptr<api::tabs::Tab> CreateTabModel(const TabRestoreService::Tab& tab,
int session_id,
int selected_index);
scoped_ptr<api::windows::Window> CreateWindowModel(
const TabRestoreService::Window& window,
int session_id);
scoped_ptr<api::sessions::Session> CreateSessionModel(
const TabRestoreService::Entry* entry);
};
class SessionsGetDevicesFunction : public ChromeSyncExtensionFunction {
protected:
virtual ~SessionsGetDevicesFunction() {}
virtual bool RunSync() override;
DECLARE_EXTENSION_FUNCTION("sessions.getDevices", SESSIONS_GETDEVICES)
private:
scoped_ptr<api::tabs::Tab> CreateTabModel(const std::string& session_tag,
const SessionTab& tab,
int tab_index,
int selected_index);
scoped_ptr<api::windows::Window> CreateWindowModel(
const SessionWindow& window,
const std::string& session_tag);
scoped_ptr<api::sessions::Session> CreateSessionModel(
const SessionWindow& window,
const std::string& session_tag);
scoped_ptr<api::sessions::Device> CreateDeviceModel(
const browser_sync::SyncedSession* session);
};
class SessionsRestoreFunction : public ChromeSyncExtensionFunction {
protected:
virtual ~SessionsRestoreFunction() {}
virtual bool RunSync() override;
DECLARE_EXTENSION_FUNCTION("sessions.restore", SESSIONS_RESTORE)
private:
void SetInvalidIdError(const std::string& invalid_id);
void SetResultRestoredTab(content::WebContents* contents);
bool SetResultRestoredWindow(int window_id);
bool RestoreMostRecentlyClosed(Browser* browser);
bool RestoreLocalSession(const SessionId& session_id, Browser* browser);
bool RestoreForeignSession(const SessionId& session_id,
Browser* browser);
};
class SessionsEventRouter : public TabRestoreServiceObserver {
public:
explicit SessionsEventRouter(Profile* profile);
virtual ~SessionsEventRouter();
// Observer callback for TabRestoreServiceObserver. Sends data on
// recently closed tabs to the javascript side of this page to
// display to the user.
virtual void TabRestoreServiceChanged(TabRestoreService* service) override;
// Observer callback to notice when our associated TabRestoreService
// is destroyed.
virtual void TabRestoreServiceDestroyed(TabRestoreService* service) override;
private:
Profile* profile_;
// TabRestoreService that we are observing.
TabRestoreService* tab_restore_service_;
DISALLOW_COPY_AND_ASSIGN(SessionsEventRouter);
};
class SessionsAPI : public BrowserContextKeyedAPI,
public extensions::EventRouter::Observer {
public:
explicit SessionsAPI(content::BrowserContext* context);
virtual ~SessionsAPI();
// BrowserContextKeyedService implementation.
virtual void Shutdown() override;
// BrowserContextKeyedAPI implementation.
static BrowserContextKeyedAPIFactory<SessionsAPI>* GetFactoryInstance();
// EventRouter::Observer implementation.
virtual void OnListenerAdded(const extensions::EventListenerInfo& details)
override;
private:
friend class BrowserContextKeyedAPIFactory<SessionsAPI>;
content::BrowserContext* browser_context_;
// BrowserContextKeyedAPI implementation.
static const char* service_name() {
return "SessionsAPI";
}
static const bool kServiceIsNULLWhileTesting = true;
// Created lazily upon OnListenerAdded.
scoped_ptr<SessionsEventRouter> sessions_event_router_;
DISALLOW_COPY_AND_ASSIGN(SessionsAPI);
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_SESSIONS_SESSIONS_API_H__
|