OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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 "content/browser/wake_lock/wake_lock_manager.h" |
| 6 |
| 7 #include "content/browser/wake_lock/lock.h" |
| 8 #include "content/browser/wake_lock/wake_lock_web_contents_observer.h" |
| 9 #include "content/public/browser/render_view_host.h" |
| 10 #include "content/public/browser/web_contents.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 // static |
| 15 WakeLockManager* WakeLockManager::GetInstance() { |
| 16 return Singleton<WakeLockManager>::get(); |
| 17 } |
| 18 |
| 19 WakeLockManager::WakeLockManager() { |
| 20 } |
| 21 |
| 22 WakeLockManager::~WakeLockManager() { |
| 23 for (std::set<Lock*>::iterator it = locks_.begin(); |
| 24 it != locks_.end(); |
| 25 ++it) |
| 26 (*it)->UnlockResource(); |
| 27 } |
| 28 |
| 29 void WakeLockManager::createScreenLock( |
| 30 int render_process_id, int routed_id) { |
| 31 if (!getLock(render_process_id, routed_id)) { |
| 32 Lock* lock = new Lock(render_process_id, routed_id); |
| 33 locks_.insert(lock); |
| 34 } |
| 35 } |
| 36 |
| 37 bool WakeLockManager::releaseScreenLock( |
| 38 int render_process_id, int routed_id) { |
| 39 if (Lock* lock = getLock(render_process_id, routed_id)) { |
| 40 locks_.erase(lock); |
| 41 delete lock; |
| 42 return true; |
| 43 } |
| 44 return false; |
| 45 } |
| 46 |
| 47 Lock* WakeLockManager::getLock( |
| 48 int render_process_id, int routed_id) { |
| 49 for (std::set<Lock*>::iterator it = locks_.begin(); |
| 50 it != locks_.end(); |
| 51 ++it) { |
| 52 Lock* lock = *it; |
| 53 if (lock->GetRenderProcessId() == render_process_id && |
| 54 lock->GetRoutedId() == routed_id) |
| 55 return lock; |
| 56 } |
| 57 return NULL; |
| 58 } |
| 59 |
| 60 void WakeLockManager::tabWasShown(int render_process_id, int routed_id) { |
| 61 tabChangeVisibility(render_process_id, routed_id, true); |
| 62 } |
| 63 |
| 64 void WakeLockManager::tabWasHidden(int render_process_id, int routed_id) { |
| 65 tabChangeVisibility(render_process_id, routed_id, false); |
| 66 } |
| 67 |
| 68 void WakeLockManager::tabChangeVisibility( |
| 69 int render_process_id, int routed_id, bool visible) { |
| 70 if (Lock* lock = getLock(render_process_id, routed_id)) { |
| 71 if (visible) |
| 72 lock->ForceLockResource(); |
| 73 else |
| 74 lock->ForceUnlockResource(); |
| 75 } |
| 76 } |
| 77 |
| 78 void WakeLockManager::createWebContentsObserver( |
| 79 int render_process_id, int routed_id) { |
| 80 RenderViewHost* view = RenderViewHost::FromID( |
| 81 render_process_id, routed_id); |
| 82 WebContents* web_contents = WebContents::FromRenderViewHost(view); |
| 83 // TODO(redchenko): check, save and delete it |
| 84 new WakeLockWebContentsObserver(web_contents); |
| 85 } |
| 86 } |
OLD | NEW |