1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/tabs/tabs_event_router.h"
#include "base/json/json_writer.h"
#include "base/values.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "chrome/browser/extensions/api/tabs/tabs_windows_api.h"
#include "chrome/browser/extensions/api/tabs/windows_event_router.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_iterator.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/extensions/extension_constants.h"
#include "content/public/browser/favicon_status.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/web_contents.h"
using base::DictionaryValue;
using base::ListValue;
using base::FundamentalValue;
using content::NavigationController;
using content::WebContents;
namespace extensions {
namespace {
namespace tabs = api::tabs;
void WillDispatchTabUpdatedEvent(
WebContents* contents,
const base::DictionaryValue* changed_properties,
content::BrowserContext* context,
const Extension* extension,
base::ListValue* event_args) {
// Overwrite the second argument with the appropriate properties dictionary,
// depending on extension permissions.
base::DictionaryValue* properties_value = changed_properties->DeepCopy();
ExtensionTabUtil::ScrubTabValueForExtension(contents,
extension,
properties_value);
event_args->Set(1, properties_value);
// Overwrite the third arg with our tab value as seen by this extension.
event_args->Set(2, ExtensionTabUtil::CreateTabValue(contents, extension));
}
} // namespace
TabsEventRouter::TabEntry::TabEntry() : complete_waiting_on_load_(false),
url_() {
}
base::DictionaryValue* TabsEventRouter::TabEntry::UpdateLoadState(
const WebContents* contents) {
// The tab may go in & out of loading (for instance if iframes navigate).
// We only want to respond to the first change from loading to !loading after
// the NAV_ENTRY_COMMITTED was fired.
if (!complete_waiting_on_load_ || contents->IsLoading())
return NULL;
// Send "complete" state change.
complete_waiting_on_load_ = false;
base::DictionaryValue* changed_properties = new base::DictionaryValue();
changed_properties->SetString(tabs_constants::kStatusKey,
tabs_constants::kStatusValueComplete);
return changed_properties;
}
base::DictionaryValue* TabsEventRouter::TabEntry::DidNavigate(
const WebContents* contents) {
// Send "loading" state change.
complete_waiting_on_load_ = true;
base::DictionaryValue* changed_properties = new base::DictionaryValue();
changed_properties->SetString(tabs_constants::kStatusKey,
tabs_constants::kStatusValueLoading);
if (contents->GetURL() != url_) {
url_ = contents->GetURL();
changed_properties->SetString(tabs_constants::kUrlKey, url_.spec());
}
return changed_properties;
}
TabsEventRouter::TabsEventRouter(Profile* profile) : profile_(profile) {
DCHECK(!profile->IsOffTheRecord());
BrowserList::AddObserver(this);
// Init() can happen after the browser is running, so catch up with any
// windows that already exist.
for (chrome::BrowserIterator it; !it.done(); it.Next()) {
RegisterForBrowserNotifications(*it);
// Also catch up our internal bookkeeping of tab entries.
Browser* browser = *it;
if (browser->tab_strip_model()) {
for (int i = 0; i < browser->tab_strip_model()->count(); ++i) {
WebContents* contents = browser->tab_strip_model()->GetWebContentsAt(i);
int tab_id = ExtensionTabUtil::GetTabId(contents);
tab_entries_[tab_id] = TabEntry();
}
}
}
}
TabsEventRouter::~TabsEventRouter() {
BrowserList::RemoveObserver(this);
}
void TabsEventRouter::OnBrowserAdded(Browser* browser) {
RegisterForBrowserNotifications(browser);
}
void TabsEventRouter::RegisterForBrowserNotifications(Browser* browser) {
if (!profile_->IsSameProfile(browser->profile()))
return;
// Start listening to TabStripModel events for this browser.
TabStripModel* tab_strip = browser->tab_strip_model();
tab_strip->AddObserver(this);
for (int i = 0; i < tab_strip->count(); ++i) {
RegisterForTabNotifications(tab_strip->GetWebContentsAt(i));
}
}
void TabsEventRouter::RegisterForTabNotifications(WebContents* contents) {
registrar_.Add(
this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<NavigationController>(&contents->GetController()));
// Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's
// possible for tabs to be created, detached and then destroyed without
// ever having been re-attached and closed. This happens in the case of
// a devtools WebContents that is opened in window, docked, then closed.
registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
content::Source<WebContents>(contents));
registrar_.Add(this, chrome::NOTIFICATION_FAVICON_UPDATED,
content::Source<WebContents>(contents));
}
void TabsEventRouter::UnregisterForTabNotifications(WebContents* contents) {
registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<NavigationController>(&contents->GetController()));
registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
content::Source<WebContents>(contents));
registrar_.Remove(this, chrome::NOTIFICATION_FAVICON_UPDATED,
content::Source<WebContents>(contents));
}
void TabsEventRouter::OnBrowserRemoved(Browser* browser) {
if (!profile_->IsSameProfile(browser->profile()))
return;
// Stop listening to TabStripModel events for this browser.
browser->tab_strip_model()->RemoveObserver(this);
}
void TabsEventRouter::OnBrowserSetLastActive(Browser* browser) {
TabsWindowsAPI* tabs_window_api = TabsWindowsAPI::Get(profile_);
if (tabs_window_api) {
tabs_window_api->windows_event_router()->OnActiveWindowChanged(
browser ? browser->extension_window_controller() : NULL);
}
}
static void WillDispatchTabCreatedEvent(WebContents* contents,
bool active,
content::BrowserContext* context,
const Extension* extension,
base::ListValue* event_args) {
base::DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue(
contents, extension);
event_args->Clear();
event_args->Append(tab_value);
tab_value->SetBoolean(tabs_constants::kSelectedKey, active);
}
void TabsEventRouter::TabCreatedAt(WebContents* contents,
int index,
bool active) {
Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
scoped_ptr<base::ListValue> args(new base::ListValue);
scoped_ptr<Event> event(new Event(tabs::OnCreated::kEventName, args.Pass()));
event->restrict_to_browser_context = profile;
event->user_gesture = EventRouter::USER_GESTURE_NOT_ENABLED;
event->will_dispatch_callback =
base::Bind(&WillDispatchTabCreatedEvent, contents, active);
EventRouter::Get(profile)->BroadcastEvent(event.Pass());
RegisterForTabNotifications(contents);
}
void TabsEventRouter::TabInsertedAt(WebContents* contents,
int index,
bool active) {
// If tab is new, send created event.
int tab_id = ExtensionTabUtil::GetTabId(contents);
if (!GetTabEntry(contents)) {
tab_entries_[tab_id] = TabEntry();
TabCreatedAt(contents, index, active);
return;
}
scoped_ptr<base::ListValue> args(new base::ListValue);
args->Append(new FundamentalValue(tab_id));
base::DictionaryValue* object_args = new base::DictionaryValue();
object_args->Set(tabs_constants::kNewWindowIdKey,
new FundamentalValue(
ExtensionTabUtil::GetWindowIdOfTab(contents)));
object_args->Set(tabs_constants::kNewPositionKey,
new FundamentalValue(index));
args->Append(object_args);
Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
DispatchEvent(profile, tabs::OnAttached::kEventName, args.Pass(),
EventRouter::USER_GESTURE_UNKNOWN);
}
void TabsEventRouter::TabDetachedAt(WebContents* contents, int index) {
if (!GetTabEntry(contents)) {
// The tab was removed. Don't send detach event.
return;
}
scoped_ptr<base::ListValue> args(new base::ListValue);
args->Append(
new FundamentalValue(ExtensionTabUtil::GetTabId(contents)));
base::DictionaryValue* object_args = new base::DictionaryValue();
object_args->Set(tabs_constants::kOldWindowIdKey,
new FundamentalValue(
ExtensionTabUtil::GetWindowIdOfTab(contents)));
object_args->Set(tabs_constants::kOldPositionKey,
new FundamentalValue(index));
args->Append(object_args);
Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
DispatchEvent(profile,
tabs::OnDetached::kEventName,
args.Pass(),
EventRouter::USER_GESTURE_UNKNOWN);
}
void TabsEventRouter::TabClosingAt(TabStripModel* tab_strip_model,
WebContents* contents,
int index) {
int tab_id = ExtensionTabUtil::GetTabId(contents);
scoped_ptr<base::ListValue> args(new base::ListValue);
args->Append(new FundamentalValue(tab_id));
base::DictionaryValue* object_args = new base::DictionaryValue();
object_args->SetInteger(tabs_constants::kWindowIdKey,
ExtensionTabUtil::GetWindowIdOfTab(contents));
object_args->SetBoolean(tabs_constants::kWindowClosing,
tab_strip_model->closing_all());
args->Append(object_args);
Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
DispatchEvent(profile,
tabs::OnRemoved::kEventName,
args.Pass(),
EventRouter::USER_GESTURE_UNKNOWN);
int removed_count = tab_entries_.erase(tab_id);
DCHECK_GT(removed_count, 0);
UnregisterForTabNotifications(contents);
}
void TabsEventRouter::ActiveTabChanged(WebContents* old_contents,
WebContents* new_contents,
int index,
int reason) {
scoped_ptr<base::ListValue> args(new base::ListValue);
int tab_id = ExtensionTabUtil::GetTabId(new_contents);
args->Append(new FundamentalValue(tab_id));
base::DictionaryValue* object_args = new base::DictionaryValue();
object_args->Set(tabs_constants::kWindowIdKey,
new FundamentalValue(
ExtensionTabUtil::GetWindowIdOfTab(new_contents)));
args->Append(object_args);
// The onActivated event replaced onActiveChanged and onSelectionChanged. The
// deprecated events take two arguments: tabId, {windowId}.
Profile* profile =
Profile::FromBrowserContext(new_contents->GetBrowserContext());
EventRouter::UserGestureState gesture =
reason & CHANGE_REASON_USER_GESTURE
? EventRouter::USER_GESTURE_ENABLED
: EventRouter::USER_GESTURE_NOT_ENABLED;
DispatchEvent(profile,
tabs::OnSelectionChanged::kEventName,
scoped_ptr<base::ListValue>(args->DeepCopy()),
gesture);
DispatchEvent(profile,
tabs::OnActiveChanged::kEventName,
scoped_ptr<base::ListValue>(args->DeepCopy()),
gesture);
// The onActivated event takes one argument: {windowId, tabId}.
args->Remove(0, NULL);
object_args->Set(tabs_constants::kTabIdKey,
new FundamentalValue(tab_id));
DispatchEvent(profile, tabs::OnActivated::kEventName, args.Pass(), gesture);
}
void TabsEventRouter::TabSelectionChanged(
TabStripModel* tab_strip_model,
const ui::ListSelectionModel& old_model) {
ui::ListSelectionModel::SelectedIndices new_selection =
tab_strip_model->selection_model().selected_indices();
scoped_ptr<base::ListValue> all_tabs(new base::ListValue);
for (size_t i = 0; i < new_selection.size(); ++i) {
int index = new_selection[i];
WebContents* contents = tab_strip_model->GetWebContentsAt(index);
if (!contents)
break;
int tab_id = ExtensionTabUtil::GetTabId(contents);
all_tabs->Append(new FundamentalValue(tab_id));
}
scoped_ptr<base::ListValue> args(new base::ListValue);
scoped_ptr<base::DictionaryValue> select_info(new base::DictionaryValue);
select_info->Set(
tabs_constants::kWindowIdKey,
new FundamentalValue(
ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model)));
select_info->Set(tabs_constants::kTabIdsKey, all_tabs.release());
args->Append(select_info.release());
// The onHighlighted event replaced onHighlightChanged.
Profile* profile = tab_strip_model->profile();
DispatchEvent(profile,
tabs::OnHighlightChanged::kEventName,
scoped_ptr<base::ListValue>(args->DeepCopy()),
EventRouter::USER_GESTURE_UNKNOWN);
DispatchEvent(profile,
tabs::OnHighlighted::kEventName,
args.Pass(),
EventRouter::USER_GESTURE_UNKNOWN);
}
void TabsEventRouter::TabMoved(WebContents* contents,
int from_index,
int to_index) {
scoped_ptr<base::ListValue> args(new base::ListValue);
args->Append(
new FundamentalValue(ExtensionTabUtil::GetTabId(contents)));
base::DictionaryValue* object_args = new base::DictionaryValue();
object_args->Set(tabs_constants::kWindowIdKey,
new FundamentalValue(
ExtensionTabUtil::GetWindowIdOfTab(contents)));
object_args->Set(tabs_constants::kFromIndexKey,
new FundamentalValue(from_index));
object_args->Set(tabs_constants::kToIndexKey,
new FundamentalValue(to_index));
args->Append(object_args);
Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
DispatchEvent(profile,
tabs::OnMoved::kEventName,
args.Pass(),
EventRouter::USER_GESTURE_UNKNOWN);
}
void TabsEventRouter::TabUpdated(WebContents* contents, bool did_navigate) {
TabEntry* entry = GetTabEntry(contents);
scoped_ptr<base::DictionaryValue> changed_properties;
CHECK(entry);
if (did_navigate)
changed_properties.reset(entry->DidNavigate(contents));
else
changed_properties.reset(entry->UpdateLoadState(contents));
if (changed_properties)
DispatchTabUpdatedEvent(contents, changed_properties.Pass());
}
void TabsEventRouter::FaviconUrlUpdated(WebContents* contents) {
content::NavigationEntry* entry =
contents->GetController().GetVisibleEntry();
if (!entry || !entry->GetFavicon().valid)
return;
scoped_ptr<base::DictionaryValue> changed_properties(
new base::DictionaryValue);
changed_properties->SetString(
tabs_constants::kFaviconUrlKey,
entry->GetFavicon().url.possibly_invalid_spec());
DispatchTabUpdatedEvent(contents, changed_properties.Pass());
}
void TabsEventRouter::DispatchEvent(
Profile* profile,
const std::string& event_name,
scoped_ptr<base::ListValue> args,
EventRouter::UserGestureState user_gesture) {
EventRouter* event_router = EventRouter::Get(profile);
if (!profile_->IsSameProfile(profile) || !event_router)
return;
scoped_ptr<Event> event(new Event(event_name, args.Pass()));
event->restrict_to_browser_context = profile;
event->user_gesture = user_gesture;
event_router->BroadcastEvent(event.Pass());
}
void TabsEventRouter::DispatchSimpleBrowserEvent(
Profile* profile, const int window_id, const std::string& event_name) {
if (!profile_->IsSameProfile(profile))
return;
scoped_ptr<base::ListValue> args(new base::ListValue);
args->Append(new FundamentalValue(window_id));
DispatchEvent(profile,
event_name,
args.Pass(),
EventRouter::USER_GESTURE_UNKNOWN);
}
void TabsEventRouter::DispatchTabUpdatedEvent(
WebContents* contents,
scoped_ptr<base::DictionaryValue> changed_properties) {
DCHECK(changed_properties);
DCHECK(contents);
// The state of the tab (as seen from the extension point of view) has
// changed. Send a notification to the extension.
scoped_ptr<base::ListValue> args_base(new base::ListValue);
// First arg: The id of the tab that changed.
args_base->AppendInteger(ExtensionTabUtil::GetTabId(contents));
// Second arg: An object containing the changes to the tab state. Filled in
// by WillDispatchTabUpdatedEvent as a copy of changed_properties, if the
// extension has the tabs permission.
// Third arg: An object containing the state of the tab. Filled in by
// WillDispatchTabUpdatedEvent.
Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
scoped_ptr<Event> event(
new Event(tabs::OnUpdated::kEventName, args_base.Pass()));
event->restrict_to_browser_context = profile;
event->user_gesture = EventRouter::USER_GESTURE_NOT_ENABLED;
event->will_dispatch_callback =
base::Bind(&WillDispatchTabUpdatedEvent,
contents,
changed_properties.get());
EventRouter::Get(profile)->BroadcastEvent(event.Pass());
}
TabsEventRouter::TabEntry* TabsEventRouter::GetTabEntry(WebContents* contents) {
int tab_id = ExtensionTabUtil::GetTabId(contents);
std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id);
if (tab_entries_.end() == i)
return NULL;
return &i->second;
}
void TabsEventRouter::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) {
NavigationController* source_controller =
content::Source<NavigationController>(source).ptr();
TabUpdated(source_controller->GetWebContents(), true);
} else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) {
// Tab was destroyed after being detached (without being re-attached).
WebContents* contents = content::Source<WebContents>(source).ptr();
registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<NavigationController>(&contents->GetController()));
registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
content::Source<WebContents>(contents));
registrar_.Remove(this, chrome::NOTIFICATION_FAVICON_UPDATED,
content::Source<WebContents>(contents));
} else if (type == chrome::NOTIFICATION_FAVICON_UPDATED) {
bool icon_url_changed = *content::Details<bool>(details).ptr();
if (icon_url_changed)
FaviconUrlUpdated(content::Source<WebContents>(source).ptr());
} else {
NOTREACHED();
}
}
void TabsEventRouter::TabChangedAt(WebContents* contents,
int index,
TabChangeType change_type) {
TabUpdated(contents, false);
}
void TabsEventRouter::TabReplacedAt(TabStripModel* tab_strip_model,
WebContents* old_contents,
WebContents* new_contents,
int index) {
// Notify listeners that the next tabs closing or being added are due to
// WebContents being swapped.
const int new_tab_id = ExtensionTabUtil::GetTabId(new_contents);
const int old_tab_id = ExtensionTabUtil::GetTabId(old_contents);
scoped_ptr<base::ListValue> args(new base::ListValue);
args->Append(new FundamentalValue(new_tab_id));
args->Append(new FundamentalValue(old_tab_id));
DispatchEvent(Profile::FromBrowserContext(new_contents->GetBrowserContext()),
tabs::OnReplaced::kEventName,
args.Pass(),
EventRouter::USER_GESTURE_UNKNOWN);
// Update tab_entries_.
const int removed_count = tab_entries_.erase(old_tab_id);
DCHECK_GT(removed_count, 0);
UnregisterForTabNotifications(old_contents);
if (!GetTabEntry(new_contents)) {
tab_entries_[new_tab_id] = TabEntry();
RegisterForTabNotifications(new_contents);
}
}
void TabsEventRouter::TabPinnedStateChanged(WebContents* contents, int index) {
TabStripModel* tab_strip = NULL;
int tab_index;
if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) {
scoped_ptr<base::DictionaryValue> changed_properties(
new base::DictionaryValue());
changed_properties->SetBoolean(tabs_constants::kPinnedKey,
tab_strip->IsTabPinned(tab_index));
DispatchTabUpdatedEvent(contents, changed_properties.Pass());
}
}
} // namespace extensions
|