1 // Copyright 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.
5 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
7 #include "chrome/browser/ui/website_settings/permission_bubble_delegate.h"
9 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PermissionBubbleManager
);
11 void PermissionBubbleManager::AddPermissionBubbleDelegate(
12 PermissionBubbleDelegate
* delegate
) {
13 // Don't re-add existing delegate.
14 std::vector
<PermissionBubbleDelegate
*>::iterator di
;
15 for (di
= delegates_
.begin(); di
!= delegates_
.end(); di
++) {
20 delegates_
.push_back(delegate
);
21 // TODO(gbillock): do we need to make default state a delegate property?
22 accept_state_
.push_back(false);
23 if (bubble_showing_
&& view_
)
24 view_
->AddPermissionBubbleDelegate(delegate
);
27 void PermissionBubbleManager::RemovePermissionBubbleDelegate(
28 PermissionBubbleDelegate
* delegate
) {
29 std::vector
<PermissionBubbleDelegate
*>::iterator di
;
30 std::vector
<bool>::iterator ai
;
31 for (di
= delegates_
.begin(), ai
= accept_state_
.begin();
32 di
!= delegates_
.end(); di
++, ai
++) {
33 if (*di
== delegate
) {
34 if (bubble_showing_
&& view_
)
35 view_
->RemovePermissionBubbleDelegate(delegate
);
37 accept_state_
.erase(ai
);
43 void PermissionBubbleManager::SetView(PermissionBubbleView
* view
) {
44 if (view
== NULL
&& view_
!= NULL
) {
45 view_
->SetDelegate(NULL
);
53 if (bubble_showing_
) {
54 view_
->SetDelegate(this);
55 view_
->Show(delegates_
, accept_state_
);
56 } else if (!delegates_
.empty()) {
57 bubble_showing_
= true;
58 view_
->SetDelegate(this);
59 view_
->Show(delegates_
, accept_state_
);
66 PermissionBubbleManager::PermissionBubbleManager(
67 content::WebContents
* web_contents
)
68 : content::WebContentsObserver(web_contents
),
69 bubble_showing_(false),
73 PermissionBubbleManager::~PermissionBubbleManager() {}
75 void PermissionBubbleManager::WebContentsDestroyed(
76 content::WebContents
* web_contents
) {
77 // Synthetic cancel event if the user closes the WebContents.
80 // The WebContents is going away; be aggressively paranoid and delete
81 // ourselves lest other parts of the system attempt to add permission bubbles
82 // or use us otherwise during the destruction.
83 web_contents
->RemoveUserData(UserDataKey());
84 // That was the equivalent of "delete this". This object is now destroyed;
85 // returning from this function is the only safe thing to do.
88 void PermissionBubbleManager::ToggleAccept(int delegate_index
, bool new_value
) {
89 DCHECK(delegate_index
< static_cast<int>(accept_state_
.size()));
90 accept_state_
[delegate_index
] = new_value
;
93 void PermissionBubbleManager::Accept() {
94 std::vector
<PermissionBubbleDelegate
*>::iterator di
;
95 std::vector
<bool>::iterator ai
;
96 for (di
= delegates_
.begin(), ai
= accept_state_
.begin();
97 di
!= delegates_
.end(); di
++, ai
++) {
99 (*di
)->PermissionGranted();
101 (*di
)->PermissionDenied();
106 void PermissionBubbleManager::Deny() {
107 std::vector
<PermissionBubbleDelegate
*>::iterator di
;
108 for (di
= delegates_
.begin(); di
!= delegates_
.end(); di
++)
109 (*di
)->PermissionDenied();
113 void PermissionBubbleManager::Closing() {
114 std::vector
<PermissionBubbleDelegate
*>::iterator di
;
115 for (di
= delegates_
.begin(); di
!= delegates_
.end(); di
++)
120 void PermissionBubbleManager::FinalizeBubble() {
121 std::vector
<PermissionBubbleDelegate
*>::iterator di
;
122 for (di
= delegates_
.begin(); di
!= delegates_
.end(); di
++)
123 (*di
)->RequestFinished();
125 accept_state_
.clear();