Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "chrome/browser/chromeos/login/ui/shared_web_view.h" | |
| 6 | |
| 7 #include "chrome/browser/chrome_notification_types.h" | |
| 8 #include "chrome/browser/profiles/profile.h" | |
| 9 #include "content/public/browser/notification_service.h" | |
| 10 #include "content/public/browser/render_view_host.h" | |
| 11 #include "content/public/browser/render_widget_host.h" | |
| 12 #include "content/public/browser/web_contents.h" | |
| 13 #include "ui/views/controls/webview/webview.h" | |
| 14 | |
| 15 namespace chromeos { | |
| 16 | |
| 17 SharedWebViewUsageHandle::SharedWebViewUsageHandle( | |
| 18 SharedWebView* shared_web_view) | |
| 19 : shared_web_view_(shared_web_view) { | |
| 20 shared_web_view_->MarkUsed(); | |
| 21 } | |
| 22 | |
| 23 SharedWebViewUsageHandle::~SharedWebViewUsageHandle() { | |
| 24 shared_web_view_->MarkUnused(); | |
| 25 } | |
| 26 | |
| 27 SharedWebView::SharedWebView(Profile* profile) : profile_(profile) { | |
| 28 registrar_.Add(this, chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, | |
| 29 content::NotificationService::AllSources()); | |
| 30 memory_pressure_listener_ = base::MakeUnique<base::MemoryPressureListener>( | |
| 31 base::Bind(&SharedWebView::OnMemoryPressure, base::Unretained(this))); | |
| 32 } | |
| 33 | |
| 34 SharedWebView::~SharedWebView() { | |
| 35 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.
| |
| 36 } | |
| 37 | |
| 38 void SharedWebView::Shutdown() { | |
| 39 web_view_.reset(); | |
| 40 } | |
| 41 | |
| 42 bool SharedWebView::Get(const GURL& url, views::WebView** out_web_view) { | |
| 43 // At the moment only one shared WebView instance is supported per profile. | |
| 44 DCHECK(web_view_url_.is_empty() || web_view_url_ == url); | |
| 45 web_view_url_ = url; | |
| 46 | |
| 47 // Clear cached reference if it is no longer valid (ie, destroyed in task | |
| 48 // manager). | |
| 49 if (web_view_ && | |
| 50 !web_view_->GetWebContents() | |
| 51 ->GetRenderViewHost() | |
| 52 ->GetWidget() | |
| 53 ->GetView()) { | |
| 54 web_view_.reset(); | |
| 55 } | |
| 56 | |
| 57 bool reused = true; | |
| 58 if (!web_view_) { | |
| 59 web_view_ = base::MakeUnique<views::WebView>(profile_); | |
| 60 web_view_->set_owned_by_client(); | |
| 61 handle_count_ = 0; | |
| 62 reused = false; | |
| 63 } | |
| 64 *out_web_view = web_view_.get(); | |
| 65 return reused; | |
| 66 } | |
| 67 | |
| 68 void SharedWebView::MarkUsed() { | |
| 69 handle_count_ += 1; | |
|
xiyuan
2016/12/09 22:37:29
nit: ++handle_count_;
jdufault
2016/12/12 18:23:08
Removed
| |
| 70 } | |
| 71 | |
| 72 void SharedWebView::MarkUnused() { | |
| 73 DCHECK(handle_count_ > 0); | |
| 74 handle_count_ -= 1; | |
|
xiyuan
2016/12/09 22:37:29
nti: --handle_count_;
jdufault
2016/12/12 18:23:09
Removed
| |
| 75 } | |
| 76 | |
| 77 void SharedWebView::Observe(int type, | |
| 78 const content::NotificationSource& source, | |
| 79 const content::NotificationDetails& details) { | |
| 80 DCHECK_EQ(chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, type); | |
| 81 web_view_.reset(); | |
| 82 } | |
| 83 | |
| 84 void SharedWebView::OnMemoryPressure( | |
| 85 base::MemoryPressureListener::MemoryPressureLevel level) { | |
| 86 // Don't destroy webview on low-memory if it is actively being used. | |
| 87 if (handle_count_ > 0) | |
| 88 return; | |
| 89 | |
| 90 switch (level) { | |
| 91 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: | |
| 92 break; | |
| 93 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: | |
| 94 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: | |
| 95 web_view_.reset(); | |
| 96 break; | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 } // namespace chromeos | |
| OLD | NEW |