-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added adblock info tab.
- Loading branch information
Showing
41 changed files
with
948 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "base/values.h" | ||
#include "brave/browser/brave_shields/ad_block_service_browsertest.h" | ||
#include "chrome/browser/interstitials/security_interstitial_page_test_utils.h" | ||
#include "content/public/test/browser_test.h" | ||
#include "content/public/test/browser_test_utils.h" | ||
#include "content/public/test/test_devtools_protocol_client.h" | ||
#include "url/gurl.h" | ||
|
||
#if BUILDFLAG(IS_ANDROID) | ||
#include "chrome/test/base/android/android_browser_test.h" | ||
#else | ||
#include "chrome/test/base/in_process_browser_test.h" | ||
#endif | ||
|
||
class AdblockDevtoolsTest : public AdBlockServiceTest, | ||
public content::TestDevToolsProtocolClient { | ||
public: | ||
AdblockDevtoolsTest() = default; | ||
~AdblockDevtoolsTest() override = default; | ||
|
||
bool IsShowingInterstitial() { | ||
return chrome_browser_interstitials::IsShowingInterstitial(web_contents()); | ||
} | ||
|
||
protected: | ||
void TearDownOnMainThread() override { | ||
DetachProtocolClient(); | ||
AdBlockServiceTest::TearDownOnMainThread(); | ||
} | ||
}; | ||
|
||
IN_PROC_BROWSER_TEST_F(AdblockDevtoolsTest, DomainBlock) { | ||
AttachToWebContents(web_contents()); | ||
SendCommandSync("Network.enable"); | ||
|
||
const GURL& url = embedded_test_server()->GetURL("a.com", "/simple.html"); | ||
UpdateAdBlockInstanceWithRules("||" + url.host() + "^"); | ||
NavigateToURL(url); | ||
|
||
EXPECT_TRUE(IsShowingInterstitial()); | ||
|
||
const base::Value::Dict& notification = | ||
WaitForNotification("Network.requestAdblockInfoReceived", true); | ||
const auto* info = notification.FindDict("info"); | ||
ASSERT_TRUE(info); | ||
EXPECT_EQ(url.spec(), *info->FindString("requestUrl")); | ||
EXPECT_EQ("Document", *info->FindString("resourceType")); | ||
EXPECT_TRUE(*info->FindBool("blocked")); | ||
EXPECT_TRUE(*info->FindBool("didMatchRule")); | ||
} | ||
|
||
IN_PROC_BROWSER_TEST_F(AdblockDevtoolsTest, ResourceBlock) { | ||
AttachToWebContents(web_contents()); | ||
SendCommandSync("Network.enable"); | ||
|
||
const GURL& url = embedded_test_server()->GetURL("/blocking.html"); | ||
UpdateCustomAdBlockInstanceWithRules("*ad_banner.png"); | ||
NavigateToURL(url); | ||
|
||
ASSERT_EQ(true, EvalJs(web_contents(), | ||
"setExpectations(0, 1, 0, 0);" | ||
"addImage('ad_banner.png')")); | ||
const base::Value::Dict& notification = | ||
WaitForNotification("Network.requestAdblockInfoReceived", true); | ||
|
||
const auto* info = notification.FindDict("info"); | ||
ASSERT_TRUE(info); | ||
const GURL& image_url = embedded_test_server()->GetURL("/ad_banner.png"); | ||
EXPECT_EQ(image_url.spec(), *info->FindString("requestUrl")); | ||
EXPECT_EQ("Image", *info->FindString("resourceType")); | ||
EXPECT_TRUE(*info->FindBool("blocked")); | ||
EXPECT_TRUE(*info->FindBool("didMatchRule")); | ||
} | ||
|
||
IN_PROC_BROWSER_TEST_F(AdblockDevtoolsTest, Exception) { | ||
AttachToWebContents(web_contents()); | ||
SendCommandSync("Network.enable"); | ||
|
||
const GURL& url = embedded_test_server()->GetURL("/blocking.html"); | ||
UpdateAdBlockInstanceWithRules("*ad_banner.png"); | ||
UpdateCustomAdBlockInstanceWithRules("@@ad_banner.png"); | ||
NavigateToURL(url); | ||
|
||
ASSERT_EQ(true, EvalJs(web_contents(), | ||
"setExpectations(1, 0, 0, 0);" | ||
"addImage('ad_banner.png')")); | ||
const base::Value::Dict& notification = | ||
WaitForNotification("Network.requestAdblockInfoReceived", true); | ||
|
||
const auto* info = notification.FindDict("info"); | ||
ASSERT_TRUE(info); | ||
const GURL& image_url = embedded_test_server()->GetURL("/ad_banner.png"); | ||
EXPECT_EQ(image_url.spec(), *info->FindString("requestUrl")); | ||
EXPECT_EQ("Image", *info->FindString("resourceType")); | ||
EXPECT_FALSE(*info->FindBool("blocked")); | ||
EXPECT_TRUE(*info->FindBool("didMatchRule")); | ||
EXPECT_TRUE(*info->FindBool("didMatchException")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
chromium_src/content/browser/devtools/protocol/network_handler.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "content/browser/devtools/protocol/network_handler.h" | ||
|
||
#include "src/content/browser/devtools/protocol/network_handler.cc" | ||
|
||
namespace content::protocol { | ||
|
||
void NetworkHandler::RequestAdblockInfoReceived( | ||
const std::string& request_id, | ||
std::unique_ptr<protocol::Network::AdblockInfo> info) { | ||
if (!enabled_) { | ||
return; | ||
} | ||
|
||
frontend_->RequestAdblockInfoReceived(request_id, std::move(info)); | ||
} | ||
|
||
} // namespace content::protocol |
19 changes: 19 additions & 0 deletions
19
chromium_src/content/browser/devtools/protocol/network_handler.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_CHROMIUM_SRC_CONTENT_BROWSER_DEVTOOLS_PROTOCOL_NETWORK_HANDLER_H_ | ||
#define BRAVE_CHROMIUM_SRC_CONTENT_BROWSER_DEVTOOLS_PROTOCOL_NETWORK_HANDLER_H_ | ||
|
||
#define NavigationRequestWillBeSent(...) \ | ||
NavigationRequestWillBeSent(__VA_ARGS__); \ | ||
void RequestAdblockInfoReceived( \ | ||
const std::string& request_id, \ | ||
std::unique_ptr<protocol::Network::AdblockInfo> info) | ||
|
||
#include "src/content/browser/devtools/protocol/network_handler.h" // IWYU pragma: export | ||
|
||
#undef NavigationRequestWillBeSent | ||
|
||
#endif // BRAVE_CHROMIUM_SRC_CONTENT_BROWSER_DEVTOOLS_PROTOCOL_NETWORK_HANDLER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
chromium_src/third_party/devtools-frontend/src/front_end/core/sdk/NetworkManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
type NetworkDispatcherType = new (...args: any[]) => { | ||
clearRequests(): void | ||
requestForId(id: string): any | ||
// private updateNetworkRequest: (request: NetworkRequest) => void; | ||
} | ||
|
||
export function PatchNetworkDispatcher( | ||
NetworkDispatcher: NetworkDispatcherType | ||
): any { | ||
return class extends NetworkDispatcher { | ||
#requestsAdblockInfo: Map<string, object> | undefined | ||
|
||
adblockInfoForId(id: string): object | null { | ||
return this.#requestsAdblockInfo?.get(id) || null | ||
} | ||
|
||
requestAdblockInfoReceived(params: any): void { | ||
if (!this.#requestsAdblockInfo) { | ||
this.#requestsAdblockInfo = new Map() | ||
} | ||
this.#requestsAdblockInfo.set(params.requestId, params.info) | ||
const networkRequest = this.requestForId(params.requestId) | ||
if (networkRequest) { | ||
;(this as any).updateNetworkRequest(networkRequest) | ||
} | ||
} | ||
|
||
override clearRequests(): void { | ||
if (this.#requestsAdblockInfo) { | ||
for (const [requestId, _] of this.#requestsAdblockInfo) { | ||
const request = this.requestForId(requestId) | ||
if (request?.finished) { | ||
this.#requestsAdblockInfo.delete(requestId) | ||
} | ||
} | ||
} | ||
|
||
super.clearRequests() | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ium_src/third_party/devtools-frontend/src/front_end/panels/network/NetworkDataGridNode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
import * as SDK from '../../core/sdk/sdk.js' | ||
|
||
type NetworkRequestNodeType = new (...args: any[]) => { | ||
request(): SDK.NetworkRequest.NetworkRequest | ||
existingElement(): Element | null | ||
updateBackgroundColor(): void | ||
} | ||
|
||
export function PatchNetworkRequestNode( | ||
NetworkRequestNode: NetworkRequestNodeType | ||
): any { | ||
return class extends NetworkRequestNode { | ||
override updateBackgroundColor(): void { | ||
const networkManager = SDK.NetworkManager.NetworkManager.forRequest( | ||
this.request() | ||
) | ||
if (networkManager && networkManager.dispatcher) { | ||
const info = (networkManager.dispatcher as any).adblockInfoForId( | ||
this.request().requestId() | ||
) | ||
const element = this.existingElement() as HTMLElement | ||
if (info && element) { | ||
if ('blocked' in info && info.blocked) { | ||
element.style.color = 'var(--sys-color-error)' | ||
} else if ('didMatchException' in info && info.didMatchException) { | ||
element.style.color = 'var(--ref-palette-green60)' | ||
} | ||
} | ||
} | ||
super.updateBackgroundColor() | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
chromium_src/third_party/devtools-frontend/src/front_end/panels/network/NetworkItemView.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
import * as SDK from '../../core/sdk/sdk.js' | ||
import * as LegacyWrapper from '../../ui/components/legacy_wrapper/legacy_wrapper.js' | ||
import * as UI from '../../ui/legacy/legacy.js' | ||
|
||
import { RequestAdblockView } from './RequestAdblockView.js' | ||
|
||
type NetworkItemViewType = new (...args: any[]) => { | ||
request(): SDK.NetworkRequest.NetworkRequest | ||
appendTab( | ||
id: string, | ||
tabTitle: string, | ||
view: any, | ||
tabTooltip?: string, | ||
userGesture?: boolean, | ||
isCloseable?: boolean, | ||
isPreviewFeature?: boolean, | ||
index?: number, | ||
jslogContext?: string | ||
): void | ||
} | ||
|
||
export function PatchNetworkItemView( | ||
NetworkItemView: NetworkItemViewType | ||
): any { | ||
return class extends NetworkItemView { | ||
constructor(...args: any[]) { | ||
super(...args) | ||
this.appendTab( | ||
'blocking', | ||
'Adblock', | ||
LegacyWrapper.LegacyWrapper.legacyWrapper( | ||
UI.Widget.VBox, | ||
new RequestAdblockView(this.request()) | ||
), | ||
'Adblock' | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.