File: web_navigation_api_helpers.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (359 lines) | stat: -rw-r--r-- 16,551 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Implements the Chrome Extensions WebNavigation API.

#include "chrome/browser/extensions/api/web_navigation/web_navigation_api_helpers.h"

#include <memory>
#include <utility>

#include "base/json/json_writer.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
#include "chrome/browser/extensions/api/web_navigation/web_navigation_api_constants.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/web_navigation.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_api_frame_id_map.h"
#include "extensions/common/mojom/event_dispatcher.mojom.h"
#include "net/base/net_errors.h"
#include "ui/base/page_transition_types.h"

namespace extensions {

namespace web_navigation = api::web_navigation;

namespace web_navigation_api_helpers {

namespace {

// Dispatches events to the extension message service.
void DispatchEvent(content::BrowserContext* browser_context,
                   std::unique_ptr<Event> event,
                   const GURL& url) {
  Profile* profile = Profile::FromBrowserContext(browser_context);
  EventRouter* event_router = EventRouter::Get(profile);
  if (profile && event_router) {
    mojom::EventFilteringInfoPtr info = mojom::EventFilteringInfo::New();
    info->url = url;
    DCHECK_EQ(profile, event->restrict_to_browser_context);
    event->filter_info = std::move(info);
    event_router->BroadcastEvent(std::move(event));
  }
}

}  // namespace

// Constructs an onBeforeNavigate event.
std::unique_ptr<Event> CreateOnBeforeNavigateEvent(
    content::NavigationHandle* navigation_handle) {
  GURL url(navigation_handle->GetURL());

  web_navigation::OnBeforeNavigate::Details details;
  details.tab_id =
      ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents());
  details.url = url.spec();
  details.process_id = -1;
  // There is no documentId for this event because the document has not
  // been created yet. It will first appear in the OnCommitted event. The
  // frameId can be used to associate OnBeforeNavigate and OnCommitted together.
  details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle);
  details.parent_frame_id =
      ExtensionApiFrameIdMap::GetParentFrameId(navigation_handle);
  // Only set the parentDocumentId value if we have a parent.
  if (content::RenderFrameHost* parent_frame_host =
          navigation_handle->GetParentFrameOrOuterDocument()) {
    details.parent_document_id =
        ExtensionApiFrameIdMap::GetDocumentId(parent_frame_host).ToString();
  }
  details.frame_type = ExtensionApiFrameIdMap::GetFrameType(navigation_handle);
  details.document_lifecycle =
      ExtensionApiFrameIdMap::GetDocumentLifecycle(navigation_handle);
  details.time_stamp = base::Time::Now().InMillisecondsFSinceUnixEpoch();

  auto event = std::make_unique<Event>(
      events::WEB_NAVIGATION_ON_BEFORE_NAVIGATE,
      web_navigation::OnBeforeNavigate::kEventName,
      web_navigation::OnBeforeNavigate::Create(details),
      navigation_handle->GetWebContents()->GetBrowserContext());

  mojom::EventFilteringInfoPtr info = mojom::EventFilteringInfo::New();
  info->url = navigation_handle->GetURL();
  event->filter_info = std::move(info);

  return event;
}

// Constructs and dispatches an onCommitted, onReferenceFragmentUpdated
// or onHistoryStateUpdated event.
void DispatchOnCommitted(events::HistogramValue histogram_value,
                         const std::string& event_name,
                         content::NavigationHandle* navigation_handle) {
  content::WebContents* web_contents = navigation_handle->GetWebContents();
  GURL url(navigation_handle->GetURL());
  content::RenderFrameHost* frame_host =
      navigation_handle->GetRenderFrameHost();
  ui::PageTransition transition_type = navigation_handle->GetPageTransition();

  base::Value::List args;
  base::Value::Dict dict;
  dict.Set(web_navigation_api_constants::kTabIdKey,
           ExtensionTabUtil::GetTabId(web_contents));
  dict.Set(web_navigation_api_constants::kUrlKey, url.spec());
  dict.Set(web_navigation_api_constants::kProcessIdKey,
           frame_host->GetProcess()->GetDeprecatedID());
  dict.Set(web_navigation_api_constants::kFrameIdKey,
           ExtensionApiFrameIdMap::GetFrameId(frame_host));
  dict.Set(web_navigation_api_constants::kParentFrameIdKey,
           ExtensionApiFrameIdMap::GetParentFrameId(frame_host));
  dict.Set(web_navigation_api_constants::kDocumentIdKey,
           ExtensionApiFrameIdMap::GetDocumentId(frame_host).ToString());
  // Only set the parentDocumentId value if we have a parent.
  if (content::RenderFrameHost* parent_frame_host =
          frame_host->GetParentOrOuterDocument()) {
    dict.Set(
        web_navigation_api_constants::kParentDocumentIdKey,
        ExtensionApiFrameIdMap::GetDocumentId(parent_frame_host).ToString());
  }
  dict.Set(web_navigation_api_constants::kFrameTypeKey,
           ToString(ExtensionApiFrameIdMap::GetFrameType(frame_host)));
  dict.Set(web_navigation_api_constants::kDocumentLifecycleKey,
           ToString(ExtensionApiFrameIdMap::GetDocumentLifecycle(frame_host)));

  if (navigation_handle->WasServerRedirect()) {
    transition_type = ui::PageTransitionFromInt(
        transition_type | ui::PAGE_TRANSITION_SERVER_REDIRECT);
  }

  std::string transition_type_string =
      ui::PageTransitionGetCoreTransitionString(transition_type);
  // For webNavigation API backward compatibility, keep "start_page" even after
  // renamed to "auto_toplevel".
  if (ui::PageTransitionCoreTypeIs(transition_type,
                                   ui::PAGE_TRANSITION_AUTO_TOPLEVEL))
    transition_type_string = "start_page";
  dict.Set(web_navigation_api_constants::kTransitionTypeKey,
           transition_type_string);
  base::Value::List qualifiers;
  if (transition_type & ui::PAGE_TRANSITION_CLIENT_REDIRECT)
    qualifiers.Append("client_redirect");
  if (transition_type & ui::PAGE_TRANSITION_SERVER_REDIRECT)
    qualifiers.Append("server_redirect");
  if (transition_type & ui::PAGE_TRANSITION_FORWARD_BACK)
    qualifiers.Append("forward_back");
  if (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)
    qualifiers.Append("from_address_bar");
  dict.Set(web_navigation_api_constants::kTransitionQualifiersKey,
           std::move(qualifiers));
  dict.Set(web_navigation_api_constants::kTimeStampKey,
           base::Time::Now().InMillisecondsFSinceUnixEpoch());
  args.Append(std::move(dict));

  content::BrowserContext* browser_context =
      navigation_handle->GetWebContents()->GetBrowserContext();
  auto event = std::make_unique<Event>(histogram_value, event_name,
                                       std::move(args), browser_context);
  DispatchEvent(browser_context, std::move(event), url);
}

// Constructs and dispatches an onDOMContentLoaded event.
void DispatchOnDOMContentLoaded(content::WebContents* web_contents,
                                content::RenderFrameHost* frame_host,
                                const GURL& url) {
  web_navigation::OnDOMContentLoaded::Details details;
  details.tab_id = ExtensionTabUtil::GetTabId(web_contents);
  details.url = url.spec();
  details.process_id = frame_host->GetProcess()->GetDeprecatedID();
  details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host);
  details.parent_frame_id =
      ExtensionApiFrameIdMap::GetParentFrameId(frame_host);
  details.document_id =
      ExtensionApiFrameIdMap::GetDocumentId(frame_host).ToString();
  // Only set the parentDocumentId value if we have a parent.
  if (content::RenderFrameHost* parent_frame_host =
          frame_host->GetParentOrOuterDocument()) {
    details.parent_document_id =
        ExtensionApiFrameIdMap::GetDocumentId(parent_frame_host).ToString();
  }
  details.frame_type = ExtensionApiFrameIdMap::GetFrameType(frame_host);
  details.document_lifecycle =
      ExtensionApiFrameIdMap::GetDocumentLifecycle(frame_host);
  details.time_stamp = base::Time::Now().InMillisecondsFSinceUnixEpoch();

  content::BrowserContext* browser_context = web_contents->GetBrowserContext();
  auto event = std::make_unique<Event>(
      events::WEB_NAVIGATION_ON_DOM_CONTENT_LOADED,
      web_navigation::OnDOMContentLoaded::kEventName,
      web_navigation::OnDOMContentLoaded::Create(details), browser_context);
  DispatchEvent(browser_context, std::move(event), url);
}

// Constructs and dispatches an onCompleted event.
void DispatchOnCompleted(content::WebContents* web_contents,
                         content::RenderFrameHost* frame_host,
                         const GURL& url) {
  web_navigation::OnCompleted::Details details;
  details.tab_id = ExtensionTabUtil::GetTabId(web_contents);
  details.url = url.spec();
  details.process_id = frame_host->GetProcess()->GetDeprecatedID();
  details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host);
  details.parent_frame_id =
      ExtensionApiFrameIdMap::GetParentFrameId(frame_host);
  details.document_id =
      ExtensionApiFrameIdMap::GetDocumentId(frame_host).ToString();
  // Only set the parentDocumentId value if we have a parent.
  if (content::RenderFrameHost* parent_frame_host =
          frame_host->GetParentOrOuterDocument()) {
    details.parent_document_id =
        ExtensionApiFrameIdMap::GetDocumentId(parent_frame_host).ToString();
  }
  details.frame_type = ExtensionApiFrameIdMap::GetFrameType(frame_host);
  details.document_lifecycle =
      ExtensionApiFrameIdMap::GetDocumentLifecycle(frame_host);
  details.time_stamp = base::Time::Now().InMillisecondsFSinceUnixEpoch();

  content::BrowserContext* browser_context = web_contents->GetBrowserContext();
  auto event = std::make_unique<Event>(
      events::WEB_NAVIGATION_ON_COMPLETED,
      web_navigation::OnCompleted::kEventName,
      web_navigation::OnCompleted::Create(details), browser_context);
  DispatchEvent(browser_context, std::move(event), url);
}

// Constructs and dispatches an onCreatedNavigationTarget event.
void DispatchOnCreatedNavigationTarget(
    int source_tab_id,
    int source_render_process_id,
    int source_extension_frame_id,
    content::BrowserContext* browser_context,
    content::WebContents* target_web_contents,
    const GURL& target_url) {
  // Check that the tab is already inserted into a tab strip model. This code
  // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab.
  DCHECK(ExtensionTabUtil::GetTabById(
      ExtensionTabUtil::GetTabId(target_web_contents),
      Profile::FromBrowserContext(target_web_contents->GetBrowserContext()),
      false, nullptr));

  web_navigation::OnCreatedNavigationTarget::Details details;
  details.source_tab_id = source_tab_id;
  details.source_process_id = source_render_process_id;
  details.source_frame_id = source_extension_frame_id;
  details.url = target_url.possibly_invalid_spec();
  details.tab_id = ExtensionTabUtil::GetTabId(target_web_contents);
  details.time_stamp = base::Time::Now().InMillisecondsFSinceUnixEpoch();

  auto event = std::make_unique<Event>(
      events::WEB_NAVIGATION_ON_CREATED_NAVIGATION_TARGET,
      web_navigation::OnCreatedNavigationTarget::kEventName,
      web_navigation::OnCreatedNavigationTarget::Create(details),
      browser_context);
  DispatchEvent(browser_context, std::move(event), target_url);

  // If the target WebContents already received the onBeforeNavigate event,
  // send it immediately after the onCreatedNavigationTarget above.
  WebNavigationTabObserver* target_observer =
      WebNavigationTabObserver::Get(target_web_contents);
  target_observer->DispatchCachedOnBeforeNavigate();
}

// Constructs and dispatches an onErrorOccurred event.
void DispatchOnErrorOccurred(content::WebContents* web_contents,
                             content::RenderFrameHost* frame_host,
                             const GURL& url,
                             int error_code) {
  web_navigation::OnErrorOccurred::Details details;
  details.tab_id = ExtensionTabUtil::GetTabId(web_contents);
  details.url = url.spec();
  details.process_id = frame_host->GetProcess()->GetDeprecatedID();
  details.frame_id = ExtensionApiFrameIdMap::GetFrameId(frame_host);
  details.parent_frame_id =
      ExtensionApiFrameIdMap::GetParentFrameId(frame_host);
  details.error = net::ErrorToString(error_code);
  details.document_id =
      ExtensionApiFrameIdMap::GetDocumentId(frame_host).ToString();
  // Only set the parentDocumentId value if we have a parent.
  if (content::RenderFrameHost* parent_frame_host =
          frame_host->GetParentOrOuterDocument()) {
    details.parent_document_id =
        ExtensionApiFrameIdMap::GetDocumentId(parent_frame_host).ToString();
  }
  details.frame_type = ExtensionApiFrameIdMap::GetFrameType(frame_host);
  details.document_lifecycle =
      ExtensionApiFrameIdMap::GetDocumentLifecycle(frame_host);
  details.time_stamp = base::Time::Now().InMillisecondsFSinceUnixEpoch();

  content::BrowserContext* browser_context = web_contents->GetBrowserContext();
  auto event =
      std::make_unique<Event>(events::WEB_NAVIGATION_ON_ERROR_OCCURRED,
                              web_navigation::OnErrorOccurred::kEventName,
                              web_navigation::OnErrorOccurred::Create(details),
                              web_contents->GetBrowserContext());
  DispatchEvent(browser_context, std::move(event), url);
}

void DispatchOnErrorOccurred(content::NavigationHandle* navigation_handle) {
  web_navigation::OnErrorOccurred::Details details;
  details.tab_id =
      ExtensionTabUtil::GetTabId(navigation_handle->GetWebContents());
  details.url = navigation_handle->GetURL().spec();
  details.process_id = -1;
  details.frame_id = ExtensionApiFrameIdMap::GetFrameId(navigation_handle);
  details.parent_frame_id =
      ExtensionApiFrameIdMap::GetParentFrameId(navigation_handle);
  details.error = (navigation_handle->GetNetErrorCode() != net::OK)
                      ? net::ErrorToString(navigation_handle->GetNetErrorCode())
                      : net::ErrorToString(net::ERR_ABORTED);
  details.document_id =
      ExtensionApiFrameIdMap::GetDocumentId(navigation_handle).ToString();
  // Only set the parentDocumentId value if we have a parent.
  if (content::RenderFrameHost* parent_frame_host =
          navigation_handle->GetParentFrameOrOuterDocument()) {
    details.parent_document_id =
        ExtensionApiFrameIdMap::GetDocumentId(parent_frame_host).ToString();
  }
  details.frame_type = ExtensionApiFrameIdMap::GetFrameType(navigation_handle);
  details.document_lifecycle =
      ExtensionApiFrameIdMap::GetDocumentLifecycle(navigation_handle);
  details.time_stamp = base::Time::Now().InMillisecondsFSinceUnixEpoch();

  content::BrowserContext* browser_context =
      navigation_handle->GetWebContents()->GetBrowserContext();
  auto event = std::make_unique<Event>(
      events::WEB_NAVIGATION_ON_ERROR_OCCURRED,
      web_navigation::OnErrorOccurred::kEventName,
      web_navigation::OnErrorOccurred::Create(details), browser_context);
  DispatchEvent(browser_context, std::move(event), navigation_handle->GetURL());
}

// Constructs and dispatches an onTabReplaced event.
void DispatchOnTabReplaced(
    content::WebContents* old_web_contents,
    content::BrowserContext* browser_context,
    content::WebContents* new_web_contents) {
  web_navigation::OnTabReplaced::Details details;
  details.replaced_tab_id = ExtensionTabUtil::GetTabId(old_web_contents);
  details.tab_id = ExtensionTabUtil::GetTabId(new_web_contents);
  details.time_stamp = base::Time::Now().InMillisecondsFSinceUnixEpoch();

  auto event = std::make_unique<Event>(
      events::WEB_NAVIGATION_ON_TAB_REPLACED,
      web_navigation::OnTabReplaced::kEventName,
      web_navigation::OnTabReplaced::Create(details), browser_context);
  DispatchEvent(browser_context, std::move(event), GURL());
}

}  // namespace web_navigation_api_helpers

}  // namespace extensions