1f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// found in the LICENSE file.
4f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
5f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/geolocation/geolocation_permission_context_extensions.h"
6f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
7f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "base/callback.h"
8f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
9f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(ENABLE_EXTENSIONS)
10f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/content_settings/core/common/permission_request_id.h"
12f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "extensions/browser/extension_registry.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/guest_view/web_view/web_view_permission_helper.h"
14f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "extensions/browser/process_map.h"
1503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "extensions/browser/suggest_permission_util.h"
16f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "extensions/browser/view_type_utils.h"
17f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "extensions/common/extension.h"
18f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
19f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)using extensions::APIPermission;
20f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)using extensions::ExtensionRegistry;
21f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
22f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
23f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)GeolocationPermissionContextExtensions::
24f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)GeolocationPermissionContextExtensions(Profile* profile)
25f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    : profile_(profile) {
26f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
27f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
28f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)GeolocationPermissionContextExtensions::
29f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)~GeolocationPermissionContextExtensions() {
30f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
31f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
32f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)bool GeolocationPermissionContextExtensions::RequestPermission(
33f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    content::WebContents* web_contents,
34f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const PermissionRequestID& request_id,
35f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    int bridge_id,
36f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const GURL& requesting_frame,
37f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    bool user_gesture,
38f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    base::Callback<void(bool)> callback,
39f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    bool* permission_set,
40f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    bool* new_permission) {
41f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(ENABLE_EXTENSIONS)
42f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  GURL requesting_frame_origin = requesting_frame.GetOrigin();
43f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  extensions::WebViewPermissionHelper* web_view_permission_helper =
456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      extensions::WebViewPermissionHelper::FromWebContents(web_contents);
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (web_view_permission_helper) {
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    web_view_permission_helper->RequestGeolocationPermission(
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        bridge_id, requesting_frame, user_gesture, callback);
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    *permission_set = false;
50f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    *new_permission = false;
51f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return true;
52f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
53f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
54f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  ExtensionRegistry* extension_registry = ExtensionRegistry::Get(profile_);
55f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (extension_registry) {
56f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const extensions::Extension* extension =
57f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        extension_registry->enabled_extensions().GetExtensionOrAppByURL(
58f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            requesting_frame_origin);
59f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (IsExtensionWithPermissionOrSuggestInConsole(
60f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            APIPermission::kGeolocation, extension,
61f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            web_contents->GetRenderViewHost())) {
62f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      // Make sure the extension is in the calling process.
63f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      if (extensions::ProcessMap::Get(profile_)->Contains(
64f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)              extension->id(), request_id.render_process_id())) {
65f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        *permission_set = true;
66f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        *new_permission = true;
67f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        return true;
68f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      }
69f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
70f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
71f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
72f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (extensions::GetViewType(web_contents) !=
73f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      extensions::VIEW_TYPE_TAB_CONTENTS) {
74f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // The tab may have gone away, or the request may not be from a tab at all.
75f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // TODO(mpcomplete): the request could be from a background page or
76f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // extension popup (web_contents will have a different ViewType). But why do
77f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // we care? Shouldn't we still put an infobar up in the current tab?
78f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    LOG(WARNING) << "Attempt to use geolocation tabless renderer: "
79f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                 << request_id.ToString()
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                 << " (can't prompt user without a visible tab)";
81f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    *permission_set = true;
82f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    *new_permission = false;
83f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return true;
84f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
85f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif  // defined(ENABLE_EXTENSIONS)
86f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return false;
87f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
88f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
89f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)bool GeolocationPermissionContextExtensions::CancelPermissionRequest(
90f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    content::WebContents* web_contents,
91f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    int bridge_id) {
92f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(ENABLE_EXTENSIONS)
936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  extensions::WebViewPermissionHelper* web_view_permission_helper =
946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      web_contents ?
956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      extensions::WebViewPermissionHelper::FromWebContents(web_contents)
966e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      : NULL;
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (web_view_permission_helper) {
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    web_view_permission_helper->CancelGeolocationPermissionRequest(bridge_id);
99f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return true;
100f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
101f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif  // defined(ENABLE_EXTENSIONS)
102f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return false;
103f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
104