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

Commit

Permalink
Merge pull request #280 from brave/tab-strip-master
Browse files Browse the repository at this point in the history
Use tab strip to control indexes in browser process
  • Loading branch information
bridiver authored Aug 30, 2017
2 parents c193923 + ddbc5d4 commit 67c0cad
Show file tree
Hide file tree
Showing 15 changed files with 385 additions and 314 deletions.
112 changes: 92 additions & 20 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 @@ -645,6 +646,31 @@ void WebContents::AddNewContents(content::WebContents* source,
user_gesture = true;
}

if (disposition != WindowOpenDisposition::NEW_WINDOW &&
disposition != WindowOpenDisposition::NEW_POPUP) {
auto tab_helper = extensions::TabHelper::FromWebContents(new_contents);
if (tab_helper &&
tab_helper->get_index() == TabStripModel::kNoTab) {
::Browser* browser = nullptr;
bool active = disposition == WindowOpenDisposition::NEW_FOREGROUND_TAB;
if (tab_helper->window_id() != -1) {
browser = tab_helper->browser();
} else {
browser = owner_window()->browser();
}
if (browser) {
int index =
browser->tab_strip_model()->order_controller()->
DetermineInsertionIndex(ui::PAGE_TRANSITION_LINK,
active ?
TabStripModel::ADD_ACTIVE :
TabStripModel::ADD_NONE);
tab_helper->SetTabIndex(index);
tab_helper->SetActive(active);
}
}
}

node::Environment* env = node::Environment::GetCurrent(isolate());
if (!env) {
return;
Expand Down Expand Up @@ -1007,9 +1033,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 +1050,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 +1413,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 +2084,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 +2538,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 @@ -2533,6 +2599,9 @@ void WebContents::OnTabCreated(const mate::Dictionary& options,
tab_helper->SetAutoDiscardable(autoDiscardable);
}

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

bool discarded = false;
if (options.Get("discarded", &discarded) && discarded && !active) {
std::string url;
Expand Down Expand Up @@ -2565,25 +2634,28 @@ void WebContents::OnTabCreated(const mate::Dictionary& options,
tab_helper->Discard();
}

int windowId = -1;
if (options.Get("windowId", &windowId) && windowId != -1) {
int window_id = -1;
::Browser *browser = nullptr;
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->SetBrowser(api_window->window()->browser());
browser = api_window->window()->browser();
tab_helper->SetWindowId(window_id);
}
}
if (!browser) {
browser = owner_window()->browser();
}

int opener_tab_id = -1;
options.Get("openerTabId", &opener_tab_id);
tab_helper->SetOpener(opener_tab_id);
tab_helper->SetBrowser(browser);

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 (!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
71 changes: 52 additions & 19 deletions atom/browser/extensions/tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model_order_controller.h"
#include "components/sessions/core/session_id.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_entry.h"
Expand Down Expand Up @@ -158,7 +159,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 +188,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 +198,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 +209,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 +285,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 +297,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 +319,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 +362,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 +403,42 @@ 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();
}

// When there is an opener tab and the index is not currently valid,
// we don't want to overwrite the index with the last tab index because
// the index will be determined by the opener tab.
bool is_invalid_tab_index = index_ == TabStripModel::kNoTab ||
index_ > browser_->tab_strip_model()->count();
if (opener_tab_id_ == TabStripModel::kNoTab &&
is_invalid_tab_index) {
index_ = browser_->tab_strip_model()->count();
} else if (is_invalid_tab_index) {
index_ =
browser_->tab_strip_model()->order_controller()->
DetermineInsertionIndex(ui::PAGE_TRANSITION_LINK,
active_ ?
TabStripModel::ADD_ACTIVE :
TabStripModel::ADD_NONE);
}

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 +490,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 +506,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 +531,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 +713,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
Loading

0 comments on commit 67c0cad

Please sign in to comment.