Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / sessions / sessions_api.cc
index eca014e..a484be4 100644 (file)
@@ -35,6 +35,7 @@
 #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"
@@ -194,7 +195,7 @@ scoped_ptr<api::sessions::Session>
                                   window.Pass());
 }
 
-bool SessionsGetRecentlyClosedFunction::RunImpl() {
+bool SessionsGetRecentlyClosedFunction::RunSync() {
   scoped_ptr<GetRecentlyClosed::Params> params(
       GetRecentlyClosed::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -273,9 +274,9 @@ scoped_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel(
 
   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()));
   }
 
@@ -341,7 +342,7 @@ SessionsGetDevicesFunction::CreateSessionModel(
 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;
@@ -361,7 +362,7 @@ scoped_ptr<api::sessions::Device> SessionsGetDevicesFunction::CreateDeviceModel(
   return device_struct.Pass();
 }
 
-bool SessionsGetDevicesFunction::RunImpl() {
+bool SessionsGetDevicesFunction::RunSync() {
   ProfileSyncService* service =
       ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile());
   if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) {
@@ -404,7 +405,7 @@ void SessionsRestoreFunction::SetInvalidIdError(const std::string& invalid_id) {
 
 
 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));
@@ -563,7 +564,7 @@ bool SessionsRestoreFunction::RestoreForeignSession(const SessionId& session_id,
   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);
 
@@ -593,4 +594,59 @@ bool SessionsRestoreFunction::RunImpl() {
       : 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