1 // Copyright (c) 2015 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.
5 #include "chrome/browser/ui/exclusive_access/exclusive_access_controller_base.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
10 #include "content/public/browser/navigation_details.h"
11 #include "content/public/browser/navigation_entry.h"
12 #include "content/public/browser/notification_service.h"
13 #include "content/public/browser/web_contents.h"
15 using content::WebContents
;
17 ExclusiveAccessControllerBase::ExclusiveAccessControllerBase(
18 ExclusiveAccessManager
* manager
)
19 : manager_(manager
), tab_with_exclusive_access_(nullptr) {
22 ExclusiveAccessControllerBase::~ExclusiveAccessControllerBase() {
25 GURL
ExclusiveAccessControllerBase::GetExclusiveAccessBubbleURL() const {
26 return manager_
->GetExclusiveAccessBubbleURL();
29 GURL
ExclusiveAccessControllerBase::GetURLForExclusiveAccessBubble() const {
30 if (tab_with_exclusive_access_
)
31 return tab_with_exclusive_access_
->GetURL();
35 void ExclusiveAccessControllerBase::OnTabDeactivated(
36 WebContents
* web_contents
) {
37 if (web_contents
== tab_with_exclusive_access_
)
38 ExitExclusiveAccessIfNecessary();
41 void ExclusiveAccessControllerBase::OnTabDetachedFromView(
42 WebContents
* old_contents
) {
43 // Derived class will have to implement if necessary.
46 void ExclusiveAccessControllerBase::OnTabClosing(WebContents
* web_contents
) {
47 if (web_contents
== tab_with_exclusive_access_
) {
48 ExitExclusiveAccessIfNecessary();
50 // The call to exit exclusive access may result in asynchronous notification
51 // of state change (e.g. fullscreen change on Linux). We don't want to rely
52 // on it to call NotifyTabExclusiveAccessLost(), because at that point
53 // |tab_with_exclusive_access_| may not be valid. Instead, we call it here
54 // to clean up exclusive access tab related state.
55 NotifyTabExclusiveAccessLost();
59 void ExclusiveAccessControllerBase::Observe(
61 const content::NotificationSource
& source
,
62 const content::NotificationDetails
& details
) {
63 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED
, type
);
64 if (content::Details
<content::LoadCommittedDetails
>(details
)
65 ->is_navigation_to_different_page())
66 ExitExclusiveAccessIfNecessary();
69 void ExclusiveAccessControllerBase::SetTabWithExclusiveAccess(
71 // Tab should never be replaced with another tab, or
72 // UpdateNotificationRegistrations would need updating.
73 DCHECK(tab_with_exclusive_access_
== tab
||
74 tab_with_exclusive_access_
== nullptr || tab
== nullptr);
75 tab_with_exclusive_access_
= tab
;
76 UpdateNotificationRegistrations();
79 void ExclusiveAccessControllerBase::UpdateNotificationRegistrations() {
80 if (tab_with_exclusive_access_
&& registrar_
.IsEmpty()) {
81 registrar_
.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
82 content::Source
<content::NavigationController
>(
83 &tab_with_exclusive_access_
->GetController()));
84 } else if (!tab_with_exclusive_access_
&& !registrar_
.IsEmpty()) {
85 registrar_
.RemoveAll();