File: background_contents.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (142 lines) | stat: -rw-r--r-- 5,654 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
// 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.

#include "chrome/browser/background/background_contents.h"

#include <utility>

#include "chrome/browser/background/background_contents_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/task_manager/web_contents_tags.h"
#include "chrome/common/url_constants.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/session_storage_namespace.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_host_delegate.h"
#include "extensions/browser/extension_host_queue.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/view_type_utils.h"
#include "extensions/common/mojom/view_type.mojom.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "ui/gfx/geometry/rect.h"

using content::SiteInstance;
using content::WebContents;

BackgroundContents::BackgroundContents(
    scoped_refptr<SiteInstance> site_instance,
    content::RenderFrameHost* opener,
    bool is_new_browsing_instance,
    Delegate* delegate,
    const content::StoragePartitionConfig& partition_config,
    content::SessionStorageNamespace* session_storage_namespace)
    : delegate_(delegate),
      extension_host_delegate_(extensions::ExtensionsBrowserClient::Get()
                                   ->CreateExtensionHostDelegate()) {
  profile_ = Profile::FromBrowserContext(
      site_instance->GetBrowserContext());

  WebContents::CreateParams create_params(profile_, std::move(site_instance));
  create_params.is_never_composited = true;
  create_params.opener_render_process_id =
      opener ? opener->GetProcess()->GetDeprecatedID() : MSG_ROUTING_NONE;
  create_params.opener_render_frame_id =
      opener ? opener->GetRoutingID() : MSG_ROUTING_NONE;

  if (session_storage_namespace) {
    content::SessionStorageNamespaceMap session_storage_namespace_map;
    session_storage_namespace_map.insert(
        std::make_pair(partition_config, session_storage_namespace));
    web_contents_ = WebContents::CreateWithSessionStorage(
        create_params, session_storage_namespace_map);
  } else {
    web_contents_ = WebContents::Create(create_params);
  }
  web_contents_->SetOwnerLocationForDebug(FROM_HERE);
  extensions::SetViewType(web_contents_.get(),
                          extensions::mojom::ViewType::kBackgroundContents);
  web_contents_->SetDelegate(this);
  content::WebContentsObserver::Observe(web_contents_.get());

  // Add the TaskManager-specific tag for the BackgroundContents.
  task_manager::WebContentsTags::CreateForBackgroundContents(
      web_contents_.get(), this);
}

// Exposed to allow creating mocks.
BackgroundContents::BackgroundContents() = default;

BackgroundContents::~BackgroundContents() {
  if (!web_contents_.get())   // Will be null for unit tests.
    return;

  extensions::ExtensionHostQueue::GetInstance().Remove(this);
}

const GURL& BackgroundContents::GetURL() const {
  return web_contents_.get() ? web_contents_->GetURL() : GURL::EmptyGURL();
}

void BackgroundContents::CreateRendererSoon(const GURL& url) {
  initial_url_ = url;
  extensions::ExtensionHostQueue::GetInstance().Add(this);
}

void BackgroundContents::CloseContents(WebContents* source) {
  delegate_->OnBackgroundContentsClosed(this);
  // |this| is deleted.
}

bool BackgroundContents::ShouldSuppressDialogs(WebContents* source) {
  return true;
}

void BackgroundContents::PrimaryPageChanged(content::Page& page) {
  // Note: because BackgroundContents are only available to extension apps,
  // navigation is limited to urls within the app's extent. This is enforced in
  // RenderView::decidePolicyForNavigation. If BackgroundContents become
  // available as a part of the web platform, it probably makes sense to have
  // some way to scope navigation of a background page to its opener's security
  // origin. Note: if the first navigation is to a URL outside the app's
  // extent a background page will be opened but will remain at about:blank.
  delegate_->OnBackgroundContentsNavigated(this);
}

// Forward requests to add a new WebContents to our delegate.
WebContents* BackgroundContents::AddNewContents(
    WebContents* source,
    std::unique_ptr<WebContents> new_contents,
    const GURL& target_url,
    WindowOpenDisposition disposition,
    const blink::mojom::WindowFeatures& window_features,
    bool user_gesture,
    bool* was_blocked) {
  delegate_->AddWebContents(std::move(new_contents), target_url, disposition,
                            window_features, was_blocked);
  return nullptr;
}

void BackgroundContents::PrimaryMainFrameRenderProcessGone(
    base::TerminationStatus status) {
  delegate_->OnBackgroundContentsTerminated(this);
  // |this| is deleted.
}

void BackgroundContents::CreateRendererNow() {
  base::WeakPtr<content::NavigationHandle> handle =
      web_contents()->GetController().LoadURL(initial_url_, content::Referrer(),
                                              ui::PAGE_TRANSITION_LINK,
                                              std::string());
  if (handle) {
    ukm::builders::Extensions_BackgroundContentsCreated(
        handle->GetNextPageUkmSourceId())
        .SetSeen(true)
        .Record(ukm::UkmRecorder::Get());
  }
}