#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_function_dispatcher.h"
#include "extensions/browser/extension_function_registry.h"
+#include "extensions/browser/extension_system.h"
#include "extensions/common/error_utils.h"
#include "net/base/net_util.h"
#include "ui/base/layout.h"
window.Pass());
}
-bool SessionsGetRecentlyClosedFunction::RunImpl() {
+bool SessionsGetRecentlyClosedFunction::RunSync() {
scoped_ptr<GetRecentlyClosed::Params> params(
GetRecentlyClosed::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
scoped_ptr<std::vector<linked_ptr<tabs::Tab> > > tabs(
new std::vector<linked_ptr<tabs::Tab> >);
- for (size_t i = 0; i < window.tabs.size(); ++i) {
+ for (size_t i = 0; i < tabs_in_window.size(); ++i) {
tabs->push_back(make_linked_ptr(
- CreateTabModel(session_tag, *window.tabs[i], i,
+ CreateTabModel(session_tag, *tabs_in_window[i], i,
window.selected_tab_index).release()));
}
scoped_ptr<api::sessions::Device> SessionsGetDevicesFunction::CreateDeviceModel(
const browser_sync::SyncedSession* session) {
int max_results = api::sessions::MAX_SESSION_RESULTS;
- // Already validated in RunImpl().
+ // Already validated in RunAsync().
scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_));
if (params->filter && params->filter->max_results)
max_results = *params->filter->max_results;
return device_struct.Pass();
}
-bool SessionsGetDevicesFunction::RunImpl() {
+bool SessionsGetDevicesFunction::RunSync() {
ProfileSyncService* service =
ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile());
if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) {
void SessionsRestoreFunction::SetResultRestoredTab(
- const content::WebContents* contents) {
+ content::WebContents* contents) {
scoped_ptr<base::DictionaryValue> tab_value(
ExtensionTabUtil::CreateTabValue(contents, GetExtension()));
scoped_ptr<tabs::Tab> tab(tabs::Tab::FromValue(*tab_value));
return SetResultRestoredWindow(ExtensionTabUtil::GetWindowId(browsers[0]));
}
-bool SessionsRestoreFunction::RunImpl() {
+bool SessionsRestoreFunction::RunSync() {
scoped_ptr<Restore::Params> params(Restore::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
: RestoreLocalSession(*session_id, browser);
}
+SessionsEventRouter::SessionsEventRouter(Profile* profile)
+ : profile_(profile),
+ tab_restore_service_(TabRestoreServiceFactory::GetForProfile(profile)) {
+ // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in
+ // incognito mode)
+ if (tab_restore_service_) {
+ tab_restore_service_->LoadTabsFromLastSession();
+ tab_restore_service_->AddObserver(this);
+ }
+}
+
+SessionsEventRouter::~SessionsEventRouter() {
+ if (tab_restore_service_)
+ tab_restore_service_->RemoveObserver(this);
+}
+
+void SessionsEventRouter::TabRestoreServiceChanged(
+ TabRestoreService* service) {
+ scoped_ptr<base::ListValue> args(new base::ListValue());
+ EventRouter::Get(profile_)->BroadcastEvent(make_scoped_ptr(
+ new Event(api::sessions::OnChanged::kEventName, args.Pass())));
+}
+
+void SessionsEventRouter::TabRestoreServiceDestroyed(
+ TabRestoreService* service) {
+ tab_restore_service_ = NULL;
+}
+
+SessionsAPI::SessionsAPI(content::BrowserContext* context)
+ : browser_context_(context) {
+ EventRouter::Get(browser_context_)->RegisterObserver(this,
+ api::sessions::OnChanged::kEventName);
+}
+
+SessionsAPI::~SessionsAPI() {
+}
+
+void SessionsAPI::Shutdown() {
+ EventRouter::Get(browser_context_)->UnregisterObserver(this);
+}
+
+static base::LazyInstance<BrowserContextKeyedAPIFactory<SessionsAPI> >
+ g_factory = LAZY_INSTANCE_INITIALIZER;
+
+BrowserContextKeyedAPIFactory<SessionsAPI>*
+SessionsAPI::GetFactoryInstance() {
+ return g_factory.Pointer();
+}
+
+void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) {
+ sessions_event_router_.reset(
+ new SessionsEventRouter(Profile::FromBrowserContext(browser_context_)));
+ EventRouter::Get(browser_context_)->UnregisterObserver(this);
+}
+
} // namespace extensions