OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/geolocation/chrome_geolocation_permission_context.h" | 5 #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h" |
6 | 6 |
7 #include <functional> | 7 #include <functional> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/content_settings/host_content_settings_map.h" | 13 #include "chrome/browser/content_settings/host_content_settings_map.h" |
14 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 14 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
15 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
16 #include "chrome/browser/geolocation/geolocation_infobar_queue_controller.h" | 16 #include "chrome/browser/geolocation/geolocation_infobar_queue_controller.h" |
| 17 #include "chrome/browser/geolocation/geolocation_permission_request_id.h" |
17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/tab_contents/tab_util.h" | 19 #include "chrome/browser/tab_contents/tab_util.h" |
19 #include "chrome/browser/view_type_utils.h" | 20 #include "chrome/browser/view_type_utils.h" |
20 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
23 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
24 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" | 25 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
26 | 27 |
27 using WebKit::WebSecurityOrigin; | |
28 using content::BrowserThread; | |
29 using content::WebContents; | |
30 | |
31 // GeolocationPermissionContext ----------------------------------------------- | |
32 | 28 |
33 ChromeGeolocationPermissionContext::ChromeGeolocationPermissionContext( | 29 ChromeGeolocationPermissionContext::ChromeGeolocationPermissionContext( |
34 Profile* profile) | 30 Profile* profile) |
35 : profile_(profile) { | 31 : profile_(profile) { |
36 } | 32 } |
37 | 33 |
38 ChromeGeolocationPermissionContext::~ChromeGeolocationPermissionContext() { | 34 ChromeGeolocationPermissionContext::~ChromeGeolocationPermissionContext() { |
39 } | 35 } |
40 | 36 |
41 void ChromeGeolocationPermissionContext::RequestGeolocationPermission( | 37 void ChromeGeolocationPermissionContext::RequestGeolocationPermission( |
42 int render_process_id, int render_view_id, int bridge_id, | 38 int render_process_id, |
43 const GURL& requesting_frame, base::Callback<void(bool)> callback) { | 39 int render_view_id, |
44 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 40 int bridge_id, |
45 BrowserThread::PostTask( | 41 const GURL& requesting_frame, |
46 BrowserThread::UI, FROM_HERE, | 42 base::Callback<void(bool)> callback) { |
| 43 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| 44 content::BrowserThread::PostTask( |
| 45 content::BrowserThread::UI, FROM_HERE, |
47 base::Bind( | 46 base::Bind( |
48 &ChromeGeolocationPermissionContext::RequestGeolocationPermission, | 47 &ChromeGeolocationPermissionContext::RequestGeolocationPermission, |
49 this, render_process_id, render_view_id, bridge_id, | 48 this, render_process_id, render_view_id, bridge_id, |
50 requesting_frame, callback)); | 49 requesting_frame, callback)); |
51 return; | 50 return; |
52 } | 51 } |
53 | 52 |
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 53 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
55 WebContents* web_contents = | 54 content::WebContents* web_contents = |
56 tab_util::GetWebContentsByID(render_process_id, render_view_id); | 55 tab_util::GetWebContentsByID(render_process_id, render_view_id); |
| 56 const GeolocationPermissionRequestID id(render_process_id, render_view_id, |
| 57 bridge_id); |
57 if (chrome::GetViewType(web_contents) != chrome::VIEW_TYPE_TAB_CONTENTS) { | 58 if (chrome::GetViewType(web_contents) != chrome::VIEW_TYPE_TAB_CONTENTS) { |
58 // The tab may have gone away, or the request may not be from a tab at all. | 59 // The tab may have gone away, or the request may not be from a tab at all. |
59 // TODO(mpcomplete): the request could be from a background page or | 60 // TODO(mpcomplete): the request could be from a background page or |
60 // extension popup (tab_contents will have a different ViewType). But why do | 61 // extension popup (tab_contents will have a different ViewType). But why do |
61 // we care? Shouldn't we still put an infobar up in the current tab? | 62 // we care? Shouldn't we still put an infobar up in the current tab? |
62 LOG(WARNING) << "Attempt to use geolocation tabless renderer: " | 63 LOG(WARNING) << "Attempt to use geolocation tabless renderer: " |
63 << render_process_id << "," << render_view_id << "," | 64 << id.ToString() |
64 << bridge_id << " (can't prompt user without a visible tab)"; | 65 << " (can't prompt user without a visible tab)"; |
65 NotifyPermissionSet(render_process_id, render_view_id, bridge_id, | 66 NotifyPermissionSet(id, requesting_frame, callback, false); |
66 requesting_frame, callback, false); | |
67 return; | 67 return; |
68 } | 68 } |
69 | 69 |
70 GURL embedder = web_contents->GetURL(); | 70 GURL embedder = web_contents->GetURL(); |
71 if (!requesting_frame.is_valid() || !embedder.is_valid()) { | 71 if (!requesting_frame.is_valid() || !embedder.is_valid()) { |
72 LOG(WARNING) << "Attempt to use geolocation from an invalid URL: " | 72 LOG(WARNING) << "Attempt to use geolocation from an invalid URL: " |
73 << requesting_frame << "," << embedder | 73 << requesting_frame << "," << embedder |
74 << " (geolocation is not supported in popups)"; | 74 << " (geolocation is not supported in popups)"; |
75 NotifyPermissionSet(render_process_id, render_view_id, bridge_id, | 75 NotifyPermissionSet(id, requesting_frame, callback, false); |
76 requesting_frame, callback, false); | |
77 return; | 76 return; |
78 } | 77 } |
79 | 78 |
80 DecidePermission(render_process_id, render_view_id, bridge_id, | 79 DecidePermission(id, requesting_frame, embedder, callback); |
81 requesting_frame, embedder, callback); | 80 |
82 } | 81 } |
83 | 82 |
84 void ChromeGeolocationPermissionContext::CancelGeolocationPermissionRequest( | 83 void ChromeGeolocationPermissionContext::CancelGeolocationPermissionRequest( |
85 int render_process_id, | 84 int render_process_id, |
86 int render_view_id, | 85 int render_view_id, |
87 int bridge_id, | 86 int bridge_id, |
88 const GURL& requesting_frame) { | 87 const GURL& requesting_frame) { |
89 CancelPendingInfoBarRequest(render_process_id, render_view_id, bridge_id); | 88 CancelPendingInfoBarRequest(GeolocationPermissionRequestID( |
| 89 render_process_id, render_view_id, bridge_id)); |
90 } | 90 } |
91 | 91 |
92 void ChromeGeolocationPermissionContext::DecidePermission( | 92 void ChromeGeolocationPermissionContext::DecidePermission( |
93 int render_process_id, int render_view_id, int bridge_id, | 93 const GeolocationPermissionRequestID& id, |
94 const GURL& requesting_frame, const GURL& embedder, | 94 const GURL& requesting_frame, |
| 95 const GURL& embedder, |
95 base::Callback<void(bool)> callback) { | 96 base::Callback<void(bool)> callback) { |
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 97 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
97 | 98 |
98 ExtensionService* extension_service = profile_->GetExtensionService(); | 99 ExtensionService* extension_service = profile_->GetExtensionService(); |
99 if (extension_service) { | 100 if (extension_service) { |
100 const extensions::Extension* extension = | 101 const extensions::Extension* extension = |
101 extension_service->extensions()->GetExtensionOrAppByURL( | 102 extension_service->extensions()->GetExtensionOrAppByURL( |
102 ExtensionURLInfo( | 103 ExtensionURLInfo(WebKit::WebSecurityOrigin::createFromString( |
103 WebSecurityOrigin::createFromString( | 104 UTF8ToUTF16(requesting_frame.spec())), |
104 UTF8ToUTF16(requesting_frame.spec())), | 105 requesting_frame)); |
105 requesting_frame)); | |
106 if (extension && | 106 if (extension && |
107 extension->HasAPIPermission(extensions::APIPermission::kGeolocation)) { | 107 extension->HasAPIPermission(extensions::APIPermission::kGeolocation)) { |
108 // Make sure the extension is in the calling process. | 108 // Make sure the extension is in the calling process. |
109 if (extension_service->process_map()->Contains( | 109 if (extension_service->process_map()->Contains(extension->id(), |
110 extension->id(), render_process_id)) { | 110 id.render_process_id())) { |
111 PermissionDecided(render_process_id, render_view_id, bridge_id, | 111 PermissionDecided(id, requesting_frame, embedder, callback, true); |
112 requesting_frame, embedder, callback, true); | |
113 return; | 112 return; |
114 } | 113 } |
115 } | 114 } |
116 } | 115 } |
117 | 116 |
118 ContentSetting content_setting = | 117 ContentSetting content_setting = |
119 profile_->GetHostContentSettingsMap()->GetContentSetting( | 118 profile_->GetHostContentSettingsMap()->GetContentSetting( |
120 requesting_frame, | 119 requesting_frame, embedder, CONTENT_SETTINGS_TYPE_GEOLOCATION, |
121 embedder, | |
122 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
123 std::string()); | 120 std::string()); |
124 switch (content_setting) { | 121 switch (content_setting) { |
125 case CONTENT_SETTING_BLOCK: | 122 case CONTENT_SETTING_BLOCK: |
126 PermissionDecided(render_process_id, render_view_id, bridge_id, | 123 PermissionDecided(id, requesting_frame, embedder, callback, false); |
127 requesting_frame, embedder, callback, false); | |
128 break; | 124 break; |
129 case CONTENT_SETTING_ALLOW: | 125 case CONTENT_SETTING_ALLOW: |
130 PermissionDecided(render_process_id, render_view_id, bridge_id, | 126 PermissionDecided(id, requesting_frame, embedder, callback, true); |
131 requesting_frame, embedder, callback, true); | |
132 break; | 127 break; |
133 default: | 128 default: |
134 // setting == ask. Prompt the user. | 129 // setting == ask. Prompt the user. |
135 QueueController()->CreateInfoBarRequest( | 130 QueueController()->CreateInfoBarRequest( |
136 render_process_id, render_view_id, bridge_id, requesting_frame, | 131 id, requesting_frame, embedder, base::Bind( |
137 embedder, base::Bind( | |
138 &ChromeGeolocationPermissionContext::NotifyPermissionSet, | 132 &ChromeGeolocationPermissionContext::NotifyPermissionSet, |
139 base::Unretained(this), | 133 base::Unretained(this), id, requesting_frame, callback)); |
140 render_process_id, render_view_id, bridge_id, requesting_frame, | |
141 callback)); | |
142 } | 134 } |
143 } | 135 } |
144 | 136 |
145 void ChromeGeolocationPermissionContext::PermissionDecided( | 137 void ChromeGeolocationPermissionContext::PermissionDecided( |
146 int render_process_id, | 138 const GeolocationPermissionRequestID& id, |
147 int render_view_id, | |
148 int bridge_id, | |
149 const GURL& requesting_frame, | 139 const GURL& requesting_frame, |
150 const GURL& embedder, | 140 const GURL& embedder, |
151 base::Callback<void(bool)> callback, | 141 base::Callback<void(bool)> callback, |
152 bool allowed) { | 142 bool allowed) { |
153 NotifyPermissionSet(render_process_id, render_view_id, bridge_id, | 143 NotifyPermissionSet(id, requesting_frame, callback, allowed); |
154 requesting_frame, callback, allowed); | |
155 } | 144 } |
156 | 145 |
157 void ChromeGeolocationPermissionContext::NotifyPermissionSet( | 146 void ChromeGeolocationPermissionContext::NotifyPermissionSet( |
158 int render_process_id, | 147 const GeolocationPermissionRequestID& id, |
159 int render_view_id, | |
160 int bridge_id, | |
161 const GURL& requesting_frame, | 148 const GURL& requesting_frame, |
162 base::Callback<void(bool)> callback, | 149 base::Callback<void(bool)> callback, |
163 bool allowed) { | 150 bool allowed) { |
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 151 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
165 | 152 |
166 // WebContents may have gone away (or not exists for extension). | 153 // WebContents may have gone away (or not exists for extension). |
167 TabSpecificContentSettings* content_settings = | 154 TabSpecificContentSettings* content_settings = |
168 TabSpecificContentSettings::Get(render_process_id, render_view_id); | 155 TabSpecificContentSettings::Get(id.render_process_id(), |
| 156 id.render_view_id()); |
169 if (content_settings) { | 157 if (content_settings) { |
170 content_settings->OnGeolocationPermissionSet(requesting_frame.GetOrigin(), | 158 content_settings->OnGeolocationPermissionSet(requesting_frame.GetOrigin(), |
171 allowed); | 159 allowed); |
172 } | 160 } |
173 | 161 |
174 callback.Run(allowed); | 162 callback.Run(allowed); |
175 } | 163 } |
176 | 164 |
177 GeolocationInfoBarQueueController* | 165 GeolocationInfoBarQueueController* |
178 ChromeGeolocationPermissionContext::QueueController() { | 166 ChromeGeolocationPermissionContext::QueueController() { |
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 167 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
180 if (!geolocation_infobar_queue_controller_) | 168 if (!geolocation_infobar_queue_controller_) |
181 geolocation_infobar_queue_controller_.reset(CreateQueueController()); | 169 geolocation_infobar_queue_controller_.reset(CreateQueueController()); |
182 return geolocation_infobar_queue_controller_.get(); | 170 return geolocation_infobar_queue_controller_.get(); |
183 } | 171 } |
184 | 172 |
185 GeolocationInfoBarQueueController* | 173 GeolocationInfoBarQueueController* |
186 ChromeGeolocationPermissionContext::CreateQueueController() { | 174 ChromeGeolocationPermissionContext::CreateQueueController() { |
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 175 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
188 return new GeolocationInfoBarQueueController(profile()); | 176 return new GeolocationInfoBarQueueController(profile()); |
189 } | 177 } |
190 | 178 |
191 void ChromeGeolocationPermissionContext::CancelPendingInfoBarRequest( | 179 void ChromeGeolocationPermissionContext::CancelPendingInfoBarRequest( |
192 int render_process_id, | 180 const GeolocationPermissionRequestID& id) { |
193 int render_view_id, | 181 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
194 int bridge_id) { | 182 content::BrowserThread::PostTask( |
195 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 183 content::BrowserThread::UI, FROM_HERE, |
196 BrowserThread::PostTask( | |
197 BrowserThread::UI, FROM_HERE, | |
198 base::Bind( | 184 base::Bind( |
199 &ChromeGeolocationPermissionContext::CancelPendingInfoBarRequest, | 185 &ChromeGeolocationPermissionContext::CancelPendingInfoBarRequest, |
200 this, render_process_id, render_view_id, bridge_id)); | 186 this, id)); |
201 return; | 187 return; |
202 } | 188 } |
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 189 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
204 QueueController()->CancelInfoBarRequest( | 190 QueueController()->CancelInfoBarRequest(id); |
205 render_process_id, | |
206 render_view_id, | |
207 bridge_id); | |
208 } | 191 } |
OLD | NEW |