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

Commit

Permalink
Fix filtering not knowing tabId
Browse files Browse the repository at this point in the history
  • Loading branch information
bbondy committed Oct 23, 2017
1 parent 46cb7c3 commit 06c7ad2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
5 changes: 5 additions & 0 deletions atom/browser/extensions/tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <map>
#include <utility>
#include "atom/browser/extensions/api/atom_extensions_api_client.h"
#include "atom/browser/extensions/atom_extension_web_contents_observer.h"
#include "atom/browser/native_window.h"
#include "atom/common/native_mate_converters/callback.h"
Expand Down Expand Up @@ -548,6 +549,10 @@ void TabHelper::RenderViewCreated(content::RenderViewHost* render_view_host) {

void TabHelper::RenderFrameCreated(content::RenderFrameHost* host) {
SetTabId(host);
// Look up the extension API frame ID to force the mapping to be cached.
// This is needed so that cached information is available for tabId in the
// filtering callbacks.
ExtensionApiFrameIdMap::Get()->CacheFrameData(host);
}

void TabHelper::WebContentsDestroyed() {
Expand Down
1 change: 0 additions & 1 deletion atom/browser/extensions/tab_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class Dictionary;

namespace keys {
extern const char kIdKey[];
extern const char kTabIdKey[];
extern const char kIncognitoKey[];
extern const char kWindowIdKey[];
extern const char kAudibleKey[];
Expand Down
42 changes: 40 additions & 2 deletions atom/browser/net/atom_network_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
#include <memory>
#include <utility>

#include "atom/browser/extensions/tab_helper.h"
#include "atom/common/native_mate_converters/net_converter.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "chrome/browser/devtools/devtools_network_transaction.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/websocket_handshake_request_info.h"
#include "extensions/features/features.h"
#include "net/url_request/url_request.h"
Expand Down Expand Up @@ -115,6 +118,25 @@ int GetTabId(net::URLRequest* request) {
#endif
}

void GetRenderFrameIdAndProcessId(net::URLRequest* request,
int* render_frame_id,
int* render_process_id) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
*render_frame_id = -1;
*render_process_id = -1;
extensions::ExtensionApiFrameIdMap::FrameData frame_data;
if (!content::ResourceRequestInfo::GetRenderFrameForRequest(
request, render_process_id, render_frame_id)) {
const content::WebSocketHandshakeRequestInfo* websocket_info =
content::WebSocketHandshakeRequestInfo::ForRequest(request);
if (websocket_info) {
*render_frame_id = websocket_info->GetRenderFrameId();
*render_process_id = websocket_info->GetChildId();
}
}
#endif
}

// Overloaded by multiple types to fill the |details| object.
void ToDictionary(base::DictionaryValue* details, net::URLRequest* request) {
FillRequestDetails(details, request);
Expand Down Expand Up @@ -422,9 +444,13 @@ int AtomNetworkDelegate::HandleResponseEvent(
// The |request| could be destroyed before the |callback| is called.
callbacks_[request->identifier()] = callback;

int render_frame_id = -1;
int render_process_id = -1;
GetRenderFrameIdAndProcessId(request, &render_frame_id, &render_process_id);
ResponseCallback response =
base::Bind(&AtomNetworkDelegate::OnListenerResultInUI<Out>,
weak_factory_.GetWeakPtr(), request->identifier(), out);
weak_factory_.GetWeakPtr(), request->identifier(),
render_frame_id, render_process_id, out);
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(RunResponseListener, info.listener, base::Passed(&details),
Expand Down Expand Up @@ -463,8 +489,20 @@ void AtomNetworkDelegate::OnListenerResultInIO(

template<typename T>
void AtomNetworkDelegate::OnListenerResultInUI(
uint64_t id, T out, const base::DictionaryValue& response) {
uint64_t id, int render_frame_id, int render_process_id,
T out, const base::DictionaryValue& response) {
std::unique_ptr<base::DictionaryValue> copy = response.CreateDeepCopy();

int tab_id = -1;
copy->GetInteger(extensions::tabs_constants::kTabIdKey, &tab_id);
content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(
render_process_id, render_frame_id);
if (rfh && tab_id == -1) {
auto web_contents = content::WebContents::FromRenderFrameHost(rfh);
int tab_id = extensions::TabHelper::IdForTab(web_contents);
copy->SetInteger(extensions::tabs_constants::kTabIdKey, tab_id);
}

BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&AtomNetworkDelegate::OnListenerResultInIO<T>,
Expand Down
3 changes: 2 additions & 1 deletion atom/browser/net/atom_network_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ class AtomNetworkDelegate : public brightray::NetworkDelegate {
uint64_t id, T out, std::unique_ptr<base::DictionaryValue> response);
template<typename T>
void OnListenerResultInUI(
uint64_t id, T out, const base::DictionaryValue& response);
uint64_t id, int render_frame_id, int render_process_id,
T out, const base::DictionaryValue& response);

std::map<SimpleEvent, SimpleListenerInfo> simple_listeners_;
std::map<ResponseEvent, ResponseListenerInfo> response_listeners_;
Expand Down
3 changes: 3 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 @@ -19,6 +19,7 @@
#include "atom/browser/web_contents_preferences.h"
#include "atom/common/native_mate_converters/content_converter.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/browser/extensions/api/atom_extensions_api_client.h"
#include "base/memory/ptr_util.h"
#include "brave/browser/brave_browser_context.h"
#include "build/build_config.h"
Expand Down Expand Up @@ -223,6 +224,8 @@ void TabViewGuest::DidInitialize(const base::DictionaryValue& create_params) {
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);

extensions::ExtensionsAPIClient::Get()->AttachWebContentsHelpers(
web_contents());
api_web_contents_ = atom::api::WebContents::CreateFrom(isolate,
web_contents(), atom::api::WebContents::Type::WEB_VIEW).get();
api_web_contents_->guest_delegate_ = this;
Expand Down

0 comments on commit 06c7ad2

Please sign in to comment.