File: app_web_contents_helper.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (134 lines) | stat: -rw-r--r-- 5,072 bytes parent folder | download | duplicates (7)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// 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 "extensions/browser/app_window/app_web_contents_helper.h"

#include "base/strings/stringprintf.h"
#include "components/input/native_web_keyboard_event.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/app_window/app_delegate.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/suggest_permission_util.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/permissions/api_permission.h"
#include "third_party/blink/public/common/input/web_gesture_event.h"
#include "third_party/blink/public/mojom/devtools/console_message.mojom.h"

namespace extensions {

AppWebContentsHelper::AppWebContentsHelper(
    content::BrowserContext* browser_context,
    const ExtensionId& extension_id,
    content::WebContents* web_contents,
    AppDelegate* app_delegate)
    : browser_context_(browser_context),
      extension_id_(extension_id),
      web_contents_(web_contents),
      app_delegate_(app_delegate) {}

// static
bool AppWebContentsHelper::ShouldSuppressGestureEvent(
    const blink::WebGestureEvent& event) {
  // Disable "smart zoom" (double-tap with two fingers on Mac trackpad).
  if (event.GetType() == blink::WebInputEvent::Type::kGestureDoubleTap)
    return true;

  // Disable pinch zooming in app windows.
  if (blink::WebInputEvent::IsPinchGestureEventType(event.GetType())) {
    // Only suppress pinch events that cause a scale change. We still
    // allow synthetic wheel events for touchpad pinch to go to the page.
    return !(event.SourceDevice() == blink::WebGestureDevice::kTouchpad &&
             event.NeedsWheelEvent());
  }

  return false;
}

content::WebContents* AppWebContentsHelper::OpenURLFromTab(
    const content::OpenURLParams& params,
    base::OnceCallback<void(content::NavigationHandle&)>
        navigation_handle_callback) const {
  // Don't allow the current tab to be navigated. It would be nice to map all
  // anchor tags (even those without target="_blank") to new tabs, but right
  // now we can't distinguish between those and <meta> refreshes or window.href
  // navigations, which we don't want to allow.
  // TODO(mihaip): Can we check for user gestures instead?
  WindowOpenDisposition disposition = params.disposition;
  if (disposition == WindowOpenDisposition::CURRENT_TAB) {
    web_contents_->GetPrimaryMainFrame()->AddMessageToConsole(
        blink::mojom::ConsoleMessageLevel::kError,
        base::StringPrintf(
            "Can't open same-window link to \"%s\"; try target=\"_blank\".",
            params.url.spec().c_str()));
    return nullptr;
  }

  // These dispositions aren't really navigations.
  if (disposition == WindowOpenDisposition::SAVE_TO_DISK ||
      disposition == WindowOpenDisposition::IGNORE_ACTION)
    return nullptr;

  content::WebContents* contents =
      app_delegate_->OpenURLFromTab(browser_context_, web_contents_, params,
                                    std::move(navigation_handle_callback));
  if (!contents) {
    web_contents_->GetPrimaryMainFrame()->AddMessageToConsole(
        blink::mojom::ConsoleMessageLevel::kError,
        base::StringPrintf(
            "Can't navigate to \"%s\"; apps do not support navigation.",
            params.url.spec().c_str()));
  }

  return contents;
}

void AppWebContentsHelper::RequestPointerLock() const {
  const Extension* extension = GetExtension();
  if (!extension)
    return;

  bool has_permission = IsExtensionWithPermissionOrSuggestInConsole(
      mojom::APIPermissionID::kPointerLock, extension,
      web_contents_->GetPrimaryMainFrame());

  if (has_permission)
    web_contents_->GotResponseToPointerLockRequest(
        blink::mojom::PointerLockResult::kSuccess);
  else
    web_contents_->GotResponseToPointerLockRequest(
        blink::mojom::PointerLockResult::kPermissionDenied);
}

void AppWebContentsHelper::RequestMediaAccessPermission(
    const content::MediaStreamRequest& request,
    content::MediaResponseCallback callback) const {
  const Extension* extension = GetExtension();
  if (!extension)
    return;

  app_delegate_->RequestMediaAccessPermission(web_contents_, request,
                                              std::move(callback), extension);
}

bool AppWebContentsHelper::CheckMediaAccessPermission(
    content::RenderFrameHost* render_frame_host,
    const url::Origin& security_origin,
    blink::mojom::MediaStreamType type) const {
  const Extension* extension = GetExtension();
  if (!extension)
    return false;

  return app_delegate_->CheckMediaAccessPermission(
      render_frame_host, security_origin, type, extension);
}

const Extension* AppWebContentsHelper::GetExtension() const {
  return ExtensionRegistry::Get(browser_context_)
      ->enabled_extensions()
      .GetByID(extension_id_);
}

}  // namespace extensions