OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 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. | |
4 | |
5 #include "chrome/browser/permissions/permission_infobar_manager.h" | |
6 | |
7 #include "chrome/browser/chrome_notification_types.h" | |
8 #include "chrome/browser/infobars/infobar_service.h" | |
9 #include "chrome/browser/permissions/permission_infobar_request.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/tab_contents/tab_util.h" | |
12 #include "components/content_settings/core/common/content_settings.h" | |
13 #include "components/infobars/core/infobar.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "content/public/browser/web_contents.h" | |
16 #include "content/public/common/url_constants.h" | |
17 | |
18 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PermissionInfoBarManager); | |
19 | |
20 PermissionInfoBarManager::PermissionInfoBarManager( | |
21 content::WebContents* web_contents) | |
22 : content::WebContentsObserver(web_contents), | |
23 is_show_pending_(false), | |
24 weak_factory_(this) { | |
25 } | |
26 | |
27 PermissionInfoBarManager::~PermissionInfoBarManager() { | |
28 } | |
29 | |
30 void PermissionInfoBarManager::CreateRequest( | |
31 const ContentSettingsType type, | |
32 const PermissionRequestID& request_id, | |
33 const GURL& requesting_frame, | |
34 const GURL& embedder, | |
35 const PermissionDecidedCallback& response_callback) { | |
36 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
37 DCHECK(!current_request_.get() || | |
38 current_request_->request_id() != request_id.request_id()); | |
39 | |
40 if (requesting_frame.SchemeIs(content::kChromeUIScheme) || | |
41 embedder.SchemeIs(content::kChromeUIScheme)) { | |
42 return; | |
43 } | |
44 | |
45 auto it = queued_requests_.find(request_id.request_id()); | |
46 PermissionInfoBarRequest* request = nullptr; | |
47 if (it == queued_requests_.end()) { | |
48 request = new PermissionInfoBarRequest(request_id.request_id(), | |
49 requesting_frame, embedder, | |
50 base::Bind(&PermissionInfoBarManager::OnInfoBarClosed, | |
51 weak_factory_.GetWeakPtr())); | |
mlamouri (slow - plz ping)
2015/10/02 11:49:39
It seems to me that we could use base::Unretained(
Lalit Maganti
2015/10/02 13:37:45
Done.
| |
52 queued_requests_.add(request_id.request_id(), make_scoped_ptr(request)); | |
53 | |
54 // Try to trigger a show for this infobar. | |
55 TriggerShowNextQueuedRequest(); | |
56 } else { | |
57 request = it->second; | |
58 } | |
59 request->AddPermission(type, response_callback); | |
60 } | |
61 | |
62 void PermissionInfoBarManager::CancelRequest( | |
63 const PermissionRequestID& request_id) { | |
64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
65 | |
66 if (current_request_.get() && | |
67 current_request_->request_id() == request_id.request_id()) { | |
68 current_request_->Cancel(); | |
69 return; | |
70 } | |
71 | |
72 // No-op if the request does not exist. | |
73 queued_requests_.erase(request_id.request_id()); | |
74 } | |
75 | |
76 void PermissionInfoBarManager::Accept() { | |
77 current_request_->Accept(); | |
78 } | |
79 | |
80 void PermissionInfoBarManager::Closing() { | |
81 current_request_->Closing(); | |
82 } | |
83 | |
84 void PermissionInfoBarManager::TriggerShowNextQueuedRequest() { | |
85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
86 if (is_show_pending_ || ShouldIgnoreQueuedRequests()) | |
87 return; | |
88 | |
89 is_show_pending_ = true; | |
90 content::BrowserThread::PostTask( | |
91 content::BrowserThread::UI, | |
92 FROM_HERE, | |
93 base::Bind(&PermissionInfoBarManager::ShowNextQueuedRequest, | |
94 weak_factory_.GetWeakPtr())); | |
95 } | |
96 | |
97 void PermissionInfoBarManager::ShowNextQueuedRequest() { | |
98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
99 | |
100 is_show_pending_ = false; | |
101 if (ShouldIgnoreQueuedRequests()) | |
102 return; | |
103 | |
104 for (auto it = queued_requests_.begin(); it != queued_requests_.end();) { | |
105 // Increment the iterator as it will be invalidated. | |
106 scoped_ptr<PermissionInfoBarRequest> infobar_request = | |
107 queued_requests_.take_and_erase(it++); | |
108 | |
109 if (infobar_request->ShowInfobar(web_contents())) { | |
110 current_request_.reset(infobar_request.release()); | |
111 break; | |
112 } | |
113 } | |
114 } | |
115 | |
116 void PermissionInfoBarManager::OnInfoBarClosed() { | |
117 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
118 | |
119 current_request_.reset(); | |
120 TriggerShowNextQueuedRequest(); | |
121 } | |
122 | |
123 bool PermissionInfoBarManager::ShouldIgnoreQueuedRequests() { | |
124 return current_request_.get() || queued_requests_.empty(); | |
125 } | |
OLD | NEW |