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 "chrome/browser/policy/url_blacklist_manager.h" | |
11 #include "content/public/browser/resource_controller.h" | 11 #include "content/public/browser/resource_controller.h" |
12 #include "net/url_request/url_request.h" | 12 #include "net/url_request/url_request.h" |
13 | 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 static base::LazyInstance<PreviewMap> | |
Bernhard Bauer
2012/11/27 18:44:46
"static" isn't necessary if you're in an anonymous
Sergiu
2012/11/28 12:53:25
Done.
| |
22 in_preview_mode = LAZY_INSTANCE_INITIALIZER; | |
Bernhard Bauer
2012/11/27 18:44:46
Maybe name this |g_in_preview_mode|?
Sergiu
2012/11/28 12:53:25
Done.
| |
23 | |
24 } | |
25 | |
14 ManagedModeResourceThrottle::ManagedModeResourceThrottle( | 26 ManagedModeResourceThrottle::ManagedModeResourceThrottle( |
15 const net::URLRequest* request, | 27 const net::URLRequest* request, |
16 int render_process_host_id, | 28 int render_process_host_id, |
17 int render_view_id, | 29 int render_view_id, |
18 bool is_main_frame) | 30 bool is_main_frame) |
19 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 31 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
20 request_(request), | 32 request_(request), |
21 render_process_host_id_(render_process_host_id), | 33 render_process_host_id_(render_process_host_id), |
22 render_view_id_(render_view_id), | 34 render_view_id_(render_view_id), |
23 is_main_frame_(is_main_frame), | 35 is_main_frame_(is_main_frame), |
36 after_interstitial_(false), | |
24 url_filter_(ManagedMode::GetURLFilterForIOThread()) {} | 37 url_filter_(ManagedMode::GetURLFilterForIOThread()) {} |
25 | 38 |
26 ManagedModeResourceThrottle::~ManagedModeResourceThrottle() {} | 39 ManagedModeResourceThrottle::~ManagedModeResourceThrottle() {} |
27 | 40 |
28 void ManagedModeResourceThrottle::WillStartRequest(bool* defer) { | 41 // static |
29 if (url_filter_->GetFilteringBehaviorForURL(request_->url()) != | 42 void ManagedModeResourceThrottle::AddTemporaryException( |
43 int render_process_host_id, | |
44 int render_view_id, | |
45 const std::string& host) { | |
46 in_preview_mode.Get().insert( | |
47 std::make_pair(std::make_pair(render_process_host_id, render_view_id), | |
48 host)); | |
49 } | |
50 | |
51 // static | |
52 void ManagedModeResourceThrottle::RemoveTemporaryException( | |
53 int render_process_host_id, | |
54 int render_view_id) { | |
55 in_preview_mode.Get().erase(std::make_pair(render_process_host_id, | |
56 render_view_id)); | |
57 } | |
58 | |
59 void ManagedModeResourceThrottle::ShowInterstitialIfNeeded(bool is_redirect, | |
60 const GURL& url, | |
61 bool* defer) { | |
62 // Only treat main frame requests for now (without subresources). | |
63 if (!is_main_frame_) | |
64 return; | |
65 | |
66 if (url_filter_->GetFilteringBehaviorForURL(url) != | |
30 ManagedModeURLFilter::BLOCK) | 67 ManagedModeURLFilter::BLOCK) |
31 return; | 68 return; |
32 | 69 |
33 if (is_main_frame_) { | 70 // Do not show interstitial for redirects in preview mode and URLs which have |
34 *defer = true; | 71 // the same hostname as the one on which the user clicked "Preview" on. |
35 ManagedModeInterstitial::ShowInterstitial( | 72 PreviewMap* preview_map = in_preview_mode.Pointer(); |
36 render_process_host_id_, render_view_id_, request_->url(), | 73 if (after_interstitial_) { |
37 base::Bind(&ManagedModeResourceThrottle::OnInterstitialResult, | 74 DCHECK(is_redirect); |
38 weak_ptr_factory_.GetWeakPtr())); | 75 return; |
39 } else { | |
40 // NOTREACHED(); | |
41 // XXX | |
42 } | 76 } |
77 | |
78 PreviewMap::iterator it = preview_map->find( | |
79 std::make_pair(render_process_host_id_, render_view_id_)); | |
80 if (it != preview_map->end() && url.host() == it->second) | |
81 return; | |
82 | |
83 *defer = true; | |
84 ManagedModeInterstitial::ShowInterstitial( | |
85 render_process_host_id_, render_view_id_, url, | |
86 base::Bind(&ManagedModeResourceThrottle::OnInterstitialResult, | |
87 weak_ptr_factory_.GetWeakPtr())); | |
88 } | |
89 | |
90 void ManagedModeResourceThrottle::WillStartRequest(bool* defer) { | |
91 ShowInterstitialIfNeeded(false, request_->url(), defer); | |
92 } | |
93 | |
94 void ManagedModeResourceThrottle::WillRedirectRequest(const GURL& new_url, | |
95 bool* defer) { | |
96 ShowInterstitialIfNeeded(true, new_url, defer); | |
43 } | 97 } |
44 | 98 |
45 void ManagedModeResourceThrottle::OnInterstitialResult(bool continue_request) { | 99 void ManagedModeResourceThrottle::OnInterstitialResult(bool continue_request) { |
46 if (continue_request) { | 100 if (continue_request) { |
101 after_interstitial_ = true; | |
47 controller()->Resume(); | 102 controller()->Resume(); |
48 } else { | 103 } else { |
49 controller()->Cancel(); | 104 controller()->Cancel(); |
50 } | 105 } |
51 } | 106 } |
OLD | NEW |