Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 5 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/ui/website_settings/permission_bubble_request.h" | 8 #include "chrome/browser/ui/website_settings/permission_bubble_request.h" |
| 9 #include "chrome/common/chrome_switches.h" | 9 #include "chrome/common/chrome_switches.h" |
| 10 | 10 |
| 11 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PermissionBubbleManager); | 11 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PermissionBubbleManager); |
| 12 | 12 |
| 13 namespace { | |
| 14 // This is how many ms to wait to see if there's another permission request | |
|
Greg Billock
2014/02/07 20:14:24
add a blank line around namespace lines
leng
2014/02/08 00:11:18
Done.
| |
| 15 // we should coalesce. | |
| 16 const int kPermissionsCoalesceIntervalMs = 400; | |
| 17 } | |
| 18 | |
| 13 // static | 19 // static |
| 14 bool PermissionBubbleManager::Enabled() { | 20 bool PermissionBubbleManager::Enabled() { |
| 15 return CommandLine::ForCurrentProcess()->HasSwitch( | 21 return CommandLine::ForCurrentProcess()->HasSwitch( |
| 16 switches::kEnablePermissionsBubbles); | 22 switches::kEnablePermissionsBubbles); |
| 17 } | 23 } |
| 18 | 24 |
| 19 void PermissionBubbleManager::AddRequest(PermissionBubbleRequest* request) { | 25 void PermissionBubbleManager::AddRequest(PermissionBubbleRequest* request) { |
| 20 // Don't re-add an existing request. | 26 // Don't re-add an existing request. |
| 21 std::vector<PermissionBubbleRequest*>::iterator di; | 27 std::vector<PermissionBubbleRequest*>::iterator di; |
| 22 for (di = requests_.begin(); di != requests_.end(); di++) { | 28 for (di = requests_.begin(); di != requests_.end(); di++) { |
| 23 if (*di == request) | 29 if (*di == request) |
| 24 return; | 30 return; |
| 25 } | 31 } |
| 26 | 32 |
| 33 if (bubble_showing_) { | |
| 34 for (di = queued_requests_.begin(); di != queued_requests_.end(); di++) { | |
| 35 if (*di == request) | |
| 36 return; | |
| 37 } | |
| 38 queued_requests_.push_back(request); | |
| 39 return; | |
| 40 } | |
| 41 | |
| 27 requests_.push_back(request); | 42 requests_.push_back(request); |
| 28 // TODO(gbillock): do we need to make default state a request property? | 43 // TODO(gbillock): do we need to make default state a request property? |
| 29 accept_state_.push_back(false); | 44 accept_state_.push_back(false); |
| 30 | 45 |
| 31 // TODO(gbillock): need significantly more complex logic here to deal | 46 // Start the timer when there is both a view and a request. |
| 32 // with various states of the manager. | 47 if (view_ && !timer_->IsRunning()) { |
| 33 | 48 timer_->Reset(); |
| 34 if (view_ && !bubble_showing_) { | |
| 35 view_->Show(requests_, accept_state_, customization_mode_); | |
| 36 bubble_showing_ = true; | |
| 37 } | 49 } |
| 38 } | 50 } |
| 39 | 51 |
| 40 void PermissionBubbleManager::SetView(PermissionBubbleView* view) { | 52 void PermissionBubbleManager::SetView(PermissionBubbleView* view) { |
| 41 if (view == view_) | 53 if (view == view_) |
| 42 return; | 54 return; |
| 43 | 55 |
| 44 if (view_ != NULL) { | 56 if (view_ != NULL) { |
| 45 view_->SetDelegate(NULL); | 57 view_->SetDelegate(NULL); |
| 46 view_->Hide(); | 58 view_->Hide(); |
| 47 } | 59 } |
| 48 | 60 |
| 49 view_ = view; | 61 view_ = view; |
| 50 if (view_ == NULL) | 62 if (view_ == NULL) |
| 51 return; | 63 return; |
| 52 | 64 |
| 53 if (bubble_showing_) { | 65 if (bubble_showing_) { |
| 54 view_->SetDelegate(this); | 66 view_->SetDelegate(this); |
| 55 view_->Show(requests_, accept_state_, customization_mode_); | 67 view_->Show(requests_, accept_state_, customization_mode_); |
| 56 } else if (!requests_.empty()) { | 68 } else if (!requests_.empty()) { |
| 57 bubble_showing_ = true; | 69 if (!timer_->IsRunning()) { |
| 58 view_->SetDelegate(this); | 70 timer_->Reset(); |
| 59 view_->Show(requests_, accept_state_, customization_mode_); | 71 } |
| 60 } else { | 72 } else { |
| 61 view_->Hide(); | 73 view_->Hide(); |
| 62 return; | 74 return; |
| 63 } | 75 } |
| 64 } | 76 } |
| 65 | 77 |
| 66 PermissionBubbleManager::PermissionBubbleManager( | 78 PermissionBubbleManager::PermissionBubbleManager( |
| 67 content::WebContents* web_contents) | 79 content::WebContents* web_contents) |
| 68 : content::WebContentsObserver(web_contents), | 80 : content::WebContentsObserver(web_contents), |
| 69 bubble_showing_(false), | 81 bubble_showing_(false), |
| 70 view_(NULL), | 82 view_(NULL), |
| 71 customization_mode_(false) { | 83 customization_mode_(false) { |
| 84 timer_.reset(new base::Timer(FROM_HERE, | |
| 85 base::TimeDelta::FromMilliseconds(kPermissionsCoalesceIntervalMs), | |
| 86 base::Bind(&PermissionBubbleManager::OnTimerExpired, | |
| 87 base::Unretained(this)), | |
| 88 false)); | |
| 72 } | 89 } |
| 73 | 90 |
| 74 PermissionBubbleManager::~PermissionBubbleManager() {} | 91 PermissionBubbleManager::~PermissionBubbleManager() {} |
| 75 | 92 |
| 76 void PermissionBubbleManager::WebContentsDestroyed( | 93 void PermissionBubbleManager::WebContentsDestroyed( |
| 77 content::WebContents* web_contents) { | 94 content::WebContents* web_contents) { |
| 78 // Synthetic cancel event if the user closes the WebContents. | 95 // Synthetic cancel event if the user closes the WebContents. |
| 79 Closing(); | 96 Closing(); |
| 80 | 97 |
| 81 // The WebContents is going away; be aggressively paranoid and delete | 98 // The WebContents is going away; be aggressively paranoid and delete |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 FinalizeBubble(); | 132 FinalizeBubble(); |
| 116 } | 133 } |
| 117 | 134 |
| 118 void PermissionBubbleManager::Closing() { | 135 void PermissionBubbleManager::Closing() { |
| 119 std::vector<PermissionBubbleRequest*>::iterator di; | 136 std::vector<PermissionBubbleRequest*>::iterator di; |
| 120 for (di = requests_.begin(); di != requests_.end(); di++) | 137 for (di = requests_.begin(); di != requests_.end(); di++) |
| 121 (*di)->Cancelled(); | 138 (*di)->Cancelled(); |
| 122 FinalizeBubble(); | 139 FinalizeBubble(); |
| 123 } | 140 } |
| 124 | 141 |
| 142 void PermissionBubbleManager::OnTimerExpired() { | |
| 143 if (view_ && !bubble_showing_ && requests_.size()) { | |
| 144 view_->SetDelegate(this); | |
| 145 view_->Show(requests_, accept_state_, customization_mode_); | |
| 146 bubble_showing_ = true; | |
| 147 } | |
| 148 } | |
| 149 | |
| 125 void PermissionBubbleManager::FinalizeBubble() { | 150 void PermissionBubbleManager::FinalizeBubble() { |
| 126 std::vector<PermissionBubbleRequest*>::iterator di; | 151 std::vector<PermissionBubbleRequest*>::iterator di; |
| 127 for (di = requests_.begin(); di != requests_.end(); di++) | 152 for (di = requests_.begin(); di != requests_.end(); di++) |
| 128 (*di)->RequestFinished(); | 153 (*di)->RequestFinished(); |
| 129 requests_.clear(); | 154 requests_.clear(); |
| 130 accept_state_.clear(); | 155 accept_state_.clear(); |
| 156 bubble_showing_ = false; | |
| 157 if (queued_requests_.size()) { | |
| 158 requests_ = queued_requests_; | |
|
Greg Billock
2014/02/07 20:14:24
Need to reset the accept_state_ here, too. (Set th
leng
2014/02/08 00:11:18
Done.
I also changed the variable name to be plura
| |
| 159 queued_requests_.clear(); | |
| 160 // TODO(leng): Explore other options of showing the next bubble. The | |
| 161 // advantage of this is that it uses the same code path as the first bubble. | |
| 162 timer_->Reset(); | |
| 163 } | |
| 131 } | 164 } |
| 132 | 165 |
| 166 void PermissionBubbleManager::SetCoalesceIntervalForTesting(int interval_ms) { | |
| 167 timer_.reset(new base::Timer(FROM_HERE, | |
| 168 base::TimeDelta::FromMilliseconds(interval_ms), | |
| 169 base::Bind(&PermissionBubbleManager::OnTimerExpired, | |
| 170 base::Unretained(this)), | |
| 171 false)); | |
| 172 } | |
| OLD | NEW |