OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/media/protected_media_identifier_permission_context.h" | 5 #include "chrome/browser/media/protected_media_identifier_permission_context.h" |
6 | 6 |
7 #include <functional> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/bind.h" | 7 #include "base/bind.h" |
12 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
13 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
14 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 10 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
15 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/tab_contents/tab_util.h" | 12 #include "chrome/browser/tab_contents/tab_util.h" |
17 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
18 #include "components/content_settings/core/browser/host_content_settings_map.h" | 14 #include "components/content_settings/core/browser/host_content_settings_map.h" |
19 #include "components/content_settings/core/common/permission_request_id.h" | 15 #include "components/content_settings/core/common/permission_request_id.h" |
20 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/render_process_host.h" | |
22 #include "content/public/browser/render_view_host.h" | |
23 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
24 | 18 |
25 #if defined(ENABLE_EXTENSIONS) | 19 #if defined(ENABLE_EXTENSIONS) |
26 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
27 #include "extensions/browser/extension_system.h" | 21 #include "extensions/browser/extension_system.h" |
28 #include "extensions/browser/suggest_permission_util.h" | 22 #include "extensions/browser/suggest_permission_util.h" |
29 #include "extensions/browser/view_type_utils.h" | 23 #include "extensions/browser/view_type_utils.h" |
30 #include "extensions/common/extension.h" | 24 #include "extensions/common/extension.h" |
31 | 25 |
32 using extensions::APIPermission; | 26 using extensions::APIPermission; |
33 #endif | 27 #endif |
34 | 28 |
35 ProtectedMediaIdentifierPermissionContext:: | 29 ProtectedMediaIdentifierPermissionContext:: |
36 ProtectedMediaIdentifierPermissionContext(Profile* profile) | 30 ProtectedMediaIdentifierPermissionContext(Profile* profile) |
37 : profile_(profile), shutting_down_(false) {} | 31 : PermissionContextBase(profile, |
32 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER), | |
33 shutting_down_(false) {} | |
38 | 34 |
39 ProtectedMediaIdentifierPermissionContext:: | 35 ProtectedMediaIdentifierPermissionContext:: |
40 ~ProtectedMediaIdentifierPermissionContext() { | 36 ~ProtectedMediaIdentifierPermissionContext() { |
41 // ProtectedMediaIdentifierPermissionContext may be destroyed on either | 37 // ProtectedMediaIdentifierPermissionContext may be destroyed on either |
42 // the UI thread or the IO thread, but the PermissionQueueController must have | 38 // the UI thread or the IO thread, but the PermissionQueueController must have |
43 // been destroyed on the UI thread. | 39 // been destroyed on the UI thread. |
44 DCHECK(!permission_queue_controller_.get()); | 40 DCHECK(!GetQueueController()); |
mlamouri (slow - plz ping)
2014/12/02 22:43:30
I think you can remove that. This dtor must be ru
timvolodine
2014/12/03 16:44:25
Done.
| |
45 } | 41 } |
46 | 42 |
47 void ProtectedMediaIdentifierPermissionContext:: | 43 void ProtectedMediaIdentifierPermissionContext::RequestPermission( |
48 RequestProtectedMediaIdentifierPermission( | 44 content::WebContents* web_contents, |
49 content::WebContents* web_contents, | 45 const PermissionRequestID& id, |
50 const GURL& origin, | 46 const GURL& requesting_frame_origin, |
51 base::Callback<void(bool)> result_callback) { | 47 bool user_gesture, |
52 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 48 const BrowserPermissionCallback& callback) { |
49 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
53 if (shutting_down_) | 50 if (shutting_down_) |
54 return; | 51 return; |
55 | 52 |
56 int render_process_id = web_contents->GetRenderProcessHost()->GetID(); | 53 GURL embedder = web_contents->GetLastCommittedURL().GetOrigin(); |
57 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); | |
58 | |
59 const PermissionRequestID id( | |
60 render_process_id, render_view_id, 0, origin); | |
61 | 54 |
62 #if defined(ENABLE_EXTENSIONS) | 55 #if defined(ENABLE_EXTENSIONS) |
63 if (extensions::GetViewType(web_contents) != | 56 if (extensions::GetViewType(web_contents) != |
64 extensions::VIEW_TYPE_TAB_CONTENTS) { | 57 extensions::VIEW_TYPE_TAB_CONTENTS) { |
65 // The tab may have gone away, or the request may not be from a tab at all. | 58 // The tab may have gone away, or the request may not be from a tab at all. |
66 LOG(WARNING) | 59 LOG(WARNING) |
67 << "Attempt to use protected media identifier in tabless renderer: " | 60 << "Attempt to use protected media identifier in tabless renderer: " |
68 << id.ToString() | 61 << id.ToString() |
69 << " (can't prompt user without a visible tab)"; | 62 << " (can't prompt user without a visible tab)"; |
70 NotifyPermissionSet(id, origin, result_callback, false); | 63 NotifyPermissionSet(id, origin, embedder, callback, false, false); |
71 return; | 64 return; |
72 } | 65 } |
73 #endif | 66 #endif |
74 | 67 |
75 GURL embedder = web_contents->GetLastCommittedURL(); | 68 if (!requesting_frame_origin.is_valid() || !embedder.is_valid()) { |
76 if (!origin.is_valid() || !embedder.is_valid()) { | |
77 LOG(WARNING) | 69 LOG(WARNING) |
78 << "Attempt to use protected media identifier from an invalid URL: " | 70 << "Attempt to use protected media identifier from an invalid URL: " |
79 << origin << "," << embedder | 71 << requesting_frame_origin << "," << embedder |
80 << " (proteced media identifier is not supported in popups)"; | 72 << " (proteced media identifier is not supported in popups)"; |
81 NotifyPermissionSet(id, origin, result_callback, false); | 73 NotifyPermissionSet(id, requesting_frame_origin, embedder, |
74 callback, false, false); | |
82 return; | 75 return; |
83 } | 76 } |
84 | 77 |
85 content::RenderViewHost* rvh = web_contents->GetRenderViewHost(); | |
86 DecidePermission(id, origin, embedder, rvh, result_callback); | |
87 } | |
88 | |
89 void ProtectedMediaIdentifierPermissionContext:: | |
90 CancelProtectedMediaIdentifierPermissionRequests( | |
91 int render_process_id, | |
92 int render_view_id, | |
93 const GURL& origin) { | |
94 CancelPendingInfobarRequests( | |
95 render_process_id, render_view_id, origin); | |
96 } | |
97 | |
98 void ProtectedMediaIdentifierPermissionContext::DecidePermission( | |
99 const PermissionRequestID& id, | |
100 const GURL& origin, | |
101 const GURL& embedder, | |
102 content::RenderViewHost* rvh, | |
103 const base::Callback<void(bool)>& callback) { | |
104 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
105 | |
106 #if defined(OS_ANDROID) | 78 #if defined(OS_ANDROID) |
107 // Check if the protected media identifier master switch is disabled. | 79 // Check if the protected media identifier master switch is disabled. |
108 if (!profile()->GetPrefs()->GetBoolean( | 80 if (!GetProfile()->GetPrefs()->GetBoolean( |
109 prefs::kProtectedMediaIdentifierEnabled)) { | 81 prefs::kProtectedMediaIdentifierEnabled)) { |
110 PermissionDecided(id, origin, embedder, callback, false); | 82 NotifyPermissionSet(id, requesting_frame_origin, embedder, callback, |
83 false, false); | |
111 return; | 84 return; |
112 } | 85 } |
113 #endif | 86 #endif |
114 | 87 |
115 ContentSetting content_setting = | 88 PermissionContextBase::RequestPermission(web_contents, id, |
116 profile_->GetHostContentSettingsMap()->GetContentSetting( | 89 requesting_frame_origin, |
117 origin, | 90 user_gesture, |
118 embedder, | 91 callback); |
119 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, | 92 } |
120 std::string()); | 93 |
121 switch (content_setting) { | 94 void ProtectedMediaIdentifierPermissionContext::CancelPermissionRequest( |
122 case CONTENT_SETTING_BLOCK: | 95 content::WebContents* web_contents, |
123 PermissionDecided(id, origin, embedder, callback, false); | 96 const PermissionRequestID& id) { |
124 break; | 97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
125 case CONTENT_SETTING_ALLOW: | 98 if (shutting_down_) |
126 PermissionDecided(id, origin, embedder, callback, true); | 99 return; |
127 break; | 100 PermissionContextBase::CancelPermissionRequest(web_contents, id); |
128 case CONTENT_SETTING_ASK: | |
129 QueueController()->CreateInfoBarRequest( | |
130 id, | |
131 origin, | |
132 embedder, | |
133 base::Bind(&ProtectedMediaIdentifierPermissionContext:: | |
134 NotifyPermissionSet, | |
135 base::Unretained(this), | |
136 id, | |
137 origin, | |
138 callback)); | |
139 break; | |
140 default: | |
141 NOTREACHED(); | |
142 } | |
143 } | 101 } |
144 | 102 |
145 void ProtectedMediaIdentifierPermissionContext::ShutdownOnUIThread() { | 103 void ProtectedMediaIdentifierPermissionContext::ShutdownOnUIThread() { |
146 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 104 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
147 permission_queue_controller_.reset(); | |
148 shutting_down_ = true; | 105 shutting_down_ = true; |
149 } | 106 } |
150 | 107 |
151 void ProtectedMediaIdentifierPermissionContext::PermissionDecided( | 108 void ProtectedMediaIdentifierPermissionContext::UpdateTabContext( |
152 const PermissionRequestID& id, | 109 const PermissionRequestID& id, |
153 const GURL& origin, | 110 const GURL& requesting_frame, |
154 const GURL& embedder, | |
155 const base::Callback<void(bool)>& callback, | |
156 bool allowed) { | 111 bool allowed) { |
157 NotifyPermissionSet(id, origin, callback, allowed); | 112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
158 } | |
159 | |
160 void ProtectedMediaIdentifierPermissionContext::NotifyPermissionSet( | |
161 const PermissionRequestID& id, | |
162 const GURL& origin, | |
163 const base::Callback<void(bool)>& callback, | |
164 bool allowed) { | |
165 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
166 | 113 |
167 // WebContents may have gone away. | 114 // WebContents may have gone away. |
168 TabSpecificContentSettings* content_settings = | 115 TabSpecificContentSettings* content_settings = |
169 TabSpecificContentSettings::Get(id.render_process_id(), | 116 TabSpecificContentSettings::Get(id.render_process_id(), |
170 id.render_view_id()); | 117 id.render_view_id()); |
171 if (content_settings) { | 118 if (content_settings) { |
172 content_settings->OnProtectedMediaIdentifierPermissionSet( | 119 content_settings->OnProtectedMediaIdentifierPermissionSet( |
173 origin.GetOrigin(), allowed); | 120 requesting_frame.GetOrigin(), allowed); |
174 } | 121 } |
175 | 122 |
176 callback.Run(allowed); | |
177 } | 123 } |
178 | |
179 PermissionQueueController* | |
180 ProtectedMediaIdentifierPermissionContext::QueueController() { | |
181 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
182 DCHECK(!shutting_down_); | |
183 if (!permission_queue_controller_) | |
184 permission_queue_controller_.reset(CreateQueueController()); | |
185 return permission_queue_controller_.get(); | |
186 } | |
187 | |
188 PermissionQueueController* | |
189 ProtectedMediaIdentifierPermissionContext::CreateQueueController() { | |
190 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
191 return new PermissionQueueController( | |
192 profile(), CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER); | |
193 } | |
194 | |
195 void | |
196 ProtectedMediaIdentifierPermissionContext::CancelPendingInfobarRequests( | |
197 int render_process_id, | |
198 int render_view_id, | |
199 const GURL& origin) { | |
200 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | |
201 content::BrowserThread::PostTask( | |
202 content::BrowserThread::UI, | |
203 FROM_HERE, | |
204 base::Bind(&ProtectedMediaIdentifierPermissionContext:: | |
205 CancelPendingInfobarRequests, | |
206 this, | |
207 render_process_id, | |
208 render_view_id, | |
209 origin)); | |
210 return; | |
211 } | |
212 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
213 if (shutting_down_) | |
214 return; | |
215 QueueController()->CancelInfoBarRequest( | |
216 PermissionRequestID(render_process_id, render_view_id, 0, | |
217 origin)); | |
218 } | |
OLD | NEW |