OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/managed_mode/managed_mode_resource_throttle.h" | 5 #include "chrome/browser/managed_mode/managed_mode_resource_throttle.h" |
6 | 6 |
| 7 #include "base/lazy_instance.h" |
7 #include "chrome/browser/managed_mode/managed_mode.h" | 8 #include "chrome/browser/managed_mode/managed_mode.h" |
8 #include "chrome/browser/managed_mode/managed_mode_interstitial.h" | 9 #include "chrome/browser/managed_mode/managed_mode_interstitial.h" |
9 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" | 10 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" |
10 #include "content/public/browser/resource_controller.h" | 11 #include "content/public/browser/resource_controller.h" |
11 #include "net/url_request/url_request.h" | 12 #include "net/url_request/url_request.h" |
12 | 13 |
| 14 namespace { |
| 15 |
| 16 // This map contains <render_process_host_id_, render_view_id> pairs mapped |
| 17 // to hostnames which identify individual tabs. If a hostname |
| 18 // is present for a specific pair then the user clicked preview, is |
| 19 // navigating around and has not clicked one of the options on the infobar. |
| 20 typedef std::map<std::pair<int, int>, std::string> PreviewMap; |
| 21 base::LazyInstance<PreviewMap> g_in_preview_mode = LAZY_INSTANCE_INITIALIZER; |
| 22 |
| 23 } |
| 24 |
13 ManagedModeResourceThrottle::ManagedModeResourceThrottle( | 25 ManagedModeResourceThrottle::ManagedModeResourceThrottle( |
14 const net::URLRequest* request, | 26 const net::URLRequest* request, |
15 int render_process_host_id, | 27 int render_process_host_id, |
16 int render_view_id, | 28 int render_view_id, |
17 bool is_main_frame) | 29 bool is_main_frame) |
18 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 30 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
19 request_(request), | 31 request_(request), |
20 render_process_host_id_(render_process_host_id), | 32 render_process_host_id_(render_process_host_id), |
21 render_view_id_(render_view_id), | 33 render_view_id_(render_view_id), |
22 is_main_frame_(is_main_frame), | 34 is_main_frame_(is_main_frame), |
| 35 temporarily_allowed_(false), |
23 url_filter_(ManagedMode::GetURLFilterForIOThread()) {} | 36 url_filter_(ManagedMode::GetURLFilterForIOThread()) {} |
24 | 37 |
25 ManagedModeResourceThrottle::~ManagedModeResourceThrottle() {} | 38 ManagedModeResourceThrottle::~ManagedModeResourceThrottle() {} |
26 | 39 |
27 void ManagedModeResourceThrottle::WillStartRequest(bool* defer) { | 40 // static |
28 if (url_filter_->GetFilteringBehaviorForURL(request_->url()) != | 41 void ManagedModeResourceThrottle::AddTemporaryException( |
| 42 int render_process_host_id, |
| 43 int render_view_id, |
| 44 const GURL& url) { |
| 45 g_in_preview_mode.Get()[std::make_pair(render_process_host_id, |
| 46 render_view_id)] = url.host(); |
| 47 } |
| 48 |
| 49 // static |
| 50 void ManagedModeResourceThrottle::RemoveTemporaryException( |
| 51 int render_process_host_id, |
| 52 int render_view_id) { |
| 53 g_in_preview_mode.Get().erase(std::make_pair(render_process_host_id, |
| 54 render_view_id)); |
| 55 } |
| 56 |
| 57 void ManagedModeResourceThrottle::ShowInterstitialIfNeeded(bool is_redirect, |
| 58 const GURL& url, |
| 59 bool* defer) { |
| 60 // Only treat main frame requests for now (ignoring subresources). |
| 61 if (!is_main_frame_) |
| 62 return; |
| 63 |
| 64 if (url_filter_->GetFilteringBehaviorForURL(url) != |
29 ManagedModeURLFilter::BLOCK) { | 65 ManagedModeURLFilter::BLOCK) { |
30 return; | 66 return; |
31 } | 67 } |
32 | 68 |
33 if (is_main_frame_) { | 69 // Do not show interstitial for redirects in preview mode and URLs which have |
34 *defer = true; | 70 // the same hostname as the one on which the user clicked "Preview" on. |
35 ManagedModeInterstitial::ShowInterstitial( | 71 PreviewMap* preview_map = g_in_preview_mode.Pointer(); |
36 render_process_host_id_, render_view_id_, request_->url(), | 72 if (temporarily_allowed_) { |
37 base::Bind(&ManagedModeResourceThrottle::OnInterstitialResult, | 73 DCHECK(is_redirect); |
38 weak_ptr_factory_.GetWeakPtr())); | 74 return; |
39 } | 75 } |
| 76 |
| 77 PreviewMap::iterator it = preview_map->find( |
| 78 std::make_pair(render_process_host_id_, render_view_id_)); |
| 79 if (it != preview_map->end() && url.host() == it->second) |
| 80 return; |
| 81 |
| 82 *defer = true; |
| 83 ManagedModeInterstitial::ShowInterstitial( |
| 84 render_process_host_id_, render_view_id_, url, |
| 85 base::Bind(&ManagedModeResourceThrottle::OnInterstitialResult, |
| 86 weak_ptr_factory_.GetWeakPtr())); |
| 87 } |
| 88 |
| 89 void ManagedModeResourceThrottle::WillStartRequest(bool* defer) { |
| 90 ShowInterstitialIfNeeded(false, request_->url(), defer); |
| 91 } |
| 92 |
| 93 void ManagedModeResourceThrottle::WillRedirectRequest(const GURL& new_url, |
| 94 bool* defer) { |
| 95 ShowInterstitialIfNeeded(true, new_url, defer); |
40 } | 96 } |
41 | 97 |
42 void ManagedModeResourceThrottle::OnInterstitialResult(bool continue_request) { | 98 void ManagedModeResourceThrottle::OnInterstitialResult(bool continue_request) { |
43 if (continue_request) { | 99 if (continue_request) { |
| 100 temporarily_allowed_ = true; |
44 controller()->Resume(); | 101 controller()->Resume(); |
45 } else { | 102 } else { |
46 controller()->Cancel(); | 103 controller()->Cancel(); |
47 } | 104 } |
48 } | 105 } |
OLD | NEW |