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" |
10 #include "chrome/browser/managed_mode/managed_mode_navigation_observer.h" | |
Bernhard Bauer
2012/11/26 16:06:30
We don't need this, I think.
Sergiu
2012/11/27 15:37:34
Done.
| |
9 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" | 11 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" |
10 #include "chrome/browser/policy/url_blacklist_manager.h" | 12 #include "chrome/browser/tab_contents/tab_util.h" |
Bernhard Bauer
2012/11/26 16:06:30
Why do we need this?
Sergiu
2012/11/27 15:37:34
I needed it, then deleted the call apparently. :(
| |
11 #include "content/public/browser/resource_controller.h" | 13 #include "content/public/browser/resource_controller.h" |
12 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
13 | 15 |
16 namespace { | |
17 | |
18 // This map contains hostnames mapped to <render_process_host_id_, | |
19 // render_view_id> pairs which identify individual tabs. If a hostname | |
Bernhard Bauer
2012/11/26 16:06:30
The other way around.
Sergiu
2012/11/27 15:37:34
Keys mapped to values, makes sense.
| |
20 // is present for a specific pair then the user clicked preview, is | |
21 // navigating around and has not clicked one of the options on the infobar. | |
22 typedef std::map<std::pair<int, int>, std::string > PreviewMap; | |
Bernhard Bauer
2012/11/26 16:06:30
Nit: no space before >
Sergiu
2012/11/27 15:37:34
Done.
| |
23 static base::LazyInstance<PreviewMap> | |
24 in_preview_mode = LAZY_INSTANCE_INITIALIZER; | |
25 | |
26 } | |
27 | |
14 ManagedModeResourceThrottle::ManagedModeResourceThrottle( | 28 ManagedModeResourceThrottle::ManagedModeResourceThrottle( |
15 const net::URLRequest* request, | 29 const net::URLRequest* request, |
16 int render_process_host_id, | 30 int render_process_host_id, |
17 int render_view_id, | 31 int render_view_id, |
18 bool is_main_frame) | 32 bool is_main_frame) |
19 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 33 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
20 request_(request), | 34 request_(request), |
21 render_process_host_id_(render_process_host_id), | 35 render_process_host_id_(render_process_host_id), |
22 render_view_id_(render_view_id), | 36 render_view_id_(render_view_id), |
23 is_main_frame_(is_main_frame), | 37 is_main_frame_(is_main_frame), |
38 is_in_preview_mode_(false), | |
24 url_filter_(ManagedMode::GetURLFilterForIOThread()) {} | 39 url_filter_(ManagedMode::GetURLFilterForIOThread()) {} |
25 | 40 |
26 ManagedModeResourceThrottle::~ManagedModeResourceThrottle() {} | 41 ManagedModeResourceThrottle::~ManagedModeResourceThrottle() {} |
27 | 42 |
28 void ManagedModeResourceThrottle::WillStartRequest(bool* defer) { | 43 // static |
29 if (url_filter_->GetFilteringBehaviorForURL(request_->url()) != | 44 void ManagedModeResourceThrottle::AddObserver(int render_process_host_id, |
45 int render_view_id, | |
46 const std::string& host) { | |
47 in_preview_mode.Get().insert( | |
48 std::make_pair(std::make_pair(render_process_host_id, render_view_id), | |
49 host)); | |
50 } | |
51 | |
52 // static | |
53 void ManagedModeResourceThrottle::RemoveObserver(int render_process_host_id, | |
54 int render_view_id) { | |
55 if (in_preview_mode.Pointer()) { | |
Bernhard Bauer
2012/11/26 16:06:30
Why this check?
Sergiu
2012/11/27 15:37:34
I was under the impression that in_preview_mode ca
| |
56 in_preview_mode.Get().erase(std::make_pair(render_process_host_id, | |
57 render_view_id)); | |
58 } | |
59 } | |
60 | |
61 void ManagedModeResourceThrottle::CheckNeedToShowInterstitial(bool is_redirect, | |
62 const GURL& url, | |
63 bool* defer) { | |
64 // Only treat main frame requests for now (without subresources). | |
65 if (!is_main_frame_) | |
66 return; | |
67 | |
68 if (url_filter_->GetFilteringBehaviorForURL(url) != | |
30 ManagedModeURLFilter::BLOCK) | 69 ManagedModeURLFilter::BLOCK) |
31 return; | 70 return; |
32 | 71 |
33 if (is_main_frame_) { | 72 // Do not show interstitial for redirects in preview mode and URLs which have |
34 *defer = true; | 73 // the same hostname as the one on which the user clicked "Preview" on. |
35 ManagedModeInterstitial::ShowInterstitial( | 74 PreviewMap::iterator it = in_preview_mode.Get().find( |
Bernhard Bauer
2012/11/26 16:06:30
You could probably save the map pointer you get he
Sergiu
2012/11/27 15:37:34
Done.
| |
36 render_process_host_id_, render_view_id_, request_->url(), | 75 std::make_pair(render_process_host_id_, render_view_id_)); |
37 base::Bind(&ManagedModeResourceThrottle::OnInterstitialResult, | 76 if ((is_redirect && is_in_preview_mode_) || |
Bernhard Bauer
2012/11/26 16:06:30
Hm. If |is_in_preview_mode_| is true, we should se
Sergiu
2012/11/27 15:37:34
Done.
| |
38 weak_ptr_factory_.GetWeakPtr())); | 77 (it != in_preview_mode.Get().end() && url.host() == it->second)) |
Bernhard Bauer
2012/11/26 16:06:30
You could split these up into separate if-statemen
Sergiu
2012/11/27 15:37:34
Done.
| |
39 } else { | 78 return; |
40 // NOTREACHED(); | 79 |
41 // XXX | 80 *defer = true; |
42 } | 81 DLOG(ERROR) << "Showing interstitial"; |
82 ManagedModeInterstitial::ShowInterstitial( | |
83 render_process_host_id_, render_view_id_, url, | |
84 base::Bind(&ManagedModeResourceThrottle::OnInterstitialResult, | |
85 weak_ptr_factory_.GetWeakPtr())); | |
86 } | |
87 | |
88 void ManagedModeResourceThrottle::WillStartRequest(bool* defer) { | |
89 CheckNeedToShowInterstitial(false, request_->url(), defer); | |
90 } | |
91 | |
92 void ManagedModeResourceThrottle::WillRedirectRequest(const GURL& new_url, | |
93 bool* defer) { | |
94 CheckNeedToShowInterstitial(true, new_url, defer); | |
43 } | 95 } |
44 | 96 |
45 void ManagedModeResourceThrottle::OnInterstitialResult(bool continue_request) { | 97 void ManagedModeResourceThrottle::OnInterstitialResult(bool continue_request) { |
46 if (continue_request) { | 98 if (continue_request) { |
99 is_in_preview_mode_ = true; | |
47 controller()->Resume(); | 100 controller()->Resume(); |
48 } else { | 101 } else { |
49 controller()->Cancel(); | 102 controller()->Cancel(); |
50 } | 103 } |
51 } | 104 } |
OLD | NEW |