diff options
author | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-10 18:50:32 +0000 |
---|---|---|
committer | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-10 18:50:32 +0000 |
commit | 9ccbb370aa45f477941e0599d4ce7c89fac64101 (patch) | |
tree | 8b21818fa95d05ff00dfe5b9986d1d39eb4be722 /base | |
parent | a9acde54584ff37bcc5fad73cf25dce5d85348bc (diff) | |
download | chromium_src-9ccbb370aa45f477941e0599d4ce7c89fac64101.zip chromium_src-9ccbb370aa45f477941e0599d4ce7c89fac64101.tar.gz chromium_src-9ccbb370aa45f477941e0599d4ce7c89fac64101.tar.bz2 |
This CL adds prompting for dangerous types of files (executable) when they are automatically downloaded.
The file is saved with a temporary name (dangerous_download_xxxx.download) in the download directory and the user is presented (in the download shelf and the download tab if opened) with a warning message and buttons to save/discard the download.
If discarded the download is removed (and its file deleted).
If saved, download goes as usual.
Dangerous downloads not confirmed by the user are deleted on shutdown.
TEST=Download a small exe file, try using the save/discard button from the download shelf and from the download tab (the intent is that the file has been entirely downloaded by the time you take action). Try again with a slow/big download (that time the download is expected not to be finished when approved/discarded).
Review URL: http://codereview.chromium.org/6043
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@3228 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/file_util.h | 4 | ||||
-rw-r--r-- | base/file_util_posix.cc | 7 | ||||
-rw-r--r-- | base/file_util_win.cc | 10 | ||||
-rw-r--r-- | base/string_util.cc | 36 | ||||
-rw-r--r-- | base/string_util.h | 8 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 26 |
6 files changed, 89 insertions, 2 deletions
diff --git a/base/file_util.h b/base/file_util.h index 9624e60..150b629 100644 --- a/base/file_util.h +++ b/base/file_util.h @@ -224,6 +224,10 @@ bool GetTempDir(std::wstring* path); // TODO(erikkay): rename this function and track down all of the callers. bool CreateTemporaryFileName(std::wstring* temp_file); +// Same as CreateTemporaryFileName but the file is created in |dir|. +bool CreateTemporaryFileNameInDir(const std::wstring& dir, + std::wstring* temp_file); + // Create a new directory under TempPath. If prefix is provided, the new // directory name is in the format of prefixyyyy. // If success, return true and output the full path of the directory created. diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc index edb20c7..e440640 100644 --- a/base/file_util_posix.cc +++ b/base/file_util_posix.cc @@ -267,6 +267,13 @@ bool CreateTemporaryFileName(std::wstring* temp_file) { return true; } +bool CreateTemporaryFileNameInDir(const std::wstring& dir, + std::wstring* temp_file) { + // Not implemented yet. + NOTREACHED(); + return false; +} + bool CreateNewTempDirectory(const std::wstring& prefix, std::wstring* new_temp_path) { std::wstring tmpdir; diff --git a/base/file_util_win.cc b/base/file_util_win.cc index 8253e53..7be172f 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -383,13 +383,19 @@ bool GetTempDir(std::wstring* path) { } bool CreateTemporaryFileName(std::wstring* temp_file) { - wchar_t temp_name[MAX_PATH + 1]; std::wstring temp_path; if (!GetTempDir(&temp_path)) return false; - if (!GetTempFileName(temp_path.c_str(), L"", 0, temp_name)) + return CreateTemporaryFileNameInDir(temp_path, temp_file); +} + +bool CreateTemporaryFileNameInDir(const std::wstring& dir, + std::wstring* temp_file) { + wchar_t temp_name[MAX_PATH + 1]; + + if (!GetTempFileName(dir.c_str(), L"", 0, temp_name)) return false; // fail! DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH); diff --git a/base/string_util.cc b/base/string_util.cc index 223c485..5fa2f75 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -1434,3 +1434,39 @@ size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { return lcpyT<wchar_t>(dst, src, dst_size); } +bool ElideString(const std::wstring& input, int max_len, std::wstring* output) { + DCHECK(max_len >= 0); + if (static_cast<int>(input.length()) <= max_len) { + output->assign(input); + return false; + } + + switch (max_len) { + case 0: + output->clear(); + break; + case 1: + output->assign(input.substr(0, 1)); + break; + case 2: + output->assign(input.substr(0, 2)); + break; + case 3: + output->assign(input.substr(0, 1) + L"." + + input.substr(input.length() - 1)); + break; + case 4: + output->assign(input.substr(0, 1) + L".." + + input.substr(input.length() - 1)); + break; + default: { + int rstr_len = (max_len - 3) / 2; + int lstr_len = rstr_len + ((max_len - 3) % 2); + output->assign(input.substr(0, lstr_len) + L"..." + + input.substr(input.length() - rstr_len)); + break; + } + } + + return true; +} diff --git a/base/string_util.h b/base/string_util.h index a9f08c4..981cd30 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -508,6 +508,14 @@ std::wstring ReplaceStringPlaceholders(const std::wstring& format_string, const std::wstring& d, std::vector<size_t>* offsets); +// If the size of |input| is more than |max_len|, this function returns true and +// |input| is shortened into |output| by removing chars in the middle (they are +// replaced with up to 3 dots, as size permits). +// Ex: ElideString(L"Hello", 10, &str) puts Hello in str and returns false. +// ElideString(L"Hello my name is Tom", 10, &str) puts "Hell...Tom" in str and +// returns true. +bool ElideString(const std::wstring& input, int max_len, std::wstring* output); + // Returns true if the string passed in matches the pattern. The pattern // string can contain wildcards like * and ? // TODO(iyengar) This function may not work correctly for CJK strings as diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index e438ebb..25f43c6 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -1380,3 +1380,29 @@ TEST(StringUtilTest, WprintfFormatPortabilityTest) { } } +TEST(StringUtilTest, ElideString) { + struct TestData { + const wchar_t* input; + int max_len; + bool result; + const wchar_t* output; + } cases[] = { + { L"Hello", 0, true, L"" }, + { L"", 0, false, L"" }, + { L"Hello, my name is Tom", 1, true, L"H" }, + { L"Hello, my name is Tom", 2, true, L"He" }, + { L"Hello, my name is Tom", 3, true, L"H.m" }, + { L"Hello, my name is Tom", 4, true, L"H..m" }, + { L"Hello, my name is Tom", 5, true, L"H...m" }, + { L"Hello, my name is Tom", 6, true, L"He...m" }, + { L"Hello, my name is Tom", 7, true, L"He...om" }, + { L"Hello, my name is Tom", 10, true, L"Hell...Tom" }, + { L"Hello, my name is Tom", 100, false, L"Hello, my name is Tom" } + }; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + std::wstring output; + EXPECT_EQ(cases[i].result, + ElideString(cases[i].input, cases[i].max_len, &output)); + EXPECT_TRUE(output == cases[i].output); + } +} |