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

Side by Side Diff: chrome/browser/extensions/api/tabs/tabs_event_router.cc

Issue 987583004: Add audible, muted to Tab, c.t.query, c.t.update, and c.t.onUpdated where relevant (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@patch1
Patch Set: Make TabsEventRouter a friend to TabEntry so that it will build on some platforms Created 5 years, 6 months 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/api/tabs/tabs_event_router.h" 5 #include "chrome/browser/extensions/api/tabs/tabs_event_router.h"
6 6
7 #include "base/command_line.h"
7 #include "base/json/json_writer.h" 8 #include "base/json/json_writer.h"
8 #include "base/values.h" 9 #include "base/values.h"
9 #include "chrome/browser/chrome_notification_types.h" 10 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" 11 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
11 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h" 12 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h"
12 #include "chrome/browser/extensions/api/tabs/windows_event_router.h" 13 #include "chrome/browser/extensions/api/tabs/windows_event_router.h"
13 #include "chrome/browser/extensions/extension_tab_util.h" 14 #include "chrome/browser/extensions/extension_tab_util.h"
14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h" 16 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_iterator.h" 17 #include "chrome/browser/ui/browser_iterator.h"
17 #include "chrome/browser/ui/browser_list.h" 18 #include "chrome/browser/ui/browser_list.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" 19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/browser/ui/tabs/tab_utils.h"
21 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/extensions/extension_constants.h" 22 #include "chrome/common/extensions/extension_constants.h"
20 #include "components/favicon/content/content_favicon_driver.h" 23 #include "components/favicon/content/content_favicon_driver.h"
21 #include "content/public/browser/favicon_status.h" 24 #include "content/public/browser/favicon_status.h"
22 #include "content/public/browser/navigation_controller.h" 25 #include "content/public/browser/navigation_controller.h"
23 #include "content/public/browser/navigation_entry.h" 26 #include "content/public/browser/navigation_entry.h"
24 #include "content/public/browser/notification_service.h" 27 #include "content/public/browser/notification_service.h"
25 #include "content/public/browser/notification_types.h" 28 #include "content/public/browser/notification_types.h"
26 #include "content/public/browser/web_contents.h" 29 #include "content/public/browser/web_contents.h"
27 30
28 using base::DictionaryValue; 31 using base::DictionaryValue;
(...skipping 23 matching lines...) Expand all
52 properties_value); 55 properties_value);
53 event_args->Set(1, properties_value); 56 event_args->Set(1, properties_value);
54 57
55 // Overwrite the third arg with our tab value as seen by this extension. 58 // Overwrite the third arg with our tab value as seen by this extension.
56 event_args->Set(2, ExtensionTabUtil::CreateTabValue(contents, extension)); 59 event_args->Set(2, ExtensionTabUtil::CreateTabValue(contents, extension));
57 return true; 60 return true;
58 } 61 }
59 62
60 } // namespace 63 } // namespace
61 64
62 TabsEventRouter::TabEntry::TabEntry() : complete_waiting_on_load_(false), 65 TabsEventRouter::TabEntry::TabEntry()
63 url_() { 66 : contents_(NULL),
67 complete_waiting_on_load_(false),
68 was_audible_(false),
69 was_muted_(false) {
64 } 70 }
65 71
66 base::DictionaryValue* TabsEventRouter::TabEntry::UpdateLoadState( 72 TabsEventRouter::TabEntry::TabEntry(content::WebContents* contents)
67 const WebContents* contents) { 73 : contents_(contents),
74 complete_waiting_on_load_(false),
75 was_audible_(contents->WasRecentlyAudible()),
76 was_muted_(contents->IsAudioMuted()) {
77 }
78
79 scoped_ptr<base::DictionaryValue> TabsEventRouter::TabEntry::UpdateLoadState() {
68 // The tab may go in & out of loading (for instance if iframes navigate). 80 // The tab may go in & out of loading (for instance if iframes navigate).
69 // We only want to respond to the first change from loading to !loading after 81 // We only want to respond to the first change from loading to !loading after
70 // the NAV_ENTRY_COMMITTED was fired. 82 // the NAV_ENTRY_COMMITTED was fired.
71 if (!complete_waiting_on_load_ || contents->IsLoading()) 83 scoped_ptr<base::DictionaryValue> changed_properties(
72 return NULL; 84 new base::DictionaryValue());
85 if (!complete_waiting_on_load_ || contents_->IsLoading()) {
86 return changed_properties.Pass();
87 }
73 88
74 // Send "complete" state change. 89 // Send "complete" state change.
75 complete_waiting_on_load_ = false; 90 complete_waiting_on_load_ = false;
76 base::DictionaryValue* changed_properties = new base::DictionaryValue();
77 changed_properties->SetString(tabs_constants::kStatusKey, 91 changed_properties->SetString(tabs_constants::kStatusKey,
78 tabs_constants::kStatusValueComplete); 92 tabs_constants::kStatusValueComplete);
79 return changed_properties; 93 return changed_properties.Pass();
80 } 94 }
81 95
82 base::DictionaryValue* TabsEventRouter::TabEntry::DidNavigate( 96 scoped_ptr<base::DictionaryValue> TabsEventRouter::TabEntry::DidNavigate() {
83 const WebContents* contents) {
84 // Send "loading" state change. 97 // Send "loading" state change.
85 complete_waiting_on_load_ = true; 98 complete_waiting_on_load_ = true;
86 base::DictionaryValue* changed_properties = new base::DictionaryValue(); 99 scoped_ptr<base::DictionaryValue> changed_properties(
100 new base::DictionaryValue());
87 changed_properties->SetString(tabs_constants::kStatusKey, 101 changed_properties->SetString(tabs_constants::kStatusKey,
88 tabs_constants::kStatusValueLoading); 102 tabs_constants::kStatusValueLoading);
89 103
90 if (contents->GetURL() != url_) { 104 if (contents_->GetURL() != url_) {
91 url_ = contents->GetURL(); 105 url_ = contents_->GetURL();
92 changed_properties->SetString(tabs_constants::kUrlKey, url_.spec()); 106 changed_properties->SetString(tabs_constants::kUrlKey, url_.spec());
93 } 107 }
94 108
95 return changed_properties; 109 return changed_properties.Pass();
110 }
111
112 bool TabsEventRouter::TabEntry::SetAudible(bool new_val) {
113 if (was_audible_ == new_val)
114 return false;
115 was_audible_ = new_val;
116 return true;
117 }
118
119 bool TabsEventRouter::TabEntry::SetMuted(bool new_val) {
120 if (was_muted_ == new_val)
121 return false;
122 was_muted_ = new_val;
123 return true;
96 } 124 }
97 125
98 TabsEventRouter::TabsEventRouter(Profile* profile) 126 TabsEventRouter::TabsEventRouter(Profile* profile)
99 : profile_(profile), favicon_scoped_observer_(this) { 127 : profile_(profile), favicon_scoped_observer_(this) {
100 DCHECK(!profile->IsOffTheRecord()); 128 DCHECK(!profile->IsOffTheRecord());
101 129
102 BrowserList::AddObserver(this); 130 BrowserList::AddObserver(this);
103 131
104 // Init() can happen after the browser is running, so catch up with any 132 // Init() can happen after the browser is running, so catch up with any
105 // windows that already exist. 133 // windows that already exist.
106 for (chrome::BrowserIterator it; !it.done(); it.Next()) { 134 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
107 RegisterForBrowserNotifications(*it); 135 RegisterForBrowserNotifications(*it);
108 136
109 // Also catch up our internal bookkeeping of tab entries. 137 // Also catch up our internal bookkeeping of tab entries.
110 Browser* browser = *it; 138 Browser* browser = *it;
111 if (browser->tab_strip_model()) { 139 if (browser->tab_strip_model()) {
112 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { 140 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) {
113 WebContents* contents = browser->tab_strip_model()->GetWebContentsAt(i); 141 WebContents* contents = browser->tab_strip_model()->GetWebContentsAt(i);
114 int tab_id = ExtensionTabUtil::GetTabId(contents); 142 int tab_id = ExtensionTabUtil::GetTabId(contents);
115 tab_entries_[tab_id] = TabEntry(); 143 tab_entries_[tab_id] = TabEntry(contents);
116 } 144 }
117 } 145 }
118 } 146 }
119 } 147 }
120 148
121 TabsEventRouter::~TabsEventRouter() { 149 TabsEventRouter::~TabsEventRouter() {
122 BrowserList::RemoveObserver(this); 150 BrowserList::RemoveObserver(this);
123 } 151 }
124 152
125 void TabsEventRouter::OnBrowserAdded(Browser* browser) { 153 void TabsEventRouter::OnBrowserAdded(Browser* browser) {
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 238
211 RegisterForTabNotifications(contents); 239 RegisterForTabNotifications(contents);
212 } 240 }
213 241
214 void TabsEventRouter::TabInsertedAt(WebContents* contents, 242 void TabsEventRouter::TabInsertedAt(WebContents* contents,
215 int index, 243 int index,
216 bool active) { 244 bool active) {
217 // If tab is new, send created event. 245 // If tab is new, send created event.
218 int tab_id = ExtensionTabUtil::GetTabId(contents); 246 int tab_id = ExtensionTabUtil::GetTabId(contents);
219 if (!GetTabEntry(contents)) { 247 if (!GetTabEntry(contents)) {
220 tab_entries_[tab_id] = TabEntry(); 248 tab_entries_[tab_id] = TabEntry(contents);
221 249
222 TabCreatedAt(contents, index, active); 250 TabCreatedAt(contents, index, active);
223 return; 251 return;
224 } 252 }
225 253
226 scoped_ptr<base::ListValue> args(new base::ListValue); 254 scoped_ptr<base::ListValue> args(new base::ListValue);
227 args->Append(new FundamentalValue(tab_id)); 255 args->Append(new FundamentalValue(tab_id));
228 256
229 base::DictionaryValue* object_args = new base::DictionaryValue(); 257 base::DictionaryValue* object_args = new base::DictionaryValue();
230 object_args->Set(tabs_constants::kNewWindowIdKey, 258 object_args->Set(tabs_constants::kNewWindowIdKey,
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 new FundamentalValue(to_index)); 413 new FundamentalValue(to_index));
386 args->Append(object_args); 414 args->Append(object_args);
387 415
388 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); 416 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
389 DispatchEvent(profile, 417 DispatchEvent(profile,
390 tabs::OnMoved::kEventName, 418 tabs::OnMoved::kEventName,
391 args.Pass(), 419 args.Pass(),
392 EventRouter::USER_GESTURE_UNKNOWN); 420 EventRouter::USER_GESTURE_UNKNOWN);
393 } 421 }
394 422
395 void TabsEventRouter::TabUpdated(WebContents* contents, bool did_navigate) { 423 void TabsEventRouter::TabUpdated(
396 TabEntry* entry = GetTabEntry(contents); 424 TabsEventRouter::TabEntry* entry,
397 scoped_ptr<base::DictionaryValue> changed_properties; 425 scoped_ptr<base::DictionaryValue> changed_properties) {
426 CHECK(entry->web_contents());
398 427
399 if (!entry) 428 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
400 return; 429 switches::kEnableTabAudioMuting)) {
430 bool audible = entry->web_contents()->WasRecentlyAudible();
431 if (entry->SetAudible(audible)) {
432 changed_properties->SetBoolean(tabs_constants::kAudibleKey, audible);
433 }
401 434
402 if (did_navigate) 435 bool muted = entry->web_contents()->IsAudioMuted();
403 changed_properties.reset(entry->DidNavigate(contents)); 436 if (entry->SetMuted(muted)) {
404 else 437 changed_properties->SetBoolean(tabs_constants::kMutedKey, muted);
405 changed_properties.reset(entry->UpdateLoadState(contents)); 438 changed_properties->SetString(
439 tabs_constants::kMutedCauseKey,
440 chrome::GetTabAudioMutedCause(entry->web_contents()));
441 }
442 }
406 443
407 if (changed_properties) 444 if (!changed_properties->empty()) {
408 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); 445 DispatchTabUpdatedEvent(entry->web_contents(), changed_properties.Pass());
446 }
409 } 447 }
410 448
411 void TabsEventRouter::FaviconUrlUpdated(WebContents* contents) { 449 void TabsEventRouter::FaviconUrlUpdated(WebContents* contents) {
412 content::NavigationEntry* entry = 450 content::NavigationEntry* entry =
413 contents->GetController().GetVisibleEntry(); 451 contents->GetController().GetVisibleEntry();
414 if (!entry || !entry->GetFavicon().valid) 452 if (!entry || !entry->GetFavicon().valid)
415 return; 453 return;
416 scoped_ptr<base::DictionaryValue> changed_properties( 454 scoped_ptr<base::DictionaryValue> changed_properties(
417 new base::DictionaryValue); 455 new base::DictionaryValue);
418 changed_properties->SetString( 456 changed_properties->SetString(
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 return NULL; 527 return NULL;
490 return &i->second; 528 return &i->second;
491 } 529 }
492 530
493 void TabsEventRouter::Observe(int type, 531 void TabsEventRouter::Observe(int type,
494 const content::NotificationSource& source, 532 const content::NotificationSource& source,
495 const content::NotificationDetails& details) { 533 const content::NotificationDetails& details) {
496 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { 534 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) {
497 NavigationController* source_controller = 535 NavigationController* source_controller =
498 content::Source<NavigationController>(source).ptr(); 536 content::Source<NavigationController>(source).ptr();
499 TabUpdated(source_controller->GetWebContents(), true); 537 TabEntry* entry = GetTabEntry(source_controller->GetWebContents());
538 CHECK(entry);
539 TabUpdated(entry, entry->DidNavigate());
500 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { 540 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) {
501 // Tab was destroyed after being detached (without being re-attached). 541 // Tab was destroyed after being detached (without being re-attached).
502 WebContents* contents = content::Source<WebContents>(source).ptr(); 542 WebContents* contents = content::Source<WebContents>(source).ptr();
503 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 543 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
504 content::Source<NavigationController>(&contents->GetController())); 544 content::Source<NavigationController>(&contents->GetController()));
505 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 545 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
506 content::Source<WebContents>(contents)); 546 content::Source<WebContents>(contents));
507 favicon_scoped_observer_.Remove( 547 favicon_scoped_observer_.Remove(
508 favicon::ContentFaviconDriver::FromWebContents(contents)); 548 favicon::ContentFaviconDriver::FromWebContents(contents));
509 } else { 549 } else {
510 NOTREACHED(); 550 NOTREACHED();
511 } 551 }
512 } 552 }
513 553
514 void TabsEventRouter::TabChangedAt(WebContents* contents, 554 void TabsEventRouter::TabChangedAt(WebContents* contents,
515 int index, 555 int index,
516 TabChangeType change_type) { 556 TabChangeType change_type) {
517 TabUpdated(contents, false); 557 TabEntry* entry = GetTabEntry(contents);
558 CHECK(entry);
559 TabUpdated(entry, entry->UpdateLoadState());
518 } 560 }
519 561
520 void TabsEventRouter::TabReplacedAt(TabStripModel* tab_strip_model, 562 void TabsEventRouter::TabReplacedAt(TabStripModel* tab_strip_model,
521 WebContents* old_contents, 563 WebContents* old_contents,
522 WebContents* new_contents, 564 WebContents* new_contents,
523 int index) { 565 int index) {
524 // Notify listeners that the next tabs closing or being added are due to 566 // Notify listeners that the next tabs closing or being added are due to
525 // WebContents being swapped. 567 // WebContents being swapped.
526 const int new_tab_id = ExtensionTabUtil::GetTabId(new_contents); 568 const int new_tab_id = ExtensionTabUtil::GetTabId(new_contents);
527 const int old_tab_id = ExtensionTabUtil::GetTabId(old_contents); 569 const int old_tab_id = ExtensionTabUtil::GetTabId(old_contents);
528 scoped_ptr<base::ListValue> args(new base::ListValue); 570 scoped_ptr<base::ListValue> args(new base::ListValue);
529 args->Append(new FundamentalValue(new_tab_id)); 571 args->Append(new FundamentalValue(new_tab_id));
530 args->Append(new FundamentalValue(old_tab_id)); 572 args->Append(new FundamentalValue(old_tab_id));
531 573
532 DispatchEvent(Profile::FromBrowserContext(new_contents->GetBrowserContext()), 574 DispatchEvent(Profile::FromBrowserContext(new_contents->GetBrowserContext()),
533 tabs::OnReplaced::kEventName, 575 tabs::OnReplaced::kEventName,
534 args.Pass(), 576 args.Pass(),
535 EventRouter::USER_GESTURE_UNKNOWN); 577 EventRouter::USER_GESTURE_UNKNOWN);
536 578
537 // Update tab_entries_. 579 // Update tab_entries_.
538 const int removed_count = tab_entries_.erase(old_tab_id); 580 const int removed_count = tab_entries_.erase(old_tab_id);
539 DCHECK_GT(removed_count, 0); 581 DCHECK_GT(removed_count, 0);
540 UnregisterForTabNotifications(old_contents); 582 UnregisterForTabNotifications(old_contents);
541 583
542 if (!GetTabEntry(new_contents)) { 584 if (!GetTabEntry(new_contents)) {
543 tab_entries_[new_tab_id] = TabEntry(); 585 tab_entries_[new_tab_id] = TabEntry(new_contents);
544 RegisterForTabNotifications(new_contents); 586 RegisterForTabNotifications(new_contents);
545 } 587 }
546 } 588 }
547 589
548 void TabsEventRouter::TabPinnedStateChanged(WebContents* contents, int index) { 590 void TabsEventRouter::TabPinnedStateChanged(WebContents* contents, int index) {
549 TabStripModel* tab_strip = NULL; 591 TabStripModel* tab_strip = NULL;
550 int tab_index; 592 int tab_index;
551 593
552 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { 594 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) {
553 scoped_ptr<base::DictionaryValue> changed_properties( 595 scoped_ptr<base::DictionaryValue> changed_properties(
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 void TabsEventRouter::OnFaviconUpdated(favicon::FaviconDriver* favicon_driver, 632 void TabsEventRouter::OnFaviconUpdated(favicon::FaviconDriver* favicon_driver,
591 bool icon_url_changed) { 633 bool icon_url_changed) {
592 if (icon_url_changed) { 634 if (icon_url_changed) {
593 favicon::ContentFaviconDriver* content_favicon_driver = 635 favicon::ContentFaviconDriver* content_favicon_driver =
594 static_cast<favicon::ContentFaviconDriver*>(favicon_driver); 636 static_cast<favicon::ContentFaviconDriver*>(favicon_driver);
595 FaviconUrlUpdated(content_favicon_driver->web_contents()); 637 FaviconUrlUpdated(content_favicon_driver->web_contents());
596 } 638 }
597 } 639 }
598 640
599 } // namespace extensions 641 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698