| 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 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_UI_PRELOADED_WEB_VIEW_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_UI_PRELOADED_WEB_VIEW_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/callback_forward.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/memory_pressure_listener.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "components/keyed_service/core/keyed_service.h" |
| 15 #include "content/public/browser/notification_observer.h" |
| 16 #include "content/public/browser/notification_registrar.h" |
| 17 |
| 18 class Profile; |
| 19 |
| 20 namespace views { |
| 21 class WebView; |
| 22 } |
| 23 |
| 24 namespace chromeos { |
| 25 |
| 26 class IdleDetector; |
| 27 |
| 28 // Stores and fetches a views::WebView instance that is ulimately owned by the |
| 29 // signin profile. This allows for a WebView to be reused over time or |
| 30 // preloaded. Use PreloadedWebViewFactory to get an instance of this class. |
| 31 class PreloadedWebView : public KeyedService, |
| 32 public content::NotificationObserver { |
| 33 public: |
| 34 using PreloadCallback = |
| 35 base::OnceCallback<std::unique_ptr<views::WebView>(Profile*)>; |
| 36 |
| 37 explicit PreloadedWebView(Profile* profile); |
| 38 ~PreloadedWebView() override; |
| 39 |
| 40 // Executes the given |preload| function when the device is idle. |
| 41 void PreloadOnIdle(PreloadCallback preload); |
| 42 |
| 43 // Try to fetch a preloaded instance. Returns nullptr if no instance has been |
| 44 // preloaded. Calling this function will cancel any pending preload. |
| 45 std::unique_ptr<views::WebView> TryTake(); |
| 46 |
| 47 private: |
| 48 // KeyedSerivce: |
| 49 void Shutdown() override; |
| 50 |
| 51 // content::NotificationObserver: |
| 52 void Observe(int type, |
| 53 const content::NotificationSource& source, |
| 54 const content::NotificationDetails& details) override; |
| 55 |
| 56 // Called when there is a memory pressure event. |
| 57 void OnMemoryPressure( |
| 58 base::MemoryPressureListener::MemoryPressureLevel level); |
| 59 |
| 60 // Runs the preload function. Called only by |idle_detector_|. |
| 61 void RunPreloader(); |
| 62 |
| 63 // Used to execute the preload function when the user is idle. |
| 64 std::unique_ptr<IdleDetector> idle_detector_; |
| 65 // The preload function. Can only be called once. |
| 66 PreloadCallback preload_function_; |
| 67 // The result of the preload function. |
| 68 std::unique_ptr<views::WebView> preloaded_instance_; |
| 69 // Profile passed into the preload function. |
| 70 Profile* profile_; |
| 71 |
| 72 // Used to destroy a preloaded but not used WebView on shutdown. |
| 73 content::NotificationRegistrar registrar_; |
| 74 // Used to destroy a preloaded but not used WebView on low-memory. |
| 75 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
| 76 |
| 77 base::WeakPtrFactory<PreloadedWebView> weak_factory_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(PreloadedWebView); |
| 80 }; |
| 81 |
| 82 } // namespace chromeos |
| 83 |
| 84 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_UI_PRELOADED_WEB_VIEW_H_ |
| OLD | NEW |