Skip to content

Commit

Permalink
Reland "Move implementation of Mojo method GetTextSurroundingSelectio…
Browse files Browse the repository at this point in the history
…n() to Blink"

We can't construct a WTF::String using the default constructor when running
the GetTextSurroundingSelectionCallback directly for those cases where the
SurroundingText is empty, since that will construct a null string instead
of an empty string, causing a crash at the Mojo level due to trying to send
a null message over the wire, which is not permitted.

To avoid this crash in Mojo, make sure we create a WTF::String out of an
empty string, instead of using the default constructor.

Last, this CL also incorporates the required changes to address Daniel's
comments after the original patch landed in CL 1696968 [1], to rename the
newly added Mojo interface to become blink::mojom::Frame.

[1] https://crrev.com/c/1696968/11/third_party/blink/public/mojom/editing/editing.mojom#9

Original change's description:
> Move this method from the content.mojom.Frame Mojo interface into Blink,
> as part of a new blink.mojom.SurroundingText Mojo interface, and add a new
> class there to implement such interface (i.e. blink::SurroundingTextImpl),
> to replace usages of content.mojom.Frame's GetTextSurroundingSelection()
> from the browser process.
>
> Note that this SurroundingTextImpl class still relies on WebSurroundingText
> and WebLocalFrame to keep the change smaller, but this is a temporary step
> that will be corrected once WebSurroundingText has been moved out of the
> public API and into renderer/core, which will happen on a follow-up CL.
>
> Bug: 980151
> Change-Id: I62fb2d5b24a98773c1acddbe1c58217c1a2d53e4
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1696968
> Commit-Queue: Mario Sanchez Prada <mario@igalia.com>
> Reviewed-by: Mike West <mkwst@chromium.org>
> Reviewed-by: Ken Buchanan <kenrb@chromium.org>
> Reviewed-by: Kentaro Hara <haraken@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#679981}

Bug: 980151, 987214, 987191, 989438
Change-Id: Ia32be36c1a7524930ddcecc79e7fac5bcb514a13
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1735459
Reviewed-by: Ken Buchanan <kenrb@chromium.org>
Reviewed-by: Mike West <mkwst@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Commit-Queue: Mario Sanchez Prada <mario@igalia.com>
Cr-Commit-Position: refs/heads/master@{#686348}
  • Loading branch information
mariospr authored and Commit Bot committed Aug 13, 2019
1 parent f382964 commit b81acd7
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 31 deletions.
13 changes: 12 additions & 1 deletion content/browser/frame_host/render_frame_host_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,9 @@ void RenderFrameHostImpl::RenderProcessExited(
// process's channel.
remote_associated_interfaces_.reset();

// Ensure that the AssociatedRemote<blink::mojom::Frame> works after a crash.
frame_remote_.reset();

// Any termination disablers in content loaded by the new process will
// be sent again.
sudden_termination_disabler_types_enabled_ = 0;
Expand Down Expand Up @@ -2921,7 +2924,8 @@ void RenderFrameHostImpl::RequestTextSurroundingSelection(
TextSurroundingSelectionCallback callback,
int max_length) {
DCHECK(!callback.is_null());
frame_->GetTextSurroundingSelection(max_length, std::move(callback));
GetAssociatedFrameRemote()->GetTextSurroundingSelection(max_length,
std::move(callback));
}

void RenderFrameHostImpl::AllowBindings(int bindings_flags) {
Expand Down Expand Up @@ -5547,6 +5551,13 @@ RenderFrameHostImpl::GetFindInPage() {
return find_in_page_;
}

const mojo::AssociatedRemote<blink::mojom::Frame>&
RenderFrameHostImpl::GetAssociatedFrameRemote() {
if (!frame_remote_)
GetRemoteAssociatedInterfaces()->GetInterface(&frame_remote_);
return frame_remote_;
}

void RenderFrameHostImpl::ResetLoadingState() {
if (is_loading()) {
// When pending deletion, just set the loading state to not loading.
Expand Down
7 changes: 7 additions & 0 deletions content/browser/frame_host/render_frame_host_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#include "third_party/blink/public/mojom/devtools/devtools_agent.mojom.h"
#include "third_party/blink/public/mojom/frame/document_interface_broker.mojom.h"
#include "third_party/blink/public/mojom/frame/find_in_page.mojom.h"
#include "third_party/blink/public/mojom/frame/frame.mojom.h"
#include "third_party/blink/public/mojom/frame/navigation_initiator.mojom.h"
#include "third_party/blink/public/mojom/idle/idle_manager.mojom.h"
#include "third_party/blink/public/mojom/image_downloader/image_downloader.mojom.h"
Expand Down Expand Up @@ -754,6 +755,9 @@ class CONTENT_EXPORT RenderFrameHostImpl
// Returns remote to renderer side FindInPage associated with this frame.
const mojo::AssociatedRemote<blink::mojom::FindInPage>& GetFindInPage();

// Returns associated remote for the blink::mojom::Frame Mojo interface.
const mojo::AssociatedRemote<blink::mojom::Frame>& GetAssociatedFrameRemote();

// Resets the loading state. Following this call, the RenderFrameHost will be
// in a non-loading state.
void ResetLoadingState();
Expand Down Expand Up @@ -1958,6 +1962,9 @@ class CONTENT_EXPORT RenderFrameHostImpl
// Holder of Mojo connection with FindInPage service in Blink.
mojo::AssociatedRemote<blink::mojom::FindInPage> find_in_page_;

// Holder of Mojo connection with the Frame service in Blink.
mojo::AssociatedRemote<blink::mojom::Frame> frame_remote_;

// Holds a NavigationRequest when it's about to commit, ie. after
// OnCrossDocumentCommitProcessed has returned a positive answer for this
// NavigationRequest but before receiving DidCommitProvisionalLoad. This
Expand Down
6 changes: 0 additions & 6 deletions content/common/frame.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ interface Frame {
// Set the lifecycle state.
SetLifecycleState(blink.mojom.FrameLifecycleState state);

// Retrieves the text surrounding the current selection for the frame up to
// the length specified by |max_length|, along with its start and end offsets.
GetTextSurroundingSelection(uint32 max_length)
=> (mojo_base.mojom.String16 content, uint32 start_offset,
uint32 end_offset);

// Samsung Galaxy Note-specific "smart clip" stylus text getter.
// Extracts the data at the given rect.
[EnableIf=is_android]
Expand Down
19 changes: 0 additions & 19 deletions content/renderer/render_frame_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@
#include "third_party/blink/public/web/web_security_policy.h"
#include "third_party/blink/public/web/web_serialized_script_value.h"
#include "third_party/blink/public/web/web_settings.h"
#include "third_party/blink/public/web/web_surrounding_text.h"
#include "third_party/blink/public/web/web_user_gesture_indicator.h"
#include "third_party/blink/public/web/web_user_media_client.h"
#include "third_party/blink/public/web/web_view.h"
Expand Down Expand Up @@ -2824,24 +2823,6 @@ void RenderFrameImpl::SetLifecycleState(
frame_->SetLifecycleState(state);
}

void RenderFrameImpl::GetTextSurroundingSelection(
uint32_t max_length,
GetTextSurroundingSelectionCallback callback) {
blink::WebSurroundingText surrounding_text(frame_, max_length);

if (surrounding_text.IsEmpty()) {
// |surrounding_text| might not be correctly initialized, for example if
// |frame_->SelectionRange().IsNull()|, in other words, if there was no
// selection.
std::move(callback).Run(base::string16(), 0, 0);
return;
}

std::move(callback).Run(surrounding_text.TextContent().Utf16(),
surrounding_text.StartOffsetInTextContent(),
surrounding_text.EndOffsetInTextContent());
}

void RenderFrameImpl::VisibilityChanged(
blink::mojom::FrameVisibility visibility) {
GetFrameHost()->VisibilityChanged(visibility);
Expand Down
3 changes: 0 additions & 3 deletions content/renderer/render_frame_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,6 @@ class CONTENT_EXPORT RenderFrameImpl
void ResumeBlockedRequests() override;
void CancelBlockedRequests() override;
void SetLifecycleState(blink::mojom::FrameLifecycleState state) override;
void GetTextSurroundingSelection(
uint32_t max_length,
GetTextSurroundingSelectionCallback callback) override;

#if defined(OS_ANDROID)
void ExtractSmartClipData(
Expand Down
1 change: 0 additions & 1 deletion content/shell/test_runner/test_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
#include "third_party/blink/public/web/web_security_policy.h"
#include "third_party/blink/public/web/web_serialized_script_value.h"
#include "third_party/blink/public/web/web_settings.h"
#include "third_party/blink/public/web/web_surrounding_text.h"
#include "third_party/blink/public/web/web_view.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
Expand Down
1 change: 0 additions & 1 deletion content/shell/test_runner/test_runner_for_specific_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
#include "third_party/blink/public/web/web_security_policy.h"
#include "third_party/blink/public/web/web_serialized_script_value.h"
#include "third_party/blink/public/web/web_settings.h"
#include "third_party/blink/public/web/web_surrounding_text.h"
#include "third_party/blink/public/web/web_view.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
Expand Down
1 change: 1 addition & 0 deletions third_party/blink/public/mojom/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mojom("mojom_platform") {
"filesystem/file_system.mojom",
"frame/document_interface_broker.mojom",
"frame/find_in_page.mojom",
"frame/frame.mojom",
"frame/frame_host_test_interface.mojom",
"frame/lifecycle.mojom",
"frame/navigation_initiator.mojom",
Expand Down
23 changes: 23 additions & 0 deletions third_party/blink/public/mojom/frame/frame.mojom
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

module blink.mojom;

import "mojo/public/mojom/base/string16.mojom";

// Implemented in Blink, this interface defines frame-specific methods that will
// be invoked from the browser process (e.g. content::RenderFrameHostImpl).
//
// Note that this is different than content/common/frame.mojom in that the
// methods defined here are handled directly in Blink without passing through
// content. In the future this interface will likely host more methods as the
// Onion Soup project advances, which can potentially leading to the removal of
// content/common/frame.mojom in the future if enough code is moved to Blink.
interface Frame {
// Retrieves the text surrounding the current selection for the frame up to
// the length specified by |max_length|, along with its start and end offsets.
GetTextSurroundingSelection(uint32 max_length)
=> (mojo_base.mojom.String16 content, uint32 start_offset,
uint32 end_offset);
};
5 changes: 5 additions & 0 deletions third_party/blink/renderer/core/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,10 @@ specific_include_rules = {
],
"element_test.cc": [
"+third_party/blink/renderer/core/exported/web_plugin_container_impl.h"
],
# TODO(crbug.com/980151): Include this while WebSurroundingText is not
# removed from the public API and implemented in renderer/core.
"frame_impl.cc" : [
"+third_party/blink/renderer/core/frame/web_local_frame_impl.h",
]
}
2 changes: 2 additions & 0 deletions third_party/blink/renderer/core/frame/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ blink_core_sources("frame") {
"frame_client.h",
"frame_console.cc",
"frame_console.h",
"frame_impl.cc",
"frame_impl.h",
"frame_lifecycle.cc",
"frame_lifecycle.h",
"frame_overlay.cc",
Expand Down
56 changes: 56 additions & 0 deletions third_party/blink/renderer/core/frame/frame_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/frame/frame_impl.h"

#include <utility>

#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/public/web/web_surrounding_text.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

FrameImpl::FrameImpl(WebLocalFrameImpl& frame,
InterfaceRegistry* interface_registry)
: frame_(&frame) {
if (!interface_registry)
return;
// TODO(crbug.com/800641): Use InterfaceValidator when it works for associated
// interfaces.
interface_registry->AddAssociatedInterface(
WTF::BindRepeating(&FrameImpl::BindToReceiver, WrapWeakPersistent(this)));
}

void FrameImpl::BindToReceiver(
mojo::PendingAssociatedReceiver<mojom::blink::Frame> receiver) {
receiver_.Bind(std::move(receiver),
frame_->GetTaskRunner(blink::TaskType::kInternalDefault));
}

void FrameImpl::GetTextSurroundingSelection(
uint32_t max_length,
GetTextSurroundingSelectionCallback callback) {
blink::WebSurroundingText surrounding_text(frame_, max_length);

// |surrounding_text| might not be correctly initialized, for example if
// |frame_->SelectionRange().IsNull()|, in other words, if there was no
// selection.
if (surrounding_text.IsEmpty()) {
// Don't use WTF::String's default constructor so that we make sure that we
// always send a valid empty string over the wire instead of a null pointer.
std::move(callback).Run(g_empty_string, 0, 0);
return;
}

std::move(callback).Run(surrounding_text.TextContent(),
surrounding_text.StartOffsetInTextContent(),
surrounding_text.EndOffsetInTextContent());
}

} // namespace blink
48 changes: 48 additions & 0 deletions third_party/blink/renderer/core/frame/frame_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_FRAME_IMPL_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_FRAME_IMPL_H_

#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
#include "third_party/blink/public/mojom/frame/frame.mojom-blink.h"
#include "third_party/blink/public/platform/interface_registry.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"

namespace blink {

class WebLocalFrameImpl;

// Implementation of blink::mojom::Frame
class CORE_EXPORT FrameImpl final : public GarbageCollectedFinalized<FrameImpl>,
public mojom::blink::Frame {
public:
// TODO(crbug.com/980151): Construct FrameImpl this way only while we need to
// rely in blink::WebSurroundingText to implement GetTextSurroundingSelection,
// and remove the dependency once WebSurroundingText is out of the public API.
FrameImpl(WebLocalFrameImpl& frame, InterfaceRegistry* interface_registry);

void BindToReceiver(
mojo::PendingAssociatedReceiver<mojom::blink::Frame> receiver);

void GetTextSurroundingSelection(
uint32_t max_length,
GetTextSurroundingSelectionCallback callback) final;

void Trace(blink::Visitor* visitor) { visitor->Trace(frame_); }

private:
const Member<WebLocalFrameImpl> frame_;

mojo::AssociatedReceiver<mojom::blink::Frame> receiver_{this};

DISALLOW_COPY_AND_ASSIGN(FrameImpl);
};

} // namespace blink

#endif // THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_FRAME_IMPL_H_
3 changes: 3 additions & 0 deletions third_party/blink/renderer/core/frame/web_local_frame_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
#include "third_party/blink/renderer/core/frame/deprecation.h"
#include "third_party/blink/renderer/core/frame/find_in_page.h"
#include "third_party/blink/renderer/core/frame/frame_console.h"
#include "third_party/blink/renderer/core/frame/frame_impl.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/frame/page_scale_constraints_set.h"
Expand Down Expand Up @@ -1711,6 +1712,7 @@ WebLocalFrameImpl::WebLocalFrameImpl(
autofill_client_(nullptr),
find_in_page_(
MakeGarbageCollected<FindInPage>(*this, interface_registry)),
frame_impl_(MakeGarbageCollected<FrameImpl>(*this, interface_registry)),
interface_registry_(interface_registry),
input_method_controller_(*this),
spell_check_panel_host_client_(nullptr),
Expand All @@ -1729,6 +1731,7 @@ WebLocalFrameImpl::~WebLocalFrameImpl() {
void WebLocalFrameImpl::Trace(blink::Visitor* visitor) {
visitor->Trace(local_frame_client_);
visitor->Trace(find_in_page_);
visitor->Trace(frame_impl_);
visitor->Trace(frame_);
visitor->Trace(dev_tools_agent_);
visitor->Trace(frame_widget_);
Expand Down
2 changes: 2 additions & 0 deletions third_party/blink/renderer/core/frame/web_local_frame_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class IntSize;
class LocalFrameClientImpl;
class ResourceError;
class ScrollableArea;
class FrameImpl;
class TextFinder;
class WebAssociatedURLLoader;
struct WebAssociatedURLLoaderOptions;
Expand Down Expand Up @@ -509,6 +510,7 @@ class CORE_EXPORT WebLocalFrameImpl final
WebContentSettingsClient* content_settings_client_ = nullptr;

Member<FindInPage> find_in_page_;
Member<FrameImpl> frame_impl_;

// Valid between calls to BeginPrint() and EndPrint(). Containts the print
// information. Is used by PrintPage().
Expand Down

0 comments on commit b81acd7

Please sign in to comment.