File: extension_action_popup_contents.cc

package info (click to toggle)
chromium 140.0.7339.127-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,201,772 kB
  • sloc: cpp: 35,093,800; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,798; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,119; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (172 lines) | stat: -rw-r--r-- 6,207 bytes parent folder | download | duplicates (4)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2025 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/ui/android/extensions/extension_action_popup_contents.h"

#include "base/android/jni_string.h"
#include "base/notimplemented.h"
#include "chrome/browser/extensions/extension_view_host.h"
#include "chrome/browser/extensions/extension_view_host_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_action.h"
#include "extensions/browser/extension_action_manager.h"
#include "extensions/browser/extension_registry.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/browser/ui/android/extensions/jni_headers/ExtensionActionPopupContents_jni.h"

using base::android::AttachCurrentThread;
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;
using content::RenderFrameHost;
using content::WebContents;

namespace extensions {

namespace {

// The minimum and maximum sizes for the extension popup.
// https://developer.chrome.com/docs/extensions/reference/api/action#popup
constexpr gfx::Size kMinSize = {25, 25};
constexpr gfx::Size kMaxSize = {800, 600};

}  // namespace

ExtensionActionPopupContents::ExtensionActionPopupContents(
    std::unique_ptr<ExtensionViewHost> host)
    : host_(std::move(host)) {
  java_object_ = Java_ExtensionActionPopupContents_Constructor(
      AttachCurrentThread(), reinterpret_cast<jlong>(this),
      host_->host_contents());
  host_->set_view(this);
  // Handle the containing view calling window.close();
  // The base::Unretained() below is safe because this object owns `host_`, so
  // the callback will never fire if `this` is deleted.
  host_->SetCloseHandler(
      base::BindOnce(&ExtensionActionPopupContents::HandleCloseExtensionHost,
                     base::Unretained(this)));
  WebContentsObserver::Observe(host_->host_contents());
  auto* primary_main_frame = host_->host_contents()->GetPrimaryMainFrame();
  if (primary_main_frame->IsRenderFrameLive()) {
    SetUpNewMainFrame(primary_main_frame);
  }
}

ExtensionActionPopupContents::~ExtensionActionPopupContents() = default;

ScopedJavaLocalRef<jobject> ExtensionActionPopupContents::GetJavaObject() {
  return java_object_.AsLocalRef(AttachCurrentThread());
}

void ExtensionActionPopupContents::RenderFrameHostChanged(
    RenderFrameHost* old_host,
    RenderFrameHost* new_host) {
  // Since we skipped speculative main frames in RenderFrameCreated, we must
  // watch for them being swapped in by watching for RenderFrameHostChanged().
  if (new_host != host_->host_contents()->GetPrimaryMainFrame()) {
    return;
  }

  // Ignore the initial main frame host, as there's no renderer frame for it
  // yet. If the DCHECK fires, then we would need to handle the initial main
  // frame when it its renderer frame is created.
  if (!old_host) {
    DCHECK(!new_host->IsRenderFrameLive());
    return;
  }

  SetUpNewMainFrame(new_host);
}

void ExtensionActionPopupContents::ResizeDueToAutoResize(
    content::WebContents* web_contents,
    const gfx::Size& new_size) {
  Java_ExtensionActionPopupContents_resizeDueToAutoResize(
      AttachCurrentThread(), java_object_, new_size.width(), new_size.height());
}

void ExtensionActionPopupContents::RenderFrameCreated(
    RenderFrameHost* render_frame_host) {
  // Only handle the initial main frame, not speculative ones.
  if (render_frame_host != host_->host_contents()->GetPrimaryMainFrame()) {
    return;
  }

  SetUpNewMainFrame(render_frame_host);
}

bool ExtensionActionPopupContents::HandleKeyboardEvent(
    content::WebContents* source,
    const input::NativeWebKeyboardEvent& event) {
  NOTIMPLEMENTED();
  return false;
}

void ExtensionActionPopupContents::OnLoaded() {
  Java_ExtensionActionPopupContents_onLoaded(AttachCurrentThread(),
                                             java_object_);
}

void ExtensionActionPopupContents::Destroy(JNIEnv* env) {
  delete this;
}

void ExtensionActionPopupContents::LoadInitialPage(JNIEnv* env) {
  host_->CreateRendererSoon();
}

void ExtensionActionPopupContents::SetUpNewMainFrame(
    RenderFrameHost* render_frame_host) {
  render_frame_host->GetView()->EnableAutoResize(kMinSize, kMaxSize);
}

void ExtensionActionPopupContents::HandleCloseExtensionHost(
    ExtensionHost* host) {
  DCHECK_EQ(host, host_.get());
  Java_ExtensionActionPopupContents_onClose(AttachCurrentThread(),
                                            java_object_);
}

// JNI method to create an ExtensionActionPopupContents instance.
// This is called from the Java side to initiate the display of an extension
// popup.
static ScopedJavaLocalRef<jobject> JNI_ExtensionActionPopupContents_Create(
    JNIEnv* env,
    Profile* profile,
    std::string& action_id,
    int tab_id) {
  ExtensionRegistry* registry = ExtensionRegistry::Get(profile);
  DCHECK(registry);

  ExtensionActionManager* manager = ExtensionActionManager::Get(profile);
  DCHECK(manager);

  const Extension* extension =
      registry->enabled_extensions().GetByID(action_id);
  DCHECK(extension);

  ExtensionAction* action = manager->GetExtensionAction(*extension);
  DCHECK(action);

  GURL popup_url = action->GetPopupUrl(tab_id);

  std::unique_ptr<ExtensionViewHost> host =
      ExtensionViewHostFactory::CreatePopupHost(popup_url, profile);
  DCHECK(host);

  // The ExtensionActionPopupContents C++ object's lifetime is managed by its
  // Java counterpart. The Java object holds a pointer to this C++ instance.
  // When the Java side is finished with the popup, it will explicitly call
  // a 'destroy()' method on its Java object, which in turn calls the native
  // ExtensionActionPopupContents::Destroy() method, leading to the deletion
  // of this C++ object. Therefore, 'new' is used here, and ownership is
  // effectively passed to the Java-controlled lifecycle.
  ExtensionActionPopupContents* popup =
      new ExtensionActionPopupContents(std::move(host));
  return popup->GetJavaObject();
}

}  // namespace extensions