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
|
// Copyright 2020 The Chromium Authors
// 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/tab_groups/tab_groups_event_router.h"
#include <utility>
#include "base/strings/utf_string_conversions.h"
#include "base/values.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_finder.h"
#include "chrome/browser/ui/tabs/tab_group_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "components/tab_groups/tab_group_color.h"
#include "components/tab_groups/tab_group_id.h"
#include "components/tab_groups/tab_group_visual_data.h"
#include "components/tabs/public/tab_group.h"
namespace extensions {
TabGroupsEventRouter::TabGroupsEventRouter(content::BrowserContext* context)
: profile_(Profile::FromBrowserContext(context)),
event_router_(EventRouter::Get(context)),
browser_tab_strip_tracker_(this, this) {
browser_tab_strip_tracker_.Init();
}
void TabGroupsEventRouter::OnTabGroupChanged(const TabGroupChange& change) {
switch (change.type) {
case TabGroupChange::kCreated: {
DispatchGroupCreated(change.group);
// Synthesize the initial kVisualsChanged notification while detaching and
// reattaching groups.
// TODO(crbug.com/398256328): Remove after fixing initial kVisualsChanged
// case.
if (change.GetCreateChange()->reason() ==
TabGroupChange::TabGroupCreationReason::
kInsertedFromAnotherTabstrip) {
DispatchGroupUpdated(change.group);
}
break;
}
case TabGroupChange::kClosed: {
DispatchGroupRemoved(change.group);
break;
}
case TabGroupChange::kMoved: {
DispatchGroupMoved(change.group);
break;
}
case TabGroupChange::kVisualsChanged: {
DispatchGroupUpdated(change.group);
break;
}
case TabGroupChange::kEditorOpened:
break;
}
return;
}
bool TabGroupsEventRouter::ShouldTrackBrowser(Browser* browser) {
return profile_ == browser->profile() &&
ExtensionTabUtil::BrowserSupportsTabs(browser);
}
void TabGroupsEventRouter::DispatchGroupCreated(tab_groups::TabGroupId group) {
auto args(api::tab_groups::OnCreated::Create(
*ExtensionTabUtil::CreateTabGroupObject(group)));
DispatchEvent(events::TAB_GROUPS_ON_CREATED,
api::tab_groups::OnCreated::kEventName, std::move(args));
}
void TabGroupsEventRouter::DispatchGroupRemoved(tab_groups::TabGroupId group) {
auto args(api::tab_groups::OnRemoved::Create(
*ExtensionTabUtil::CreateTabGroupObject(group)));
DispatchEvent(events::TAB_GROUPS_ON_REMOVED,
api::tab_groups::OnRemoved::kEventName, std::move(args));
}
void TabGroupsEventRouter::DispatchGroupMoved(tab_groups::TabGroupId group) {
auto args(api::tab_groups::OnMoved::Create(
*ExtensionTabUtil::CreateTabGroupObject(group)));
DispatchEvent(events::TAB_GROUPS_ON_MOVED,
api::tab_groups::OnMoved::kEventName, std::move(args));
}
void TabGroupsEventRouter::DispatchGroupUpdated(tab_groups::TabGroupId group) {
auto args(api::tab_groups::OnUpdated::Create(
*ExtensionTabUtil::CreateTabGroupObject(group)));
DispatchEvent(events::TAB_GROUPS_ON_UPDATED,
api::tab_groups::OnUpdated::kEventName, std::move(args));
}
void TabGroupsEventRouter::DispatchEvent(events::HistogramValue histogram_value,
const std::string& event_name,
base::Value::List args) {
// |event_router_| can be null in tests.
if (!event_router_)
return;
auto event = std::make_unique<Event>(histogram_value, event_name,
std::move(args), profile_);
event_router_->BroadcastEvent(std::move(event));
}
} // namespace extensions
|