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 "base/lazy_instance.h" |
8 #include "chrome/browser/managed_mode/managed_mode.h" | 8 #include "chrome/browser/managed_mode/managed_mode.h" |
9 #include "chrome/browser/managed_mode/managed_mode_interstitial.h" | 9 #include "chrome/browser/managed_mode/managed_mode_interstitial.h" |
10 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" | 10 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 bool operator<(const WebContentsId& key) const { | 22 bool operator<(const WebContentsId& key) const { |
23 if (render_process_host_id != key.render_process_host_id) | 23 if (render_process_host_id != key.render_process_host_id) |
24 return render_process_host_id < key.render_process_host_id; | 24 return render_process_host_id < key.render_process_host_id; |
25 return render_view_id < key.render_view_id; | 25 return render_view_id < key.render_view_id; |
26 } | 26 } |
27 | 27 |
28 int render_process_host_id; | 28 int render_process_host_id; |
29 int render_view_id; | 29 int render_view_id; |
30 }; | 30 }; |
31 | 31 |
32 struct TemporaryExceptionData { | |
33 // Hostname for which the temporary exception was added. | |
34 std::string host; | |
35 // Whether the user initiated a new navigation or not. | |
36 bool new_navigation; | |
37 }; | |
38 | |
39 // This map contains <render_process_host_id_, render_view_id> pairs mapped | 32 // This map contains <render_process_host_id_, render_view_id> pairs mapped |
40 // to |TemporaryExceptionData| which identifies individual tabs. If a | 33 // to a host string which identifies individual tabs. If a host is present for |
41 // |TemporaryExceptionData| is present for a specific pair then the user | 34 // a specific pair then the user clicked preview, is navigating around and has |
42 // clicked preview, is navigating around and has not clicked one of the options | 35 // not clicked one of the options on the infobar. |
43 // on the infobar. | 36 typedef std::map<WebContentsId, std::string> PreviewMap; |
44 typedef std::map<WebContentsId, TemporaryExceptionData> PreviewMap; | |
45 base::LazyInstance<PreviewMap> g_in_preview_mode = LAZY_INSTANCE_INITIALIZER; | 37 base::LazyInstance<PreviewMap> g_in_preview_mode = LAZY_INSTANCE_INITIALIZER; |
46 | 38 |
47 } | 39 } |
48 | 40 |
49 ManagedModeResourceThrottle::ManagedModeResourceThrottle( | 41 ManagedModeResourceThrottle::ManagedModeResourceThrottle( |
50 const net::URLRequest* request, | 42 const net::URLRequest* request, |
51 int render_process_host_id, | 43 int render_process_host_id, |
52 int render_view_id, | 44 int render_view_id, |
53 bool is_main_frame, | 45 bool is_main_frame, |
54 const ManagedModeURLFilter* url_filter) | 46 const ManagedModeURLFilter* url_filter) |
55 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 47 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
56 request_(request), | 48 request_(request), |
57 render_process_host_id_(render_process_host_id), | 49 render_process_host_id_(render_process_host_id), |
58 render_view_id_(render_view_id), | 50 render_view_id_(render_view_id), |
59 is_main_frame_(is_main_frame), | 51 is_main_frame_(is_main_frame), |
60 temporarily_allowed_(false), | 52 temporarily_allowed_(false), |
61 url_filter_(url_filter) {} | 53 url_filter_(url_filter) {} |
62 | 54 |
63 ManagedModeResourceThrottle::~ManagedModeResourceThrottle() {} | 55 ManagedModeResourceThrottle::~ManagedModeResourceThrottle() {} |
64 | 56 |
65 // static | 57 // static |
66 void ManagedModeResourceThrottle::AddTemporaryException( | 58 void ManagedModeResourceThrottle::AddTemporaryException( |
67 int render_process_host_id, | 59 int render_process_host_id, |
68 int render_view_id, | 60 int render_view_id, |
69 const GURL& url, | 61 const GURL& url) { |
70 bool new_navigation) { | |
71 TemporaryExceptionData data; | |
72 data.host = url.host(); | |
73 data.new_navigation = new_navigation; | |
74 WebContentsId web_contents_id(render_process_host_id, render_view_id); | 62 WebContentsId web_contents_id(render_process_host_id, render_view_id); |
75 g_in_preview_mode.Get()[web_contents_id] = data; | 63 g_in_preview_mode.Get()[web_contents_id] = url.host(); |
76 } | 64 } |
77 | 65 |
78 // static | 66 // static |
79 void ManagedModeResourceThrottle::UpdateExceptionNavigationStatus( | |
80 int render_process_host_id, | |
81 int render_view_id, | |
82 bool new_navigation) { | |
83 PreviewMap* preview_map = g_in_preview_mode.Pointer(); | |
84 WebContentsId web_contents_id(render_process_host_id, render_view_id); | |
85 PreviewMap::iterator it = preview_map->find(web_contents_id); | |
86 if (it == preview_map->end()) | |
87 return; | |
88 | |
89 it->second.new_navigation = new_navigation; | |
90 } | |
91 | |
92 // static | |
93 void ManagedModeResourceThrottle::RemoveTemporaryException( | 67 void ManagedModeResourceThrottle::RemoveTemporaryException( |
94 int render_process_host_id, | 68 int render_process_host_id, |
95 int render_view_id) { | 69 int render_view_id) { |
96 WebContentsId web_contents_id(render_process_host_id, render_view_id); | 70 WebContentsId web_contents_id(render_process_host_id, render_view_id); |
97 g_in_preview_mode.Get().erase(web_contents_id); | 71 g_in_preview_mode.Get().erase(web_contents_id); |
98 } | 72 } |
99 | 73 |
100 void ManagedModeResourceThrottle::ShowInterstitialIfNeeded(bool is_redirect, | 74 void ManagedModeResourceThrottle::ShowInterstitialIfNeeded(bool is_redirect, |
101 const GURL& url, | 75 const GURL& url, |
102 bool* defer) { | 76 bool* defer) { |
103 // Only treat main frame requests for now (ignoring subresources). | 77 // Only treat main frame requests for now (ignoring subresources). |
104 if (!is_main_frame_) | 78 if (!is_main_frame_) |
105 return; | 79 return; |
106 | 80 |
107 if (url_filter_->GetFilteringBehaviorForURL(url) != | 81 if (url_filter_->GetFilteringBehaviorForURL(url) != |
108 ManagedModeURLFilter::BLOCK) { | 82 ManagedModeURLFilter::BLOCK) { |
109 return; | 83 return; |
110 } | 84 } |
111 | 85 |
112 // Do not show interstitial for redirects in preview mode and URLs which have | 86 // Do not show interstitial for redirects in preview mode and URLs which have |
113 // the same hostname as the one on which the user clicked "Preview" on. | 87 // the same hostname as the one on which the user clicked "Preview" on. |
114 PreviewMap* preview_map = g_in_preview_mode.Pointer(); | 88 PreviewMap* preview_map = g_in_preview_mode.Pointer(); |
115 if (temporarily_allowed_) { | 89 if (temporarily_allowed_) { |
116 DCHECK(is_redirect); | 90 DCHECK(is_redirect); |
117 return; | 91 return; |
118 } | 92 } |
119 | 93 |
120 WebContentsId web_contents_id(render_process_host_id_, render_view_id_); | 94 WebContentsId web_contents_id(render_process_host_id_, render_view_id_); |
121 PreviewMap::iterator it = preview_map->find(web_contents_id); | 95 PreviewMap::iterator it = preview_map->find(web_contents_id); |
122 if (it != preview_map->end() && | 96 if (it != preview_map->end() && url.host() == it->second) { |
123 (!it->second.new_navigation || url.host() == it->second.host)) { | |
124 temporarily_allowed_ = true; | 97 temporarily_allowed_ = true; |
125 RemoveTemporaryException(render_process_host_id_, render_view_id_); | 98 RemoveTemporaryException(render_process_host_id_, render_view_id_); |
126 return; | 99 return; |
127 } | 100 } |
128 | 101 |
129 *defer = true; | 102 *defer = true; |
130 ManagedModeInterstitial::ShowInterstitial( | 103 ManagedModeInterstitial::ShowInterstitial( |
131 render_process_host_id_, render_view_id_, url, | 104 render_process_host_id_, render_view_id_, url, |
132 base::Bind(&ManagedModeResourceThrottle::OnInterstitialResult, | 105 base::Bind(&ManagedModeResourceThrottle::OnInterstitialResult, |
133 weak_ptr_factory_.GetWeakPtr())); | 106 weak_ptr_factory_.GetWeakPtr())); |
134 } | 107 } |
135 | 108 |
136 void ManagedModeResourceThrottle::WillStartRequest(bool* defer) { | 109 void ManagedModeResourceThrottle::WillStartRequest(bool* defer) { |
137 ShowInterstitialIfNeeded(false, request_->url(), defer); | 110 ShowInterstitialIfNeeded(false, request_->url(), defer); |
138 } | 111 } |
139 | 112 |
140 void ManagedModeResourceThrottle::WillRedirectRequest(const GURL& new_url, | 113 void ManagedModeResourceThrottle::WillRedirectRequest(const GURL& new_url, |
141 bool* defer) { | 114 bool* defer) { |
142 ShowInterstitialIfNeeded(true, new_url, defer); | 115 ShowInterstitialIfNeeded(true, new_url, defer); |
143 } | 116 } |
144 | 117 |
145 void ManagedModeResourceThrottle::OnInterstitialResult(bool continue_request) { | 118 void ManagedModeResourceThrottle::OnInterstitialResult(bool continue_request) { |
146 if (continue_request) { | 119 if (continue_request) { |
147 temporarily_allowed_ = true; | 120 temporarily_allowed_ = true; |
148 controller()->Resume(); | 121 controller()->Resume(); |
149 } else { | 122 } else { |
150 controller()->Cancel(); | 123 controller()->Cancel(); |
151 } | 124 } |
152 } | 125 } |
OLD | NEW |