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

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

Issue 1156043002: chrome.tabs.onUpdated doesn't called with 'loading' twice. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 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/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/chrome_notification_types.h" 9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" 10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
11 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h" 11 #include "chrome/browser/extensions/api/tabs/tabs_windows_api.h"
12 #include "chrome/browser/extensions/api/tabs/windows_event_router.h" 12 #include "chrome/browser/extensions/api/tabs/windows_event_router.h"
13 #include "chrome/browser/extensions/extension_tab_util.h" 13 #include "chrome/browser/extensions/extension_tab_util.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_iterator.h" 16 #include "chrome/browser/ui/browser_iterator.h"
17 #include "chrome/browser/ui/browser_list.h" 17 #include "chrome/browser/ui/browser_list.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" 18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/extensions/extension_constants.h" 19 #include "chrome/common/extensions/extension_constants.h"
20 #include "components/favicon/content/content_favicon_driver.h" 20 #include "components/favicon/content/content_favicon_driver.h"
21 #include "content/public/browser/favicon_status.h" 21 #include "content/public/browser/favicon_status.h"
22 #include "content/public/browser/navigation_controller.h" 22 #include "content/public/browser/navigation_controller.h"
23 #include "content/public/browser/navigation_details.h"
23 #include "content/public/browser/navigation_entry.h" 24 #include "content/public/browser/navigation_entry.h"
24 #include "content/public/browser/notification_service.h" 25 #include "content/public/browser/notification_service.h"
25 #include "content/public/browser/notification_types.h" 26 #include "content/public/browser/notification_types.h"
26 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
27 28
28 using base::DictionaryValue; 29 using base::DictionaryValue;
29 using base::ListValue; 30 using base::ListValue;
30 using base::FundamentalValue; 31 using base::FundamentalValue;
31 using content::NavigationController; 32 using content::NavigationController;
32 using content::WebContents; 33 using content::WebContents;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 tabs_constants::kStatusValueLoading); 89 tabs_constants::kStatusValueLoading);
89 90
90 if (contents->GetURL() != url_) { 91 if (contents->GetURL() != url_) {
91 url_ = contents->GetURL(); 92 url_ = contents->GetURL();
92 changed_properties->SetString(tabs_constants::kUrlKey, url_.spec()); 93 changed_properties->SetString(tabs_constants::kUrlKey, url_.spec());
93 } 94 }
94 95
95 return changed_properties; 96 return changed_properties;
96 } 97 }
97 98
99 base::DictionaryValue* TabsEventRouter::TabEntry::UpdateUrl(
100 const WebContents* contents) {
101 if (contents->GetURL() == url_)
102 return nullptr;
103
104 url_ = contents->GetURL();
105
106 // Send updated URL only.
107 base::DictionaryValue* changed_properties = new base::DictionaryValue();
not at google - send to devlin 2015/07/01 15:57:15 To imply type-safe pointer ownership transfer to t
limasdf 2015/07/06 17:14:44 Done. Yeah they should be.
108 changed_properties->SetString(tabs_constants::kUrlKey, url_.spec());
109
110 return changed_properties;
111 }
112
98 TabsEventRouter::TabsEventRouter(Profile* profile) 113 TabsEventRouter::TabsEventRouter(Profile* profile)
99 : profile_(profile), favicon_scoped_observer_(this) { 114 : profile_(profile), favicon_scoped_observer_(this) {
100 DCHECK(!profile->IsOffTheRecord()); 115 DCHECK(!profile->IsOffTheRecord());
101 116
102 BrowserList::AddObserver(this); 117 BrowserList::AddObserver(this);
103 118
104 // Init() can happen after the browser is running, so catch up with any 119 // Init() can happen after the browser is running, so catch up with any
105 // windows that already exist. 120 // windows that already exist.
106 for (chrome::BrowserIterator it; !it.done(); it.Next()) { 121 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
107 RegisterForBrowserNotifications(*it); 122 RegisterForBrowserNotifications(*it);
(...skipping 24 matching lines...) Expand all
132 // Start listening to TabStripModel events for this browser. 147 // Start listening to TabStripModel events for this browser.
133 TabStripModel* tab_strip = browser->tab_strip_model(); 148 TabStripModel* tab_strip = browser->tab_strip_model();
134 tab_strip->AddObserver(this); 149 tab_strip->AddObserver(this);
135 150
136 for (int i = 0; i < tab_strip->count(); ++i) { 151 for (int i = 0; i < tab_strip->count(); ++i) {
137 RegisterForTabNotifications(tab_strip->GetWebContentsAt(i)); 152 RegisterForTabNotifications(tab_strip->GetWebContentsAt(i));
138 } 153 }
139 } 154 }
140 155
141 void TabsEventRouter::RegisterForTabNotifications(WebContents* contents) { 156 void TabsEventRouter::RegisterForTabNotifications(WebContents* contents) {
157 // TODO(limasdf): Attach WebContentsObservers to |contents| rather than
158 // registering for notifications.
not at google - send to devlin 2015/07/01 15:57:15 Typically we don't indent comments like this.
limasdf 2015/07/06 17:14:44 Done.
142 registrar_.Add( 159 registrar_.Add(
143 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 160 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
144 content::Source<NavigationController>(&contents->GetController())); 161 content::Source<NavigationController>(&contents->GetController()));
145 162
146 // Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's 163 // Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's
147 // possible for tabs to be created, detached and then destroyed without 164 // possible for tabs to be created, detached and then destroyed without
148 // ever having been re-attached and closed. This happens in the case of 165 // ever having been re-attached and closed. This happens in the case of
149 // a devtools WebContents that is opened in window, docked, then closed. 166 // a devtools WebContents that is opened in window, docked, then closed.
150 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 167 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
151 content::Source<WebContents>(contents)); 168 content::Source<WebContents>(contents));
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 new FundamentalValue(to_index)); 402 new FundamentalValue(to_index));
386 args->Append(object_args); 403 args->Append(object_args);
387 404
388 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); 405 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
389 DispatchEvent(profile, 406 DispatchEvent(profile,
390 tabs::OnMoved::kEventName, 407 tabs::OnMoved::kEventName,
391 args.Pass(), 408 args.Pass(),
392 EventRouter::USER_GESTURE_UNKNOWN); 409 EventRouter::USER_GESTURE_UNKNOWN);
393 } 410 }
394 411
395 void TabsEventRouter::TabUpdated(WebContents* contents, bool did_navigate) { 412 void TabsEventRouter::TabUpdated(WebContents* contents,
413 api::tabs::TabStatus status) {
396 TabEntry* entry = GetTabEntry(contents); 414 TabEntry* entry = GetTabEntry(contents);
397 scoped_ptr<base::DictionaryValue> changed_properties; 415 scoped_ptr<base::DictionaryValue> changed_properties;
398 416
399 if (!entry) 417 if (!entry)
400 return; 418 return;
401 419
402 if (did_navigate) 420 switch (status) {
403 changed_properties.reset(entry->DidNavigate(contents)); 421 case api::tabs::TAB_STATUS_LOADING:
404 else 422 changed_properties.reset(entry->DidNavigate(contents));
not at google - send to devlin 2015/07/01 15:57:15 and then, following on from my comment above, thes
limasdf 2015/07/06 17:14:44 Done.
405 changed_properties.reset(entry->UpdateLoadState(contents)); 423 break;
424 case api::tabs::TAB_STATUS_COMPLETE:
425 changed_properties.reset(entry->UpdateLoadState(contents));
426 break;
427 case api::tabs::TAB_STATUS_NONE:
428 changed_properties.reset(entry->UpdateUrl(contents));
not at google - send to devlin 2015/07/01 15:57:15 It's actually not clear to me - or it wouldn't be
limasdf 2015/07/06 17:14:44 Agree. I tried to have "Update" method instead of
not at google - send to devlin 2015/07/06 19:33:19 Why does that make it impossible?
limasdf 2015/07/21 18:28:54 I tried to make single "Update" method(See patch s
429 break;
430 default:
431 NOTREACHED();
432 }
406 433
407 if (changed_properties) 434 if (changed_properties)
408 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); 435 DispatchTabUpdatedEvent(contents, changed_properties.Pass());
409 } 436 }
410 437
411 void TabsEventRouter::FaviconUrlUpdated(WebContents* contents) { 438 void TabsEventRouter::FaviconUrlUpdated(WebContents* contents) {
412 content::NavigationEntry* entry = 439 content::NavigationEntry* entry =
413 contents->GetController().GetVisibleEntry(); 440 contents->GetController().GetVisibleEntry();
414 if (!entry || !entry->GetFavicon().valid) 441 if (!entry || !entry->GetFavicon().valid)
415 return; 442 return;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); 514 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id);
488 if (tab_entries_.end() == i) 515 if (tab_entries_.end() == i)
489 return NULL; 516 return NULL;
490 return &i->second; 517 return &i->second;
491 } 518 }
492 519
493 void TabsEventRouter::Observe(int type, 520 void TabsEventRouter::Observe(int type,
494 const content::NotificationSource& source, 521 const content::NotificationSource& source,
495 const content::NotificationDetails& details) { 522 const content::NotificationDetails& details) {
496 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { 523 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) {
524 content::LoadCommittedDetails* commit =
525 content::Details<content::LoadCommittedDetails>(details).ptr();
497 NavigationController* source_controller = 526 NavigationController* source_controller =
498 content::Source<NavigationController>(source).ptr(); 527 content::Source<NavigationController>(source).ptr();
499 TabUpdated(source_controller->GetWebContents(), true); 528 if (commit->type == content::NavigationType::NAVIGATION_TYPE_IN_PAGE) {
not at google - send to devlin 2015/07/01 15:57:15 It may be simpler to just do: if (commit->is_in_p
529 // 'History.ReplaceState()' falls here. Just update url only.
not at google - send to devlin 2015/07/01 15:57:15 It's not just that, it's also hash fragment naviga
530 TabUpdated(source_controller->GetWebContents(),
531 api::tabs::TAB_STATUS_NONE);
532 } else {
533 TabUpdated(source_controller->GetWebContents(),
534 api::tabs::TAB_STATUS_LOADING);
535 }
500 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { 536 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) {
501 // Tab was destroyed after being detached (without being re-attached). 537 // Tab was destroyed after being detached (without being re-attached).
502 WebContents* contents = content::Source<WebContents>(source).ptr(); 538 WebContents* contents = content::Source<WebContents>(source).ptr();
503 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 539 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
504 content::Source<NavigationController>(&contents->GetController())); 540 content::Source<NavigationController>(&contents->GetController()));
505 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 541 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
506 content::Source<WebContents>(contents)); 542 content::Source<WebContents>(contents));
507 favicon_scoped_observer_.Remove( 543 favicon_scoped_observer_.Remove(
508 favicon::ContentFaviconDriver::FromWebContents(contents)); 544 favicon::ContentFaviconDriver::FromWebContents(contents));
509 } else { 545 } else {
510 NOTREACHED(); 546 NOTREACHED();
511 } 547 }
512 } 548 }
513 549
514 void TabsEventRouter::TabChangedAt(WebContents* contents, 550 void TabsEventRouter::TabChangedAt(WebContents* contents,
515 int index, 551 int index,
516 TabChangeType change_type) { 552 TabChangeType change_type) {
517 TabUpdated(contents, false); 553 TabUpdated(contents, api::tabs::TAB_STATUS_COMPLETE);
518 } 554 }
519 555
520 void TabsEventRouter::TabReplacedAt(TabStripModel* tab_strip_model, 556 void TabsEventRouter::TabReplacedAt(TabStripModel* tab_strip_model,
521 WebContents* old_contents, 557 WebContents* old_contents,
522 WebContents* new_contents, 558 WebContents* new_contents,
523 int index) { 559 int index) {
524 // Notify listeners that the next tabs closing or being added are due to 560 // Notify listeners that the next tabs closing or being added are due to
525 // WebContents being swapped. 561 // WebContents being swapped.
526 const int new_tab_id = ExtensionTabUtil::GetTabId(new_contents); 562 const int new_tab_id = ExtensionTabUtil::GetTabId(new_contents);
527 const int old_tab_id = ExtensionTabUtil::GetTabId(old_contents); 563 const int old_tab_id = ExtensionTabUtil::GetTabId(old_contents);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 void TabsEventRouter::OnFaviconUpdated(favicon::FaviconDriver* favicon_driver, 626 void TabsEventRouter::OnFaviconUpdated(favicon::FaviconDriver* favicon_driver,
591 bool icon_url_changed) { 627 bool icon_url_changed) {
592 if (icon_url_changed) { 628 if (icon_url_changed) {
593 favicon::ContentFaviconDriver* content_favicon_driver = 629 favicon::ContentFaviconDriver* content_favicon_driver =
594 static_cast<favicon::ContentFaviconDriver*>(favicon_driver); 630 static_cast<favicon::ContentFaviconDriver*>(favicon_driver);
595 FaviconUrlUpdated(content_favicon_driver->web_contents()); 631 FaviconUrlUpdated(content_favicon_driver->web_contents());
596 } 632 }
597 } 633 }
598 634
599 } // namespace extensions 635 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698