ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_navigation_observer.cc
blobb88475c4f1aae22263e4cb1837aa6a01fabdd60d
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"
7 #include "base/bind.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"
21 using base::Time;
22 using content::NavigationEntry;
24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SupervisedUserNavigationObserver);
26 SupervisedUserNavigationObserver::~SupervisedUserNavigationObserver() {
29 SupervisedUserNavigationObserver::SupervisedUserNavigationObserver(
30 content::WebContents* web_contents)
31 : web_contents_(web_contents) {
32 Profile* profile =
33 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
34 SupervisedUserService* supervised_user_service =
35 SupervisedUserServiceFactory::GetForProfile(profile);
36 url_filter_ = supervised_user_service->GetURLFilterForUIThread();
39 // static
40 void SupervisedUserNavigationObserver::OnRequestBlocked(
41 int render_process_host_id,
42 int render_view_id,
43 const GURL& url,
44 SupervisedUserURLFilter::FilteringBehaviorReason reason,
45 const base::Callback<void(bool)>& callback) {
46 content::WebContents* web_contents =
47 tab_util::GetWebContentsByID(render_process_host_id, render_view_id);
48 if (!web_contents) {
49 content::BrowserThread::PostTask(
50 content::BrowserThread::IO, FROM_HERE, base::Bind(callback, false));
51 return;
54 SupervisedUserNavigationObserver* navigation_observer =
55 SupervisedUserNavigationObserver::FromWebContents(web_contents);
56 if (navigation_observer)
57 navigation_observer->OnRequestBlockedInternal(url);
59 // Show the interstitial.
60 SupervisedUserInterstitial::Show(web_contents, url, reason, callback);
63 void SupervisedUserNavigationObserver::OnRequestBlockedInternal(
64 const GURL& url) {
65 Time timestamp = Time::Now(); // TODO(bauerb): Use SaneTime when available.
66 // Create a history entry for the attempt and mark it as such.
67 history::HistoryAddPageArgs add_page_args(
68 url, timestamp, history::ContextIDForWebContents(web_contents_), 0, url,
69 history::RedirectList(), ui::PAGE_TRANSITION_BLOCKED,
70 history::SOURCE_BROWSED, false);
72 // Add the entry to the history database.
73 Profile* profile =
74 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
75 history::HistoryService* history_service =
76 HistoryServiceFactory::GetForProfile(profile,
77 ServiceAccessType::IMPLICIT_ACCESS);
79 // |history_service| is null if saving history is disabled.
80 if (history_service)
81 history_service->AddPage(add_page_args);
83 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create());
84 entry->SetVirtualURL(url);
85 entry->SetTimestamp(timestamp);
86 blocked_navigations_.push_back(entry.release());
87 SupervisedUserService* supervised_user_service =
88 SupervisedUserServiceFactory::GetForProfile(profile);
89 supervised_user_service->DidBlockNavigation(web_contents_);