Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/content_service_delegate_impl.h" |
| 6 | |
| 7 | #include "base/macros.h" |
Xiyuan Xia | b5ebe94b | 2018-12-21 19:24:04 | [diff] [blame] | 8 | #include "base/optional.h" |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 9 | #include "content/public/browser/navigation_handle.h" |
Ken Rockot | 6bcda46 | 2018-10-15 21:08:01 | [diff] [blame] | 10 | #include "content/public/browser/render_view_host.h" |
Xiyuan Xia | b5ebe94b | 2018-12-21 19:24:04 | [diff] [blame] | 11 | #include "content/public/browser/render_widget_host.h" |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 12 | #include "content/public/browser/render_widget_host_view.h" |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 13 | #include "content/public/browser/web_contents.h" |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 14 | #include "content/public/browser/web_contents_delegate.h" |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 15 | #include "content/public/browser/web_contents_observer.h" |
Ken Rockot | f892cf5f | 2018-07-16 21:32:19 | [diff] [blame] | 16 | #include "services/content/navigable_contents_delegate.h" |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 17 | #include "services/content/service.h" |
Leon Han | c819dc6 | 2019-01-28 04:30:19 | [diff] [blame] | 18 | #include "third_party/blink/public/mojom/renderer_preferences.mojom.h" |
Xiyuan Xia | b5ebe94b | 2018-12-21 19:24:04 | [diff] [blame] | 19 | #include "third_party/skia/include/core/SkColor.h" |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 20 | |
| 21 | namespace content { |
| 22 | |
| 23 | namespace { |
| 24 | |
Ken Rockot | f892cf5f | 2018-07-16 21:32:19 | [diff] [blame] | 25 | // Bridge between Content Service navigable contents delegation API and a |
| 26 | // WebContentsImpl. |
| 27 | class NavigableContentsDelegateImpl : public content::NavigableContentsDelegate, |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 28 | public WebContentsDelegate, |
Ken Rockot | f892cf5f | 2018-07-16 21:32:19 | [diff] [blame] | 29 | public WebContentsObserver { |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 30 | public: |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 31 | explicit NavigableContentsDelegateImpl( |
| 32 | BrowserContext* browser_context, |
| 33 | const mojom::NavigableContentsParams& params, |
| 34 | mojom::NavigableContentsClient* client) |
| 35 | : client_(client), |
David Black | 992c445 | 2018-11-09 21:01:31 | [diff] [blame] | 36 | enable_view_auto_resize_(params.enable_view_auto_resize), |
| 37 | auto_resize_min_size_( |
| 38 | params.auto_resize_min_size.value_or(gfx::Size(1, 1))), |
| 39 | auto_resize_max_size_( |
Xiyuan Xia | b5ebe94b | 2018-12-21 19:24:04 | [diff] [blame] | 40 | params.auto_resize_max_size.value_or(gfx::Size(INT_MAX, INT_MAX))), |
| 41 | background_color_(params.override_background_color |
| 42 | ? base::make_optional(params.background_color) |
| 43 | : base::nullopt) { |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 44 | WebContents::CreateParams create_params(browser_context); |
| 45 | web_contents_ = WebContents::Create(create_params); |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 46 | WebContentsObserver::Observe(web_contents_.get()); |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 47 | web_contents_->SetDelegate(this); |
Ken Rockot | 6bcda46 | 2018-10-15 21:08:01 | [diff] [blame] | 48 | |
Leon Han | c819dc6 | 2019-01-28 04:30:19 | [diff] [blame] | 49 | blink::mojom::RendererPreferences* renderer_prefs = |
Ken Rockot | 6bcda46 | 2018-10-15 21:08:01 | [diff] [blame] | 50 | web_contents_->GetMutableRendererPrefs(); |
| 51 | renderer_prefs->can_accept_load_drops = false; |
| 52 | renderer_prefs->browser_handles_all_top_level_requests = |
| 53 | params.suppress_navigations; |
| 54 | web_contents_->GetRenderViewHost()->SyncRendererPrefs(); |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 55 | } |
| 56 | |
Ken Rockot | f892cf5f | 2018-07-16 21:32:19 | [diff] [blame] | 57 | ~NavigableContentsDelegateImpl() override { |
| 58 | WebContentsObserver::Observe(nullptr); |
| 59 | } |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 60 | |
David Black | 531da0ed | 2019-01-29 03:59:52 | [diff] [blame] | 61 | bool TakeFocus(WebContents* source, bool reverse) override { |
| 62 | client_->ClearViewFocus(); |
| 63 | return true; |
| 64 | } |
| 65 | |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 66 | private: |
Ken Rockot | 71e957aa | 2018-12-10 20:49:53 | [diff] [blame] | 67 | void NotifyAXTreeChange() { |
| 68 | auto* rfh = web_contents_->GetMainFrame(); |
| 69 | if (rfh) |
| 70 | client_->UpdateContentAXTree(rfh->GetAXTreeID()); |
| 71 | else |
| 72 | client_->UpdateContentAXTree(ui::AXTreeIDUnknown()); |
| 73 | } |
| 74 | |
Ken Rockot | f892cf5f | 2018-07-16 21:32:19 | [diff] [blame] | 75 | // content::NavigableContentsDelegate: |
Ken Rockot | 99c5bc74 | 2018-07-12 15:36:56 | [diff] [blame] | 76 | gfx::NativeView GetNativeView() override { |
| 77 | return web_contents_->GetNativeView(); |
| 78 | } |
| 79 | |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 80 | void Navigate(const GURL& url, |
| 81 | content::mojom::NavigateParamsPtr params) override { |
| 82 | NavigationController::LoadURLParams load_url_params(url); |
| 83 | load_url_params.transition_type = ui::PAGE_TRANSITION_AUTO_TOPLEVEL; |
| 84 | load_url_params.should_clear_history_list = |
| 85 | params->should_clear_session_history; |
| 86 | web_contents_->GetController().LoadURLWithParams(load_url_params); |
| 87 | } |
| 88 | |
David Black | 992c445 | 2018-11-09 21:01:31 | [diff] [blame] | 89 | void GoBack( |
| 90 | content::mojom::NavigableContents::GoBackCallback callback) override { |
| 91 | content::NavigationController& controller = web_contents_->GetController(); |
| 92 | if (controller.CanGoBack()) { |
| 93 | std::move(callback).Run(/*success=*/true); |
| 94 | controller.GoBack(); |
| 95 | } else { |
| 96 | std::move(callback).Run(/*success=*/false); |
| 97 | } |
| 98 | } |
| 99 | |
Ken Rockot | 71e957aa | 2018-12-10 20:49:53 | [diff] [blame] | 100 | void Focus() override { web_contents_->Focus(); } |
| 101 | |
| 102 | void FocusThroughTabTraversal(bool reverse) override { |
| 103 | web_contents_->FocusThroughTabTraversal(reverse); |
| 104 | } |
| 105 | |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 106 | // WebContentsDelegate: |
David Black | 992c445 | 2018-11-09 21:01:31 | [diff] [blame] | 107 | bool ShouldCreateWebContents( |
| 108 | content::WebContents* web_contents, |
| 109 | content::RenderFrameHost* opener, |
| 110 | content::SiteInstance* source_site_instance, |
| 111 | int32_t route_id, |
| 112 | int32_t main_frame_route_id, |
| 113 | int32_t main_frame_widget_route_id, |
| 114 | content::mojom::WindowContainerType window_container_type, |
| 115 | const GURL& opener_url, |
| 116 | const std::string& frame_name, |
| 117 | const GURL& target_url, |
| 118 | const std::string& partition_id, |
| 119 | content::SessionStorageNamespace* session_storage_namespace) override { |
| 120 | // This method is invoked when attempting to open links in a new tab, e.g.: |
| 121 | // <a href="https://www.google.com/" target="_blank">Link</a> |
| 122 | client_->DidSuppressNavigation(target_url, |
| 123 | WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 124 | /*from_user_gesture=*/true); |
| 125 | return false; |
| 126 | } |
| 127 | |
Ken Rockot | 6bcda46 | 2018-10-15 21:08:01 | [diff] [blame] | 128 | WebContents* OpenURLFromTab(WebContents* source, |
| 129 | const OpenURLParams& params) override { |
| 130 | client_->DidSuppressNavigation(params.url, params.disposition, |
| 131 | params.user_gesture); |
| 132 | return nullptr; |
| 133 | } |
| 134 | |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 135 | void ResizeDueToAutoResize(WebContents* web_contents, |
| 136 | const gfx::Size& new_size) override { |
| 137 | DCHECK_EQ(web_contents, web_contents_.get()); |
| 138 | client_->DidAutoResizeView(new_size); |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // WebContentsObserver: |
Xiyuan Xia | b5ebe94b | 2018-12-21 19:24:04 | [diff] [blame] | 142 | void RenderViewReady() override { |
| 143 | if (background_color_) { |
| 144 | web_contents_->GetRenderViewHost() |
| 145 | ->GetWidget() |
| 146 | ->GetView() |
| 147 | ->SetBackgroundColor(background_color_.value()); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void RenderViewCreated(RenderViewHost* render_view_host) override { |
| 152 | if (background_color_) { |
| 153 | render_view_host->GetWidget()->GetView()->SetBackgroundColor( |
| 154 | background_color_.value()); |
| 155 | } |
| 156 | } |
| 157 | |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 158 | void RenderViewHostChanged(RenderViewHost* old_host, |
| 159 | RenderViewHost* new_host) override { |
| 160 | if (enable_view_auto_resize_ && web_contents_->GetRenderWidgetHostView()) { |
| 161 | web_contents_->GetRenderWidgetHostView()->EnableAutoResize( |
David Black | 992c445 | 2018-11-09 21:01:31 | [diff] [blame] | 162 | auto_resize_min_size_, auto_resize_max_size_); |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 163 | } |
Ken Rockot | 71e957aa | 2018-12-10 20:49:53 | [diff] [blame] | 164 | |
Xiyuan Xia | b5ebe94b | 2018-12-21 19:24:04 | [diff] [blame] | 165 | if (background_color_) { |
| 166 | new_host->GetWidget()->GetView()->SetBackgroundColor( |
| 167 | background_color_.value()); |
| 168 | } |
| 169 | |
Ken Rockot | 71e957aa | 2018-12-10 20:49:53 | [diff] [blame] | 170 | NotifyAXTreeChange(); |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void DidFinishNavigation(NavigationHandle* navigation_handle) override { |
| 174 | client_->DidFinishNavigation( |
| 175 | navigation_handle->GetURL(), navigation_handle->IsInMainFrame(), |
| 176 | navigation_handle->IsErrorPage(), |
Ken Rockot | 5b09ac35 | 2018-09-19 18:01:51 | [diff] [blame] | 177 | navigation_handle->GetResponseHeaders() |
| 178 | ? base::MakeRefCounted<net::HttpResponseHeaders>( |
| 179 | navigation_handle->GetResponseHeaders()->raw_headers()) |
| 180 | : nullptr); |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 181 | } |
| 182 | |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 183 | void DidStopLoading() override { client_->DidStopLoading(); } |
| 184 | |
| 185 | std::unique_ptr<WebContents> web_contents_; |
Ken Rockot | f892cf5f | 2018-07-16 21:32:19 | [diff] [blame] | 186 | mojom::NavigableContentsClient* const client_; |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 187 | |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 188 | const bool enable_view_auto_resize_; |
David Black | 992c445 | 2018-11-09 21:01:31 | [diff] [blame] | 189 | const gfx::Size auto_resize_min_size_; |
| 190 | const gfx::Size auto_resize_max_size_; |
Xiyuan Xia | b5ebe94b | 2018-12-21 19:24:04 | [diff] [blame] | 191 | const base::Optional<SkColor> background_color_; |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 192 | |
Ken Rockot | f892cf5f | 2018-07-16 21:32:19 | [diff] [blame] | 193 | DISALLOW_COPY_AND_ASSIGN(NavigableContentsDelegateImpl); |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | } // namespace |
| 197 | |
| 198 | ContentServiceDelegateImpl::ContentServiceDelegateImpl( |
| 199 | BrowserContext* browser_context) |
| 200 | : browser_context_(browser_context) {} |
| 201 | |
| 202 | ContentServiceDelegateImpl::~ContentServiceDelegateImpl() { |
| 203 | // This delegate is destroyed immediately before |browser_context_| is |
| 204 | // destroyed. We force-kill any Content Service instances which depend on |
| 205 | // |this|, since they will no longer be functional anyway. |
| 206 | std::set<content::Service*> instances; |
| 207 | std::swap(instances, service_instances_); |
| 208 | for (content::Service* service : instances) { |
| 209 | // Eventually destroys |service|. Ensures that no more calls into |this| |
| 210 | // will occur. |
| 211 | service->ForceQuit(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void ContentServiceDelegateImpl::AddService(content::Service* service) { |
| 216 | service_instances_.insert(service); |
| 217 | } |
| 218 | |
| 219 | void ContentServiceDelegateImpl::WillDestroyServiceInstance( |
| 220 | content::Service* service) { |
| 221 | service_instances_.erase(service); |
| 222 | } |
| 223 | |
Ken Rockot | f892cf5f | 2018-07-16 21:32:19 | [diff] [blame] | 224 | std::unique_ptr<content::NavigableContentsDelegate> |
| 225 | ContentServiceDelegateImpl::CreateNavigableContentsDelegate( |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 226 | const mojom::NavigableContentsParams& params, |
Ken Rockot | f892cf5f | 2018-07-16 21:32:19 | [diff] [blame] | 227 | mojom::NavigableContentsClient* client) { |
| 228 | return std::make_unique<NavigableContentsDelegateImpl>(browser_context_, |
Ken Rockot | d7f83e3 | 2018-09-17 23:36:30 | [diff] [blame] | 229 | params, client); |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | } // namespace content |