Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / website_settings / permission_bubble_manager.cc
blob17d15837e275e9f7f7154c56351a15f10b9a5a59
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++) {
16 if (*di == delegate)
17 return;
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);
36 delegates_.erase(di);
37 accept_state_.erase(ai);
38 return;
43 void PermissionBubbleManager::SetView(PermissionBubbleView* view) {
44 if (view == NULL && view_ != NULL) {
45 view_->SetDelegate(NULL);
46 view_->Hide();
49 view_ = view;
50 if (view_ == NULL)
51 return;
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_);
60 } else {
61 view_->Hide();
62 return;
66 PermissionBubbleManager::PermissionBubbleManager(
67 content::WebContents* web_contents)
68 : content::WebContentsObserver(web_contents),
69 bubble_showing_(false),
70 view_(NULL) {
73 PermissionBubbleManager::~PermissionBubbleManager() {}
75 void PermissionBubbleManager::WebContentsDestroyed(
76 content::WebContents* web_contents) {
77 // Synthetic cancel event if the user closes the WebContents.
78 Closing();
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++) {
98 if (*ai)
99 (*di)->PermissionGranted();
100 else
101 (*di)->PermissionDenied();
103 FinalizeBubble();
106 void PermissionBubbleManager::Deny() {
107 std::vector<PermissionBubbleDelegate*>::iterator di;
108 for (di = delegates_.begin(); di != delegates_.end(); di++)
109 (*di)->PermissionDenied();
110 FinalizeBubble();
113 void PermissionBubbleManager::Closing() {
114 std::vector<PermissionBubbleDelegate*>::iterator di;
115 for (di = delegates_.begin(); di != delegates_.end(); di++)
116 (*di)->Cancelled();
117 FinalizeBubble();
120 void PermissionBubbleManager::FinalizeBubble() {
121 std::vector<PermissionBubbleDelegate*>::iterator di;
122 for (di = delegates_.begin(); di != delegates_.end(); di++)
123 (*di)->RequestFinished();
124 delegates_.clear();
125 accept_state_.clear();