Skip to content

Commit ce511e8

Browse files
committed
Added nullchecks to prevent page crash
1 parent 97877aa commit ce511e8

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

src/components/action_url/content/browser/content_action_url_driver.cc

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class ActionUrlHandlerAndroid;
2929
namespace {
3030

3131
bool IsRenderFrameHostSupported(content::RenderFrameHost* rfh) {
32+
if (!rfh) {
33+
return false;
34+
}
35+
3236
if (rfh->GetLifecycleState() ==
3337
content::RenderFrameHost::LifecycleState::kPendingCommit) {
3438
return true;
@@ -46,13 +50,21 @@ bool IsRenderFrameHostSupported(content::RenderFrameHost* rfh) {
4650
ContentActionUrlDriver::ContentActionUrlDriver(
4751
content::RenderFrameHost* render_frame_host)
4852
: render_frame_host_(render_frame_host) {
53+
if (!render_frame_host_) {
54+
LOG(ERROR) << "AMIT ContentActionUrlDriver: render_frame_host is null!";
55+
return;
56+
}
4957
static unsigned next_free_id = 0;
5058
id_ = next_free_id++;
5159
handler_processing_remaining_ = 0;
5260
action_block_draw_remaining_ = 0;
5361
requested_for_header_ = false;
5462

55-
LOG(INFO) << "AMIT ContentActionUrlDriver constructor before get interface";
63+
if (!render_frame_host_->GetRemoteAssociatedInterfaces()) {
64+
LOG(ERROR) << "AMIT ContentActionUrlDriver: RemoteAssociatedInterfaces is null!";
65+
return;
66+
}
67+
5668
render_frame_host_->GetRemoteAssociatedInterfaces()->GetInterface(
5769
&action_url_agent_);
5870
LOG(INFO) << "AMIT action_url_agent_ is bound: " << action_url_agent_.is_bound();
@@ -64,12 +76,23 @@ ContentActionUrlDriver::~ContentActionUrlDriver() = default;
6476
void ContentActionUrlDriver::BindPendingReceiver(
6577
mojo::PendingAssociatedReceiver<action_url::mojom::ActionUrlDriver>
6678
pending_receiver) {
79+
if (!render_frame_host_) {
80+
LOG(ERROR) << "AMIT BindPendingReceiver: render_frame_host_ is null!";
81+
return;
82+
}
83+
6784
if (IsRenderFrameHostSupported(render_frame_host_)) {
6885
action_url_receiver_.Bind(std::move(pending_receiver));
6986
}
7087
}
7188

7289
void ContentActionUrlDriver::DidNavigate() {
90+
if (!render_frame_host_) {
91+
LOG(ERROR) << "AMIT DidNavigate: render_frame_host_ is null!";
92+
action_url_receiver_.reset();
93+
return;
94+
}
95+
7396
if (!IsRenderFrameHostSupported(render_frame_host_)) {
7497
action_url_receiver_.reset();
7598
}
@@ -83,6 +106,11 @@ int ContentActionUrlDriver::GetId() const {
83106
void ContentActionUrlDriver::AllAnchorsParsed(
84107
const std::vector<action_url::AnchorData>& anchors_data) {
85108

109+
if (!render_frame_host_) {
110+
LOG(ERROR) << "AMIT AllAnchorsParsed: render_frame_host_ is null!";
111+
return;
112+
}
113+
86114
Profile* profile = Profile::FromBrowserContext(render_frame_host_->GetBrowserContext());
87115
if (!profile->GetPrefs()->GetBoolean(action_url::prefs::kBlinksEnabled)) {
88116
LOG(INFO) << "AMIT Blinks are disabled";
@@ -165,6 +193,11 @@ void ContentActionUrlDriver::ActionUrlFetched(action_url::AnchorData anchor,
165193
void ContentActionUrlDriver::ProcessActionUrl(GURL action_url,
166194
action_url::AnchorData anchor,
167195
std::string tag) {
196+
if (!render_frame_host_) {
197+
LOG(ERROR) << "AMIT ProcessActionUrl: render_frame_host_ is null!";
198+
return;
199+
}
200+
168201
std::unique_ptr<network::ResourceRequest> request =
169202
std::make_unique<network::ResourceRequest>();
170203
request->url = action_url;
@@ -228,6 +261,12 @@ void ContentActionUrlDriver::OnDownloadedJson(
228261
const mojo::AssociatedRemote<action_url::mojom::ActionUrlAgent>&
229262
ContentActionUrlDriver::GetActionUrlAgent() {
230263
LOG(INFO) << "AMIT GetActionUrlAgent";
264+
265+
if (!render_frame_host_) {
266+
LOG(ERROR) << "AMIT GetActionUrlAgent: render_frame_host_ is null!";
267+
return action_url_agent_unbound_;
268+
}
269+
231270
CHECK_NE(render_frame_host_->GetLifecycleState(),
232271
content::RenderFrameHost::LifecycleState::kPendingCommit);
233272
LOG(INFO) << "AMIT GetActionUrlAgent 2";

src/components/action_url/content/renderer/action_url_agent.cc

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ mojom::ActionUrlDriver& ActionUrlAgent::GetActionUrlDriver() {
5454
// return *deferring_password_manager_driver_;
5555
// }
5656
LOG(INFO) << "AMIT GetActionUrlDriver";
57+
if (!render_frame()) {
58+
LOG(ERROR) << "AMIT GetActionUrlDriver: render_frame() is null!";
59+
// Return a static unbound driver to prevent crash
60+
static mojo::AssociatedRemote<mojom::ActionUrlDriver> unbound_driver_;
61+
return *unbound_driver_;
62+
}
63+
5764
// Lazily bind this interface.
5865
if (!action_url_driver_) {
5966
LOG(INFO) << "AMIT GetActionUrlDriver 1";
@@ -70,12 +77,23 @@ void ActionUrlAgent::Reset() {
7077
action_block_counter_ = 1;
7178
renderer_anchor_cache_.clear();
7279
buffer_for_anchors_.clear();
73-
GetActionUrlDriver().Reset();
80+
81+
if (render_frame()) {
82+
GetActionUrlDriver().Reset();
83+
} else {
84+
LOG(ERROR) << "AMIT Reset: render_frame() is null!";
85+
}
7486
}
7587

7688
// mojom::ActionUrlAgent:
7789
void ActionUrlAgent::SetUpHeader() {
7890
LOG(INFO) << "Unfurling :: " << __func__;
91+
92+
if (!render_frame()) {
93+
LOG(ERROR) << "AMIT SetUpHeader: render_frame() is null!";
94+
return;
95+
}
96+
7997
render_frame()->GetWebFrame()->GetDocument().SetUpActionUrlHeader();
8098
}
8199

@@ -107,6 +125,12 @@ void ActionUrlAgent::ReplaceUrL(const std::string& json,
107125
void ActionUrlAgent::SetUpScriptBlock() {
108126
LOG(INFO)<< "AMIT Setting up action url script block in action url agent";
109127
LOG(INFO) << "Unfurling :: " << __func__;
128+
129+
if (!render_frame()) {
130+
LOG(ERROR) << "AMIT SetUpScriptBlock: render_frame() is null!";
131+
return;
132+
}
133+
110134
render_frame()->GetWebFrame()->GetDocument().SetUpActionUrlScriptBlock();
111135
}
112136

@@ -127,6 +151,12 @@ void ActionUrlAgent::DidCommitProvisionalLoad(ui::PageTransition transition) {
127151

128152
void ActionUrlAgent::DidCreateDocumentElement() {
129153
Reset();
154+
155+
if (!render_frame()) {
156+
LOG(ERROR) << "AMIT DidCreateDocumentElement: render_frame() is null!";
157+
return;
158+
}
159+
130160
render_frame()->GetWebFrame()->GetDocument().ResetScriptState();
131161
}
132162

@@ -135,6 +165,12 @@ void ActionUrlAgent::DidDispatchDOMContentLoadedEvent() {
135165
// Parse the content and find all anchor elements
136166
LOG(INFO) << "Unfurling :: " << __func__;
137167
is_dom_content_loaded_ = true;
168+
169+
if (!render_frame()) {
170+
LOG(ERROR) << "AMIT DidDispatchDOMContentLoadedEvent: render_frame() is null!";
171+
return;
172+
}
173+
138174
render_frame()->GetWebFrame()->GetDocument().ResetScriptState();
139175
FindAnchorElementsOnPage(false);
140176
}
@@ -158,10 +194,23 @@ void ActionUrlAgent::WaitTillDynamicChangeTimer(base::OneShotTimer& timer) {
158194

159195
// Top-level wrapper call to trigger DOM traversal to find anchor element.
160196
void ActionUrlAgent::FindAnchorElementsOnPage(bool is_dynamic) {
197+
if (!render_frame()) {
198+
LOG(ERROR) << "AMIT FindAnchorElementsOnPage: render_frame() is null!";
199+
return;
200+
}
201+
161202
const blink::WebDocument doc = render_frame()->GetWebFrame()->GetDocument();
162203
if (doc.IsNull() || doc.Body().IsNull()) {
163204
return;
164205
}
206+
207+
// Enabling only for x.com pages
208+
GURL page_url = GURL(doc.Url());
209+
if (!page_url.is_valid() || page_url.host() != "x.com") {
210+
LOG(INFO) << "AMIT Skipping non-x.com page";
211+
return;
212+
}
213+
165214
WebVector<WebAnchorElement> anchor_elements;
166215
if (is_dynamic) {
167216
buffer_mu_.Acquire();
@@ -405,6 +454,12 @@ void ActionUrlAgent::OnJsonParsed(WebAnchorElement anchor,
405454
void ActionUrlAgent::ActionBlockDrawCompleted() {
406455
LOG(INFO) << "Unfurling :: " << __func__;
407456
LOG(INFO) << "AMIT Action block draw completed";
457+
458+
if (!render_frame()) {
459+
LOG(ERROR) << "AMIT ActionBlockDrawCompleted: render_frame() is null!";
460+
return;
461+
}
462+
408463
GetActionUrlDriver().OnBlockDrawCompleted();
409464
}
410465

0 commit comments

Comments
 (0)