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

Commit

Permalink
fix tab discarding
Browse files Browse the repository at this point in the history
fix #496
  • Loading branch information
bridiver committed Feb 22, 2018
1 parent 70d95fb commit b31d829
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
23 changes: 21 additions & 2 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include "chrome/browser/spellchecker/spellcheck_service.h"
#include "chrome/browser/ssl/security_state_tab_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_strip_model_impl.h"
#include "chrome/browser/ui/tabs/tab_strip_model_order_controller.h"
#include "chrome/common/custom_handlers/protocol_handler.h"
Expand Down Expand Up @@ -808,7 +809,7 @@ void WebContents::AddNewContents(content::WebContents* source,
if (was_blocked)
*was_blocked = blocked;

if (blocked) {
if (was_blocked && *was_blocked) {
auto guest = brave::TabViewGuest::FromWebContents(new_contents);
if (guest) {
guest->Destroy(true);
Expand Down Expand Up @@ -862,8 +863,9 @@ void WebContents::IsPlaceholder(mate::Arguments* args) {
auto tab_helper = extensions::TabHelper::FromWebContents(web_contents());
if (tab_helper) {
args->Return(tab_helper->is_placeholder());
} else {
args->Return(false);
}
args->Return(false);
}

void WebContents::AttachGuest(mate::Arguments* args) {
Expand Down Expand Up @@ -1219,6 +1221,23 @@ void WebContents::TabSelectionChanged(TabStripModel* tab_strip_model,
Emit("tab-selection-changed");
}

void WebContents::TabReplacedAt(TabStripModel* tab_strip_model,
content::WebContents* old_contents,
content::WebContents* new_contents,
int index) {
if (old_contents == web_contents()) {
::Browser* browser = nullptr;
for (auto* b : *BrowserList::GetInstance()) {
if (b->tab_strip_model() == tab_strip_model) {
browser = b;
break;
}
}

Emit("tab-replaced-at",
browser->session_id().id(), index, new_contents);
}
}

bool WebContents::OnGoToEntryOffset(int offset) {
GoToOffset(offset);
Expand Down
4 changes: 4 additions & 0 deletions atom/browser/api/atom_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ class WebContents : public mate::TrackableObject<WebContents>,
void TabStripEmpty() override;
void TabSelectionChanged(TabStripModel* tab_strip_model,
const ui::ListSelectionModel& old_model) override;
void TabReplacedAt(TabStripModel* tab_strip_model,
content::WebContents* old_contents,
content::WebContents* new_contents,
int index) override;

// content::WebContentsDelegate:
void RegisterProtocolHandler(content::WebContents* web_contents,
Expand Down
16 changes: 9 additions & 7 deletions atom/browser/extensions/tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ void TabHelper::SetActive(bool active) {
}

void TabHelper::WasShown() {
if (discarded_) {
auto helper = content::RestoreHelper::FromWebContents(web_contents());
if (helper) {
// load the tab if it is shown without being activate (tab preview)
discarded_ = false;
SetAutoDiscardable(true);
auto helper = content::RestoreHelper::FromWebContents(web_contents());
if (helper) {
helper->RemoveRestoreHelper();
}
Expand Down Expand Up @@ -470,11 +470,7 @@ void TabHelper::SetAutoDiscardable(bool auto_discardable) {
}

bool TabHelper::Discard() {
if (guest()->attached()) {
int64_t web_contents_id = TabManager::IdFromWebContents(web_contents());
return !!GetTabManager()->DiscardTabById(
web_contents_id, resource_coordinator::DiscardReason::kProactive);
} else {
if (web_contents()->GetController().IsInitialNavigation()) {
discarded_ = true;
content::RestoreHelper::CreateForWebContents(web_contents());
auto helper = content::RestoreHelper::FromWebContents(web_contents());
Expand All @@ -483,6 +479,12 @@ bool TabHelper::Discard() {
// registered as a discarded tab
SetAutoDiscardable(false);
return true;
} else {
if (guest()->attached()) {
int64_t web_contents_id = TabManager::IdFromWebContents(web_contents());
return !!GetTabManager()->DiscardTabById(
web_contents_id, resource_coordinator::DiscardReason::kProactive);
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions atom/common/api/resources/web_view_api_bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ WebViewImpl.prototype.attachWindow$ = function (opt_guestInstanceId) {

WebViewImpl.prototype.detachGuest = function () {
if (this.guest.getState() === GuestViewImpl.GuestState.GUEST_STATE_ATTACHED) {
this.guest.detach(() => {
this.guest = new GuestView('webview')
})
this.guest.detach()
}
}

Expand Down
5 changes: 5 additions & 0 deletions brave/browser/guest_view/tab_view/tab_view_guest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ void TabViewGuest::ApplyAttributes(const base::DictionaryValue& params) {

if (attached() &&
web_contents()->GetController().IsInitialNavigation()) {
// don't reload if we're already loading
if (web_contents()->GetController().GetPendingEntry() &&
web_contents()->GetController().GetPendingEntry()->GetURL() == src_) {
return;
}
NavigateGuest(src_.spec(), true);
}
}
Expand Down

0 comments on commit b31d829

Please sign in to comment.