diff options
author | alyssad@chromium.org <alyssad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-14 21:14:19 +0000 |
---|---|---|
committer | alyssad@chromium.org <alyssad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-14 21:14:19 +0000 |
commit | a0fc50d70d4f98debbc99c312cec44c3eff5bed5 (patch) | |
tree | 6401bbf0c6745dc2beaae8173e381012d3125c70 /chrome | |
parent | f0d47ac8879f6f26df2c35a3e78528f0ee4dce05 (diff) | |
download | chromium_src-a0fc50d70d4f98debbc99c312cec44c3eff5bed5.zip chromium_src-a0fc50d70d4f98debbc99c312cec44c3eff5bed5.tar.gz chromium_src-a0fc50d70d4f98debbc99c312cec44c3eff5bed5.tar.bz2 |
Clear Browsing Data hook added and some small tests. BUG=36176
Review URL: http://codereview.chromium.org/2909005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52402 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/automation/automation_provider.cc | 74 | ||||
-rw-r--r-- | chrome/browser/automation/automation_provider.h | 7 | ||||
-rw-r--r-- | chrome/browser/automation/automation_provider_observers.cc | 8 | ||||
-rw-r--r-- | chrome/browser/automation/automation_provider_observers.h | 17 | ||||
-rw-r--r-- | chrome/test/functional/PYAUTO_TESTS | 1 | ||||
-rw-r--r-- | chrome/test/functional/browsing_data.py | 83 | ||||
-rw-r--r-- | chrome/test/pyautolib/pyauto.py | 25 |
7 files changed, 215 insertions, 0 deletions
diff --git a/chrome/browser/automation/automation_provider.cc b/chrome/browser/automation/automation_provider.cc index efca124..95b524f 100644 --- a/chrome/browser/automation/automation_provider.cc +++ b/chrome/browser/automation/automation_provider.cc @@ -2349,6 +2349,78 @@ void AutomationProvider::SaveTabContents(Browser* browser, Send(reply_message); } +// Refer to ClearBrowsingData() in chrome/test/pyautolib/pyauto.py for sample +// json input. +// Sample json output: {} +void AutomationProvider::ClearBrowsingData(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message) { + std::string json_return = "{}"; + + std::map<std::string, BrowsingDataRemover::TimePeriod> string_to_time_period; + string_to_time_period["LAST_HOUR"] = BrowsingDataRemover::LAST_HOUR; + string_to_time_period["LAST_DAY"] = BrowsingDataRemover::LAST_DAY; + string_to_time_period["LAST_WEEK"] = BrowsingDataRemover::LAST_WEEK; + string_to_time_period["FOUR_WEEKS"] = BrowsingDataRemover::FOUR_WEEKS; + string_to_time_period["EVERYTHING"] = BrowsingDataRemover::EVERYTHING; + + std::map<std::string, int> string_to_mask_value; + string_to_mask_value["HISTORY"] = BrowsingDataRemover::REMOVE_HISTORY; + string_to_mask_value["DOWNLOADS"] = BrowsingDataRemover::REMOVE_DOWNLOADS; + string_to_mask_value["COOKIES"] = BrowsingDataRemover::REMOVE_COOKIES; + string_to_mask_value["PASSWORDS"] = BrowsingDataRemover::REMOVE_PASSWORDS; + string_to_mask_value["FORM_DATA"] = BrowsingDataRemover::REMOVE_FORM_DATA; + string_to_mask_value["CACHE"] = BrowsingDataRemover::REMOVE_CACHE; + + std::string time_period; + ListValue* to_remove; + if (!args->GetString(L"time_period", &time_period) || + !args->GetList(L"to_remove", &to_remove)) { + // TODO(nirnimesh): Here and below refactor returns with pending CL. + std::string json_return = + JSONErrorString("time_period must be a string and to_remove a list."); + AutomationMsg_SendJSONRequest::WriteReplyParams( + reply_message, json_return, false); + Send(reply_message); + return; + } + + int remove_mask = 0; + int num_removals = to_remove->GetSize(); + for (int i = 0; i < num_removals; i++) { + std::string removal; + to_remove->GetString(i, &removal); + // If the provided string is not part of the map, then error out. + if (!ContainsKey(string_to_mask_value, removal)) { + std::string json_return = + JSONErrorString("Invalid browsing data string found in to_remove."); + AutomationMsg_SendJSONRequest::WriteReplyParams( + reply_message, json_return, false); + Send(reply_message); + return; + } + remove_mask |= string_to_mask_value[removal]; + } + + if (!ContainsKey(string_to_time_period, time_period)) { + std::string json_return = + JSONErrorString("Invalid string for time_period."); + AutomationMsg_SendJSONRequest::WriteReplyParams( + reply_message, json_return, false); + Send(reply_message); + return; + } + + BrowsingDataRemover* remover = new BrowsingDataRemover( + profile(), string_to_time_period[time_period], base::Time()); + + remover->AddObserver( + new AutomationProviderBrowsingDataObserver(this, reply_message)); + remover->Remove(remove_mask); + // BrowsingDataRemover deletes itself using DeleteTask. + // The observer also deletes itself after sending the reply. +} + // Sample json input: { "command": "GetThemeInfo" } // Refer GetThemeInfo() in chrome/test/pyautolib/pyauto.py for sample output. void AutomationProvider::GetThemeInfo(Browser* browser, @@ -2700,6 +2772,8 @@ void AutomationProvider::SendJSONRequest(int handle, handler_map["SaveTabContents"] = &AutomationProvider::SaveTabContents; + handler_map["ClearBrowsingData"] = &AutomationProvider::ClearBrowsingData; + // SetTheme() implemented using InstallExtension(). handler_map["GetThemeInfo"] = &AutomationProvider::GetThemeInfo; diff --git a/chrome/browser/automation/automation_provider.h b/chrome/browser/automation/automation_provider.h index c55e04c..b4782c3 100644 --- a/chrome/browser/automation/automation_provider.h +++ b/chrome/browser/automation/automation_provider.h @@ -441,6 +441,13 @@ class AutomationProvider : public base::RefCounted<AutomationProvider>, DictionaryValue* args, IPC::Message* reply_message); + // Clear the specified browsing data. This call provides similar + // functionality to RemoveBrowsingData but is synchronous. + // Uses the JSON interface for input/output. + void ClearBrowsingData(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); + // Get info about theme. // Uses the JSON interface for input/output. void GetThemeInfo(Browser* browser, diff --git a/chrome/browser/automation/automation_provider_observers.cc b/chrome/browser/automation/automation_provider_observers.cc index 0b7f0af..a8c441d 100644 --- a/chrome/browser/automation/automation_provider_observers.cc +++ b/chrome/browser/automation/automation_provider_observers.cc @@ -1012,6 +1012,14 @@ void AutomationProviderHistoryObserver::HistoryQueryComplete( delete this; } +void AutomationProviderBrowsingDataObserver::OnBrowsingDataRemoverDone() { + // Send back an empty success message + AutomationMsg_SendJSONRequest::WriteReplyParams( + reply_message_, std::string("{}"), true); + provider_->Send(reply_message_); + delete this; +} + OmniboxAcceptNotificationObserver::OmniboxAcceptNotificationObserver( NavigationController* controller, AutomationProvider* automation, diff --git a/chrome/browser/automation/automation_provider_observers.h b/chrome/browser/automation/automation_provider_observers.h index fbe8c15..72147c6 100644 --- a/chrome/browser/automation/automation_provider_observers.h +++ b/chrome/browser/automation/automation_provider_observers.h @@ -10,6 +10,7 @@ #include <set> #include "chrome/browser/bookmarks/bookmark_model_observer.h" +#include "chrome/browser/browsing_data_remover.h" #include "chrome/browser/download/download_manager.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" @@ -611,6 +612,22 @@ class AutomationProviderHistoryObserver { IPC::Message* reply_message_; }; +// Allows the automation provider to wait for clearing browser data to finish. +class AutomationProviderBrowsingDataObserver : + public BrowsingDataRemover::Observer { + public: + AutomationProviderBrowsingDataObserver( + AutomationProvider* provider, + IPC::Message* reply_message) + : provider_(provider), + reply_message_(reply_message) {} + void OnBrowsingDataRemoverDone(); + + private: + AutomationProvider* provider_; + IPC::Message* reply_message_; +}; + // Allows automation provider to wait until page load after selecting an item // in the omnibox popup. class OmniboxAcceptNotificationObserver : public NotificationObserver { diff --git a/chrome/test/functional/PYAUTO_TESTS b/chrome/test/functional/PYAUTO_TESTS index 12d5adf..5029b06 100644 --- a/chrome/test/functional/PYAUTO_TESTS +++ b/chrome/test/functional/PYAUTO_TESTS @@ -25,6 +25,7 @@ 'bookmark_bar', 'bookmarks', 'browser', + 'browsing_data', 'codesign', 'content', 'downloads', diff --git a/chrome/test/functional/browsing_data.py b/chrome/test/functional/browsing_data.py new file mode 100644 index 0000000..f217c5b --- /dev/null +++ b/chrome/test/functional/browsing_data.py @@ -0,0 +1,83 @@ +#!/usr/bin/python +# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import os +import time + +import pyauto_functional # Must be imported before pyauto +import pyauto + + +class BrowsingDataTest(pyauto.PyUITest): + """Tests that clearing browsing data works correctly.""" + + def testClearHistory(self): + """Verify that clearing the history works.""" + url = self.GetFileURLForPath(os.path.join(self.DataDir(), 'title2.html')) + self.NavigateToURL(url) + + history = self.GetHistoryInfo().History() + self.assertEqual(1, len(history)) + + self.ClearBrowsingData(['HISTORY'], 'EVERYTHING') + history = self.GetHistoryInfo().History() + self.assertEqual(0, len(history)) + + def testClearHistoryPastHour(self): + """Verify that clearing the history of the past hour works and does not + clear history older than one hour. + """ + title = 'Google' + num_secs_in_hour = 3600 + + # Forge a history item for two hours ago + now = time.time() + last_hour = now - (2 * num_secs_in_hour) + self.AddHistoryItem({'title': title, + 'url': 'http://www.google.com', + 'time': last_hour}) + + # Forge a history item for right now + self.AddHistoryItem({'title': 'This Will Be Cleared', + 'url': 'http://www.dev.chromium.org', + 'time': now}) + + history = self.GetHistoryInfo().History() + self.assertEqual(2, len(history)) + + self.ClearBrowsingData(['HISTORY'], 'LAST_HOUR') + history = self.GetHistoryInfo().History() + self.assertEqual(1, len(history)) + self.assertEqual(title, history[0]['title']) + + def testClearHistoryAndDownloads(self): + """Verify that we can clear history and downloads at the same time.""" + # First build up some history and download something. + url = self.GetFileURLForPath(os.path.join(self.DataDir(), 'title2.html')) + self.NavigateToURL(url) + + test_dir = os.path.join(os.path.abspath(self.DataDir()), 'downloads') + file_path = os.path.join(test_dir, 'a_zip_file.zip') + file_url = self.GetFileURLForPath(file_path) + downloaded_pkg = os.path.join(self.GetDownloadDirectory().value(), + 'a_zip_file.zip') + os.path.exists(downloaded_pkg) and os.remove(downloaded_pkg) + self.DownloadAndWaitForStart(file_url) + self.WaitForAllDownloadsToComplete() + + # Verify that the history and download exist. + self.assertEqual(1, len(self.GetHistoryInfo().History())) + self.assertEqual(1, len(self.GetDownloadsInfo().Downloads())) + + # Clear the history and downloads and verify they're both gone. + self.ClearBrowsingData(['HISTORY', 'DOWNLOADS'], 'EVERYTHING') + history = self.GetHistoryInfo().History() + downloads = self.GetDownloadsInfo().Downloads() + self.assertEqual(0, len(history)) + self.assertEqual(0, len(downloads)) + + +if __name__ == '__main__': + pyauto_functional.Main() diff --git a/chrome/test/pyautolib/pyauto.py b/chrome/test/pyautolib/pyauto.py index 4b94b3d..c25b893 100644 --- a/chrome/test/pyautolib/pyauto.py +++ b/chrome/test/pyautolib/pyauto.py @@ -757,6 +757,31 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): finally: shutil.rmtree(tempdir) + def ClearBrowsingData(self, to_remove, time_period): + """Clear the specified browsing data. Implements the features available in + the "ClearBrowsingData" UI. + + Args: + to_remove: a list of strings indicating which types of browsing data + should be removed. Strings that can be in the list are: + HISTORY, DOWNLOADS, COOKIES, PASSWORDS, FORM_DATA, CACHE + time_period: a string indicating the time period for the removal. + Possible strings are: + LAST_HOUR, LAST_DAY, LAST_WEEK, FOUR_WEEKS, EVERYTHING + + Raises: + pyauto_errors.JSONInterfaceError if the automation call returns an error. + """ + cmd_dict = { # Prepare command for the json interface + 'command': 'ClearBrowsingData', + 'to_remove': to_remove, + 'time_period': time_period + } + ret_dict = json.loads(self._SendJSONRequest(0, json.dumps(cmd_dict))) + if ret_dict.has_key('error'): + raise JSONInterfaceError(ret_dict['error']) + return ret_dict + def SetTheme(self, crx_file_path): """Installs the given theme synchronously. |