#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"
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();
}
tab.tabstrip_index,
tab.pinned,
selected_index,
- GetExtension());
+ extension());
}
scoped_ptr<windows::Window>
window.Pass());
}
-bool SessionsGetRecentlyClosedFunction::RunImpl() {
+bool SessionsGetRecentlyClosedFunction::RunSync() {
scoped_ptr<GetRecentlyClosed::Params> params(
GetRecentlyClosed::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
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(
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()));
}
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;
}
break;
case ui::SHOW_STATE_DEFAULT:
case ui::SHOW_STATE_INACTIVE:
- case ui::SHOW_STATE_DETACHED:
case ui::SHOW_STATE_END:
break;
}
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() &&
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()));
+ 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(),
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(
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