Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / sessions / sessions_api.cc
index eca014e..cc48308 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"
@@ -79,25 +80,29 @@ scoped_ptr<tabs::Tab> CreateTabModelHelper(
     const Extension* extension) {
   scoped_ptr<tabs::Tab> tab_struct(new tabs::Tab);
 
-  GURL gurl = current_navigation.virtual_url();
+  const GURL& url = current_navigation.virtual_url();
   std::string title = base::UTF16ToUTF8(current_navigation.title());
 
   tab_struct->session_id.reset(new std::string(session_id));
-  tab_struct->url.reset(new std::string(gurl.spec()));
+  tab_struct->url.reset(new std::string(url.spec()));
+  tab_struct->fav_icon_url.reset(
+      new std::string(current_navigation.favicon_url().spec()));
   if (!title.empty()) {
     tab_struct->title.reset(new std::string(title));
   } else {
     const std::string languages =
         profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
-    tab_struct->title.reset(new std::string(base::UTF16ToUTF8(
-        net::FormatUrl(gurl, languages))));
+    tab_struct->title.reset(
+        new std::string(base::UTF16ToUTF8(net::FormatUrl(url, languages))));
   }
   tab_struct->index = index;
   tab_struct->pinned = pinned;
-  tab_struct->selected = index == selected_index;
-  tab_struct->active = false;
-  tab_struct->highlighted = false;
-  tab_struct->incognito = false;
+  // Note: |selected_index| from the sync sessions model is what we call
+  // "active" in extensions terminology.  "selected" is deprecated because it's
+  // not clear whether it means "active" (user can see) or "highlighted" (user
+  // has highlighted, since you can select tabs without bringing them into the
+  // foreground).
+  tab_struct->active = index == selected_index;
   ExtensionTabUtil::ScrubTabForExtension(extension, tab_struct.get());
   return tab_struct.Pass();
 }
@@ -149,7 +154,7 @@ scoped_ptr<tabs::Tab> SessionsGetRecentlyClosedFunction::CreateTabModel(
                               tab.tabstrip_index,
                               tab.pinned,
                               selected_index,
-                              GetExtension());
+                              extension());
 }
 
 scoped_ptr<windows::Window>
@@ -194,7 +199,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);
@@ -239,14 +244,14 @@ scoped_ptr<tabs::Tab> SessionsGetDevicesFunction::CreateTabModel(
     int tab_index,
     int selected_index) {
   std::string session_id = SessionId(session_tag, tab.tab_id.id()).ToString();
-  return CreateTabModelHelper(GetProfile(),
-                              tab.navigations[
-                                tab.normalized_navigation_index()],
-                              session_id,
-                              tab_index,
-                              tab.pinned,
-                              selected_index,
-                              GetExtension());
+  return CreateTabModelHelper(
+      GetProfile(),
+      tab.navigations[tab.normalized_navigation_index()],
+      session_id,
+      tab_index,
+      tab.pinned,
+      selected_index,
+      extension());
 }
 
 scoped_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel(
@@ -273,9 +278,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()));
   }
 
@@ -284,10 +289,10 @@ scoped_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel(
 
   windows::Window::Type type = windows::Window::TYPE_NONE;
   switch (window.type) {
-    case Browser::TYPE_TABBED:
+    case SessionWindow::TYPE_TABBED:
       type = windows::Window::TYPE_NORMAL;
       break;
-    case Browser::TYPE_POPUP:
+    case SessionWindow::TYPE_POPUP:
       type = windows::Window::TYPE_POPUP;
       break;
   }
@@ -308,7 +313,6 @@ scoped_ptr<windows::Window> SessionsGetDevicesFunction::CreateWindowModel(
       break;
     case ui::SHOW_STATE_DEFAULT:
     case ui::SHOW_STATE_INACTIVE:
-    case ui::SHOW_STATE_DETACHED:
     case ui::SHOW_STATE_END:
       break;
   }
@@ -341,13 +345,14 @@ 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;
 
   scoped_ptr<api::sessions::Device> device_struct(new api::sessions::Device);
   device_struct->info = session->session_name;
+  device_struct->device_name = session->session_name;
 
   for (browser_sync::SyncedSession::SyncedWindowMap::const_iterator it =
        session->windows.begin(); it != session->windows.end() &&
@@ -361,7 +366,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,9 +409,9 @@ 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()));
+      ExtensionTabUtil::CreateTabValue(contents, extension()));
   scoped_ptr<tabs::Tab> tab(tabs::Tab::FromValue(*tab_value));
   scoped_ptr<api::sessions::Session> restored_session(CreateSessionModelHelper(
       base::Time::Now().ToTimeT(),
@@ -422,7 +427,7 @@ bool SessionsRestoreFunction::SetResultRestoredWindow(int window_id) {
     return false;
   }
   scoped_ptr<base::DictionaryValue> window_value(
-      controller->CreateWindowValueWithTabs(GetExtension()));
+      controller->CreateWindowValueWithTabs(extension()));
   scoped_ptr<windows::Window> window(windows::Window::FromValue(
       *window_value));
   results_ = Restore::Results::Create(*CreateSessionModelHelper(
@@ -563,7 +568,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 +598,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