1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/supervised_user/navigation_finished_waiter.h"
6 
7 #include "content/public/browser/navigation_handle.h"
8 #include "content/public/browser/render_frame_host.h"
9 #include "content/public/browser/web_contents.h"
10 
NavigationFinishedWaiter(content::WebContents * web_contents,int frame_id,const GURL & url)11 NavigationFinishedWaiter::NavigationFinishedWaiter(
12     content::WebContents* web_contents,
13     int frame_id,
14     const GURL& url)
15     : content::WebContentsObserver(web_contents),
16       frame_id_(frame_id),
17       url_(url) {}
18 
NavigationFinishedWaiter(content::WebContents * web_contents,const GURL & url)19 NavigationFinishedWaiter::NavigationFinishedWaiter(
20     content::WebContents* web_contents,
21     const GURL& url)
22     : content::WebContentsObserver(web_contents) {
23   frame_id_ = web_contents->GetMainFrame()->GetFrameTreeNodeId();
24   url_ = url;
25 }
Wait()26 void NavigationFinishedWaiter::Wait() {
27   if (did_finish_)
28     return;
29   run_loop_.Run();
30 }
31 
DidFinishNavigation(content::NavigationHandle * navigation_handle)32 void NavigationFinishedWaiter::DidFinishNavigation(
33     content::NavigationHandle* navigation_handle) {
34   if (!navigation_handle->HasCommitted())
35     return;
36 
37   if (navigation_handle->GetFrameTreeNodeId() != frame_id_ ||
38       navigation_handle->GetURL() != url_)
39     return;
40 
41   did_finish_ = true;
42   run_loop_.Quit();
43 }
44