diff options
Diffstat (limited to 'chrome/browser')
5 files changed, 74 insertions, 4 deletions
diff --git a/chrome/browser/extensions/extension_browser_event_router.cc b/chrome/browser/extensions/extension_browser_event_router.cc index cefef15..c05e793 100644 --- a/chrome/browser/extensions/extension_browser_event_router.cc +++ b/chrome/browser/extensions/extension_browser_event_router.cc @@ -13,6 +13,8 @@ #include "chrome/browser/extensions/extension_tabs_module.h" #include "chrome/common/notification_service.h" +const char* kOnWindowCreated = "window-created"; +const char* kOnWindowRemoved = "window-removed"; const char* kOnTabCreated = "tab-created"; const char* kOnTabMoved = "tab-moved"; const char* kOnTabSelectionChanged = "tab-selection-changed"; @@ -55,10 +57,12 @@ void ExtensionBrowserEventRouter::Observe(NotificationType type, case(NotificationType::BROWSER_OPENED) : browser = Source<Browser>(source).ptr(); browser->tabstrip_model()->AddObserver(this); + BrowserOpened(browser); break; case(NotificationType::BROWSER_CLOSED) : browser = Source<Browser>(source).ptr(); browser->tabstrip_model()->RemoveObserver(this); + BrowserClosed(browser); break; default: NOTREACHED(); @@ -66,6 +70,30 @@ void ExtensionBrowserEventRouter::Observe(NotificationType type, } } +void ExtensionBrowserEventRouter::BrowserOpened(Browser* browser) { + int window_id = ExtensionTabUtil::GetWindowId(browser); + + ListValue args; + args.Append(Value::CreateIntegerValue(window_id)); + + std::string json_args; + JSONWriter::Write(&args, false, &json_args); + + DispatchEvent(browser->profile(), kOnWindowCreated, json_args); +} + +void ExtensionBrowserEventRouter::BrowserClosed(Browser* browser) { + int window_id = ExtensionTabUtil::GetWindowId(browser); + + ListValue args; + args.Append(Value::CreateIntegerValue(window_id)); + + std::string json_args; + JSONWriter::Write(&args, false, &json_args); + + DispatchEvent(browser->profile(), kOnWindowRemoved, json_args); +} + void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, int index, bool foreground) { diff --git a/chrome/browser/extensions/extension_browser_event_router.h b/chrome/browser/extensions/extension_browser_event_router.h index 3a2f5ec..29ec398 100644 --- a/chrome/browser/extensions/extension_browser_event_router.h +++ b/chrome/browser/extensions/extension_browser_event_router.h @@ -45,6 +45,10 @@ class ExtensionBrowserEventRouter : public TabStripModelObserver, const NotificationSource& source, const NotificationDetails& details); private: + // Construct and dispatch windows.onCreated event. + void BrowserOpened(Browser* browser); + // Construct and dispatch windows.onRemoved event. + void BrowserClosed(Browser* browser); ExtensionBrowserEventRouter(); friend struct DefaultSingletonTraits<ExtensionBrowserEventRouter>; diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index 7e6099f..0d45d84 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -53,6 +53,7 @@ FactoryRegistry::FactoryRegistry() { // Tabs factories_["GetWindows"] = &NewExtensionFunction<GetWindowsFunction>; factories_["CreateWindow"] = &NewExtensionFunction<CreateWindowFunction>; + factories_["RemoveWindow"] = &NewExtensionFunction<RemoveWindowFunction>; factories_["GetTabsForWindow"] = &NewExtensionFunction<GetTabsForWindowFunction>; factories_["GetTab"] = &NewExtensionFunction<GetTabFunction>; diff --git a/chrome/browser/extensions/extension_tabs_module.cc b/chrome/browser/extensions/extension_tabs_module.cc index 67f4cc6..269ffce 100644 --- a/chrome/browser/extensions/extension_tabs_module.cc +++ b/chrome/browser/extensions/extension_tabs_module.cc @@ -70,10 +70,13 @@ bool GetWindowsFunction::RunImpl() { result_.reset(new ListValue()); for (BrowserList::const_iterator browser = BrowserList::begin(); browser != BrowserList::end(); ++browser) { - if (all_windows || (window_ids.find(ExtensionTabUtil::GetWindowId(*browser)) - != window_ids.end())) { - static_cast<ListValue*>(result_.get())->Append( + // Only examine browsers in the current profile. + if ((*browser)->profile() == profile()) { + if (all_windows || (window_ids.find(ExtensionTabUtil:: + GetWindowId(*browser)) != window_ids.end())) { + static_cast<ListValue*>(result_.get())->Append( CreateWindowValue(*browser)); + } } } @@ -146,11 +149,42 @@ bool CreateWindowFunction::RunImpl() { return true; } +bool RemoveWindowFunction::RunImpl() { + if (!args_->IsType(Value::TYPE_INTEGER)) + return false; + + int window_id; + if (!args_->GetAsInteger(&window_id)) + return false; + + Browser* target = NULL; + for (BrowserList::const_iterator browser = BrowserList::begin(); + browser != BrowserList::end(); ++browser) { + // Only examine browsers in the current profile. + if ((*browser)->profile() == profile()) { + if (ExtensionTabUtil::GetWindowId(*browser) == window_id) { + target = *browser; + break; + } + } + } + + if (target == NULL) { + // TODO(rafaelw): need error message. + return false; + } + + target->CloseWindow(); + + return true; +} + + bool GetTabsForWindowFunction::RunImpl() { if (!args_->IsType(Value::TYPE_NULL)) return false; - Browser* browser = BrowserList::GetLastActive(); + Browser* browser = dispatcher_->browser(); if (!browser) return false; diff --git a/chrome/browser/extensions/extension_tabs_module.h b/chrome/browser/extensions/extension_tabs_module.h index 617fcf9..90c8647 100644 --- a/chrome/browser/extensions/extension_tabs_module.h +++ b/chrome/browser/extensions/extension_tabs_module.h @@ -23,6 +23,9 @@ class GetWindowsFunction : public SyncExtensionFunction { class CreateWindowFunction : public SyncExtensionFunction { virtual bool RunImpl(); }; +class RemoveWindowFunction : public SyncExtensionFunction { + virtual bool RunImpl(); +}; class GetTabsForWindowFunction : public SyncExtensionFunction { virtual bool RunImpl(); }; |