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/services/gcm/permission_context_base.h" | 5 #include "chrome/browser/content_settings/permission_context_base.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "chrome/browser/content_settings/host_content_settings_map.h" | 9 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 10 #include "chrome/browser/content_settings/permission_bubble_request_impl.h" |
10 #include "chrome/browser/content_settings/permission_queue_controller.h" | 11 #include "chrome/browser/content_settings/permission_queue_controller.h" |
11 #include "chrome/browser/content_settings/permission_request_id.h" | 12 #include "chrome/browser/content_settings/permission_request_id.h" |
12 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 13 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/services/gcm/permission_bubble_request_impl.h" | |
15 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 15 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
19 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
20 #include "grit/theme_resources.h" | 20 #include "grit/theme_resources.h" |
21 | 21 |
22 namespace gcm { | |
23 | 22 |
24 PermissionContextBase::PermissionContextBase( | 23 PermissionContextBase::PermissionContextBase( |
25 Profile* profile, | 24 Profile* profile, |
26 const ContentSettingsType permission_type) | 25 const ContentSettingsType permission_type) |
27 : profile_(profile), | 26 : profile_(profile), |
28 permission_type_(permission_type), | 27 permission_type_(permission_type), |
29 weak_factory_(this) { | 28 weak_factory_(this) { |
30 permission_queue_controller_.reset( | 29 permission_queue_controller_.reset( |
31 new PermissionQueueController(profile_, permission_type_)); | 30 new PermissionQueueController(profile_, permission_type_)); |
32 } | 31 } |
33 | 32 |
34 PermissionContextBase::~PermissionContextBase() { | 33 PermissionContextBase::~PermissionContextBase() { |
35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
36 } | 35 } |
37 | 36 |
38 void PermissionContextBase::RequestPermission( | 37 void PermissionContextBase::RequestPermission( |
39 content::WebContents* web_contents, | 38 content::WebContents* web_contents, |
40 const PermissionRequestID& id, | 39 const PermissionRequestID& id, |
41 const GURL& requesting_frame, | 40 const GURL& requesting_frame, |
42 bool user_gesture, | 41 bool user_gesture, |
43 const BrowserPermissionCallback& callback) { | 42 const BrowserPermissionCallback& callback) { |
44 // TODO(miguelg): Add UMA instrumentation. | 43 // TODO(miguelg): Add UMA instrumentation. |
45 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 44 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
46 | 45 |
47 DecidePermission(web_contents, | 46 DecidePermission(web_contents, |
48 id, | 47 id, |
49 requesting_frame, | 48 requesting_frame.GetOrigin(), |
50 requesting_frame, | 49 web_contents->GetLastCommittedURL().GetOrigin(), |
51 user_gesture, | 50 user_gesture, |
52 callback); | 51 callback); |
53 } | 52 } |
54 | 53 |
55 void PermissionContextBase::DecidePermission( | 54 void PermissionContextBase::DecidePermission( |
56 content::WebContents* web_contents, | 55 content::WebContents* web_contents, |
57 const PermissionRequestID& id, | 56 const PermissionRequestID& id, |
58 const GURL& requesting_frame, | 57 const GURL& requesting_origin, |
59 const GURL& embedder, | 58 const GURL& embedder_origin, |
60 bool user_gesture, | 59 bool user_gesture, |
61 const BrowserPermissionCallback& callback) { | 60 const BrowserPermissionCallback& callback) { |
62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
63 | 62 |
64 ContentSetting content_setting = | 63 ContentSetting content_setting = |
65 profile_->GetHostContentSettingsMap()->GetContentSetting( | 64 profile_->GetHostContentSettingsMap()->GetContentSetting( |
66 requesting_frame, embedder, permission_type_, std::string()); | 65 requesting_origin, embedder_origin, permission_type_, std::string()); |
67 switch (content_setting) { | 66 switch (content_setting) { |
68 case CONTENT_SETTING_BLOCK: | 67 case CONTENT_SETTING_BLOCK: |
69 PermissionDecided(id, requesting_frame, embedder, callback, false); | 68 PermissionDecided( |
| 69 id, requesting_origin, embedder_origin, callback, false); |
| 70 return; |
| 71 case CONTENT_SETTING_ALLOW: |
| 72 PermissionDecided(id, requesting_origin, embedder_origin, callback, true); |
| 73 return; |
| 74 default: |
70 break; | 75 break; |
71 case CONTENT_SETTING_ALLOW: | 76 } |
72 PermissionDecided(id, requesting_frame, embedder, callback, true); | |
73 break; | |
74 default: | |
75 if (PermissionBubbleManager::Enabled()) { | |
76 PermissionBubbleManager* bubble_manager = | |
77 PermissionBubbleManager::FromWebContents(web_contents); | |
78 DCHECK(bubble_manager); | |
79 scoped_ptr<PermissionBubbleRequest> request_ptr( | |
80 new PermissionBubbleRequestImpl( | |
81 requesting_frame, | |
82 user_gesture, | |
83 permission_type_, | |
84 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), | |
85 base::Bind(&PermissionContextBase::NotifyPermissionSet, | |
86 weak_factory_.GetWeakPtr(), | |
87 id, | |
88 requesting_frame, | |
89 embedder, | |
90 callback), | |
91 base::Bind(&PermissionContextBase::CleanUpBubble, | |
92 weak_factory_.GetWeakPtr(), id))); | |
93 PermissionBubbleRequest* request = request_ptr.get(); | |
94 | 77 |
95 bool inserted = pending_bubbles_.add( | 78 if (PermissionBubbleManager::Enabled()) { |
96 id.ToString(), request_ptr.Pass()).second; | 79 PermissionBubbleManager* bubble_manager = |
97 DCHECK(inserted) << "Duplicate id " << id.ToString(); | 80 PermissionBubbleManager::FromWebContents(web_contents); |
98 bubble_manager->AddRequest(request); | 81 DCHECK(bubble_manager); |
99 return; | 82 scoped_ptr<PermissionBubbleRequest> request_ptr( |
100 } | 83 new PermissionBubbleRequestImpl( |
| 84 requesting_origin, |
| 85 user_gesture, |
| 86 permission_type_, |
| 87 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), |
| 88 base::Bind(&PermissionContextBase::NotifyPermissionSet, |
| 89 weak_factory_.GetWeakPtr(), |
| 90 id, |
| 91 requesting_origin, |
| 92 embedder_origin, |
| 93 callback), |
| 94 base::Bind(&PermissionContextBase::CleanUpBubble, |
| 95 weak_factory_.GetWeakPtr(), id))); |
| 96 PermissionBubbleRequest* request = request_ptr.get(); |
101 | 97 |
102 // TODO(gbillock): Delete this and the infobar delegate when | 98 bool inserted = pending_bubbles_.add( |
103 // we're using only bubbles. crbug.com/337458 | 99 id.ToString(), request_ptr.Pass()).second; |
104 GetQueueController()->CreateInfoBarRequest( | 100 DCHECK(inserted) << "Duplicate id " << id.ToString(); |
105 id, | 101 bubble_manager->AddRequest(request); |
106 requesting_frame, | 102 return; |
107 embedder, | |
108 std::string(), | |
109 base::Bind(&PermissionContextBase::NotifyPermissionSet, | |
110 weak_factory_.GetWeakPtr(), | |
111 id, | |
112 requesting_frame, | |
113 embedder, | |
114 callback, | |
115 // the queue controller takes care of persisting the | |
116 // permission | |
117 false)); | |
118 } | 103 } |
| 104 |
| 105 // TODO(gbillock): Delete this and the infobar delegate when |
| 106 // we're using only bubbles. crbug.com/337458 |
| 107 GetQueueController()->CreateInfoBarRequest( |
| 108 id, |
| 109 requesting_origin, |
| 110 embedder_origin, |
| 111 std::string(), |
| 112 base::Bind(&PermissionContextBase::NotifyPermissionSet, |
| 113 weak_factory_.GetWeakPtr(), |
| 114 id, |
| 115 requesting_origin, |
| 116 embedder_origin, |
| 117 callback, |
| 118 // the queue controller takes care of persisting the |
| 119 // permission |
| 120 false)); |
119 } | 121 } |
120 | 122 |
121 void PermissionContextBase::PermissionDecided( | 123 void PermissionContextBase::PermissionDecided( |
122 const PermissionRequestID& id, | 124 const PermissionRequestID& id, |
123 const GURL& requesting_frame, | 125 const GURL& requesting_origin, |
124 const GURL& embedder, | 126 const GURL& embedder_origin, |
125 const BrowserPermissionCallback& callback, | 127 const BrowserPermissionCallback& callback, |
126 bool allowed) { | 128 bool allowed) { |
127 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 129 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
128 NotifyPermissionSet(id, requesting_frame, embedder, callback, false, allowed); | 130 NotifyPermissionSet( |
| 131 id, requesting_origin, embedder_origin, callback, false, allowed); |
129 } | 132 } |
130 | 133 |
131 PermissionQueueController* PermissionContextBase::GetQueueController() { | 134 PermissionQueueController* PermissionContextBase::GetQueueController() { |
132 return permission_queue_controller_.get(); | 135 return permission_queue_controller_.get(); |
133 } | 136 } |
134 | 137 |
135 void PermissionContextBase::NotifyPermissionSet( | 138 void PermissionContextBase::NotifyPermissionSet( |
136 const PermissionRequestID& id, | 139 const PermissionRequestID& id, |
137 const GURL& requesting_frame, | 140 const GURL& requesting_origin, |
138 const GURL& embedder, | 141 const GURL& embedder_origin, |
139 const BrowserPermissionCallback& callback, | 142 const BrowserPermissionCallback& callback, |
140 bool persist, | 143 bool persist, |
141 bool allowed) { | 144 bool allowed) { |
142 if (persist) { | |
143 UpdateContentSetting(requesting_frame, embedder, allowed); | |
144 } | |
145 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 145 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
146 UpdateTabContext(id, requesting_frame, allowed); | 146 if (persist) |
| 147 UpdateContentSetting(requesting_origin, embedder_origin, allowed); |
147 | 148 |
| 149 UpdateTabContext(id, requesting_origin, allowed); |
148 callback.Run(allowed); | 150 callback.Run(allowed); |
149 } | 151 } |
150 | 152 |
151 void PermissionContextBase::CleanUpBubble(const PermissionRequestID& id) { | 153 void PermissionContextBase::CleanUpBubble(const PermissionRequestID& id) { |
152 base::ScopedPtrHashMap<std::string, PermissionBubbleRequest>::iterator it; | 154 bool success = pending_bubbles_.erase(id.ToString()); |
153 it = pending_bubbles_.find(id.ToString()); | 155 DCHECK(success) << "Missing request " << id.ToString(); |
154 if (it != pending_bubbles_.end()) { | |
155 pending_bubbles_.take_and_erase(it); | |
156 return; | |
157 } | |
158 | |
159 NOTREACHED() << "Missing request"; | |
160 } | 156 } |
161 | 157 |
162 void PermissionContextBase::UpdateContentSetting( | 158 void PermissionContextBase::UpdateContentSetting( |
163 const GURL& requesting_frame, | 159 const GURL& requesting_origin, |
164 const GURL& embedder, | 160 const GURL& embedder_origin, |
165 bool allowed) { | 161 bool allowed) { |
| 162 DCHECK_EQ(requesting_origin, requesting_origin.GetOrigin()); |
| 163 DCHECK_EQ(embedder_origin, embedder_origin.GetOrigin()); |
166 ContentSetting content_setting = | 164 ContentSetting content_setting = |
167 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK; | 165 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK; |
168 profile_->GetHostContentSettingsMap()->SetContentSetting( | 166 profile_->GetHostContentSettingsMap()->SetContentSetting( |
169 ContentSettingsPattern::FromURLNoWildcard(requesting_frame.GetOrigin()), | 167 ContentSettingsPattern::FromURLNoWildcard(requesting_origin), |
170 ContentSettingsPattern::FromURLNoWildcard(embedder.GetOrigin()), | 168 ContentSettingsPattern::FromURLNoWildcard(embedder_origin), |
171 permission_type_, | 169 permission_type_, |
172 std::string(), | 170 std::string(), |
173 content_setting); | 171 content_setting); |
174 } | 172 } |
175 | |
176 } // namespace gcm | |
OLD | NEW |