Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Use tab strip to control indexes in browser process
Browse files Browse the repository at this point in the history
  • Loading branch information
bbondy committed Aug 23, 2017
1 parent 75170a6 commit f526b9d
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 310 deletions.
83 changes: 67 additions & 16 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "chrome/browser/ssl/security_state_tab_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model_order_controller.h"
#include "chrome/common/custom_handlers/protocol_handler.h"
#include "components/autofill/content/browser/content_autofill_driver_factory.h"
#include "components/autofill/core/browser/autofill_manager.h"
Expand Down Expand Up @@ -1007,9 +1008,9 @@ void WebContents::TabPinnedStateChanged(TabStripModel* tab_strip_model,
void WebContents::TabDetachedAt(content::WebContents* contents, int index) {
if (contents != web_contents())
return;

if (owner_window() && owner_window()->browser())
owner_window()->browser()->tab_strip_model()->RemoveObserver(this);
Emit("tab-detached-at", index);
}

void WebContents::ActiveTabChanged(content::WebContents* old_contents,
Expand All @@ -1024,6 +1025,49 @@ void WebContents::ActiveTabChanged(content::WebContents* old_contents,
}
}

void WebContents::TabInsertedAt(TabStripModel* tab_strip_model,
content::WebContents* contents,
int index,
bool foreground) {
if (contents != web_contents())
return;
Emit("tab-inserted-at", index, foreground);
}

void WebContents::TabMoved(content::WebContents* contents,
int from_index,
int to_index) {
if (contents != web_contents())
return;
Emit("tab-moved", from_index, to_index);
}

void WebContents::TabClosingAt(TabStripModel* tab_strip_model,
content::WebContents* contents,
int index) {
if (contents != web_contents())
return;
Emit("tab-closing-at", index);
}

void WebContents::TabChangedAt(content::WebContents* contents,
int index,
TabChangeType change_type) {
if (contents != web_contents())
return;
Emit("tab-changed-at", index);
}

void WebContents::TabStripEmpty() {
Emit("tab-strip-empty");
}

void WebContents::TabSelectionChanged(TabStripModel* tab_strip_model,
const ui::ListSelectionModel& old_model) {
Emit("tab-selection-changed");
}


bool WebContents::OnGoToEntryOffset(int offset) {
GoToOffset(offset);
return false;
Expand Down Expand Up @@ -1344,11 +1388,9 @@ void WebContents::SetOwnerWindow(NativeWindow* new_owner_window) {
if (owner_window() == new_owner_window)
return;

if (IsGuest()) {
if (owner_window())
owner_window()->browser()->tab_strip_model()->RemoveObserver(this);
new_owner_window->browser()->tab_strip_model()->AddObserver(this);
}
if (owner_window())
owner_window()->browser()->tab_strip_model()->RemoveObserver(this);
new_owner_window->browser()->tab_strip_model()->AddObserver(this);

SetOwnerWindow(web_contents(), new_owner_window);
}
Expand Down Expand Up @@ -2017,8 +2059,6 @@ void WebContents::SetTabIndex(int index) {
if (tab_helper)
tab_helper->SetTabIndex(index);
#endif

Emit("set-tab-index", index);
}

void WebContents::SetPinned(bool pinned) {
Expand Down Expand Up @@ -2473,8 +2513,9 @@ void WebContents::OnRendererMessageSync(const base::string16& channel,
EmitWithSender(base::UTF16ToUTF8(channel), web_contents(), message, args);
}

void WebContents::OnRendererMessageShared(const base::string16& channel,
const base::SharedMemoryHandle& handle) {
void WebContents::OnRendererMessageShared(
const base::string16& channel,
const base::SharedMemoryHandle& handle) {
std::vector<v8::Local<v8::Value>> args = {
mate::StringToV8(isolate(), channel),
brave::SharedMemoryWrapper::CreateFrom(isolate(), handle).ToV8(),
Expand Down Expand Up @@ -2565,25 +2606,35 @@ void WebContents::OnTabCreated(const mate::Dictionary& options,
tab_helper->Discard();
}

int windowId = -1;
if (options.Get("windowId", &windowId) && windowId != -1) {
TabStripModel *tab_strip = nullptr;
int window_id = -1;
if (options.Get("windowId", &window_id) && window_id != -1) {
auto api_window =
mate::TrackableObject<Window>::FromWeakMapID(isolate(), windowId);
mate::TrackableObject<Window>::FromWeakMapID(isolate(), window_id);
if (api_window) {
// TODO(bridiver) - combine these two methods
tab_helper->SetWindowId(windowId);
tab_helper->SetWindowId(window_id);
tab_helper->SetBrowser(api_window->window()->browser());
tab_strip = api_window->window()->browser()->tab_strip_model();
}
}

int opener_tab_id = -1;
int opener_tab_id = TabStripModel::kNoTab;
options.Get("openerTabId", &opener_tab_id);

content::WebContents* source = nullptr;
if (opener_tab_id != -1) {
if (opener_tab_id != TabStripModel::kNoTab) {
source = extensions::TabHelper::GetTabById(opener_tab_id);
tab_helper->SetOpener(opener_tab_id);
if (!tab_strip) {
tab_strip = owner_window()->browser()->tab_strip_model();
}
int index = tab_strip->order_controller()->
DetermineInsertionIndex(ui::PAGE_TRANSITION_LINK,
active ? TabStripModel::ADD_ACTIVE : TabStripModel::ADD_NONE);
tab_helper->SetTabIndex(index);
}

if (!source)
source = web_contents();

Expand Down
16 changes: 16 additions & 0 deletions atom/browser/api/atom_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,22 @@ class WebContents : public mate::TrackableObject<WebContents>,
content::WebContents* new_contents,
int index,
int reason) override;
void TabInsertedAt(TabStripModel* tab_strip_model,
content::WebContents* contents,
int index,
bool foreground) override;
void TabMoved(content::WebContents* contents,
int from_index,
int to_index) override;
void TabClosingAt(TabStripModel* tab_strip_model,
content::WebContents* contents,
int index) override;
void TabChangedAt(content::WebContents* contents,
int index,
TabChangeType change_type) override;
void TabStripEmpty() override;
void TabSelectionChanged(TabStripModel* tab_strip_model,
const ui::ListSelectionModel& old_model) override;

// content::WebContentsDelegate:
void RegisterProtocolHandler(content::WebContents* web_contents,
Expand Down
57 changes: 38 additions & 19 deletions atom/browser/extensions/tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ int TabHelper::GetTabStripIndex(int window_id, int index) {
if (tab_helper &&
tab_helper->get_index() == index &&
tab_helper->window_id() == window_id)
return tab_helper->get_tab_strip_index();
return tab_helper->get_index();
}
return TabStripModel::kNoTab;
}
Expand Down Expand Up @@ -187,7 +187,7 @@ content::WebContents* TabHelper::DetachGuest() {
web_contents()->GetController());

auto null_helper = FromWebContents(null_contents);
null_helper->index_ = index_;
null_helper->index_ = get_index();
null_helper->pinned_ = pinned_;
// transfer window closing state
null_helper->window_closing_ = window_closing_;
Expand All @@ -197,7 +197,7 @@ content::WebContents* TabHelper::DetachGuest() {

// Replace the detached tab with the null placeholder
browser_->tab_strip_model()->ReplaceWebContentsAt(
get_tab_strip_index(), null_contents);
get_index(), null_contents);

return null_contents;
}
Expand All @@ -208,7 +208,7 @@ void TabHelper::DidAttach() {
MaybeRequestWindowClose();

if (active_) {
browser_->tab_strip_model()->ActivateTabAt(get_tab_strip_index(), true);
browser_->tab_strip_model()->ActivateTabAt(get_index(), true);
active_ = false;
}
if (is_placeholder()) {
Expand Down Expand Up @@ -284,7 +284,7 @@ void TabHelper::MaybeAttachOrCreatePinnedTab() {
// content::WebContents* pinned_web_contents = nullptr;
// for (auto* browser : *BrowserList::GetInstance()) {
// auto web_contents =
// browser->tab_strip_model()->GetWebContentsAt(get_tab_strip_index());
// browser->tab_strip_model()->GetWebContentsAt(get_index());
// if (web_contents) {
// auto tab_helper = FromWebContents(web_contents);
// if (!tab_helper->is_placeholder()) {
Expand All @@ -296,7 +296,7 @@ void TabHelper::MaybeAttachOrCreatePinnedTab() {
// }

// if (pinned_web_contents) {
// browser_->tab_strip_model()->ReplaceWebContentsAt(get_tab_strip_index(),
// browser_->tab_strip_model()->ReplaceWebContentsAt(get_index(),
// pinned_web_contents);
// } else {
SetPlaceholder(false);
Expand All @@ -318,8 +318,9 @@ void TabHelper::TabReplacedAt(TabStripModel* tab_strip_model,
int guest_instance_id = old_guest->guest_instance_id();

auto new_helper = FromWebContents(new_contents);
new_helper->index_ = index_;
new_helper->index_ = get_index();
new_helper->pinned_ = pinned_;
new_helper->opener_tab_id_ = opener_tab_id_;

OnBrowserRemoved(old_browser);
new_helper->UpdateBrowser(old_browser);
Expand Down Expand Up @@ -360,8 +361,8 @@ void TabHelper::SetActive(bool active) {
SetAutoDiscardable(true);
}

if (browser_ && index_ != TabStripModel::kNoTab) {
browser_->tab_strip_model()->ActivateTabAt(get_tab_strip_index(), true);
if (browser_) {
browser_->tab_strip_model()->ActivateTabAt(get_index(), true);
if (!IsDiscarded()) {
web_contents()->WasShown();
}
Expand Down Expand Up @@ -401,15 +402,29 @@ void TabHelper::SetBrowser(Browser* browser) {
return;

if (browser_) {
if (get_tab_strip_index() != TabStripModel::kNoTab)
browser_->tab_strip_model()->DetachWebContentsAt(get_tab_strip_index());
if (get_index() != TabStripModel::kNoTab)
browser_->tab_strip_model()->DetachWebContentsAt(get_index());

OnBrowserRemoved(browser_);
}

if (browser) {
UpdateBrowser(browser);
browser_->tab_strip_model()->AppendWebContents(web_contents(), false);
if (opener_tab_id_ != TabStripModel::kNoTab && browser->tab_strip_model()) {
auto tab_strip = browser->tab_strip_model();
}

if (index_ == TabStripModel::kNoTab ||
index_ > browser_->tab_strip_model()->count()) {
index_ = browser_->tab_strip_model()->count();
}

int add_types = TabStripModel::ADD_NONE;
add_types |= active_ ? TabStripModel::ADD_ACTIVE : 0;
add_types |= opener_tab_id_ != TabStripModel::kNoTab ?
TabStripModel::ADD_INHERIT_OPENER : 0;
browser_->tab_strip_model()->InsertWebContentsAt(
index_, web_contents(), add_types);
} else {
browser_ = nullptr;
}
Expand Down Expand Up @@ -461,7 +476,7 @@ void TabHelper::SetPinned(bool pinned) {

pinned_ = pinned;
if (browser()) {
browser()->tab_strip_model()->SetTabPinned(get_tab_strip_index(), pinned);
browser()->tab_strip_model()->SetTabPinned(get_index(), pinned);
}

if (pinned_) {
Expand All @@ -477,12 +492,16 @@ bool TabHelper::IsPinned() const {

void TabHelper::SetTabIndex(int index) {
index_ = index;
if (browser()) {
browser()->tab_strip_model()->MoveWebContentsAt(
get_index(), index, false);
}
}

bool TabHelper::is_active() const {
if (browser()) {
return browser()->tab_strip_model()->
GetActiveWebContents() == web_contents();
return browser()->tab_strip_model()->GetActiveWebContents()==
web_contents();
} else {
return active_;
}
Expand All @@ -498,8 +517,8 @@ void TabHelper::SetTabValues(const base::DictionaryValue& values) {
values_->MergeDictionary(&values);
}

void TabHelper::SetOpener(int openerTabId) {
opener_tab_id_ = openerTabId;
void TabHelper::SetOpener(int opener_tab_id) {
opener_tab_id_ = opener_tab_id;
}

void TabHelper::RenderViewCreated(content::RenderViewHost* render_view_host) {
Expand Down Expand Up @@ -680,11 +699,11 @@ void TabHelper::ExecuteScript(
callback);
}

int TabHelper::get_tab_strip_index() const {
int TabHelper::get_index() const {
if (browser())
return browser()->tab_strip_model()->GetIndexOfWebContents(web_contents());

return TabStripModel::kNoTab;
return index_;
}

// static
Expand Down
3 changes: 1 addition & 2 deletions atom/browser/extensions/tab_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class TabHelper : public content::WebContentsObserver,

brave::TabViewGuest* guest() const;

int get_index() const { return index_; }
int get_index() const;
bool is_pinned() const { return pinned_; }
bool is_active() const;

Expand Down Expand Up @@ -157,7 +157,6 @@ class TabHelper : public content::WebContentsObserver,
void TabPinnedStateChanged(TabStripModel* tab_strip_model,
content::WebContents* contents,
int index) override;
int get_tab_strip_index() const;

void OnBrowserRemoved(Browser* browser) override;
void OnBrowserSetLastActive(Browser* browser) override;
Expand Down
1 change: 1 addition & 0 deletions brave/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ source_set("apis") {
"api/navigation_controller.h",
"api/navigation_handle.cc",
"api/navigation_handle.h",
"ui/brave_tab_strip_model_delegate.cc",
]

deps = [
Expand Down
Loading

0 comments on commit f526b9d

Please sign in to comment.