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.
5 #include "chrome/browser/geolocation/geolocation_permission_context_extensions.h"
7 #include "base/callback.h"
9 #if defined(ENABLE_EXTENSIONS)
10 #include "chrome/browser/permissions/permission_request_id.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "extensions/browser/extension_registry.h"
13 #include "extensions/browser/guest_view/web_view/web_view_permission_helper.h"
14 #include "extensions/browser/process_map.h"
15 #include "extensions/browser/suggest_permission_util.h"
16 #include "extensions/browser/view_type_utils.h"
17 #include "extensions/common/extension.h"
19 using extensions::APIPermission
;
20 using extensions::ExtensionRegistry
;
26 void CallbackContentSettingWrapper(
27 const base::Callback
<void(ContentSetting
)>& callback
,
29 callback
.Run(allowed
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
);
31 #endif // ENABLE_EXTENSIONS
33 } // anonymous namespace
35 GeolocationPermissionContextExtensions::
36 GeolocationPermissionContextExtensions(Profile
* profile
)
40 GeolocationPermissionContextExtensions::
41 ~GeolocationPermissionContextExtensions() {
44 bool GeolocationPermissionContextExtensions::RequestPermission(
45 content::WebContents
* web_contents
,
46 const PermissionRequestID
& request_id
,
48 const GURL
& requesting_frame
,
50 const base::Callback
<void(ContentSetting
)>& callback
,
52 bool* new_permission
) {
53 #if defined(ENABLE_EXTENSIONS)
54 GURL requesting_frame_origin
= requesting_frame
.GetOrigin();
56 extensions::WebViewPermissionHelper
* web_view_permission_helper
=
57 extensions::WebViewPermissionHelper::FromWebContents(web_contents
);
58 if (web_view_permission_helper
) {
59 web_view_permission_helper
->RequestGeolocationPermission(
60 bridge_id
, requesting_frame
, user_gesture
,
61 base::Bind(&CallbackContentSettingWrapper
, callback
));
62 *permission_set
= false;
63 *new_permission
= false;
67 ExtensionRegistry
* extension_registry
= ExtensionRegistry::Get(profile_
);
68 if (extension_registry
) {
69 const extensions::Extension
* extension
=
70 extension_registry
->enabled_extensions().GetExtensionOrAppByURL(
71 requesting_frame_origin
);
72 if (IsExtensionWithPermissionOrSuggestInConsole(
73 APIPermission::kGeolocation
, extension
,
74 web_contents
->GetMainFrame())) {
75 // Make sure the extension is in the calling process.
76 if (extensions::ProcessMap::Get(profile_
)->Contains(
77 extension
->id(), request_id
.render_process_id())) {
78 *permission_set
= true;
79 *new_permission
= true;
85 if (extensions::GetViewType(web_contents
) !=
86 extensions::VIEW_TYPE_TAB_CONTENTS
) {
87 // The tab may have gone away, or the request may not be from a tab at all.
88 // TODO(mpcomplete): the request could be from a background page or
89 // extension popup (web_contents will have a different ViewType). But why do
90 // we care? Shouldn't we still put an infobar up in the current tab?
91 LOG(WARNING
) << "Attempt to use geolocation tabless renderer: "
92 << request_id
.ToString()
93 << " (can't prompt user without a visible tab)";
94 *permission_set
= true;
95 *new_permission
= false;
98 #endif // defined(ENABLE_EXTENSIONS)
102 bool GeolocationPermissionContextExtensions::CancelPermissionRequest(
103 content::WebContents
* web_contents
,
105 #if defined(ENABLE_EXTENSIONS)
106 extensions::WebViewPermissionHelper
* web_view_permission_helper
=
108 extensions::WebViewPermissionHelper::FromWebContents(web_contents
)
110 if (web_view_permission_helper
) {
111 web_view_permission_helper
->CancelGeolocationPermissionRequest(bridge_id
);
114 #endif // defined(ENABLE_EXTENSIONS)