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" |
16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
17 #include "chrome/grit/generated_resources.h" | 18 #include "chrome/grit/generated_resources.h" |
18 #include "components/infobars/core/infobar.h" | 19 #include "components/infobars/core/infobar.h" |
19 #include "components/infobars/core/infobar_delegate.h" | 20 #include "components/infobars/core/infobar_delegate.h" |
20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/interstitial_page.h" | 22 #include "content/public/browser/interstitial_page.h" |
22 #include "content/public/browser/navigation_controller.h" | 23 #include "content/public/browser/navigation_controller.h" |
23 #include "content/public/browser/navigation_details.h" | 24 #include "content/public/browser/navigation_details.h" |
24 #include "content/public/browser/navigation_entry.h" | 25 #include "content/public/browser/navigation_entry.h" |
25 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 27 #include "content/public/browser/web_contents_user_data.h" |
26 #include "content/public/browser/web_ui.h" | 28 #include "content/public/browser/web_ui.h" |
27 #include "grit/browser_resources.h" | 29 #include "grit/browser_resources.h" |
28 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
29 #include "ui/base/resource/resource_bundle.h" | 31 #include "ui/base/resource/resource_bundle.h" |
30 #include "ui/base/webui/jstemplate_builder.h" | 32 #include "ui/base/webui/jstemplate_builder.h" |
31 #include "ui/base/webui/web_ui_util.h" | 33 #include "ui/base/webui/web_ui_util.h" |
32 | 34 |
| 35 #if !defined(OS_ANDROID) |
| 36 #include "chrome/browser/ui/browser_finder.h" |
| 37 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 38 #endif |
| 39 |
33 using content::BrowserThread; | 40 using content::BrowserThread; |
| 41 using content::WebContents; |
34 | 42 |
35 namespace { | 43 namespace { |
36 | 44 |
37 static const int kAvatarSize1x = 45; | 45 static const int kAvatarSize1x = 45; |
38 static const int kAvatarSize2x = 90; | 46 static const int kAvatarSize2x = 90; |
39 | 47 |
40 std::string BuildAvatarImageUrl(const std::string& url, int size) { | 48 std::string BuildAvatarImageUrl(const std::string& url, int size) { |
41 std::string result = url; | 49 std::string result = url; |
42 size_t slash = result.rfind('/'); | 50 size_t slash = result.rfind('/'); |
43 if (slash != std::string::npos) | 51 if (slash != std::string::npos) |
44 result.insert(slash, "/s" + base::IntToString(size)); | 52 result.insert(slash, "/s" + base::IntToString(size)); |
45 return result; | 53 return result; |
46 } | 54 } |
47 | 55 |
48 } // namespace | 56 class TabCloser : public content::WebContentsUserData<TabCloser> { |
| 57 // To use, call TabCloser::CreateForWebContents. |
| 58 private: |
| 59 friend class content::WebContentsUserData<TabCloser>; |
| 60 |
| 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 // On Android, FindBrowserWithWebContents and TabStripModel don't exist. |
| 72 #if !defined(OS_ANDROID) |
| 73 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
| 74 DCHECK(browser); |
| 75 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 76 DCHECK_NE(TabStripModel::kNoTab, |
| 77 tab_strip->GetIndexOfWebContents(web_contents_)); |
| 78 if (tab_strip->count() <= 1) { |
| 79 // Don't close the last tab in the window. |
| 80 web_contents_->RemoveUserData(UserDataKey()); |
| 81 return; |
| 82 } |
| 83 #endif |
| 84 web_contents_->Close(); |
| 85 } |
| 86 |
| 87 WebContents* web_contents_; |
| 88 base::WeakPtrFactory<TabCloser> weak_ptr_factory_; |
| 89 }; |
| 90 |
| 91 } // namespace |
| 92 |
| 93 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabCloser); |
49 | 94 |
50 // static | 95 // static |
51 void SupervisedUserInterstitial::Show( | 96 void SupervisedUserInterstitial::Show( |
52 content::WebContents* web_contents, | 97 WebContents* web_contents, |
53 const GURL& url, | 98 const GURL& url, |
54 const base::Callback<void(bool)>& callback) { | 99 const base::Callback<void(bool)>& callback) { |
55 SupervisedUserInterstitial* interstitial = | 100 SupervisedUserInterstitial* interstitial = |
56 new SupervisedUserInterstitial(web_contents, url, callback); | 101 new SupervisedUserInterstitial(web_contents, url, callback); |
57 | 102 |
58 // If Init() does not complete fully, immediately delete the interstitial. | 103 // If Init() does not complete fully, immediately delete the interstitial. |
59 if (!interstitial->Init()) | 104 if (!interstitial->Init()) |
60 delete interstitial; | 105 delete interstitial; |
61 // Otherwise |interstitial_page_| is responsible for deleting it. | 106 // Otherwise |interstitial_page_| is responsible for deleting it. |
62 } | 107 } |
63 | 108 |
64 SupervisedUserInterstitial::SupervisedUserInterstitial( | 109 SupervisedUserInterstitial::SupervisedUserInterstitial( |
65 content::WebContents* web_contents, | 110 WebContents* web_contents, |
66 const GURL& url, | 111 const GURL& url, |
67 const base::Callback<void(bool)>& callback) | 112 const base::Callback<void(bool)>& callback) |
68 : web_contents_(web_contents), | 113 : web_contents_(web_contents), |
69 interstitial_page_(NULL), | 114 interstitial_page_(NULL), |
70 url_(url), | 115 url_(url), |
71 callback_(callback) {} | 116 callback_(callback) {} |
72 | 117 |
73 SupervisedUserInterstitial::~SupervisedUserInterstitial() {} | 118 SupervisedUserInterstitial::~SupervisedUserInterstitial() {} |
74 | 119 |
75 bool SupervisedUserInterstitial::Init() { | 120 bool SupervisedUserInterstitial::Init() { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 BACK, | 241 BACK, |
197 NTP, | 242 NTP, |
198 ACCESS_REQUEST, | 243 ACCESS_REQUEST, |
199 HISTOGRAM_BOUNDING_VALUE | 244 HISTOGRAM_BOUNDING_VALUE |
200 }; | 245 }; |
201 | 246 |
202 if (command == "\"back\"") { | 247 if (command == "\"back\"") { |
203 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", | 248 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
204 BACK, | 249 BACK, |
205 HISTOGRAM_BOUNDING_VALUE); | 250 HISTOGRAM_BOUNDING_VALUE); |
| 251 |
| 252 // Close the tab if there is no history entry to go back to. |
| 253 DCHECK(web_contents_->GetController().GetTransientEntry()); |
| 254 if (web_contents_->GetController().GetEntryCount() == 1) |
| 255 TabCloser::CreateForWebContents(web_contents_); |
| 256 |
206 interstitial_page_->DontProceed(); | 257 interstitial_page_->DontProceed(); |
207 return; | 258 return; |
208 } | 259 } |
209 | 260 |
210 if (command == "\"request\"") { | 261 if (command == "\"request\"") { |
211 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", | 262 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
212 ACCESS_REQUEST, | 263 ACCESS_REQUEST, |
213 HISTOGRAM_BOUNDING_VALUE); | 264 HISTOGRAM_BOUNDING_VALUE); |
214 | 265 |
215 Profile* profile = | 266 Profile* profile = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 SupervisedUserURLFilter::BLOCK; | 298 SupervisedUserURLFilter::BLOCK; |
248 } | 299 } |
249 | 300 |
250 void SupervisedUserInterstitial::OnFilteringPrefsChanged() { | 301 void SupervisedUserInterstitial::OnFilteringPrefsChanged() { |
251 if (ShouldProceed()) | 302 if (ShouldProceed()) |
252 interstitial_page_->Proceed(); | 303 interstitial_page_->Proceed(); |
253 } | 304 } |
254 | 305 |
255 void SupervisedUserInterstitial::DispatchContinueRequest( | 306 void SupervisedUserInterstitial::DispatchContinueRequest( |
256 bool continue_request) { | 307 bool continue_request) { |
257 // If there is no history entry to go back to, close the tab instead. | |
258 int nav_entry_count = web_contents_->GetController().GetEntryCount(); | |
259 if (!continue_request && nav_entry_count == 0) | |
260 web_contents_->Close(); | |
261 | |
262 BrowserThread::PostTask( | 308 BrowserThread::PostTask( |
263 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); | 309 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); |
264 } | 310 } |
OLD | NEW |