diff options
author | rafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-20 05:01:03 +0000 |
---|---|---|
committer | rafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-20 05:01:03 +0000 |
commit | ff6456c561bc99141ff06384c8c3fbd45162a628 (patch) | |
tree | 0a133d7d992ba374d48fd182495fbebc1407d33f /chrome/browser/extensions | |
parent | 123603228f84d9d63f6e247b73574cbc0bb68819 (diff) | |
download | chromium_src-ff6456c561bc99141ff06384c8c3fbd45162a628.zip chromium_src-ff6456c561bc99141ff06384c8c3fbd45162a628.tar.gz chromium_src-ff6456c561bc99141ff06384c8c3fbd45162a628.tar.bz2 |
tabs.onUpdated now sends 'url' when 'state' has changed to 'loading' when navigating to a new url. If a reload is in progress, 'loading' will not be accompanied by 'url'.
Also, refactored some code so that string constants are defined and shared.
BUG=11200
R=erikkay
Review URL: http://codereview.chromium.org/113552
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16467 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
4 files changed, 254 insertions, 110 deletions
diff --git a/chrome/browser/extensions/extension_browser_event_router.cc b/chrome/browser/extensions/extension_browser_event_router.cc index 869b02c..868bc9c 100644 --- a/chrome/browser/extensions/extension_browser_event_router.cc +++ b/chrome/browser/extensions/extension_browser_event_router.cc @@ -10,34 +10,78 @@ #include "chrome/browser/profile.h" #include "chrome/browser/extensions/extension.h" #include "chrome/browser/extensions/extension_message_service.h" +#include "chrome/browser/tab_contents/navigation_entry.h" #include "chrome/common/notification_service.h" const char* kOnPageActionExecuted = "page-action-executed"; -const char* kOnTabCreated = "tab-created"; -const char* kOnTabUpdated = "tab-updated"; -const char* kOnTabMoved = "tab-moved"; -const char* kOnTabSelectionChanged = "tab-selection-changed"; const char* kOnTabAttached = "tab-attached"; +const char* kOnTabCreated = "tab-created"; const char* kOnTabDetached = "tab-detached"; +const char* kOnTabMoved = "tab-moved"; const char* kOnTabRemoved = "tab-removed"; +const char* kOnTabSelectionChanged = "tab-selection-changed"; +const char* kOnTabUpdated = "tab-updated"; const char* kOnWindowCreated = "window-created"; -const char* kOnWindowRemoved = "window-removed"; const char* kOnWindowFocusedChanged = "window-focus-changed"; +const char* kOnWindowRemoved = "window-removed"; ExtensionBrowserEventRouter::TabEntry::TabEntry() - : state_(ExtensionTabUtil::TAB_COMPLETE) { + : state_(ExtensionTabUtil::TAB_COMPLETE), + pending_navigate_(false), + url_() { } ExtensionBrowserEventRouter::TabEntry::TabEntry(const TabContents* contents) - : state_(ExtensionTabUtil::TAB_COMPLETE) { - UpdateState(contents); + : state_(ExtensionTabUtil::TAB_COMPLETE), + pending_navigate_(false), + url_(contents->GetURL()) { + UpdateLoadState(contents); } -bool ExtensionBrowserEventRouter::TabEntry::UpdateState( +DictionaryValue* ExtensionBrowserEventRouter::TabEntry::UpdateLoadState( const TabContents* contents) { ExtensionTabUtil::TabStatus old_state = state_; state_ = ExtensionTabUtil::GetTabStatus(contents); - return old_state != state_; + + if (old_state == state_) + return false; + + if (state_ == ExtensionTabUtil::TAB_LOADING) { + // Do not send "loading" state changed now. Wait for navigate so the new + // url is available. + pending_navigate_ = true; + return NULL; + + } else if (state_ == ExtensionTabUtil::TAB_COMPLETE) { + // Send "complete" state change. + DictionaryValue* changed_properties = new DictionaryValue(); + changed_properties->SetString(ExtensionTabUtil::kStatusKey, + ExtensionTabUtil::kStatusValueComplete); + return changed_properties; + + } else { + NOTREACHED(); + return NULL; + } +} + +DictionaryValue* ExtensionBrowserEventRouter::TabEntry::DidNavigate( + const TabContents* contents) { + if(!pending_navigate_) + return NULL; + + DictionaryValue* changed_properties = new DictionaryValue(); + changed_properties->SetString(ExtensionTabUtil::kStatusKey, + ExtensionTabUtil::kStatusValueLoading); + + GURL new_url = contents->GetURL(); + if (new_url != url_) { + url_ = new_url; + changed_properties->SetString(ExtensionTabUtil::kUrlKey, url_.spec()); + } + + pending_navigate_ = false; + return changed_properties; } ExtensionBrowserEventRouter* ExtensionBrowserEventRouter::GetInstance() { @@ -108,6 +152,10 @@ void ExtensionBrowserEventRouter::TabCreatedAt(TabContents* contents, JSONWriter::Write(&args, false, &json_args); DispatchEvent(contents->profile(), kOnTabCreated, json_args); + + NotificationService::current()->AddObserver( + this, NotificationType::NAV_ENTRY_COMMITTED, + Source<NavigationController>(&contents->controller())); } void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, @@ -126,9 +174,10 @@ void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, args.Append(Value::CreateIntegerValue(tab_id)); DictionaryValue *object_args = new DictionaryValue(); - object_args->Set(L"newWindowId", Value::CreateIntegerValue( + object_args->Set(ExtensionTabUtil::kNewWindowIdKey, Value::CreateIntegerValue( ExtensionTabUtil::GetWindowIdOfTab(contents))); - object_args->Set(L"newPosition", Value::CreateIntegerValue(index)); + object_args->Set(ExtensionTabUtil::kNewPositionKey, Value::CreateIntegerValue( + index)); args.Append(object_args); std::string json_args; @@ -149,9 +198,10 @@ void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, args.Append(Value::CreateIntegerValue(tab_id)); DictionaryValue *object_args = new DictionaryValue(); - object_args->Set(L"oldWindowId", Value::CreateIntegerValue( + object_args->Set(ExtensionTabUtil::kOldWindowIdKey, Value::CreateIntegerValue( ExtensionTabUtil::GetWindowIdOfTab(contents))); - object_args->Set(L"oldPosition", Value::CreateIntegerValue(index)); + object_args->Set(ExtensionTabUtil::kOldPositionKey, Value::CreateIntegerValue( + index)); args.Append(object_args); std::string json_args; @@ -174,6 +224,10 @@ void ExtensionBrowserEventRouter::TabClosingAt(TabContents* contents, int removed_count = tab_entries_.erase(tab_id); DCHECK(removed_count > 0); + + NotificationService::current()->RemoveObserver( + this, NotificationType::NAV_ENTRY_COMMITTED, + Source<NavigationController>(&contents->controller())); } void ExtensionBrowserEventRouter::TabSelectedAt(TabContents* old_contents, @@ -185,7 +239,7 @@ void ExtensionBrowserEventRouter::TabSelectedAt(TabContents* old_contents, ExtensionTabUtil::GetTabId(new_contents))); DictionaryValue *object_args = new DictionaryValue(); - object_args->Set(L"windowId", Value::CreateIntegerValue( + object_args->Set(ExtensionTabUtil::kWindowIdKey, Value::CreateIntegerValue( ExtensionTabUtil::GetWindowIdOfTab(new_contents))); args.Append(object_args); @@ -202,10 +256,12 @@ void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, args.Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); DictionaryValue *object_args = new DictionaryValue(); - object_args->Set(L"windowId", Value::CreateIntegerValue( + object_args->Set(ExtensionTabUtil::kWindowIdKey, Value::CreateIntegerValue( ExtensionTabUtil::GetWindowIdOfTab(contents))); - object_args->Set(L"fromIndex", Value::CreateIntegerValue(from_index)); - object_args->Set(L"toIndex", Value::CreateIntegerValue(to_index)); + object_args->Set(ExtensionTabUtil::kFromIndexKey, Value::CreateIntegerValue( + from_index)); + object_args->Set(ExtensionTabUtil::kToIndexKey, Value::CreateIntegerValue( + to_index)); args.Append(object_args); std::string json_args; @@ -214,21 +270,25 @@ void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, DispatchEvent(contents->profile(), kOnTabMoved, json_args); } -void ExtensionBrowserEventRouter::TabChangedAt(TabContents* contents, - int index, - bool loading_only) { +void ExtensionBrowserEventRouter::TabUpdated(TabContents* contents, + bool did_navigate) { int tab_id = ExtensionTabUtil::GetTabId(contents); std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); - if (tab_entries_.end() == i) - return; - + CHECK(tab_entries_.end() != i); TabEntry& entry = i->second; - if (entry.UpdateState(contents)) { + + DictionaryValue* changed_properties = NULL; + if (did_navigate) + changed_properties = entry.DidNavigate(contents); + else + changed_properties = entry.UpdateLoadState(contents); + + if (changed_properties) { // The state of the tab (as seen from the extension point of view) has // changed. Send a notification to the extension. ListValue args; args.Append(Value::CreateIntegerValue(tab_id)); - args.Append(ExtensionTabUtil::CreateTabChangedValue(contents)); + args.Append(changed_properties); std::string json_args; JSONWriter::Write(&args, false, &json_args); @@ -237,6 +297,24 @@ void ExtensionBrowserEventRouter::TabChangedAt(TabContents* contents, } } +void ExtensionBrowserEventRouter::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + if (type == NotificationType::NAV_ENTRY_COMMITTED) { + NavigationController* source_controller = + Source<NavigationController>(source).ptr(); + TabUpdated(source_controller->tab_contents(), true); + } else { + NOTREACHED(); + } +} + +void ExtensionBrowserEventRouter::TabChangedAt(TabContents* contents, + int index, + bool loading_only) { + TabUpdated(contents, false); +} + void ExtensionBrowserEventRouter::TabStripEmpty() { } void ExtensionBrowserEventRouter::PageActionExecuted(Profile *profile, @@ -245,12 +323,12 @@ void ExtensionBrowserEventRouter::PageActionExecuted(Profile *profile, std::string url) { ListValue args; DictionaryValue *object_args = new DictionaryValue(); - object_args->Set(L"pageActionId", Value::CreateStringValue(page_action_id)); - + object_args->Set(ExtensionTabUtil::kPageActionIdKey, + Value::CreateStringValue(page_action_id)); DictionaryValue *data = new DictionaryValue(); - data->Set(L"tabId", Value::CreateIntegerValue(tab_id)); - data->Set(L"tabUrl", Value::CreateStringValue(url)); - object_args->Set(L"data", data); + data->Set(ExtensionTabUtil::kTabIdKey, Value::CreateIntegerValue(tab_id)); + data->Set(ExtensionTabUtil::kTabUrlKey, Value::CreateStringValue(url)); + object_args->Set(ExtensionTabUtil::kDataKey, data); args.Append(object_args); diff --git a/chrome/browser/extensions/extension_browser_event_router.h b/chrome/browser/extensions/extension_browser_event_router.h index 749c6e3..67dd0f4 100644 --- a/chrome/browser/extensions/extension_browser_event_router.h +++ b/chrome/browser/extensions/extension_browser_event_router.h @@ -22,7 +22,8 @@ // events from windows/tabs within a profile to extension processes in the same // profile. class ExtensionBrowserEventRouter : public TabStripModelObserver, - public BrowserList::Observer { + public BrowserList::Observer, + public NotificationObserver { public: // Get Browser-Global instance. static ExtensionBrowserEventRouter* GetInstance(); @@ -60,6 +61,11 @@ class ExtensionBrowserEventRouter : public TabStripModelObserver, private: // "Synthetic" event. Called from TabInsertedAt if new tab is detected. void TabCreatedAt(TabContents* contents, int index, bool foreground); + + // Internal processing of tab updated events. Is called by both TabChangedAt + // and Observe/NAV_ENTRY_COMMITTED. + void TabUpdated(TabContents *contents, bool did_navigate); + ExtensionBrowserEventRouter(); friend struct DefaultSingletonTraits<ExtensionBrowserEventRouter>; @@ -84,15 +90,29 @@ class ExtensionBrowserEventRouter : public TabStripModelObserver, // Returns the current state of the tab. ExtensionTabUtil::TabStatus state() const { return state_; } - // Update the state of the tab based on its TabContents. Returns true if - // the state changed, false otherwise. Whether the state has changed or not - // is used to determine if events needs to be sent to extensions during - // processing of TabChangedAt(). - bool UpdateState(const TabContents* contents); + // Update the load state of the tab based on its TabContents. Returns true + // if the state changed, false otherwise. Whether the state has changed or + // not is used to determine if events needs to be sent to extensions during + // processing of TabChangedAt(). This method will "hold" a state-change + // to "loading", until the DidNavigate() method which should always follow + // it. Returns NULL if no updates should be sent. + DictionaryValue* UpdateLoadState(const TabContents* contents); + + // Indicates that a tab load has resulted in a navigation and the + // destination url is available for inspection. Returns NULL if no updates + // should be sent. + DictionaryValue* DidNavigate(const TabContents* contents); private: // Tab state used for last notification to extensions. ExtensionTabUtil::TabStatus state_; + + // Remember that the LOADING state has been captured, but not yet reported + // because it is waiting on the navigation event to know what the + // destination url is. + bool pending_navigate_; + + GURL url_; }; std::map<int, TabEntry> tab_entries_; diff --git a/chrome/browser/extensions/extension_tabs_module.cc b/chrome/browser/extensions/extension_tabs_module.cc index 073f54d..cddafa5 100644 --- a/chrome/browser/extensions/extension_tabs_module.cc +++ b/chrome/browser/extensions/extension_tabs_module.cc @@ -22,26 +22,6 @@ #endif namespace { -// Keys. -const wchar_t* kIdKey = L"id"; -const wchar_t* kIndexKey = L"index"; -const wchar_t* kWindowIdKey = L"windowId"; -const wchar_t* kUrlKey = L"url"; -const wchar_t* kTitleKey = L"title"; -const wchar_t* kStatusKey = L"status"; -const wchar_t* kSelectedKey = L"selected"; -const wchar_t* kFocusedKey = L"focused"; -const wchar_t* kFavIconUrlKey = L"favIconUrl"; -const wchar_t* kLeftKey = L"left"; -const wchar_t* kTopKey = L"top"; -const wchar_t* kWidthKey = L"width"; -const wchar_t* kHeightKey = L"height"; -const wchar_t* kTabsKey = L"tabs"; - -// Tab status text -const char* kStatusValueLoading = "loading"; -const char* kStatusValueComplete = "complete"; - // Error messages. const char* kWindowNotFoundError = "No window with id: *."; const char* kTabNotFoundError = "No tab with id: *."; @@ -70,6 +50,35 @@ static GURL AbsolutePath(Profile* profile, std::string extension_id, std::string relative_url); // ExtensionTabUtil +const wchar_t* ExtensionTabUtil::kDataKey = L"data"; +const wchar_t* ExtensionTabUtil::kFavIconUrlKey = L"favIconUrl"; +const wchar_t* ExtensionTabUtil::kFocusedKey = L"focused"; +const wchar_t* ExtensionTabUtil::kFromIndexKey = L"fromIndex"; +const wchar_t* ExtensionTabUtil::kHeightKey = L"height"; +const wchar_t* ExtensionTabUtil::kIdKey = L"id"; +const wchar_t* ExtensionTabUtil::kIndexKey = L"index"; +const wchar_t* ExtensionTabUtil::kLeftKey = L"left"; +const wchar_t* ExtensionTabUtil::kNewPositionKey = L"newPosition"; +const wchar_t* ExtensionTabUtil::kNewWindowIdKey = L"newWindowId"; +const wchar_t* ExtensionTabUtil::kOldPositionKey = L"oldPosition"; +const wchar_t* ExtensionTabUtil::kOldWindowIdKey = L"oldWindowId"; +const wchar_t* ExtensionTabUtil::kPageActionIdKey = L"pageActionId"; +const wchar_t* ExtensionTabUtil::kSelectedKey = L"selected"; +const wchar_t* ExtensionTabUtil::kStatusKey = L"status"; +const wchar_t* ExtensionTabUtil::kTabIdKey = L"tabId"; +const wchar_t* ExtensionTabUtil::kTabsKey = L"tabs"; +const wchar_t* ExtensionTabUtil::kTabUrlKey = L"tabUrl"; +const wchar_t* ExtensionTabUtil::kTitleKey = L"title"; +const wchar_t* ExtensionTabUtil::kToIndexKey = L"toIndex"; +const wchar_t* ExtensionTabUtil::kTopKey = L"top"; +const wchar_t* ExtensionTabUtil::kUrlKey = L"url"; +const wchar_t* ExtensionTabUtil::kWidthKey = L"width"; +const wchar_t* ExtensionTabUtil::kWindowIdKey = L"windowId"; + +// Value consts. +const char* ExtensionTabUtil::kStatusValueComplete = "complete"; +const char* ExtensionTabUtil::kStatusValueLoading = "loading"; + int ExtensionTabUtil::GetWindowId(const Browser* browser) { return browser->session_id().id(); } @@ -117,15 +126,6 @@ DictionaryValue* ExtensionTabUtil::CreateTabValue( return ExtensionTabUtil::CreateTabValue(contents, NULL, -1); } -DictionaryValue* ExtensionTabUtil::CreateTabChangedValue( - const TabContents* contents) { - // A tab changed event should include a tab value that contains only the - // changed properties. For now, this means only the status property. - DictionaryValue* result = new DictionaryValue(); - result->SetString(kStatusKey, GetTabStatusText(GetTabStatus(contents))); - return result; -} - DictionaryValue* ExtensionTabUtil::CreateTabValue( const TabContents* contents, TabStripModel* tab_strip, int tab_index) { TabStatus status = GetTabStatus(contents); @@ -238,8 +238,9 @@ bool CreateWindowFunction::RunImpl() { EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); std::string url_input; - if (args->HasKey(kUrlKey)) { - EXTENSION_FUNCTION_VALIDATE(args->GetString(kUrlKey, &url_input)); + if (args->HasKey(ExtensionTabUtil::kUrlKey)) { + EXTENSION_FUNCTION_VALIDATE(args->GetString(ExtensionTabUtil::kUrlKey, + &url_input)); url.reset(new GURL(url_input)); if (!url->is_valid()) { error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidUrlError, @@ -263,23 +264,27 @@ bool CreateWindowFunction::RunImpl() { if (args_->IsType(Value::TYPE_DICTIONARY)) { const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); int bounds_val; - if (args->HasKey(kLeftKey)) { - EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kLeftKey, &bounds_val)); + if (args->HasKey(ExtensionTabUtil::kLeftKey)) { + EXTENSION_FUNCTION_VALIDATE(args->GetInteger(ExtensionTabUtil::kLeftKey, + &bounds_val)); bounds.set_x(bounds_val); } - if (args->HasKey(kTopKey)) { - EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kTopKey, &bounds_val)); + if (args->HasKey(ExtensionTabUtil::kTopKey)) { + EXTENSION_FUNCTION_VALIDATE(args->GetInteger(ExtensionTabUtil::kTopKey, + &bounds_val)); bounds.set_y(bounds_val); } - if (args->HasKey(kWidthKey)) { - EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kWidthKey, &bounds_val)); + if (args->HasKey(ExtensionTabUtil::kWidthKey)) { + EXTENSION_FUNCTION_VALIDATE(args->GetInteger(ExtensionTabUtil::kWidthKey, + &bounds_val)); bounds.set_width(bounds_val); } - if (args->HasKey(kHeightKey)) { - EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kHeightKey, &bounds_val)); + if (args->HasKey(ExtensionTabUtil::kHeightKey)) { + EXTENSION_FUNCTION_VALIDATE(args->GetInteger(ExtensionTabUtil::kHeightKey, + &bounds_val)); bounds.set_height(bounds_val); } } @@ -313,26 +318,30 @@ bool UpdateWindowFunction::RunImpl() { gfx::Rect bounds = browser->window()->GetNormalBounds(); // Any part of the bounds can optionally be set by the caller. int bounds_val; - if (update_props->HasKey(kLeftKey)) { - EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kLeftKey, + if (update_props->HasKey(ExtensionTabUtil::kLeftKey)) { + EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( + ExtensionTabUtil::kLeftKey, &bounds_val)); bounds.set_x(bounds_val); } - if (update_props->HasKey(kTopKey)) { - EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kTopKey, + if (update_props->HasKey(ExtensionTabUtil::kTopKey)) { + EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( + ExtensionTabUtil::kTopKey, &bounds_val)); bounds.set_y(bounds_val); } - if (update_props->HasKey(kWidthKey)) { - EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kWidthKey, + if (update_props->HasKey(ExtensionTabUtil::kWidthKey)) { + EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( + ExtensionTabUtil::kWidthKey, &bounds_val)); bounds.set_width(bounds_val); } - if (update_props->HasKey(kHeightKey)) { - EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kHeightKey, + if (update_props->HasKey(ExtensionTabUtil::kHeightKey)) { + EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( + ExtensionTabUtil::kHeightKey, &bounds_val)); bounds.set_height(bounds_val); } @@ -409,8 +418,9 @@ bool CreateTabFunction::RunImpl() { Browser *browser; // windowId defaults to "current" window. int window_id = -1; - if (args->HasKey(kWindowIdKey)) { - EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kWindowIdKey, &window_id)); + if (args->HasKey(ExtensionTabUtil::kWindowIdKey)) { + EXTENSION_FUNCTION_VALIDATE(args->GetInteger( + ExtensionTabUtil::kWindowIdKey, &window_id)); browser = GetBrowserInProfileWithId(profile(), window_id, &error_); if (!browser) return false; @@ -426,8 +436,9 @@ bool CreateTabFunction::RunImpl() { std::string url_string; scoped_ptr<GURL> url(new GURL()); - if (args->HasKey(kUrlKey)) { - EXTENSION_FUNCTION_VALIDATE(args->GetString(kUrlKey, &url_string)); + if (args->HasKey(ExtensionTabUtil::kUrlKey)) { + EXTENSION_FUNCTION_VALIDATE(args->GetString(ExtensionTabUtil::kUrlKey, + &url_string)); url.reset(new GURL(url_string)); if (!url->is_valid()) { // The path as passed in is not valid. Try converting to absolute path. @@ -443,14 +454,15 @@ bool CreateTabFunction::RunImpl() { // Default to foreground for the new tab. The presence of 'selected' property // will override this default. bool selected = true; - if (args->HasKey(kSelectedKey)) - EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(kSelectedKey, &selected)); - + if (args->HasKey(ExtensionTabUtil::kSelectedKey)) + EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(ExtensionTabUtil::kSelectedKey, + &selected)); // If index is specified, honor the value, but keep it bound to // 0 <= index <= tab_strip->count() int index = -1; - if (args->HasKey(kIndexKey)) - EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kIndexKey, &index)); + if (args->HasKey(ExtensionTabUtil::kIndexKey)) + EXTENSION_FUNCTION_VALIDATE(args->GetInteger(ExtensionTabUtil::kIndexKey, + &index)); if (index < 0) { // Default insert behavior. @@ -510,8 +522,9 @@ bool UpdateTabFunction::RunImpl() { // Navigate the tab to a new location if the url different. std::string url; - if (update_props->HasKey(kUrlKey)) { - EXTENSION_FUNCTION_VALIDATE(update_props->GetString(kUrlKey, &url)); + if (update_props->HasKey(ExtensionTabUtil::kUrlKey)) { + EXTENSION_FUNCTION_VALIDATE(update_props->GetString( + ExtensionTabUtil::kUrlKey, &url)); GURL new_gurl(url); if (!new_gurl.is_valid()) { @@ -529,9 +542,10 @@ bool UpdateTabFunction::RunImpl() { bool selected = false; // TODO(rafaelw): Setting |selected| from js doesn't make much sense. // Move tab selection management up to window. - if (update_props->HasKey(kSelectedKey)) { - EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean(kSelectedKey, - &selected)); + if (update_props->HasKey(ExtensionTabUtil::kSelectedKey)) { + EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( + ExtensionTabUtil::kSelectedKey, + &selected)); if (selected && tab_strip->selected_index() != tab_index) { tab_strip->SelectTabContentsAt(tab_index, false); } @@ -549,7 +563,8 @@ bool MoveTabFunction::RunImpl() { EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &update_props)); int new_index; - EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kIndexKey, &new_index)); + EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( + ExtensionTabUtil::kIndexKey, &new_index)); EXTENSION_FUNCTION_VALIDATE(new_index >= 0); Browser* source_browser = NULL; @@ -559,11 +574,11 @@ bool MoveTabFunction::RunImpl() { &tab_index, &error_)) return false; - if (update_props->HasKey(kWindowIdKey)) { + if (update_props->HasKey(ExtensionTabUtil::kWindowIdKey)) { Browser* target_browser; int window_id; - EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kWindowIdKey, - &window_id)); + EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( + ExtensionTabUtil::kWindowIdKey, &window_id)); target_browser = GetBrowserInProfileWithId(profile(), window_id, &error_); if (!target_browser) @@ -625,18 +640,20 @@ bool RemoveTabFunction::RunImpl() { static DictionaryValue* CreateWindowValue(Browser* browser, bool populate_tabs) { DictionaryValue* result = new DictionaryValue(); - result->SetInteger(kIdKey, ExtensionTabUtil::GetWindowId(browser)); - result->SetBoolean(kFocusedKey, browser->window()->IsActive()); + result->SetInteger(ExtensionTabUtil::kIdKey, ExtensionTabUtil::GetWindowId( + browser)); + result->SetBoolean(ExtensionTabUtil::kFocusedKey, + browser->window()->IsActive()); gfx::Rect bounds = browser->window()->GetNormalBounds(); // TODO(rafaelw): zIndex ? - result->SetInteger(kLeftKey, bounds.x()); - result->SetInteger(kTopKey, bounds.y()); - result->SetInteger(kWidthKey, bounds.width()); - result->SetInteger(kHeightKey, bounds.height()); + result->SetInteger(ExtensionTabUtil::kLeftKey, bounds.x()); + result->SetInteger(ExtensionTabUtil::kTopKey, bounds.y()); + result->SetInteger(ExtensionTabUtil::kWidthKey, bounds.width()); + result->SetInteger(ExtensionTabUtil::kHeightKey, bounds.height()); if (populate_tabs) { - result->Set(kTabsKey, CreateTabList(browser)); + result->Set(ExtensionTabUtil::kTabsKey, CreateTabList(browser)); } return result; diff --git a/chrome/browser/extensions/extension_tabs_module.h b/chrome/browser/extensions/extension_tabs_module.h index 2dd2c27..7e27e37 100644 --- a/chrome/browser/extensions/extension_tabs_module.h +++ b/chrome/browser/extensions/extension_tabs_module.h @@ -23,6 +23,36 @@ class ExtensionTabUtil { TAB_COMPLETE // Tab loading and rendering is complete. }; + // Keys used in serializing tab data & events. + static const wchar_t* kDataKey; + static const wchar_t* kFavIconUrlKey; + static const wchar_t* kFocusedKey; + static const wchar_t* kFromIndexKey; + static const wchar_t* kHeightKey; + static const wchar_t* kIdKey; + static const wchar_t* kIndexKey; + static const wchar_t* kLeftKey; + static const wchar_t* kNewPositionKey; + static const wchar_t* kNewWindowIdKey; + static const wchar_t* kOldPositionKey; + static const wchar_t* kOldWindowIdKey; + static const wchar_t* kPageActionIdKey; + static const wchar_t* kSelectedKey; + static const wchar_t* kStatusKey; + static const wchar_t* kTabIdKey; + static const wchar_t* kTabsKey; + static const wchar_t* kTabUrlKey; + static const wchar_t* kTitleKey; + static const wchar_t* kToIndexKey; + static const wchar_t* kTopKey; + static const wchar_t* kUrlKey; + static const wchar_t* kWidthKey; + static const wchar_t* kWindowIdKey; + + // Value consts. + static const char* kStatusValueComplete; + static const char* kStatusValueLoading; + static int GetWindowId(const Browser* browser); static int GetTabId(const TabContents* tab_contents); static TabStatus GetTabStatus(const TabContents* tab_contents); @@ -32,7 +62,6 @@ class ExtensionTabUtil { static DictionaryValue* CreateTabValue(const TabContents* tab_contents, TabStripModel* tab_strip, int tab_index); - static DictionaryValue* CreateTabChangedValue(const TabContents* contents); // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may // be NULL and will not be set within the function. |