OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "components/keyboard_lock/page_observer.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" |
| 11 /* cycle dependency |
| 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_finder.h" |
| 14 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h" |
| 15 */ |
| 16 #include "components/keyboard_lock/keyboard_lock_host.h" |
| 17 #include "components/keyboard_lock/key_hook_activator.h" |
| 18 #include "components/keyboard_lock/key_hook_activator_collection.h" |
| 19 #include "content/public/browser/notification_details.h" |
| 20 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/notification_source.h" |
| 22 #include "content/public/browser/web_contents.h" |
| 23 |
| 24 namespace keyboard_lock { |
| 25 |
| 26 // static |
| 27 void PageObserver::Observe(content::WebContents* web_contents, |
| 28 KeyHookActivatorCollection* collection) { |
| 29 new PageObserver(web_contents, collection); |
| 30 } |
| 31 |
| 32 PageObserver::PageObserver( |
| 33 content::WebContents* contents, |
| 34 KeyHookActivatorCollection* collection) |
| 35 : WebContentsObserver(contents), |
| 36 collection_(collection) { |
| 37 DCHECK(web_contents()); |
| 38 LOG(ERROR) << "PageObserver observes on thread " << base::PlatformThread::Curr
entId(); |
| 39 // TODO(zijiehe): Cannot receive this notification. |
| 40 is_fullscreen_ = true; |
| 41 registrar_.Add(this, |
| 42 chrome::NOTIFICATION_FULLSCREEN_CHANGED, |
| 43 content::NotificationService::AllSources()); |
| 44 /* cycle dependency |
| 45 content::Source<FullscreenController>( |
| 46 chrome::FindBrowserWithWebContents(web_contents()) |
| 47 ->exclusive_access_manager() |
| 48 ->fullscreen_controller())); |
| 49 */ |
| 50 } |
| 51 |
| 52 PageObserver::~PageObserver() { |
| 53 collection_->Erase(web_contents()); |
| 54 } |
| 55 |
| 56 void PageObserver::Observe(int type, |
| 57 const content::NotificationSource& source, |
| 58 const content::NotificationDetails& details) { |
| 59 DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED, type); |
| 60 is_fullscreen_ = *(content::Details<bool>(details).ptr()); |
| 61 LOG(ERROR) << "Received fullscreen notification: " |
| 62 << (is_fullscreen_ ? "True" : "False"); |
| 63 TriggerActivator(); |
| 64 } |
| 65 |
| 66 void PageObserver::OnWebContentsLostFocus() { |
| 67 is_focused_ = false; |
| 68 LOG(ERROR) << "Received OnWebContentsLostFocus"; |
| 69 TriggerActivator(); |
| 70 } |
| 71 |
| 72 void PageObserver::OnWebContentsFocused() { |
| 73 is_focused_ = true; |
| 74 LOG(ERROR) << "Received OnWebContentsFocused"; |
| 75 TriggerActivator(); |
| 76 } |
| 77 |
| 78 void PageObserver::WebContentsDestroyed() { |
| 79 LOG(ERROR) << "Received WebContentsDestroyed"; |
| 80 KeyHookActivator* activator = collection_->Find(web_contents()); |
| 81 if (activator) { |
| 82 activator->Deactivate(base::Callback<void(bool)>()); |
| 83 } |
| 84 delete this; |
| 85 } |
| 86 |
| 87 void PageObserver::TriggerActivator() { |
| 88 KeyHookActivator* activator = collection_->Find(web_contents()); |
| 89 if (activator == nullptr) { |
| 90 LOG(ERROR) << "No KeyHookActivator found for WebContents " |
| 91 << web_contents(); |
| 92 // The WebContents may be called CancelKeyboardLock(). This observer is not |
| 93 // necessary anymore. |
| 94 // "delete this" immediately stops all following callbacks. |
| 95 delete this; |
| 96 return; |
| 97 } |
| 98 |
| 99 if (is_fullscreen_ && is_focused_) { |
| 100 LOG(ERROR) << "Going to Activate"; |
| 101 activator->Activate(base::Callback<void(bool)>()); |
| 102 } else { |
| 103 LOG(ERROR) << "Going to Deactivate"; |
| 104 activator->Deactivate(base::Callback<void(bool)>()); |
| 105 } |
| 106 } |
| 107 |
| 108 } // namespace keyboard_lock |
OLD | NEW |