Skip to content

Commit

Permalink
Hide unused public functions of WebFramesManagerImpl
Browse files Browse the repository at this point in the history
Some public functions WebFramesManagerImpl are only used for unittests.
These functions should be converted to private to improve encapsulation
and unittests of WebFramesManagerImpl.

Bug: 956516,956511
Change-Id: I9d5b85f9014ffe7cf60a77331cb308a27fb6b1be
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1718408
Commit-Queue: Yi Su <mrsuyi@chromium.org>
Reviewed-by: Mike Dougherty <michaeldo@chromium.org>
Reviewed-by: Eugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#682633}
  • Loading branch information
Yi Su authored and Commit Bot committed Jul 31, 2019
1 parent fb8afa3 commit 16a4168
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 303 deletions.
33 changes: 18 additions & 15 deletions ios/web/js_messaging/web_frames_manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,38 @@ class WebFramesManagerImpl : public WebFramesManager {
explicit WebFramesManagerImpl(WebFramesManagerDelegate& delegate);
~WebFramesManagerImpl() override;

// Adds |frame| to the list of web frames associated with WebState.
// The frame must not be already in the frame manager (the frame manager must
// not have a frame with the same frame ID). If |frame| is a main frame, the
// frame manager must not have a main frame already.
void AddFrame(std::unique_ptr<WebFrame> frame);
// Removes the web frame with |frame_id|, if one exists, from the list of
// associated web frames.
// If the frame manager does not contain a frame with this ID, operation is a
// no-op.
void RemoveFrameWithId(const std::string& frame_id);
// Removes all web frames from the list of associated web frames.
void RemoveAllWebFrames();

// Broadcasts a (not encrypted) JavaScript message to get the identifiers
// and keys of existing frames.
void RegisterExistingFrames();

// WebFramesManager overrides
std::set<WebFrame*> GetAllWebFrames() override;
WebFrame* GetMainWebFrame() override;
WebFrame* GetFrameWithId(const std::string& frame_id) override;

// Use |message_router| to unregister JS message handlers for |old_web_view|
// and register handlers for |new_web_view|. Owner of this class should call
// this method whenever associated WKWebView changes.
void OnWebViewUpdated(WKWebView* old_web_view,
WKWebView* new_web_view,
CRWWKScriptMessageRouter* message_router);

// WebFramesManager overrides.
std::set<WebFrame*> GetAllWebFrames() override;
WebFrame* GetMainWebFrame() override;
WebFrame* GetFrameWithId(const std::string& frame_id) override;

private:
// Adds |frame| to the list of web frames associated with WebState and invoke
// |delegate_|.OnWebFrameAvailable with |frame|. The frame must not be already
// in the frame manager (the frame manager must not have a frame with the same
// frame ID). If |frame| is a main frame, the frame manager must not have a
// main frame already.
void AddFrame(std::unique_ptr<WebFrame> frame);
// Removes the web frame with |frame_id|, if one exists, from the list of
// associated web frames, and invoke |delegate_|.OnWebFrameUnavailable with
// the web frame. If the frame manager does not contain a frame with
// |frame_id|, operation is a no-op.
void RemoveFrameWithId(const std::string& frame_id);

// Handles FrameBecameAvailable JS message and creates new WebFrame based on
// frame info from the message (e.g. ID, encryption key, message counter,
// etc.).
Expand Down
119 changes: 61 additions & 58 deletions ios/web/js_messaging/web_frames_manager_impl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -33,64 +33,12 @@
RemoveAllWebFrames();
}

void WebFramesManagerImpl::AddFrame(std::unique_ptr<WebFrame> frame) {
DCHECK(frame);
DCHECK(!frame->GetFrameId().empty());
if (frame->IsMainFrame()) {
DCHECK(!main_web_frame_);
main_web_frame_ = frame.get();
}
DCHECK(web_frames_.count(frame->GetFrameId()) == 0);
std::string frame_id = frame->GetFrameId();
web_frames_[frame_id] = std::move(frame);
}

void WebFramesManagerImpl::RemoveFrameWithId(const std::string& frame_id) {
DCHECK(!frame_id.empty());
// If the removed frame is a main frame, it should be the current one.
DCHECK(web_frames_.count(frame_id) == 0 ||
!web_frames_[frame_id]->IsMainFrame() ||
main_web_frame_ == web_frames_[frame_id].get());
if (web_frames_.count(frame_id) == 0) {
return;
}
if (main_web_frame_ && main_web_frame_->GetFrameId() == frame_id) {
main_web_frame_ = nullptr;
}
// The web::WebFrame destructor can call some callbacks that will try to
// access the frame via GetFrameWithId. This can lead to a reentrancy issue
// on |web_frames_|.
// To avoid this issue, keep the frame alive during the map operation and
// destroy it after.
auto keep_frame_alive = std::move(web_frames_[frame_id]);
web_frames_.erase(frame_id);
}

void WebFramesManagerImpl::RemoveAllWebFrames() {
while (web_frames_.size()) {
RemoveFrameWithId(web_frames_.begin()->first);
}
}

WebFrame* WebFramesManagerImpl::GetFrameWithId(const std::string& frame_id) {
DCHECK(!frame_id.empty());
auto web_frames_it = web_frames_.find(frame_id);
return web_frames_it == web_frames_.end() ? nullptr
: web_frames_it->second.get();
}

std::set<WebFrame*> WebFramesManagerImpl::GetAllWebFrames() {
std::set<WebFrame*> frames;
for (const auto& it : web_frames_) {
frames.insert(it.second.get());
}
return frames;
}

WebFrame* WebFramesManagerImpl::GetMainWebFrame() {
return main_web_frame_;
}

void WebFramesManagerImpl::RegisterExistingFrames() {
delegate_.GetWebState()->ExecuteJavaScript(
base::UTF8ToUTF16("__gCrWeb.message.getExistingFrames();"));
Expand All @@ -102,6 +50,7 @@
CRWWKScriptMessageRouter* message_router) {
DCHECK(old_web_view != new_web_view);
if (old_web_view) {
RemoveAllWebFrames();
[message_router
removeScriptMessageHandlerForName:kFrameBecameAvailableMessageName
webView:old_web_view];
Expand Down Expand Up @@ -132,6 +81,65 @@
}
}

#pragma mark - WebFramesManager

std::set<WebFrame*> WebFramesManagerImpl::GetAllWebFrames() {
std::set<WebFrame*> frames;
for (const auto& it : web_frames_) {
frames.insert(it.second.get());
}
return frames;
}

WebFrame* WebFramesManagerImpl::GetMainWebFrame() {
return main_web_frame_;
}

WebFrame* WebFramesManagerImpl::GetFrameWithId(const std::string& frame_id) {
DCHECK(!frame_id.empty());
auto web_frames_it = web_frames_.find(frame_id);
return web_frames_it == web_frames_.end() ? nullptr
: web_frames_it->second.get();
}

#pragma mark - Private

void WebFramesManagerImpl::AddFrame(std::unique_ptr<WebFrame> frame) {
DCHECK(frame);
DCHECK(!frame->GetFrameId().empty());
if (frame->IsMainFrame()) {
DCHECK(!main_web_frame_);
main_web_frame_ = frame.get();
}
DCHECK(web_frames_.count(frame->GetFrameId()) == 0);
std::string frame_id = frame->GetFrameId();
web_frames_[frame_id] = std::move(frame);

delegate_.OnWebFrameAvailable(GetFrameWithId(frame_id));
}

void WebFramesManagerImpl::RemoveFrameWithId(const std::string& frame_id) {
DCHECK(!frame_id.empty());
// If the removed frame is a main frame, it should be the current one.
DCHECK(web_frames_.count(frame_id) == 0 ||
!web_frames_[frame_id]->IsMainFrame() ||
main_web_frame_ == web_frames_[frame_id].get());
if (web_frames_.count(frame_id) == 0) {
return;
}
delegate_.OnWebFrameUnavailable(web_frames_[frame_id].get());
if (main_web_frame_ && main_web_frame_->GetFrameId() == frame_id) {
main_web_frame_ = nullptr;
}
// The web::WebFrame destructor can call some callbacks that will try to
// access the frame via GetFrameWithId. This can lead to a reentrancy issue
// on |web_frames_|.
// To avoid this issue, keep the frame alive during the map operation and
// destroy it after.
auto keep_frame_alive = std::move(web_frames_[frame_id]);
web_frames_.erase(frame_id);
}

void WebFramesManagerImpl::OnFrameBecameAvailable(WKScriptMessage* message) {
DCHECK(!delegate_.GetWebState()->IsBeingDestroyed());
// Validate all expected message components because any frame could falsify
Expand Down Expand Up @@ -172,7 +180,6 @@
}

AddFrame(std::move(new_frame));
delegate_.OnWebFrameAvailable(GetFrameWithId(frame_id));
}
}

Expand All @@ -183,11 +190,7 @@
return;
}
std::string frame_id = base::SysNSStringToUTF8(message.body);
WebFrame* frame = GetFrameWithId(frame_id);
if (frame) {
delegate_.OnWebFrameUnavailable(frame);
RemoveFrameWithId(frame_id);
}
RemoveFrameWithId(frame_id);
}

} // namespace
Loading

0 comments on commit 16a4168

Please sign in to comment.