1 // Copyright 2014 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.
5 #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h"
8 #include "base/callback.h"
9 #include "chrome/browser/history/history_service_factory.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/supervised_user/supervised_user_interstitial.h"
12 #include "chrome/browser/supervised_user/supervised_user_service.h"
13 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
14 #include "chrome/browser/tab_contents/tab_util.h"
15 #include "components/history/content/browser/history_context_helper.h"
16 #include "components/history/core/browser/history_service.h"
17 #include "components/history/core/browser/history_types.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/navigation_entry.h"
22 using content::NavigationEntry
;
24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SupervisedUserNavigationObserver
);
26 SupervisedUserNavigationObserver::~SupervisedUserNavigationObserver() {
27 supervised_user_service_
->RemoveObserver(this);
30 SupervisedUserNavigationObserver::SupervisedUserNavigationObserver(
31 content::WebContents
* web_contents
)
32 : web_contents_(web_contents
), weak_ptr_factory_(this) {
34 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
35 supervised_user_service_
=
36 SupervisedUserServiceFactory::GetForProfile(profile
);
37 url_filter_
= supervised_user_service_
->GetURLFilterForUIThread();
38 supervised_user_service_
->AddObserver(this);
42 void SupervisedUserNavigationObserver::OnRequestBlocked(
43 int render_process_host_id
,
46 SupervisedUserURLFilter::FilteringBehaviorReason reason
,
47 const base::Callback
<void(bool)>& callback
) {
48 content::WebContents
* web_contents
=
49 tab_util::GetWebContentsByID(render_process_host_id
, render_view_id
);
51 content::BrowserThread::PostTask(
52 content::BrowserThread::IO
, FROM_HERE
, base::Bind(callback
, false));
56 SupervisedUserNavigationObserver
* navigation_observer
=
57 SupervisedUserNavigationObserver::FromWebContents(web_contents
);
58 if (navigation_observer
)
59 navigation_observer
->OnRequestBlockedInternal(url
);
61 // Show the interstitial.
62 SupervisedUserInterstitial::Show(web_contents
, url
, reason
, callback
);
65 void SupervisedUserNavigationObserver::OnRequestBlockedInternal(
67 Time timestamp
= Time::Now(); // TODO(bauerb): Use SaneTime when available.
68 // Create a history entry for the attempt and mark it as such.
69 history::HistoryAddPageArgs
add_page_args(
70 url
, timestamp
, history::ContextIDForWebContents(web_contents_
), 0, url
,
71 history::RedirectList(), ui::PAGE_TRANSITION_BLOCKED
,
72 history::SOURCE_BROWSED
, false);
74 // Add the entry to the history database.
76 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
77 history::HistoryService
* history_service
=
78 HistoryServiceFactory::GetForProfile(profile
,
79 ServiceAccessType::IMPLICIT_ACCESS
);
81 // |history_service| is null if saving history is disabled.
83 history_service
->AddPage(add_page_args
);
85 scoped_ptr
<NavigationEntry
> entry(NavigationEntry::Create());
86 entry
->SetVirtualURL(url
);
87 entry
->SetTimestamp(timestamp
);
88 blocked_navigations_
.push_back(entry
.release());
89 supervised_user_service_
->DidBlockNavigation(web_contents_
);
92 void SupervisedUserNavigationObserver::OnURLFilterChanged() {
93 url_filter_
->GetFilteringBehaviorForURLWithAsyncChecks(
94 web_contents_
->GetLastCommittedURL(),
95 base::Bind(&SupervisedUserNavigationObserver::URLFilterCheckCallback
,
96 weak_ptr_factory_
.GetWeakPtr(),
97 web_contents_
->GetLastCommittedURL()));
100 void SupervisedUserNavigationObserver::URLFilterCheckCallback(
102 SupervisedUserURLFilter::FilteringBehavior behavior
,
103 SupervisedUserURLFilter::FilteringBehaviorReason reason
,
105 // If the page has been changed in the meantime, we can exit.
106 if (url
!= web_contents_
->GetLastCommittedURL())
109 if (behavior
== SupervisedUserURLFilter::FilteringBehavior::BLOCK
) {
110 SupervisedUserInterstitial::Show(web_contents_
, url
, reason
,
111 base::Callback
<void(bool)>());