OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/services/gcm/permission_context_base.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/prefs/pref_service.h" | |
9 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
10 #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/tab_specific_content_settings.h" | |
13 #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" | |
16 #include "chrome/common/pref_names.h" | |
17 #include "content/public/browser/browser_thread.h" | |
18 #include "content/public/browser/web_contents.h" | |
19 #include "grit/generated_resources.h" | |
20 #include "grit/theme_resources.h" | |
21 | |
22 namespace gcm { | |
23 | |
24 PermissionContextBase::PermissionContextBase( | |
25 Profile* profile, | |
26 const ContentSettingsType permission_type) | |
27 : profile_(profile), | |
28 permission_type_(permission_type), | |
29 weak_factory_(this) { | |
30 permission_queue_controller_.reset( | |
31 new PermissionQueueController(profile_, permission_type_)); | |
32 } | |
33 | |
34 PermissionContextBase::~PermissionContextBase() { | |
35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
36 } | |
37 | |
38 void PermissionContextBase::RequestPermission( | |
39 content::WebContents* web_contents, | |
40 const PermissionRequestID& id, | |
41 const GURL& requesting_frame, | |
42 bool user_gesture, | |
43 const BrowserPermissionCallback& callback) { | |
44 // TODO(miguelg): Add UMA instrumentation. | |
45 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
46 | |
47 DecidePermission(web_contents, | |
48 id, | |
49 requesting_frame, | |
50 requesting_frame, | |
51 user_gesture, | |
52 callback); | |
53 } | |
54 | |
55 void PermissionContextBase::DecidePermission( | |
56 content::WebContents* web_contents, | |
57 const PermissionRequestID& id, | |
58 const GURL& requesting_frame, | |
59 const GURL& embedder, | |
60 bool user_gesture, | |
61 const BrowserPermissionCallback& callback) { | |
62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
63 | |
64 ContentSetting content_setting = | |
65 profile_->GetHostContentSettingsMap()->GetContentSetting( | |
66 requesting_frame, embedder, permission_type_, std::string()); | |
67 switch (content_setting) { | |
68 case CONTENT_SETTING_BLOCK: | |
69 PermissionDecided(id, requesting_frame, embedder, callback, false); | |
70 break; | |
71 case CONTENT_SETTING_ALLOW: | |
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 | |
95 bool inserted = pending_bubbles_.add( | |
96 id.ToString(), request_ptr.Pass()).second; | |
97 DCHECK(inserted) << "Duplicate id " << id.ToString(); | |
98 bubble_manager->AddRequest(request); | |
99 return; | |
100 } | |
101 | |
102 // TODO(gbillock): Delete this and the infobar delegate when | |
103 // we're using only bubbles. crbug.com/337458 | |
104 GetQueueController()->CreateInfoBarRequest( | |
105 id, | |
106 requesting_frame, | |
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 } | |
119 } | |
120 | |
121 void PermissionContextBase::PermissionDecided( | |
122 const PermissionRequestID& id, | |
123 const GURL& requesting_frame, | |
124 const GURL& embedder, | |
125 const BrowserPermissionCallback& callback, | |
126 bool allowed) { | |
127 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
128 NotifyPermissionSet(id, requesting_frame, embedder, callback, false, allowed); | |
129 } | |
130 | |
131 PermissionQueueController* PermissionContextBase::GetQueueController() { | |
132 return permission_queue_controller_.get(); | |
133 } | |
134 | |
135 void PermissionContextBase::NotifyPermissionSet( | |
136 const PermissionRequestID& id, | |
137 const GURL& requesting_frame, | |
138 const GURL& embedder, | |
139 const BrowserPermissionCallback& callback, | |
140 bool persist, | |
141 bool allowed) { | |
142 if (persist) { | |
143 UpdateContentSetting(requesting_frame, embedder, allowed); | |
144 } | |
145 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
146 UpdateTabContext(id, requesting_frame, allowed); | |
147 | |
148 callback.Run(allowed); | |
149 } | |
150 | |
151 void PermissionContextBase::CleanUpBubble(const PermissionRequestID& id) { | |
152 base::ScopedPtrHashMap<std::string, PermissionBubbleRequest>::iterator it; | |
153 it = pending_bubbles_.find(id.ToString()); | |
154 if (it != pending_bubbles_.end()) { | |
155 pending_bubbles_.take_and_erase(it); | |
156 return; | |
157 } | |
158 | |
159 NOTREACHED() << "Missing request"; | |
160 } | |
161 | |
162 void PermissionContextBase::UpdateContentSetting( | |
163 const GURL& requesting_frame, | |
164 const GURL& embedder, | |
165 bool allowed) { | |
166 ContentSetting content_setting = | |
167 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK; | |
168 profile_->GetHostContentSettingsMap()->SetContentSetting( | |
169 ContentSettingsPattern::FromURLNoWildcard(requesting_frame.GetOrigin()), | |
170 ContentSettingsPattern::FromURLNoWildcard(embedder.GetOrigin()), | |
171 permission_type_, | |
172 std::string(), | |
173 content_setting); | |
174 } | |
175 | |
176 } // namespace gcm | |
OLD | NEW |