Chromium Code Reviews| Index: chrome/browser/extensions/extension_browser_event_router.cc |
| diff --git a/chrome/browser/extensions/extension_browser_event_router.cc b/chrome/browser/extensions/extension_browser_event_router.cc |
| index 3d814ce817b6a55c7e85ad7fdb322848c1a82031..adf86a6f3b3791659e3d5ab598c466578a45e22f 100644 |
| --- a/chrome/browser/extensions/extension_browser_event_router.cc |
| +++ b/chrome/browser/extensions/extension_browser_event_router.cc |
| @@ -172,17 +172,14 @@ void ExtensionBrowserEventRouter::UnregisterForTabNotifications( |
| } |
| void ExtensionBrowserEventRouter::OnBrowserWindowReady(Browser* browser) { |
| - ListValue args; |
| + ListValue* args = new ListValue(); |
| DCHECK(browser->extension_window_controller()); |
| DictionaryValue* window_dictionary = |
| browser->extension_window_controller()->CreateWindowValue(); |
| - args.Append(window_dictionary); |
| + args->Append(window_dictionary); |
| - std::string json_args; |
| - base::JSONWriter::Write(&args, &json_args); |
| - |
| - DispatchEvent(browser->profile(), events::kOnWindowCreated, json_args); |
| + DispatchEvent(browser->profile(), events::kOnWindowCreated, args); |
| } |
| void ExtensionBrowserEventRouter::OnBrowserRemoved(Browser* browser) { |
| @@ -233,29 +230,25 @@ void ExtensionBrowserEventRouter::OnBrowserSetLastActive( |
| focused_profile_ = window_profile; |
| focused_window_id_ = window_id; |
| - ListValue real_args; |
| - real_args.Append(Value::CreateIntegerValue(window_id)); |
| - std::string real_json_args; |
| - base::JSONWriter::Write(&real_args, &real_json_args); |
| + ListValue* real_args = new ListValue(); |
| + real_args->Append(Value::CreateIntegerValue(window_id)); |
| // When switching between windows in the default and incognitoi profiles, |
| // dispatch WINDOW_ID_NONE to extensions whose profile lost focus that |
| // can't see the new focused window across the incognito boundary. |
| // See crbug.com/46610. |
| - std::string none_json_args; |
| + ListValue* none_args = new ListValue(); |
| if (focused_profile_ != NULL && previous_focused_profile != NULL && |
| focused_profile_ != previous_focused_profile) { |
| - ListValue none_args; |
| - none_args.Append( |
| + none_args->Append( |
| Value::CreateIntegerValue(extension_misc::kUnknownWindowId)); |
| - base::JSONWriter::Write(&none_args, &none_json_args); |
| } |
| DispatchEventsAcrossIncognito((focused_profile_ ? focused_profile_ : |
| previous_focused_profile), |
| events::kOnWindowFocusedChanged, |
| - real_json_args, |
| - none_json_args); |
| + real_args, |
| + none_args); |
| } |
| void ExtensionBrowserEventRouter::TabCreatedAt(WebContents* contents, |
| @@ -279,20 +272,17 @@ void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, |
| return; |
| } |
| - ListValue args; |
| - args.Append(Value::CreateIntegerValue(tab_id)); |
| + ListValue* args = new ListValue(); |
| + args->Append(Value::CreateIntegerValue(tab_id)); |
| DictionaryValue* object_args = new DictionaryValue(); |
| object_args->Set(tab_keys::kNewWindowIdKey, Value::CreateIntegerValue( |
| ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); |
| object_args->Set(tab_keys::kNewPositionKey, Value::CreateIntegerValue( |
| index)); |
| - args.Append(object_args); |
| - |
| - std::string json_args; |
| - base::JSONWriter::Write(&args, &json_args); |
| + args->Append(object_args); |
| - DispatchEvent(contents->profile(), events::kOnTabAttached, json_args); |
| + DispatchEvent(contents->profile(), events::kOnTabAttached, args); |
| } |
| void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, |
| @@ -302,8 +292,8 @@ void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, |
| return; |
| } |
| - ListValue args; |
| - args.Append(Value::CreateIntegerValue( |
| + ListValue* args = new ListValue(); |
| + args->Append(Value::CreateIntegerValue( |
| ExtensionTabUtil::GetTabId(contents->web_contents()))); |
| DictionaryValue* object_args = new DictionaryValue(); |
| @@ -311,12 +301,9 @@ void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, |
| ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); |
| object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( |
| index)); |
| - args.Append(object_args); |
| + args->Append(object_args); |
| - std::string json_args; |
| - base::JSONWriter::Write(&args, &json_args); |
| - |
| - DispatchEvent(contents->profile(), events::kOnTabDetached, json_args); |
| + DispatchEvent(contents->profile(), events::kOnTabDetached, args); |
| } |
| void ExtensionBrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, |
| @@ -324,18 +311,15 @@ void ExtensionBrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, |
| int index) { |
| int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); |
| - ListValue args; |
| - args.Append(Value::CreateIntegerValue(tab_id)); |
| + ListValue* args = new ListValue(); |
| + args->Append(Value::CreateIntegerValue(tab_id)); |
| DictionaryValue* object_args = new DictionaryValue(); |
| object_args->SetBoolean(tab_keys::kWindowClosing, |
| tab_strip_model->closing_all()); |
| - args.Append(object_args); |
| - |
| - std::string json_args; |
| - base::JSONWriter::Write(&args, &json_args); |
| + args->Append(object_args); |
| - DispatchEvent(contents->profile(), events::kOnTabRemoved, json_args); |
| + DispatchEvent(contents->profile(), events::kOnTabRemoved, args); |
| int removed_count = tab_entries_.erase(tab_id); |
| DCHECK_GT(removed_count, 0); |
| @@ -348,30 +332,24 @@ void ExtensionBrowserEventRouter::ActiveTabChanged( |
| TabContents* new_contents, |
| int index, |
| bool user_gesture) { |
| - ListValue args; |
| + ListValue* args = new ListValue(); |
| int tab_id = ExtensionTabUtil::GetTabId(new_contents->web_contents()); |
| - args.Append(Value::CreateIntegerValue(tab_id)); |
| + args->Append(Value::CreateIntegerValue(tab_id)); |
| DictionaryValue* object_args = new DictionaryValue(); |
| object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
| ExtensionTabUtil::GetWindowIdOfTab(new_contents->web_contents()))); |
| - args.Append(object_args); |
| + args->Append(object_args); |
| // The onActivated event replaced onActiveChanged and onSelectionChanged. The |
| // deprecated events take two arguments: tabId, {windowId}. |
| - std::string old_json_args; |
| - base::JSONWriter::Write(&args, &old_json_args); |
| + Profile* profile = new_contents->profile(); |
| + DispatchEvent(profile, events::kOnTabSelectionChanged, args->DeepCopy()); |
| + DispatchEvent(profile, events::kOnTabActiveChanged, args->DeepCopy()); |
| // The onActivated event takes one argument: {windowId, tabId}. |
| - std::string new_json_args; |
| - args.Remove(0, NULL); |
| - object_args->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); |
| - base::JSONWriter::Write(&args, &new_json_args); |
| - |
| - Profile* profile = new_contents->profile(); |
| - DispatchEvent(profile, events::kOnTabSelectionChanged, old_json_args); |
| - DispatchEvent(profile, events::kOnTabActiveChanged, old_json_args); |
| - DispatchEvent(profile, events::kOnTabActivated, new_json_args); |
| + args->Remove(0, NULL); |
| + DispatchEvent(profile, events::kOnTabActivated, args); |
| } |
| void ExtensionBrowserEventRouter::TabSelectionChanged( |
| @@ -390,29 +368,26 @@ void ExtensionBrowserEventRouter::TabSelectionChanged( |
| all->Append(Value::CreateIntegerValue(tab_id)); |
| } |
| - ListValue args; |
| + ListValue* args = new ListValue(); |
| DictionaryValue* select_info = new DictionaryValue(); |
| select_info->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
| ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); |
| select_info->Set(tab_keys::kTabIdsKey, all); |
| - args.Append(select_info); |
| - |
| - std::string json_args; |
| - base::JSONWriter::Write(&args, &json_args); |
| + args->Append(select_info); |
| // The onHighlighted event replaced onHighlightChanged. |
| Profile* profile = tab_strip_model->profile(); |
| - DispatchEvent(profile, events::kOnTabHighlightChanged, json_args); |
| - DispatchEvent(profile, events::kOnTabHighlighted, json_args); |
| + DispatchEvent(profile, events::kOnTabHighlightChanged, args->DeepCopy()); |
| + DispatchEvent(profile, events::kOnTabHighlighted, args); |
| } |
| void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, |
| int from_index, |
| int to_index) { |
| - ListValue args; |
| - args.Append(Value::CreateIntegerValue( |
| + ListValue* args = new ListValue(); |
| + args->Append(Value::CreateIntegerValue( |
| ExtensionTabUtil::GetTabId(contents->web_contents()))); |
| DictionaryValue* object_args = new DictionaryValue(); |
| @@ -422,12 +397,9 @@ void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, |
| from_index)); |
| object_args->Set(tab_keys::kToIndexKey, Value::CreateIntegerValue( |
| to_index)); |
| - args.Append(object_args); |
| - |
| - std::string json_args; |
| - base::JSONWriter::Write(&args, &json_args); |
| + args->Append(object_args); |
| - DispatchEvent(contents->profile(), events::kOnTabMoved, json_args); |
| + DispatchEvent(contents->profile(), events::kOnTabMoved, args); |
| } |
| void ExtensionBrowserEventRouter::TabUpdated(WebContents* contents, |
| @@ -448,36 +420,39 @@ void ExtensionBrowserEventRouter::TabUpdated(WebContents* contents, |
| void ExtensionBrowserEventRouter::DispatchEvent(Profile* profile, |
| const char* event_name, |
| - const std::string& json_args) { |
| - if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) |
| + base::ListValue* arguments) { |
| + if (!profile_->IsSameProfile(profile) || |
| + !profile->GetExtensionEventRouter()) { |
| + delete arguments; |
| return; |
| + } |
| profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| - event_name, json_args, profile, GURL(), extensions::EventFilteringInfo()); |
| + event_name, arguments, profile, GURL(), extensions::EventFilteringInfo()); |
| } |
| void ExtensionBrowserEventRouter::DispatchEventToExtension( |
| Profile* profile, |
| const std::string& extension_id, |
| const char* event_name, |
| - const std::string& json_args) { |
| + ListValue* event_args) { |
| if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) |
|
miket_OOO
2012/07/10 22:33:19
If this guy doesn't take ownership of args, it's c
|
| return; |
| profile->GetExtensionEventRouter()->DispatchEventToExtension( |
| - extension_id, event_name, json_args, profile, GURL()); |
| + extension_id, event_name, event_args, profile, GURL()); |
| } |
| void ExtensionBrowserEventRouter::DispatchEventsAcrossIncognito( |
| Profile* profile, |
| const char* event_name, |
| - const std::string& json_args, |
| - const std::string& cross_incognito_args) { |
| + ListValue* event_args, |
| + ListValue* cross_incognito_args) { |
| if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) |
|
miket_OOO
2012/07/10 22:33:19
Same comment about ownership/leak on return.
|
| return; |
| profile->GetExtensionEventRouter()->DispatchEventsToRenderersAcrossIncognito( |
| - event_name, json_args, profile, cross_incognito_args, GURL()); |
| + event_name, event_args, profile, cross_incognito_args, GURL()); |
| } |
| void ExtensionBrowserEventRouter::DispatchEventWithTab( |
| @@ -489,15 +464,13 @@ void ExtensionBrowserEventRouter::DispatchEventWithTab( |
| if (!profile_->IsSameProfile(profile)) |
| return; |
| - ListValue args; |
| - args.Append(ExtensionTabUtil::CreateTabValueActive( |
| + ListValue* args = new ListValue(); |
| + args->Append(ExtensionTabUtil::CreateTabValueActive( |
| web_contents, active)); |
| - std::string json_args; |
| - base::JSONWriter::Write(&args, &json_args); |
| if (!extension_id.empty()) { |
| - DispatchEventToExtension(profile, extension_id, event_name, json_args); |
| + DispatchEventToExtension(profile, extension_id, event_name, args); |
| } else { |
| - DispatchEvent(profile, event_name, json_args); |
| + DispatchEvent(profile, event_name, args); |
| } |
| } |
| @@ -506,13 +479,10 @@ void ExtensionBrowserEventRouter::DispatchSimpleBrowserEvent( |
| if (!profile_->IsSameProfile(profile)) |
| return; |
| - ListValue args; |
| - args.Append(Value::CreateIntegerValue(window_id)); |
| + ListValue* args = new ListValue(); |
| + args->Append(Value::CreateIntegerValue(window_id)); |
| - std::string json_args; |
| - base::JSONWriter::Write(&args, &json_args); |
| - |
| - DispatchEvent(profile, event_name, json_args); |
| + DispatchEvent(profile, event_name, args); |
| } |
| void ExtensionBrowserEventRouter::DispatchTabUpdatedEvent( |
| @@ -522,22 +492,19 @@ void ExtensionBrowserEventRouter::DispatchTabUpdatedEvent( |
| // The state of the tab (as seen from the extension point of view) has |
| // changed. Send a notification to the extension. |
| - ListValue args; |
| + ListValue* args = new ListValue(); |
| // First arg: The id of the tab that changed. |
| - args.Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); |
| + args->Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); |
| // Second arg: An object containing the changes to the tab state. |
| - args.Append(changed_properties); |
| + args->Append(changed_properties); |
| // Third arg: An object containing the state of the tab. |
| - args.Append(ExtensionTabUtil::CreateTabValue(contents)); |
| - |
| - std::string json_args; |
| - base::JSONWriter::Write(&args, &json_args); |
| + args->Append(ExtensionTabUtil::CreateTabValue(contents)); |
| Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
| - DispatchEvent(profile, events::kOnTabUpdated, json_args); |
| + DispatchEvent(profile, events::kOnTabUpdated, args); |
| } |
| ExtensionBrowserEventRouter::TabEntry* ExtensionBrowserEventRouter::GetTabEntry( |
| @@ -615,19 +582,16 @@ void ExtensionBrowserEventRouter::DispatchOldPageActionEvent( |
| int tab_id, |
| const std::string& url, |
| int button) { |
| - ListValue args; |
| - args.Append(Value::CreateStringValue(page_action_id)); |
| + ListValue* args = new ListValue(); |
| + args->Append(Value::CreateStringValue(page_action_id)); |
| DictionaryValue* data = new DictionaryValue(); |
| data->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); |
| data->Set(tab_keys::kTabUrlKey, Value::CreateStringValue(url)); |
| data->Set(page_action_keys::kButtonKey, Value::CreateIntegerValue(button)); |
| - args.Append(data); |
| - |
| - std::string json_args; |
| - base::JSONWriter::Write(&args, &json_args); |
| + args->Append(data); |
| - DispatchEventToExtension(profile, extension_id, "pageActions", json_args); |
| + DispatchEventToExtension(profile, extension_id, "pageActions", args); |
| } |
| void ExtensionBrowserEventRouter::BrowserActionExecuted( |
| @@ -673,15 +637,13 @@ void ExtensionBrowserEventRouter::CommandExecuted( |
| Profile* profile, |
| const std::string& extension_id, |
| const std::string& command) { |
| - ListValue args; |
| - args.Append(Value::CreateStringValue(command)); |
| - std::string json_args; |
| - base::JSONWriter::Write(&args, &json_args); |
| + ListValue* args = new ListValue(); |
| + args->Append(Value::CreateStringValue(command)); |
| DispatchEventToExtension(profile, |
| extension_id, |
| "experimental.keybinding.onCommand", |
| - json_args); |
| + args); |
| } |
| void ExtensionBrowserEventRouter::ExtensionActionExecuted( |