OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/supervised_user/supervised_user_interstitial.h" | 5 #include "chrome/browser/supervised_user/supervised_user_interstitial.h" |
6 | 6 |
7 #include "base/memory/weak_ptr.h" | |
7 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
8 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
11 #include "base/values.h" | 12 #include "base/values.h" |
12 #include "chrome/browser/infobars/infobar_service.h" | 13 #include "chrome/browser/infobars/infobar_service.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/supervised_user/supervised_user_service.h" | 15 #include "chrome/browser/supervised_user/supervised_user_service.h" |
15 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | 16 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
17 #include "chrome/browser/ui/browser_finder.h" | |
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
16 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
17 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
18 #include "components/infobars/core/infobar.h" | 21 #include "components/infobars/core/infobar.h" |
19 #include "components/infobars/core/infobar_delegate.h" | 22 #include "components/infobars/core/infobar_delegate.h" |
20 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/interstitial_page.h" | 24 #include "content/public/browser/interstitial_page.h" |
22 #include "content/public/browser/navigation_controller.h" | 25 #include "content/public/browser/navigation_controller.h" |
23 #include "content/public/browser/navigation_details.h" | 26 #include "content/public/browser/navigation_details.h" |
24 #include "content/public/browser/navigation_entry.h" | 27 #include "content/public/browser/navigation_entry.h" |
25 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
29 #include "content/public/browser/web_contents_user_data.h" | |
26 #include "content/public/browser/web_ui.h" | 30 #include "content/public/browser/web_ui.h" |
27 #include "grit/browser_resources.h" | 31 #include "grit/browser_resources.h" |
28 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
29 #include "ui/base/resource/resource_bundle.h" | 33 #include "ui/base/resource/resource_bundle.h" |
30 #include "ui/base/webui/jstemplate_builder.h" | 34 #include "ui/base/webui/jstemplate_builder.h" |
31 #include "ui/base/webui/web_ui_util.h" | 35 #include "ui/base/webui/web_ui_util.h" |
32 | 36 |
33 using content::BrowserThread; | 37 using content::BrowserThread; |
38 using content::WebContents; | |
34 | 39 |
35 namespace { | 40 namespace { |
36 | 41 |
37 static const int kAvatarSize1x = 45; | 42 static const int kAvatarSize1x = 45; |
38 static const int kAvatarSize2x = 90; | 43 static const int kAvatarSize2x = 90; |
39 | 44 |
40 std::string BuildAvatarImageUrl(const std::string& url, int size) { | 45 std::string BuildAvatarImageUrl(const std::string& url, int size) { |
41 std::string result = url; | 46 std::string result = url; |
42 size_t slash = result.rfind('/'); | 47 size_t slash = result.rfind('/'); |
43 if (slash != std::string::npos) | 48 if (slash != std::string::npos) |
44 result.insert(slash, "/s" + base::IntToString(size)); | 49 result.insert(slash, "/s" + base::IntToString(size)); |
45 return result; | 50 return result; |
46 } | 51 } |
47 | 52 |
48 } // namespace | 53 class TabCloser : public content::WebContentsUserData<TabCloser> { |
54 public: | |
55 static void CloseTab(WebContents* web_contents) { | |
56 CreateForWebContents(web_contents); | |
57 } | |
58 | |
59 private: | |
60 friend class content::WebContentsUserData<TabCloser>; | |
61 explicit TabCloser(WebContents* web_contents) | |
62 : web_contents_(web_contents), weak_ptr_factory_(this) { | |
63 BrowserThread::PostTask( | |
64 BrowserThread::UI, | |
65 FROM_HERE, | |
66 base::Bind(&TabCloser::CloseTabImpl, weak_ptr_factory_.GetWeakPtr())); | |
67 } | |
68 virtual ~TabCloser() {} | |
69 | |
70 void CloseTabImpl() { | |
71 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); | |
72 DCHECK(browser); | |
73 TabStripModel* tab_strip = browser->tab_strip_model(); | |
74 // Don't close the last tab in the window. | |
75 if (tab_strip->count() > 1) { | |
76 int index = tab_strip->GetIndexOfWebContents(web_contents_); | |
77 DCHECK_NE(TabStripModel::kNoTab, index); | |
78 tab_strip->CloseWebContentsAt( | |
79 index, TabStripModel::CLOSE_CREATE_HISTORICAL_TAB); | |
80 } else { | |
81 web_contents_->RemoveUserData(UserDataKey()); | |
82 } | |
83 } | |
84 | |
85 WebContents* web_contents_; | |
86 base::WeakPtrFactory<TabCloser> weak_ptr_factory_; | |
87 }; | |
88 | |
89 } // namespace | |
90 | |
91 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabCloser); | |
49 | 92 |
50 // static | 93 // static |
51 void SupervisedUserInterstitial::Show( | 94 void SupervisedUserInterstitial::Show( |
52 content::WebContents* web_contents, | 95 WebContents* web_contents, |
53 const GURL& url, | 96 const GURL& url, |
54 const base::Callback<void(bool)>& callback) { | 97 const base::Callback<void(bool)>& callback) { |
55 SupervisedUserInterstitial* interstitial = | 98 SupervisedUserInterstitial* interstitial = |
56 new SupervisedUserInterstitial(web_contents, url, callback); | 99 new SupervisedUserInterstitial(web_contents, url, callback); |
57 | 100 |
58 // If Init() does not complete fully, immediately delete the interstitial. | 101 // If Init() does not complete fully, immediately delete the interstitial. |
59 if (!interstitial->Init()) | 102 if (!interstitial->Init()) |
60 delete interstitial; | 103 delete interstitial; |
61 // Otherwise |interstitial_page_| is responsible for deleting it. | 104 // Otherwise |interstitial_page_| is responsible for deleting it. |
62 } | 105 } |
63 | 106 |
64 SupervisedUserInterstitial::SupervisedUserInterstitial( | 107 SupervisedUserInterstitial::SupervisedUserInterstitial( |
65 content::WebContents* web_contents, | 108 WebContents* web_contents, |
66 const GURL& url, | 109 const GURL& url, |
67 const base::Callback<void(bool)>& callback) | 110 const base::Callback<void(bool)>& callback) |
68 : web_contents_(web_contents), | 111 : web_contents_(web_contents), |
69 interstitial_page_(NULL), | 112 interstitial_page_(NULL), |
70 url_(url), | 113 url_(url), |
71 callback_(callback) {} | 114 callback_(callback), |
115 back_button_pressed_(false) {} | |
72 | 116 |
73 SupervisedUserInterstitial::~SupervisedUserInterstitial() {} | 117 SupervisedUserInterstitial::~SupervisedUserInterstitial() {} |
74 | 118 |
75 bool SupervisedUserInterstitial::Init() { | 119 bool SupervisedUserInterstitial::Init() { |
76 if (ShouldProceed()) { | 120 if (ShouldProceed()) { |
77 // It can happen that the site was only allowed very recently and the URL | 121 // It can happen that the site was only allowed very recently and the URL |
78 // filter on the IO thread had not been updated yet. Proceed with the | 122 // filter on the IO thread had not been updated yet. Proceed with the |
79 // request without showing the interstitial. | 123 // request without showing the interstitial. |
80 DispatchContinueRequest(true); | 124 DispatchContinueRequest(true); |
81 return false; | 125 return false; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
196 BACK, | 240 BACK, |
197 NTP, | 241 NTP, |
198 ACCESS_REQUEST, | 242 ACCESS_REQUEST, |
199 HISTOGRAM_BOUNDING_VALUE | 243 HISTOGRAM_BOUNDING_VALUE |
200 }; | 244 }; |
201 | 245 |
202 if (command == "\"back\"") { | 246 if (command == "\"back\"") { |
203 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", | 247 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
204 BACK, | 248 BACK, |
205 HISTOGRAM_BOUNDING_VALUE); | 249 HISTOGRAM_BOUNDING_VALUE); |
250 back_button_pressed_ = true; | |
206 interstitial_page_->DontProceed(); | 251 interstitial_page_->DontProceed(); |
207 return; | 252 return; |
208 } | 253 } |
209 | 254 |
210 if (command == "\"request\"") { | 255 if (command == "\"request\"") { |
211 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", | 256 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
212 ACCESS_REQUEST, | 257 ACCESS_REQUEST, |
213 HISTOGRAM_BOUNDING_VALUE); | 258 HISTOGRAM_BOUNDING_VALUE); |
214 | 259 |
215 Profile* profile = | 260 Profile* profile = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
247 SupervisedUserURLFilter::BLOCK; | 292 SupervisedUserURLFilter::BLOCK; |
248 } | 293 } |
249 | 294 |
250 void SupervisedUserInterstitial::OnFilteringPrefsChanged() { | 295 void SupervisedUserInterstitial::OnFilteringPrefsChanged() { |
251 if (ShouldProceed()) | 296 if (ShouldProceed()) |
252 interstitial_page_->Proceed(); | 297 interstitial_page_->Proceed(); |
253 } | 298 } |
254 | 299 |
255 void SupervisedUserInterstitial::DispatchContinueRequest( | 300 void SupervisedUserInterstitial::DispatchContinueRequest( |
256 bool continue_request) { | 301 bool continue_request) { |
257 // If there is no history entry to go back to, close the tab instead. | 302 // On "go back", close the tab if there is no history entry to go back to. |
258 int nav_entry_count = web_contents_->GetController().GetEntryCount(); | 303 int nav_entry_count = web_contents_->GetController().GetEntryCount(); |
259 if (!continue_request && nav_entry_count == 0) | 304 if (back_button_pressed_ && !continue_request && nav_entry_count == 0) |
Bernhard Bauer
2014/09/09 11:32:31
Just call this directly from CommandReceived(). If
Marc Treib
2014/09/09 11:51:25
Right, that makes more sense now.
Small wrinkle: A
| |
260 web_contents_->Close(); | 305 TabCloser::CloseTab(web_contents_); |
261 | 306 |
262 BrowserThread::PostTask( | 307 BrowserThread::PostTask( |
263 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); | 308 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); |
264 } | 309 } |
OLD | NEW |