| 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/preloaded_web_view.h" |
| 6 |
| 7 #include "base/callback_helpers.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/chromeos/idle_detector.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "content/public/browser/notification_service.h" |
| 13 #include "content/public/browser/render_view_host.h" |
| 14 #include "content/public/browser/render_widget_host.h" |
| 15 #include "content/public/browser/web_contents.h" |
| 16 #include "ui/views/controls/webview/webview.h" |
| 17 |
| 18 namespace chromeos { |
| 19 |
| 20 namespace { |
| 21 // Duration of user inactivity before running the preload function. |
| 22 constexpr int kIdleSecondsBeforePreloadingLockScreen = 8; |
| 23 } |
| 24 |
| 25 PreloadedWebView::PreloadedWebView(Profile* profile) |
| 26 : profile_(profile), weak_factory_(this) { |
| 27 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
| 28 content::NotificationService::AllSources()); |
| 29 memory_pressure_listener_ = |
| 30 base::MakeUnique<base::MemoryPressureListener>(base::Bind( |
| 31 &PreloadedWebView::OnMemoryPressure, weak_factory_.GetWeakPtr())); |
| 32 } |
| 33 |
| 34 PreloadedWebView::~PreloadedWebView() {} |
| 35 |
| 36 void PreloadedWebView::PreloadOnIdle(PreloadCallback preload) { |
| 37 preload_function_ = std::move(preload); |
| 38 idle_detector_ = base::MakeUnique<chromeos::IdleDetector>( |
| 39 base::Bind(&PreloadedWebView::RunPreloader, weak_factory_.GetWeakPtr())); |
| 40 idle_detector_->Start( |
| 41 base::TimeDelta::FromSeconds(kIdleSecondsBeforePreloadingLockScreen)); |
| 42 } |
| 43 |
| 44 std::unique_ptr<views::WebView> PreloadedWebView::TryTake() { |
| 45 idle_detector_.reset(); |
| 46 return std::move(preloaded_instance_); |
| 47 } |
| 48 |
| 49 void PreloadedWebView::Shutdown() { |
| 50 preloaded_instance_.reset(); |
| 51 } |
| 52 |
| 53 void PreloadedWebView::Observe(int type, |
| 54 const content::NotificationSource& source, |
| 55 const content::NotificationDetails& details) { |
| 56 DCHECK_EQ(chrome::NOTIFICATION_APP_TERMINATING, type); |
| 57 preloaded_instance_.reset(); |
| 58 } |
| 59 |
| 60 void PreloadedWebView::OnMemoryPressure( |
| 61 base::MemoryPressureListener::MemoryPressureLevel level) { |
| 62 switch (level) { |
| 63 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: |
| 64 break; |
| 65 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: |
| 66 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: |
| 67 preloaded_instance_.reset(); |
| 68 break; |
| 69 } |
| 70 } |
| 71 |
| 72 void PreloadedWebView::RunPreloader() { |
| 73 idle_detector_.reset(); |
| 74 preloaded_instance_ = std::move(preload_function_).Run(profile_); |
| 75 } |
| 76 |
| 77 } // namespace chromeos |
| OLD | NEW |