| 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 "base/memory/ptr_util.h" | |
| 8 #include "chrome/browser/chrome_notification_types.h" | |
| 9 #include "chrome/browser/chromeos/login/ui/web_view_handle.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "content/public/browser/notification_service.h" | |
| 12 #include "content/public/browser/render_view_host.h" | |
| 13 #include "content/public/browser/render_widget_host.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 #include "ui/views/controls/webview/webview.h" | |
| 16 | |
| 17 namespace chromeos { | |
| 18 | |
| 19 SharedWebView::SharedWebView(Profile* profile) : profile_(profile) { | |
| 20 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | |
| 21 content::NotificationService::AllSources()); | |
| 22 memory_pressure_listener_ = base::MakeUnique<base::MemoryPressureListener>( | |
| 23 base::Bind(&SharedWebView::OnMemoryPressure, base::Unretained(this))); | |
| 24 } | |
| 25 | |
| 26 SharedWebView::~SharedWebView() {} | |
| 27 | |
| 28 void SharedWebView::Shutdown() { | |
| 29 web_view_handle_ = nullptr; | |
| 30 } | |
| 31 | |
| 32 bool SharedWebView::Get(const GURL& url, | |
| 33 scoped_refptr<WebViewHandle>* out_handle) { | |
| 34 // At the moment only one shared WebView instance is supported per profile. | |
| 35 DCHECK(web_view_url_.is_empty() || web_view_url_ == url); | |
| 36 web_view_url_ = url; | |
| 37 | |
| 38 // Ensure the WebView is not being reused simultaneously. | |
| 39 DCHECK(!web_view_handle_ || web_view_handle_->HasOneRef()); | |
| 40 | |
| 41 // Clear cached reference if it is no longer valid (ie, destroyed in task | |
| 42 // manager). | |
| 43 if (web_view_handle_ && | |
| 44 !web_view_handle_->web_view() | |
| 45 ->GetWebContents() | |
| 46 ->GetRenderViewHost() | |
| 47 ->GetWidget() | |
| 48 ->GetView()) { | |
| 49 web_view_handle_ = nullptr; | |
| 50 } | |
| 51 | |
| 52 // Create WebView if needed. | |
| 53 bool reused = true; | |
| 54 if (!web_view_handle_) { | |
| 55 web_view_handle_ = new WebViewHandle(profile_); | |
| 56 reused = false; | |
| 57 } | |
| 58 | |
| 59 *out_handle = web_view_handle_; | |
| 60 return reused; | |
| 61 } | |
| 62 | |
| 63 void SharedWebView::Observe(int type, | |
| 64 const content::NotificationSource& source, | |
| 65 const content::NotificationDetails& details) { | |
| 66 DCHECK_EQ(chrome::NOTIFICATION_APP_TERMINATING, type); | |
| 67 web_view_handle_ = nullptr; | |
| 68 } | |
| 69 | |
| 70 void SharedWebView::OnMemoryPressure( | |
| 71 base::MemoryPressureListener::MemoryPressureLevel level) { | |
| 72 switch (level) { | |
| 73 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: | |
| 74 break; | |
| 75 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: | |
| 76 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: | |
| 77 web_view_handle_ = nullptr; | |
| 78 break; | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 } // namespace chromeos | |
| OLD | NEW |