Home | History | Annotate | Download | only in supervised_user
      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.
      4 
      5 #include "chrome/browser/supervised_user/supervised_user_resource_throttle.h"
      6 
      7 #include "base/bind.h"
      8 #include "chrome/browser/supervised_user/supervised_user_interstitial.h"
      9 #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h"
     10 #include "chrome/browser/supervised_user/supervised_user_url_filter.h"
     11 #include "content/public/browser/browser_thread.h"
     12 #include "content/public/browser/resource_controller.h"
     13 #include "content/public/browser/resource_request_info.h"
     14 #include "net/url_request/url_request.h"
     15 
     16 using content::BrowserThread;
     17 
     18 SupervisedUserResourceThrottle::SupervisedUserResourceThrottle(
     19     const net::URLRequest* request,
     20     bool is_main_frame,
     21     const SupervisedUserURLFilter* url_filter)
     22     : request_(request),
     23       is_main_frame_(is_main_frame),
     24       url_filter_(url_filter),
     25       weak_ptr_factory_(this) {}
     26 
     27 SupervisedUserResourceThrottle::~SupervisedUserResourceThrottle() {}
     28 
     29 void SupervisedUserResourceThrottle::ShowInterstitialIfNeeded(bool is_redirect,
     30                                                               const GURL& url,
     31                                                               bool* defer) {
     32   // Only treat main frame requests for now (ignoring subresources).
     33   if (!is_main_frame_)
     34     return;
     35 
     36   if (url_filter_->GetFilteringBehaviorForURL(url) !=
     37       SupervisedUserURLFilter::BLOCK) {
     38     return;
     39   }
     40 
     41   *defer = true;
     42   const content::ResourceRequestInfo* info =
     43       content::ResourceRequestInfo::ForRequest(request_);
     44   BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
     45       base::Bind(&SupervisedUserNavigationObserver::OnRequestBlocked,
     46                  info->GetChildID(), info->GetRouteID(), url,
     47                  base::Bind(
     48                      &SupervisedUserResourceThrottle::OnInterstitialResult,
     49                      weak_ptr_factory_.GetWeakPtr())));
     50 }
     51 
     52 void SupervisedUserResourceThrottle::WillStartRequest(bool* defer) {
     53   ShowInterstitialIfNeeded(false, request_->url(), defer);
     54 }
     55 
     56 void SupervisedUserResourceThrottle::WillRedirectRequest(const GURL& new_url,
     57                                                          bool* defer) {
     58   ShowInterstitialIfNeeded(true, new_url, defer);
     59 }
     60 
     61 const char* SupervisedUserResourceThrottle::GetNameForLogging() const {
     62   return "SupervisedUserResourceThrottle";
     63 }
     64 
     65 void SupervisedUserResourceThrottle::OnInterstitialResult(
     66     bool continue_request) {
     67   if (continue_request)
     68     controller()->Resume();
     69   else
     70     controller()->Cancel();
     71 }
     72