Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Unified Diff: chrome/browser/managed_mode/managed_mode_navigation_observer.cc

Issue 11299035: Support manual (white|black)list, previewing and allowing after interstitial (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changes according to comments. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/managed_mode/managed_mode_navigation_observer.cc
diff --git a/chrome/browser/managed_mode/managed_mode_navigation_observer.cc b/chrome/browser/managed_mode/managed_mode_navigation_observer.cc
index 4abf30f2afe845a4e4205ab37a4c1acd28551dad..f37a1aca9a2fc464aaa2258ebc6a97983087695b 100644
--- a/chrome/browser/managed_mode/managed_mode_navigation_observer.cc
+++ b/chrome/browser/managed_mode/managed_mode_navigation_observer.cc
@@ -11,6 +11,7 @@
#include "chrome/browser/infobars/infobar_tab_helper.h"
#include "chrome/browser/managed_mode/managed_mode.h"
#include "chrome/browser/managed_mode/managed_mode_interstitial.h"
+#include "chrome/browser/managed_mode/managed_mode_resource_throttle.h"
#include "chrome/browser/managed_mode/managed_mode_url_filter.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
@@ -18,17 +19,18 @@
#include "chrome/common/jstemplate_builder.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/render_process_host.h"
+#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/frame_navigate_params.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "ui/base/l10n/l10n_util.h"
-namespace {
+using content::BrowserThread;
-bool IsInList(const ListValue *list, const std::string& url_to_add) {
- return list->Find(*Value::CreateStringValue(url_to_add)) != list->end();
-}
+namespace {
class ManagedModeWarningInfobarDelegate : public ConfirmInfoBarDelegate {
public:
@@ -157,22 +159,26 @@ string16 ManagedModePreviewInfobarDelegate::GetButtonLabel(
InfoBarButton button) const {
return l10n_util::GetStringUTF16(
(button == BUTTON_OK) ? IDS_MANAGED_MODE_PREVIEW_ACCEPT
- : IDS_MANAGED_MODE_PREVIEW_CANCEL);
+ : IDS_MANAGED_MODE_GO_BACK_ACTION);
}
bool ManagedModePreviewInfobarDelegate::Accept() {
ManagedModeNavigationObserver* observer =
ManagedModeNavigationObserver::FromWebContents(
owner()->GetWebContents());
- observer->AddURLList();
- // Clear the pointer as the infobar was closed.
- observer->PreviewInfobarDismissed();
+ observer->AddSavedURLsToWhitelist();
+ // Notify the navigation observer that the infobar was dismissed.
+ observer->ClearObserverState();
return true;
}
bool ManagedModePreviewInfobarDelegate::Cancel() {
// TODO(bauerb): Go back to the last page.
+ ManagedModeNavigationObserver* observer =
+ ManagedModeNavigationObserver::FromWebContents(
+ owner()->GetWebContents());
+ observer->ClearObserverState();
return false;
}
@@ -193,17 +199,45 @@ void ManagedModePreviewInfobarDelegate::InfoBarDismissed() {
DEFINE_WEB_CONTENTS_USER_DATA_KEY(ManagedModeNavigationObserver)
-ManagedModeNavigationObserver::~ManagedModeNavigationObserver() {}
+ManagedModeNavigationObserver::~ManagedModeNavigationObserver() {
+ RemoveTemporaryException();
+}
ManagedModeNavigationObserver::ManagedModeNavigationObserver(
content::WebContents* web_contents)
: WebContentsObserver(web_contents),
url_filter_(ManagedMode::GetURLFilterForUIThread()),
+ state_(RECORDING_URLS_BEFORE_PREVIEW),
Bernhard Bauer 2012/11/27 18:44:46 Ohhhh! I was assuming that "not recording" is the
Sergiu 2012/11/28 12:53:25 By default we record all the URLs that are blackli
warn_infobar_delegate_(NULL),
preview_infobar_delegate_(NULL),
- after_interstitial_(false),
last_allowed_page_(-1) {}
+void ManagedModeNavigationObserver::AddTemporaryException() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ DCHECK(web_contents());
+
+ BrowserThread::PostTask(
+ BrowserThread::IO,
+ FROM_HERE,
+ base::Bind(&ManagedModeResourceThrottle::AddTemporaryException,
+ web_contents()->GetRenderProcessHost()->GetID(),
+ web_contents()->GetRenderViewHost()->GetRoutingID(),
+ navigated_urls_.back().host()));
+}
+
+void ManagedModeNavigationObserver::RemoveTemporaryException() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ if (!web_contents())
Bernhard Bauer 2012/11/27 18:44:46 Can you add a comment to explain when web_contents
Sergiu 2012/11/28 12:53:25 Done.
+ return;
+
+ BrowserThread::PostTask(
+ BrowserThread::IO,
+ FROM_HERE,
+ base::Bind(&ManagedModeResourceThrottle::RemoveTemporaryException,
+ web_contents()->GetRenderProcessHost()->GetID(),
+ web_contents()->GetRenderViewHost()->GetRoutingID()));
+}
+
void ManagedModeNavigationObserver::WarnInfobarDismissed() {
DCHECK(warn_infobar_delegate_);
warn_infobar_delegate_ = NULL;
@@ -214,70 +248,94 @@ void ManagedModeNavigationObserver::PreviewInfobarDismissed() {
preview_infobar_delegate_ = NULL;
}
-void ManagedModeNavigationObserver::AddNavigatedURL(const GURL& url) {
- if (std::find(navigated_urls_.begin(), navigated_urls_.end(), url) !=
- navigated_urls_.end()) {
- navigated_urls_.push_back(url);
- }
-}
+void ManagedModeNavigationObserver::AddSavedURLsToWhitelist() {
+ // |whitelist| is used to add the URLs to the preference list while
+ // AddURLPatternToManualWhitelist adds the navigated urls to the URL filter.
+ base::ListValue whitelist;
+ std::string pattern_to_add;
+ bool added_urls = false;
+
+ if (!navigated_urls_.empty()) {
+ for (std::vector<GURL>::const_iterator it = navigated_urls_.begin();
+ it+1 != navigated_urls_.end(); ++it) {
+ GURL current_url = *it;
+ GURL::Replacements replaced_components;
+ // Add the . to match the exact host.
+ replaced_components.SetHostStr("." + it->host());
+ replaced_components.SetRefStr("");
+ replaced_components.SetSchemeStr("");
+ current_url.ReplaceComponents(replaced_components);
+ pattern_to_add = current_url.spec();
+ whitelist.AppendString(pattern_to_add);
+ }
-void ManagedModeNavigationObserver::AddURLList() {
- // Get a copy of the whitelist since it can't be edited in place.
- // |whitelist| is the preference list while AddStringToManualWhitelist adds
- // the navigated urls to the URL filter.
- scoped_ptr<base::ListValue> whitelist(
- ManagedMode::GetWhitelist()->DeepCopy());
- std::string url_to_add;
- int added_url_count = 0;
-
- for (std::vector<GURL>::const_iterator it = navigated_urls_.begin();
- it+1 != navigated_urls_.end(); ++it) {
- url_to_add = it->spec();
- if (!IsInList(ManagedMode::GetWhitelist().get(), url_to_add)) {
- DLOG(ERROR) << "Adding (exact):" << url_to_add;
- ManagedMode::AddStringToManualWhitelist(url_to_add);
- whitelist->Append(Value::CreateStringValue(url_to_add));
- ++added_url_count;
+ // Add the protocol as well if the URL uses HTTPS. Also, since this is the
+ // final destination, add the whole subdomain.
+ if (navigated_urls_.back().SchemeIs("https")) {
+ pattern_to_add = "https://" + navigated_urls_.back().host();
+ } else {
+ pattern_to_add = navigated_urls_.back().host();
}
- }
- // If the URL uses https add the protocol as well instead of just the
- // hostname.
- if (navigated_urls_.back().SchemeIs("https")) {
- url_to_add = navigated_urls_.back().GetOrigin().spec();
- } else {
- url_to_add = navigated_urls_.back().host();
+ whitelist.AppendString(pattern_to_add);
+
+ added_urls = ManagedMode::AddToManualWhitelist(whitelist);
}
- // Use the local whitelist to see if this last URL is already there.
- if (!IsInList(ManagedMode::GetWhitelist().get(), url_to_add)) {
- DLOG(ERROR) << "Adding (hostname): " << url_to_add;
- ManagedMode::AddStringToManualWhitelist(url_to_add);
- whitelist->Append(Value::CreateStringValue(url_to_add));
- ++added_url_count;
- } else {
+ if ((navigated_urls_.empty() || !added_urls) &&
+ state_ != RECORDING_URLS_BEFORE_PREVIEW) {
Bernhard Bauer 2012/11/27 18:44:46 So, IIUC we have multiple entry points to this met
Sergiu 2012/11/28 12:53:25 It should work, yes, I've refactored and simplifie
// Tell the user that the site was already present in the whitelist.
InfoBarTabHelper* infobar_tab_helper =
InfoBarTabHelper::FromWebContents(web_contents());
infobar_tab_helper->AddInfoBar(new SimpleAlertInfoBarDelegate(
infobar_tab_helper,
NULL,
- l10n_util::GetStringFUTF16(IDS_MANAGED_MODE_ALREADY_ADDED_MESSAGE,
- base::IntToString16(added_url_count)),
+ l10n_util::GetStringUTF16(IDS_MANAGED_MODE_ALREADY_ADDED_MESSAGE),
true));
}
- ManagedMode::SetWhitelist(whitelist.get());
+}
+void ManagedModeNavigationObserver::SaveNavigatedURL(const GURL& url) {
+ if (state_ == NOT_RECORDING_URLS)
+ return;
+
+ if (std::find(navigated_urls_.begin(), navigated_urls_.end(), url) ==
+ navigated_urls_.end()) {
+ navigated_urls_.push_back(url);
+ }
+}
+
+
+void ManagedModeNavigationObserver::SetStateToRecordingAfterPreview() {
+ state_ = RECORDING_URLS_AFTER_PREVIEW;
+}
+
+void ManagedModeNavigationObserver::ClearObserverState() {
+ if (state_ == NOT_RECORDING_URLS && preview_infobar_delegate_) {
+ InfoBarTabHelper* infobar_tab_helper =
+ InfoBarTabHelper::FromWebContents(web_contents());
+ infobar_tab_helper->RemoveInfoBar(preview_infobar_delegate_);
+ preview_infobar_delegate_ = NULL;
+ }
+ navigated_urls_.clear();
+ state_ = RECORDING_URLS_BEFORE_PREVIEW;
+ RemoveTemporaryException();
}
void ManagedModeNavigationObserver::NavigateToPendingEntry(
const GURL& url,
content::NavigationController::ReloadType reload_type) {
DLOG(ERROR) << "NavigateToPendingEntry: " << url;
- // This means that a new navigation was instantiated and the data related to
- // the list of URLs needs to be cleared.
- navigated_urls_.clear();
- after_interstitial_ = false;
+
+ // This method gets called first when a user navigates to a (new) URL.
+ // This means that the data related to the list of URLs needs to be cleared
+ // in certain circumstances.
+ if (web_contents()->GetController().GetCurrentEntryIndex() <
+ last_allowed_page_ ||
+ navigated_urls_.empty() ||
+ navigated_urls_.back().host() != url.host()) {
+ ClearObserverState();
+ }
}
void ManagedModeNavigationObserver::DidNavigateMainFrame(
@@ -288,14 +346,19 @@ void ManagedModeNavigationObserver::DidNavigateMainFrame(
ManagedModeURLFilter::FilteringBehavior behavior =
url_filter_->GetFilteringBehaviorForURL(params.url);
- if (behavior != ManagedModeURLFilter::ALLOW)
- AddNavigatedURL(params.url);
+ if (state_ != NOT_RECORDING_URLS)
+ SaveNavigatedURL(params.url);
- if (behavior == ManagedModeURLFilter::ALLOW && after_interstitial_) {
+ if (behavior == ManagedModeURLFilter::ALLOW &&
+ state_ != RECORDING_URLS_BEFORE_PREVIEW) {
// The initial page that triggered the interstitial was blocked but the
// final page is already in the whitelist so add the series of URLs
// which lead to the final page to the whitelist as well.
- AddURLList();
+ AddSavedURLsToWhitelist();
+ }
+ if (state_ == RECORDING_URLS_AFTER_PREVIEW) {
+ state_ = NOT_RECORDING_URLS;
+ AddTemporaryException();
}
}
@@ -317,14 +380,17 @@ void ManagedModeNavigationObserver::ProvisionalChangeToMainFrameUrl(
const GURL& opener_url,
content::RenderViewHost* render_view_host) {
DLOG(ERROR) << "ProvisionalChangeToMainFrameUrl: " << url;
- // Mark the fact that an interstitial will be triggered here if the URL
- // must be blocked.
+ // This function is the last one to be called before the resource throttle
+ // shows the interstitial if the URL must be blocked.
ManagedModeURLFilter::FilteringBehavior behavior =
url_filter_->GetFilteringBehaviorForURL(url);
- if (behavior == ManagedModeURLFilter::BLOCK)
- after_interstitial_ = true;
- if (behavior != ManagedModeURLFilter::ALLOW)
- AddNavigatedURL(url);
+
+ if (!navigated_urls_.empty() && state_ == NOT_RECORDING_URLS &&
+ navigated_urls_.back().host() != url.host())
+ ClearObserverState();
+
+ if (behavior != ManagedModeURLFilter::ALLOW && state_ != NOT_RECORDING_URLS)
+ SaveNavigatedURL(url);
}
void ManagedModeNavigationObserver::DidCommitProvisionalLoadForFrame(
@@ -340,7 +406,6 @@ void ManagedModeNavigationObserver::DidCommitProvisionalLoadForFrame(
ManagedModeURLFilter::FilteringBehavior behavior =
url_filter_->GetFilteringBehaviorForURL(url);
- DLOG(ERROR) << "Current behavior: " << behavior;
if (behavior == ManagedModeURLFilter::WARN) {
if (!warn_infobar_delegate_) {
InfoBarTabHelper* infobar_tab_helper =
@@ -355,12 +420,12 @@ void ManagedModeNavigationObserver::DidCommitProvisionalLoadForFrame(
InfoBarTabHelper* infobar_tab_helper =
InfoBarTabHelper::FromWebContents(web_contents());
infobar_tab_helper->RemoveInfoBar(warn_infobar_delegate_);
- warn_infobar_delegate_= NULL;
+ warn_infobar_delegate_ = NULL;
}
- last_allowed_page_ = web_contents()->GetController().GetCurrentEntryIndex();
}
- if (behavior == ManagedModeURLFilter::BLOCK) {
+ if (state_ != RECORDING_URLS_BEFORE_PREVIEW &&
+ behavior == ManagedModeURLFilter::BLOCK) {
if (!preview_infobar_delegate_) {
InfoBarTabHelper* infobar_tab_helper =
InfoBarTabHelper::FromWebContents(web_contents());
@@ -373,7 +438,11 @@ void ManagedModeNavigationObserver::DidCommitProvisionalLoadForFrame(
InfoBarTabHelper* infobar_tab_helper =
InfoBarTabHelper::FromWebContents(web_contents());
infobar_tab_helper->RemoveInfoBar(preview_infobar_delegate_);
- preview_infobar_delegate_= NULL;
+ preview_infobar_delegate_ = NULL;
}
}
+
+ if (behavior == ManagedModeURLFilter::ALLOW) {
+ last_allowed_page_ = web_contents()->GetController().GetCurrentEntryIndex();
+ }
}

Powered by Google App Engine
This is Rietveld 408576698