File: geolocation_permission_context_extensions.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (113 lines) | stat: -rw-r--r-- 4,176 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/geolocation/geolocation_permission_context_extensions.h"

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "extensions/buildflags/buildflags.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/profiles/profile.h"
#include "components/permissions/permission_request_id.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/guest_view/web_view/web_view_permission_helper.h"
#include "extensions/browser/process_map.h"
#include "extensions/browser/suggest_permission_util.h"
#include "extensions/browser/view_type_utils.h"
#include "extensions/common/extension.h"
#include "extensions/common/mojom/view_type.mojom.h"

using extensions::APIPermission;
using extensions::ExtensionRegistry;
#endif

namespace {

#if BUILDFLAG(ENABLE_EXTENSIONS)
void CallbackContentSettingWrapper(
    base::OnceCallback<void(ContentSetting)> callback,
    bool allowed) {
  std::move(callback).Run(allowed ? CONTENT_SETTING_ALLOW
                                  : CONTENT_SETTING_BLOCK);
}
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

}  // anonymous namespace

GeolocationPermissionContextExtensions::GeolocationPermissionContextExtensions(
    Profile* profile)
#if BUILDFLAG(ENABLE_EXTENSIONS)
    : profile_(profile)
#endif
{
}

GeolocationPermissionContextExtensions::
    ~GeolocationPermissionContextExtensions() = default;

bool GeolocationPermissionContextExtensions::DecidePermission(
    const permissions::PermissionRequestID& request_id,
    const GURL& requesting_frame,
    bool user_gesture,
    base::OnceCallback<void(ContentSetting)>* callback,
    bool* permission_set,
    bool* new_permission) {
#if BUILDFLAG(ENABLE_EXTENSIONS)

  content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(
      request_id.global_render_frame_host_id());

  content::WebContents* web_contents =
      content::WebContents::FromRenderFrameHost(rfh);

  GURL requesting_frame_origin = requesting_frame.DeprecatedGetOriginAsURL();

  extensions::WebViewPermissionHelper* web_view_permission_helper =
      extensions::WebViewPermissionHelper::FromRenderFrameHost(rfh);
  if (web_view_permission_helper) {
    web_view_permission_helper->RequestGeolocationPermission(
        requesting_frame, user_gesture,
        base::BindOnce(&CallbackContentSettingWrapper, std::move(*callback)));
    *permission_set = false;
    *new_permission = false;
    return true;
  }

  ExtensionRegistry* extension_registry = ExtensionRegistry::Get(profile_);
  if (extension_registry) {
    const extensions::Extension* extension =
        extension_registry->enabled_extensions().GetExtensionOrAppByURL(
            requesting_frame_origin);
    if (IsExtensionWithPermissionOrSuggestInConsole(
            extensions::mojom::APIPermissionID::kGeolocation, extension,
            web_contents->GetPrimaryMainFrame())) {
      // Make sure the extension is in the calling process.
      if (extensions::ProcessMap::Get(profile_)->Contains(
              extension->id(),
              request_id.global_render_frame_host_id().child_id)) {
        *permission_set = true;
        *new_permission = true;
        return true;
      }
    }
  }

  extensions::mojom::ViewType view_type = extensions::GetViewType(web_contents);
  if (view_type != extensions::mojom::ViewType::kTabContents &&
      view_type != extensions::mojom::ViewType::kInvalid) {
    // The tab may have gone away, or the request may not be from a tab at all.
    // TODO(mpcomplete): the request could be from a background page or
    // extension popup (web_contents will have a different ViewType). But why do
    // we care? Shouldn't we still put an infobar up in the current tab?
    LOG(WARNING) << "Attempt to use geolocation tabless renderer: "
                 << request_id.ToString()
                 << " (can't prompt user without a visible tab)";
    *permission_set = true;
    *new_permission = false;
    return true;
  }
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)
  return false;
}