Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: chrome/browser/extensions/extension_browser_event_router.cc

Issue 346006: New event style for pageAction API.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/extension_browser_event_router.h" 5 #include "chrome/browser/extensions/extension_browser_event_router.h"
6 6
7 #include "base/json/json_writer.h" 7 #include "base/json/json_writer.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/browser/browser.h" 9 #include "chrome/browser/browser.h"
10 #include "chrome/browser/profile.h" 10 #include "chrome/browser/profile.h"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 86
87 static void DispatchEvent(Profile* profile, 87 static void DispatchEvent(Profile* profile,
88 const char* event_name, 88 const char* event_name,
89 const std::string json_args) { 89 const std::string json_args) {
90 if (profile->GetExtensionMessageService()) { 90 if (profile->GetExtensionMessageService()) {
91 profile->GetExtensionMessageService()-> 91 profile->GetExtensionMessageService()->
92 DispatchEventToRenderers(event_name, json_args); 92 DispatchEventToRenderers(event_name, json_args);
93 } 93 }
94 } 94 }
95 95
96 static void DispatchEventWithTab(Profile* profile,
97 const char* event_name,
98 const TabContents* tab_contents) {
99 ListValue args;
100 args.Append(ExtensionTabUtil::CreateTabValue(tab_contents));
101 std::string json_args;
102 base::JSONWriter::Write(&args, false, &json_args);
103 DispatchEvent(profile, event_name, json_args);
104 }
105
96 static void DispatchSimpleBrowserEvent(Profile* profile, 106 static void DispatchSimpleBrowserEvent(Profile* profile,
97 const int window_id, 107 const int window_id,
98 const char* event_name) { 108 const char* event_name) {
99 ListValue args; 109 ListValue args;
100 args.Append(Value::CreateIntegerValue(window_id)); 110 args.Append(Value::CreateIntegerValue(window_id));
101 111
102 std::string json_args; 112 std::string json_args;
103 base::JSONWriter::Write(&args, false, &json_args); 113 base::JSONWriter::Write(&args, false, &json_args);
104 114
105 DispatchEvent(profile, event_name, json_args); 115 DispatchEvent(profile, event_name, json_args);
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 void ExtensionBrowserEventRouter::OnBrowserSetLastActive( 207 void ExtensionBrowserEventRouter::OnBrowserSetLastActive(
198 const Browser* browser) { 208 const Browser* browser) {
199 DispatchSimpleBrowserEvent(browser->profile(), 209 DispatchSimpleBrowserEvent(browser->profile(),
200 ExtensionTabUtil::GetWindowId(browser), 210 ExtensionTabUtil::GetWindowId(browser),
201 events::kOnWindowFocusedChanged); 211 events::kOnWindowFocusedChanged);
202 } 212 }
203 213
204 void ExtensionBrowserEventRouter::TabCreatedAt(TabContents* contents, 214 void ExtensionBrowserEventRouter::TabCreatedAt(TabContents* contents,
205 int index, 215 int index,
206 bool foreground) { 216 bool foreground) {
207 ListValue args; 217 DispatchEventWithTab(contents->profile(), events::kOnTabCreated, contents);
208 args.Append(ExtensionTabUtil::CreateTabValue(contents));
209 std::string json_args;
210 base::JSONWriter::Write(&args, false, &json_args);
211
212 DispatchEvent(contents->profile(), events::kOnTabCreated, json_args);
213 218
214 RegisterForTabNotifications(contents); 219 RegisterForTabNotifications(contents);
215 } 220 }
216 221
217 void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, 222 void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents,
218 int index, 223 int index,
219 bool foreground) { 224 bool foreground) {
220 // If tab is new, send created event. 225 // If tab is new, send created event.
221 int tab_id = ExtensionTabUtil::GetTabId(contents); 226 int tab_id = ExtensionTabUtil::GetTabId(contents);
222 if (tab_entries_.find(tab_id) == tab_entries_.end()) { 227 if (tab_entries_.find(tab_id) == tab_entries_.end()) {
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 } 391 }
387 392
388 void ExtensionBrowserEventRouter::TabChangedAt(TabContents* contents, 393 void ExtensionBrowserEventRouter::TabChangedAt(TabContents* contents,
389 int index, 394 int index,
390 TabChangeType change_type) { 395 TabChangeType change_type) {
391 TabUpdated(contents, false); 396 TabUpdated(contents, false);
392 } 397 }
393 398
394 void ExtensionBrowserEventRouter::TabStripEmpty() { } 399 void ExtensionBrowserEventRouter::TabStripEmpty() { }
395 400
396 void ExtensionBrowserEventRouter::PageActionExecuted( 401 void ExtensionBrowserEventRouter::DispatchOldPageActionEvent(
397 Profile* profile, 402 Profile* profile,
398 const std::string& extension_id, 403 const std::string& extension_id,
399 const std::string& page_action_id, 404 const std::string& page_action_id,
400 int tab_id, 405 int tab_id,
401 const std::string& url, 406 const std::string& url,
402 int button) { 407 int button) {
403 ListValue args; 408 ListValue args;
404 args.Append(Value::CreateStringValue(page_action_id)); 409 args.Append(Value::CreateStringValue(page_action_id));
405 410
406 DictionaryValue* data = new DictionaryValue(); 411 DictionaryValue* data = new DictionaryValue();
407 data->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); 412 data->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id));
408 data->Set(tab_keys::kTabUrlKey, Value::CreateStringValue(url)); 413 data->Set(tab_keys::kTabUrlKey, Value::CreateStringValue(url));
409 data->Set(page_action_keys::kButtonKey, Value::CreateIntegerValue(button)); 414 data->Set(page_action_keys::kButtonKey, Value::CreateIntegerValue(button));
410 args.Append(data); 415 args.Append(data);
411 416
412 std::string json_args; 417 std::string json_args;
413 base::JSONWriter::Write(&args, false, &json_args); 418 base::JSONWriter::Write(&args, false, &json_args);
414 419
420 std::string event_name = std::string("pageActions/") + extension_id;
421 DispatchEvent(profile, event_name.c_str(), json_args);
422 }
423
424 void ExtensionBrowserEventRouter::PageActionExecuted(
425 Profile* profile,
426 const std::string& extension_id,
427 const std::string& page_action_id,
428 int tab_id,
429 const std::string& url,
430 int button) {
431 DispatchOldPageActionEvent(profile, extension_id, page_action_id, tab_id, url,
432 button);
433 TabContents* tab_contents = NULL;
434 if (!ExtensionTabUtil::GetTabById(tab_id, profile, NULL, NULL, &tab_contents,
435 NULL)) {
436 return;
437 }
415 std::string event_name = std::string("pageAction/") + extension_id; 438 std::string event_name = std::string("pageAction/") + extension_id;
416 DispatchEvent(profile, event_name.c_str(), json_args); 439 DispatchEventWithTab(profile, event_name.c_str(), tab_contents);
417 } 440 }
418 441
419 void ExtensionBrowserEventRouter::BrowserActionExecuted( 442 void ExtensionBrowserEventRouter::BrowserActionExecuted(
420 Profile* profile, const std::string& extension_id, Browser* browser) { 443 Profile* profile, const std::string& extension_id, Browser* browser) {
421 TabContents* tab_contents = NULL; 444 TabContents* tab_contents = NULL;
422 int tab_id = 0; 445 int tab_id = 0;
423 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) 446 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id))
424 return; 447 return;
425
426 ListValue args;
427 args.Append(ExtensionTabUtil::CreateTabValue(tab_contents));
428 std::string json_args;
429 base::JSONWriter::Write(&args, false, &json_args);
430
431 std::string event_name = std::string("browserAction/") + extension_id; 448 std::string event_name = std::string("browserAction/") + extension_id;
432 DispatchEvent(profile, event_name.c_str(), json_args); 449 DispatchEventWithTab(profile, event_name.c_str(), tab_contents);
433 } 450 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_browser_event_router.h ('k') | chrome/browser/extensions/page_action_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698