| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 "chrome/browser/ui/website_settings/mock_permission_bubble_factory.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "chrome/browser/permissions/permission_request_manager.h" | |
| 11 #include "chrome/browser/ui/website_settings/mock_permission_bubble_view.h" | |
| 12 | |
| 13 MockPermissionBubbleFactory::MockPermissionBubbleFactory( | |
| 14 PermissionRequestManager* manager) | |
| 15 : can_update_ui_(false), | |
| 16 show_count_(0), | |
| 17 requests_count_(0), | |
| 18 total_requests_count_(0), | |
| 19 response_type_(PermissionRequestManager::NONE), | |
| 20 manager_(manager) { | |
| 21 manager->view_factory_ = | |
| 22 base::Bind(&MockPermissionBubbleFactory::Create, base::Unretained(this)); | |
| 23 } | |
| 24 | |
| 25 MockPermissionBubbleFactory::~MockPermissionBubbleFactory() { | |
| 26 manager_->view_factory_ = | |
| 27 base::Bind(&MockPermissionBubbleFactory::DoNotCreate); | |
| 28 while (!views_.empty()) { | |
| 29 views_.back()->factory_ = nullptr; | |
| 30 views_.pop_back(); | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 std::unique_ptr<PermissionBubbleView> MockPermissionBubbleFactory::Create( | |
| 35 Browser* browser) { | |
| 36 MockPermissionBubbleView* view = | |
| 37 new MockPermissionBubbleView(this, manager_); | |
| 38 view->can_update_ui_ = can_update_ui_; | |
| 39 return base::WrapUnique(view); | |
| 40 } | |
| 41 | |
| 42 void MockPermissionBubbleFactory::SetCanUpdateUi(bool can_update_ui) { | |
| 43 can_update_ui_ = can_update_ui; | |
| 44 for (auto* view : views_) | |
| 45 view->can_update_ui_ = can_update_ui_; | |
| 46 } | |
| 47 | |
| 48 void MockPermissionBubbleFactory::ResetCounts() { | |
| 49 show_count_ = 0; | |
| 50 requests_count_ = 0; | |
| 51 total_requests_count_ = 0; | |
| 52 } | |
| 53 | |
| 54 void MockPermissionBubbleFactory::DocumentOnLoadCompletedInMainFrame() { | |
| 55 manager_->DocumentOnLoadCompletedInMainFrame(); | |
| 56 } | |
| 57 | |
| 58 bool MockPermissionBubbleFactory::is_visible() { | |
| 59 for (auto* view : views_) { | |
| 60 if (view->IsVisible()) | |
| 61 return true; | |
| 62 } | |
| 63 return false; | |
| 64 } | |
| 65 | |
| 66 void MockPermissionBubbleFactory::WaitForPermissionBubble() { | |
| 67 if (is_visible()) | |
| 68 return; | |
| 69 DCHECK(show_bubble_quit_closure_.is_null()); | |
| 70 base::RunLoop loop; | |
| 71 show_bubble_quit_closure_ = loop.QuitClosure(); | |
| 72 loop.Run(); | |
| 73 show_bubble_quit_closure_ = base::Closure(); | |
| 74 } | |
| 75 | |
| 76 // static | |
| 77 std::unique_ptr<PermissionBubbleView> MockPermissionBubbleFactory::DoNotCreate( | |
| 78 Browser* browser) { | |
| 79 NOTREACHED(); | |
| 80 return base::WrapUnique( | |
| 81 new MockPermissionBubbleView(nullptr, nullptr)); | |
| 82 } | |
| 83 | |
| 84 void MockPermissionBubbleFactory::UpdateResponseType() { | |
| 85 manager_->set_auto_response_for_test(response_type_); | |
| 86 } | |
| 87 | |
| 88 void MockPermissionBubbleFactory::ShowView(MockPermissionBubbleView* view) { | |
| 89 for (std::vector<MockPermissionBubbleView*>::iterator iter = views_.begin(); | |
| 90 iter != views_.end(); ++iter) { | |
| 91 if (*iter == view) | |
| 92 return; | |
| 93 } | |
| 94 views_.push_back(view); | |
| 95 | |
| 96 if (!show_bubble_quit_closure_.is_null()) | |
| 97 show_bubble_quit_closure_.Run(); | |
| 98 } | |
| 99 | |
| 100 void MockPermissionBubbleFactory::HideView(MockPermissionBubbleView* view) { | |
| 101 for (std::vector<MockPermissionBubbleView*>::iterator iter = views_.begin(); | |
| 102 iter != views_.end(); ++iter) { | |
| 103 if (*iter == view) { | |
| 104 views_.erase(iter); | |
| 105 return; | |
| 106 } | |
| 107 } | |
| 108 } | |
| OLD | NEW |