 Chromium Code Reviews
 Chromium Code Reviews| OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This program generates a user profile and history by randomly generating | 5 // This program generates a user profile and history by randomly generating | 
| 6 // data and feeding it to the history service. | 6 // data and feeding it to the history service. | 
| 7 | 7 | 
| 8 #include "chrome/tools/profiles/thumbnail-inl.h" | 8 #include "chrome/tools/profiles/thumbnail-inl.h" | 
| 9 | 9 | 
| 10 #include "base/at_exit.h" | 10 #include "base/at_exit.h" | 
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" | 
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" | 
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" | 
| 14 #include "base/i18n/icu_util.h" | 14 #include "base/i18n/icu_util.h" | 
| 15 #include "base/logging.h" | |
| 15 #include "base/message_loop.h" | 16 #include "base/message_loop.h" | 
| 16 #include "base/path_service.h" | 17 #include "base/path_service.h" | 
| 17 #include "base/process_util.h" | 18 #include "base/process_util.h" | 
| 18 #include "base/string_number_conversions.h" | 19 #include "base/string_number_conversions.h" | 
| 19 #include "base/time.h" | 20 #include "base/time.h" | 
| 20 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" | 
| 21 #include "chrome/browser/history/history.h" | 22 #include "chrome/browser/history/history.h" | 
| 22 #include "chrome/browser/history/history_service_factory.h" | 23 #include "chrome/browser/history/history_service_factory.h" | 
| 23 #include "chrome/browser/history/top_sites.h" | 24 #include "chrome/browser/history/top_sites.h" | 
| 24 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" | 
| 25 #include "chrome/common/thumbnail_score.h" | 26 #include "chrome/common/thumbnail_score.h" | 
| 27 #include "chrome/test/base/testing_browser_process.h" | |
| 26 #include "chrome/test/base/testing_profile.h" | 28 #include "chrome/test/base/testing_profile.h" | 
| 27 #include "content/browser/browser_thread_impl.h" | 29 #include "content/browser/browser_thread_impl.h" | 
| 28 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" | 
| 29 #include "content/public/browser/notification_service.h" | 31 #include "content/public/browser/notification_service.h" | 
| 30 #include "third_party/skia/include/core/SkBitmap.h" | 32 #include "third_party/skia/include/core/SkBitmap.h" | 
| 31 #include "ui/base/resource/resource_bundle.h" | 33 #include "ui/base/resource/resource_bundle.h" | 
| 32 #include "ui/base/ui_base_paths.h" | 34 #include "ui/base/ui_base_paths.h" | 
| 33 #include "ui/gfx/codec/jpeg_codec.h" | 35 #include "ui/gfx/codec/jpeg_codec.h" | 
| 34 | 36 | 
| 37 #if defined(TOOLKIT_GTK) | |
| 38 #include <gtk/gtk.h> | |
| 39 #endif | |
| 40 | |
| 35 using base::Time; | 41 using base::Time; | 
| 36 using content::BrowserThread; | 42 using content::BrowserThread; | 
| 37 | 43 | 
| 44 // This is unfortunately dependent on the definition of FilePath::StringType. | |
| 
Mark Mentovai
2012/12/19 19:11:26
You can use |"%" PRFilePath|, apparently. I didn’t
 
Randy Smith (Not in Mondays)
2012/12/19 19:46:47
The more you know!  Thank you.  Done.
 | |
| 45 #if defined(OS_WIN) | |
| 46 #define FILE_PATH_VALUE_PRINTF "%ls" | |
| 47 #else | |
| 48 #define FILE_PATH_VALUE_PRINTF "%s" | |
| 49 #endif | |
| 50 | |
| 38 // Addition types data can be generated for. By default only urls/visits are | 51 // Addition types data can be generated for. By default only urls/visits are | 
| 39 // added. | 52 // added. | 
| 40 enum Types { | 53 enum Types { | 
| 41 TOP_SITES = 1 << 0, | 54 TOP_SITES = 1 << 0, | 
| 42 FULL_TEXT = 1 << 1 | 55 FULL_TEXT = 1 << 1 | 
| 43 }; | 56 }; | 
| 44 | 57 | 
| 58 // RAII for initializing and shutting down the TestBrowserProcess | |
| 59 class InitBrowserProcess { | |
| 60 public: | |
| 61 InitBrowserProcess() { | |
| 62 DCHECK(!g_browser_process); | |
| 63 g_browser_process = new TestingBrowserProcess; | |
| 64 } | |
| 65 | |
| 66 ~InitBrowserProcess() { | |
| 67 DCHECK(g_browser_process); | |
| 68 delete g_browser_process; | |
| 69 g_browser_process = NULL; | |
| 70 } | |
| 71 }; | |
| 72 | |
| 45 // Probabilities of different word lengths, as measured from Darin's profile. | 73 // Probabilities of different word lengths, as measured from Darin's profile. | 
| 46 // kWordLengthProbabilities[n-1] = P(word of length n) | 74 // kWordLengthProbabilities[n-1] = P(word of length n) | 
| 47 const float kWordLengthProbabilities[] = { 0.069f, 0.132f, 0.199f, | 75 const float kWordLengthProbabilities[] = { 0.069f, 0.132f, 0.199f, | 
| 48 0.137f, 0.088f, 0.115f, 0.081f, 0.055f, 0.034f, 0.021f, 0.019f, 0.018f, | 76 0.137f, 0.088f, 0.115f, 0.081f, 0.055f, 0.034f, 0.021f, 0.019f, 0.018f, | 
| 49 0.007f, 0.007f, 0.005f, 0.004f, 0.003f, 0.003f, 0.003f }; | 77 0.007f, 0.007f, 0.005f, 0.004f, 0.003f, 0.003f, 0.003f }; | 
| 50 | 78 | 
| 51 // Return a float uniformly in [0,1]. | 79 // Return a float uniformly in [0,1]. | 
| 52 // Useful for making probabilistic decisions. | 80 // Useful for making probabilistic decisions. | 
| 53 float RandomFloat() { | 81 float RandomFloat() { | 
| 54 return rand() / static_cast<float>(RAND_MAX); | 82 return rand() / static_cast<float>(RAND_MAX); | 
| 55 } | 83 } | 
| 56 | 84 | 
| 57 // Return an integer uniformly in [min,max). | 85 // Return an integer uniformly in [min,max). | 
| 58 int RandomInt(int min, int max) { | 86 int RandomInt(int min, int max) { | 
| 59 return min + (rand() % (max-min)); | 87 return min + (rand() % (max-min)); | 
| 60 } | 88 } | 
| 61 | 89 | 
| 62 // Return a string of |count| lowercase random characters. | 90 // Return a string of |count| lowercase random characters. | 
| 63 std::wstring RandomChars(int count) { | 91 string16 RandomChars(int count) { | 
| 64 std::wstring str; | 92 string16 str; | 
| 65 for (int i = 0; i < count; ++i) | 93 for (int i = 0; i < count; ++i) | 
| 66 str += L'a' + rand() % 26; | 94 str += L'a' + rand() % 26; | 
| 67 return str; | 95 return str; | 
| 68 } | 96 } | 
| 69 | 97 | 
| 70 std::wstring RandomWord() { | 98 string16 RandomWord() { | 
| 71 // TODO(evanm): should we instead use the markov chain based | 99 // TODO(evanm): should we instead use the markov chain based | 
| 72 // version of this that I already wrote? | 100 // version of this that I already wrote? | 
| 73 | 101 | 
| 74 // Sample a word length from kWordLengthProbabilities. | 102 // Sample a word length from kWordLengthProbabilities. | 
| 75 float sample = RandomFloat(); | 103 float sample = RandomFloat(); | 
| 76 int i; | 104 size_t i; | 
| 77 for (i = 0; i < arraysize(kWordLengthProbabilities); ++i) { | 105 for (i = 0; i < arraysize(kWordLengthProbabilities); ++i) { | 
| 78 sample -= kWordLengthProbabilities[i]; | 106 sample -= kWordLengthProbabilities[i]; | 
| 79 if (sample < 0) break; | 107 if (sample < 0) break; | 
| 80 } | 108 } | 
| 81 const int word_length = i + 1; | 109 const int word_length = i + 1; | 
| 82 return RandomChars(word_length); | 110 return RandomChars(word_length); | 
| 83 } | 111 } | 
| 84 | 112 | 
| 85 // Return a string of |count| random words. | 113 // Return a string of |count| random words. | 
| 86 std::wstring RandomWords(int count) { | 114 string16 RandomWords(int count) { | 
| 87 std::wstring str; | 115 string16 str; | 
| 88 for (int i = 0; i < count; ++i) { | 116 for (int i = 0; i < count; ++i) { | 
| 89 if (!str.empty()) | 117 if (!str.empty()) | 
| 90 str += L' '; | 118 str += L' '; | 
| 91 str += RandomWord(); | 119 str += RandomWord(); | 
| 92 } | 120 } | 
| 93 return str; | 121 return str; | 
| 94 } | 122 } | 
| 95 | 123 | 
| 96 // Return a random URL-looking string. | 124 // Return a random URL-looking string. | 
| 97 GURL ConstructRandomURL() { | 125 GURL ConstructRandomURL() { | 
| 98 return GURL(std::wstring(L"http://") + RandomChars(3) + L".com/" + | 126 return GURL(ASCIIToUTF16("http://") + RandomChars(3) + ASCIIToUTF16(".com/") + | 
| 99 RandomChars(RandomInt(5, 20))); | 127 RandomChars(RandomInt(5, 20))); | 
| 100 } | 128 } | 
| 101 | 129 | 
| 102 // Return a random page title-looking string. | 130 // Return a random page title-looking string. | 
| 103 std::wstring ConstructRandomTitle() { | 131 string16 ConstructRandomTitle() { | 
| 104 return RandomWords(RandomInt(3, 15)); | 132 return RandomWords(RandomInt(3, 15)); | 
| 105 } | 133 } | 
| 106 | 134 | 
| 107 // Return a random string that could function as page contents. | 135 // Return a random string that could function as page contents. | 
| 108 std::wstring ConstructRandomPage() { | 136 string16 ConstructRandomPage() { | 
| 109 return RandomWords(RandomInt(10, 4000)); | 137 return RandomWords(RandomInt(10, 4000)); | 
| 110 } | 138 } | 
| 111 | 139 | 
| 112 // Insert a batch of |batch_size| URLs, starting at pageid |page_id|. | 140 // Insert a batch of |batch_size| URLs, starting at pageid |page_id|. | 
| 113 void InsertURLBatch(Profile* profile, | 141 void InsertURLBatch(Profile* profile, | 
| 114 int page_id, | 142 int page_id, | 
| 115 int batch_size, | 143 int batch_size, | 
| 116 int types) { | 144 int types) { | 
| 117 HistoryService* history_service = | 145 HistoryService* history_service = | 
| 118 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); | 146 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); | 
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 top_sites->SetPageThumbnail(url, image, score); | 216 top_sites->SetPageThumbnail(url, image, score); | 
| 189 } | 217 } | 
| 190 | 218 | 
| 191 previous_url = url; | 219 previous_url = url; | 
| 192 | 220 | 
| 193 if (revisit_urls.empty() || RandomFloat() < kRevisitableURLProbability) | 221 if (revisit_urls.empty() || RandomFloat() < kRevisitableURLProbability) | 
| 194 revisit_urls.push_back(url); | 222 revisit_urls.push_back(url); | 
| 195 } | 223 } | 
| 196 } | 224 } | 
| 197 | 225 | 
| 198 int main(int argc, const char* argv[]) { | 226 int main(int argc, char* argv[]) { | 
| 199 CommandLine::Init(argc, argv); | 227 CommandLine::Init(argc, argv); | 
| 200 base::EnableTerminationOnHeapCorruption(); | 228 base::EnableTerminationOnHeapCorruption(); | 
| 201 base::AtExitManager exit_manager; | 229 base::AtExitManager exit_manager; | 
| 202 CommandLine* cl = CommandLine::ForCurrentProcess(); | 230 CommandLine* cl = CommandLine::ForCurrentProcess(); | 
| 203 | 231 | 
| 204 int types = 0; | 232 int types = 0; | 
| 205 if (cl->HasSwitch("top-sites")) | 233 if (cl->HasSwitch("top-sites")) | 
| 206 types |= TOP_SITES; | 234 types |= TOP_SITES; | 
| 207 if (cl->HasSwitch("full-text")) | 235 if (cl->HasSwitch("full-text")) | 
| 208 types |= FULL_TEXT; | 236 types |= FULL_TEXT; | 
| 209 | 237 | 
| 210 // We require two arguments: urlcount and profiledir. | 238 // We require two arguments: urlcount and profiledir. | 
| 211 const CommandLine::StringVector& args = cl->GetArgs(); | 239 const CommandLine::StringVector& args = cl->GetArgs(); | 
| 
Mark Mentovai
2012/12/19 19:11:26
Fix the indentation.
 
Randy Smith (Not in Mondays)
2012/12/19 19:46:47
Whoops; done.
 | |
| 212 if (args.size() < 2) { | 240 if (args.size() < 2) { | 
| 213 printf("usage: %s [--top-sites] [--full-text] <urlcount> " | 241 printf("usage: %s [--top-sites] [--full-text] <urlcount> " | 
| 214 "<profiledir>\n", argv[0]); | 242 "<profiledir>\n", argv[0]); | 
| 215 printf("\n --top-sites Generate thumbnails\n"); | 243 printf("\n --top-sites Generate thumbnails\n"); | 
| 216 printf("\n --full-text Generate full text index\n"); | 244 printf("\n --full-text Generate full text index\n"); | 
| 217 return -1; | 245 return -1; | 
| 218 } | 246 } | 
| 219 | 247 | 
| 220 int url_count = 0; | 248 int url_count = 0; | 
| 221 base::StringToInt(WideToUTF8(args[0]), &url_count); | 249 base::StringToInt(args[0], &url_count); | 
| 222 FilePath dst_dir(args[1]); | 250 FilePath dst_dir(args[1]); | 
| 223 if (!dst_dir.IsAbsolute()) { | 251 if (!dst_dir.IsAbsolute()) { | 
| 224 FilePath current_dir; | 252 FilePath current_dir; | 
| 225 file_util::GetCurrentDirectory(¤t_dir); | 253 file_util::GetCurrentDirectory(¤t_dir); | 
| 226 dst_dir = current_dir.Append(dst_dir); | 254 dst_dir = current_dir.Append(dst_dir); | 
| 227 } | 255 } | 
| 228 if (!file_util::CreateDirectory(dst_dir)) { | 256 if (!file_util::CreateDirectory(dst_dir)) { | 
| 229 printf("Unable to create directory %ls: %d\n", | 257 PLOG(ERROR) << "Unable to create directory " << dst_dir.value().c_str(); | 
| 230 dst_dir.value().c_str(), | |
| 231 ::GetLastError()); | |
| 232 } | 258 } | 
| 233 | 259 | 
| 234 icu_util::Initialize(); | 260 icu_util::Initialize(); | 
| 261 // Copied from base/test/test_suite.cc. | |
| 262 #if defined(TOOLKIT_GTK) | |
| 263 gtk_init_check(&argc, &argv); | |
| 264 #endif | |
| 235 | 265 | 
| 266 InitBrowserProcess initialize_browser_process; | |
| 236 chrome::RegisterPathProvider(); | 267 chrome::RegisterPathProvider(); | 
| 237 ui::RegisterPathProvider(); | 268 ui::RegisterPathProvider(); | 
| 238 ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL); | |
| 239 scoped_ptr<content::NotificationService> notification_service( | |
| 240 content::NotificationService::Create()); | |
| 241 MessageLoopForUI message_loop; | 269 MessageLoopForUI message_loop; | 
| 242 content::BrowserThreadImpl ui_thread(BrowserThread::UI, &message_loop); | 270 content::BrowserThreadImpl ui_thread(BrowserThread::UI, &message_loop); | 
| 243 content::BrowserThreadImpl db_thread(BrowserThread::DB, &message_loop); | 271 content::BrowserThreadImpl db_thread(BrowserThread::DB, &message_loop); | 
| 272 ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL); | |
| 244 TestingProfile profile; | 273 TestingProfile profile; | 
| 245 profile.CreateHistoryService(false, false); | 274 profile.CreateHistoryService(false, false); | 
| 246 if (types & TOP_SITES) { | 275 if (types & TOP_SITES) { | 
| 247 profile.CreateTopSites(); | 276 profile.CreateTopSites(); | 
| 248 profile.BlockUntilTopSitesLoaded(); | 277 profile.BlockUntilTopSitesLoaded(); | 
| 249 } | 278 } | 
| 250 | 279 | 
| 251 srand(static_cast<unsigned int>(Time::Now().ToInternalValue())); | 280 srand(static_cast<unsigned int>(Time::Now().ToInternalValue())); | 
| 252 | 281 | 
| 253 // The maximum number of URLs to insert into history in one batch. | 282 // The maximum number of URLs to insert into history in one batch. | 
| (...skipping 13 matching lines...) Expand all Loading... | |
| 267 profile.DestroyHistoryService(); | 296 profile.DestroyHistoryService(); | 
| 268 | 297 | 
| 269 message_loop.RunUntilIdle(); | 298 message_loop.RunUntilIdle(); | 
| 270 | 299 | 
| 271 file_util::FileEnumerator file_iterator(profile.GetPath(), false, | 300 file_util::FileEnumerator file_iterator(profile.GetPath(), false, | 
| 272 file_util::FileEnumerator::FILES); | 301 file_util::FileEnumerator::FILES); | 
| 273 FilePath path = file_iterator.Next(); | 302 FilePath path = file_iterator.Next(); | 
| 274 while (!path.empty()) { | 303 while (!path.empty()) { | 
| 275 FilePath dst_file = dst_dir.Append(path.BaseName()); | 304 FilePath dst_file = dst_dir.Append(path.BaseName()); | 
| 276 file_util::Delete(dst_file, false); | 305 file_util::Delete(dst_file, false); | 
| 277 printf("Copying file %ls to %ls\n", path.value().c_str(), | 306 printf("Copying file " FILE_PATH_VALUE_PRINTF " to " | 
| 
Mark Mentovai
2012/12/19 19:11:26
Is it even necessary to use printf instead of, say
 
Randy Smith (Not in Mondays)
2012/12/19 19:46:47
Hmmm.  I'm torn on this.  That would be simpler fr
 | |
| 307 FILE_PATH_VALUE_PRINTF "\n", path.value().c_str(), | |
| 278 dst_file.value().c_str()); | 308 dst_file.value().c_str()); | 
| 279 if (!file_util::CopyFile(path, dst_file)) { | 309 if (!file_util::CopyFile(path, dst_file)) { | 
| 280 printf("Copying file failed: %d\n", ::GetLastError()); | 310 PLOG(ERROR) << "Copying file failed"; | 
| 281 return -1; | 311 return -1; | 
| 282 } | 312 } | 
| 283 path = file_iterator.Next(); | 313 path = file_iterator.Next(); | 
| 284 } | 314 } | 
| 285 | 315 | 
| 286 return 0; | 316 return 0; | 
| 287 } | 317 } | 
| OLD | NEW |