Chromium Code Reviews| Index: chrome/browser/chromeos/login/ui/shared_web_view.cc |
| diff --git a/chrome/browser/chromeos/login/ui/shared_web_view.cc b/chrome/browser/chromeos/login/ui/shared_web_view.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..017cae6b85d08b57ba9d395d4480c5a55e822786 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/login/ui/shared_web_view.cc |
| @@ -0,0 +1,100 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/login/ui/shared_web_view.h" |
| + |
| +#include "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/render_view_host.h" |
| +#include "content/public/browser/render_widget_host.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "ui/views/controls/webview/webview.h" |
| + |
| +namespace chromeos { |
| + |
| +SharedWebViewUsageHandle::SharedWebViewUsageHandle( |
| + SharedWebView* shared_web_view) |
| + : shared_web_view_(shared_web_view) { |
| + shared_web_view_->MarkUsed(); |
| +} |
| + |
| +SharedWebViewUsageHandle::~SharedWebViewUsageHandle() { |
| + shared_web_view_->MarkUnused(); |
| +} |
| + |
| +SharedWebView::SharedWebView(Profile* profile) : profile_(profile) { |
| + registrar_.Add(this, chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
| + content::NotificationService::AllSources()); |
| + memory_pressure_listener_ = base::MakeUnique<base::MemoryPressureListener>( |
| + base::Bind(&SharedWebView::OnMemoryPressure, base::Unretained(this))); |
| +} |
| + |
| +SharedWebView::~SharedWebView() { |
| + web_view_.reset(); |
|
xiyuan
2016/12/09 22:37:29
nit: Unnecessary since Shutdown should always be c
jdufault
2016/12/12 18:23:09
Done.
|
| +} |
| + |
| +void SharedWebView::Shutdown() { |
| + web_view_.reset(); |
| +} |
| + |
| +bool SharedWebView::Get(const GURL& url, views::WebView** out_web_view) { |
| + // At the moment only one shared WebView instance is supported per profile. |
| + DCHECK(web_view_url_.is_empty() || web_view_url_ == url); |
| + web_view_url_ = url; |
| + |
| + // Clear cached reference if it is no longer valid (ie, destroyed in task |
| + // manager). |
| + if (web_view_ && |
| + !web_view_->GetWebContents() |
| + ->GetRenderViewHost() |
| + ->GetWidget() |
| + ->GetView()) { |
| + web_view_.reset(); |
| + } |
| + |
| + bool reused = true; |
| + if (!web_view_) { |
| + web_view_ = base::MakeUnique<views::WebView>(profile_); |
| + web_view_->set_owned_by_client(); |
| + handle_count_ = 0; |
| + reused = false; |
| + } |
| + *out_web_view = web_view_.get(); |
| + return reused; |
| +} |
| + |
| +void SharedWebView::MarkUsed() { |
| + handle_count_ += 1; |
|
xiyuan
2016/12/09 22:37:29
nit: ++handle_count_;
jdufault
2016/12/12 18:23:08
Removed
|
| +} |
| + |
| +void SharedWebView::MarkUnused() { |
| + DCHECK(handle_count_ > 0); |
| + handle_count_ -= 1; |
|
xiyuan
2016/12/09 22:37:29
nti: --handle_count_;
jdufault
2016/12/12 18:23:09
Removed
|
| +} |
| + |
| +void SharedWebView::Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + DCHECK_EQ(chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, type); |
| + web_view_.reset(); |
| +} |
| + |
| +void SharedWebView::OnMemoryPressure( |
| + base::MemoryPressureListener::MemoryPressureLevel level) { |
| + // Don't destroy webview on low-memory if it is actively being used. |
| + if (handle_count_ > 0) |
| + return; |
| + |
| + switch (level) { |
| + case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: |
| + break; |
| + case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: |
| + case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: |
| + web_view_.reset(); |
| + break; |
| + } |
| +} |
| + |
| +} // namespace chromeos |