15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chrome/test/perf/generate_profile.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/at_exit.h" 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/file_util.h" 10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/files/file_enumerator.h" 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/i18n/icu_util.h" 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/logging.h" 149ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h" 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h" 17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h" 18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h" 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/history/history_service.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_service_factory.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/top_sites.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_paths.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/thumbnail_score.h" 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/testing_browser_process.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/testing_profile.h" 26a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chrome/tools/profiles/thumbnail-inl.h" 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h" 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_service.h" 29a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "content/public/test/test_browser_thread.h" 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkBitmap.h" 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h" 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/ui_base_paths.h" 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/codec/jpeg_codec.h" 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::Time; 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread; 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 38a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace { 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Probabilities of different word lengths, as measured from Darin's profile. 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// kWordLengthProbabilities[n-1] = P(word of length n) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const float kWordLengthProbabilities[] = { 0.069f, 0.132f, 0.199f, 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 0.137f, 0.088f, 0.115f, 0.081f, 0.055f, 0.034f, 0.021f, 0.019f, 0.018f, 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 0.007f, 0.007f, 0.005f, 0.004f, 0.003f, 0.003f, 0.003f }; 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Return a float uniformly in [0,1]. 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Useful for making probabilistic decisions. 48a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)inline float RandomFloat() { 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return rand() / static_cast<float>(RAND_MAX); 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Return an integer uniformly in [min,max). 53a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)inline int RandomInt(int min, int max) { 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return min + (rand() % (max-min)); 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Return a string of |count| lowercase random characters. 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 RandomChars(int count) { 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) string16 str; 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (int i = 0; i < count; ++i) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) str += L'a' + rand() % 26; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return str; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 RandomWord() { 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(evanm): should we instead use the markov chain based 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // version of this that I already wrote? 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sample a word length from kWordLengthProbabilities. 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) float sample = RandomFloat(); 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) size_t i; 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (i = 0; i < arraysize(kWordLengthProbabilities); ++i) { 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sample -= kWordLengthProbabilities[i]; 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (sample < 0) break; 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const int word_length = i + 1; 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return RandomChars(word_length); 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Return a string of |count| random words. 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 RandomWords(int count) { 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) string16 str; 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (int i = 0; i < count; ++i) { 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!str.empty()) 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) str += L' '; 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) str += RandomWord(); 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return str; 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Return a random URL-looking string. 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL ConstructRandomURL() { 932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return GURL(ASCIIToUTF16("http://") + RandomChars(3) + ASCIIToUTF16(".com/") + 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) RandomChars(RandomInt(5, 20))); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Return a random page title-looking string. 982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)string16 ConstructRandomTitle() { 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return RandomWords(RandomInt(3, 15)); 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Insert a batch of |batch_size| URLs, starting at pageid |page_id|. 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void InsertURLBatch(Profile* profile, 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int page_id, 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int batch_size, 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int types) { 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HistoryService* history_service = 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Probability of following a link on the current "page" 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // (vs randomly jumping to a new page). 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const float kFollowLinkProbability = 0.85f; 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Probability of visiting a page we've visited before. 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const float kRevisitLinkProbability = 0.1f; 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Probability of a URL being "good enough" to revisit. 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const float kRevisitableURLProbability = 0.05f; 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Probability of a URL being the end of a redirect chain. 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const float kRedirectProbability = 0.05f; 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // A list of URLs that we sometimes revisit. 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<GURL> revisit_urls; 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Scoping value for page IDs (required by the history service). 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void* id_scope = reinterpret_cast<void*>(1); 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 126a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) scoped_refptr<base::RefCountedMemory> google_bitmap( 127a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) new base::RefCountedStaticMemory(kGoogleThumbnail, 128a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) sizeof(kGoogleThumbnail))); 129a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) scoped_refptr<base::RefCountedMemory> weewar_bitmap( 130a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) new base::RefCountedStaticMemory(kWeewarThumbnail, 131a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) sizeof(kWeewarThumbnail))); 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) printf("Inserting %d URLs...\n", batch_size); 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL previous_url; 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::PageTransition transition = content::PAGE_TRANSITION_TYPED; 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const int end_page_id = page_id + batch_size; 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history::TopSites* top_sites = profile->GetTopSites(); 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (; page_id < end_page_id; ++page_id) { 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Randomly decide whether this new URL simulates following a link or 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // whether it's a jump to a new URL. 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!previous_url.is_empty() && RandomFloat() < kFollowLinkProbability) { 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) transition = content::PAGE_TRANSITION_LINK; 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) previous_url = GURL(); 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) transition = content::PAGE_TRANSITION_TYPED; 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Pick a URL, either newly at random or from our list of previously 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // visited URLs. 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url; 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!revisit_urls.empty() && RandomFloat() < kRevisitLinkProbability) { 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Draw a URL from revisit_urls at random. 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) url = revisit_urls[RandomInt(0, static_cast<int>(revisit_urls.size()))]; 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) url = ConstructRandomURL(); 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Randomly construct a redirect chain. 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history::RedirectList redirects; 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (RandomFloat() < kRedirectProbability) { 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const int redir_count = RandomInt(1, 4); 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (int i = 0; i < redir_count; ++i) 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) redirects.push_back(ConstructRandomURL()); 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) redirects.push_back(url); 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add all of this information to the history service. 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history_service->AddPage(url, base::Time::Now(), 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) id_scope, page_id, 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) previous_url, redirects, 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) transition, history::SOURCE_BROWSED, true); 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailScore score(0.75, false, false); 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history_service->SetPageTitle(url, ConstructRandomTitle()); 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (types & TOP_SITES && top_sites) { 175a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) top_sites->SetPageThumbnailToJPEGBytes( 176a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) url, 177a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) (RandomInt(0, 2) == 0) ? google_bitmap.get() : weewar_bitmap.get(), 178a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) score); 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) previous_url = url; 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (revisit_urls.empty() || RandomFloat() < kRevisitableURLProbability) 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) revisit_urls.push_back(url); 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 188a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)} // namespace 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 190a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)bool GenerateProfile(GenerateProfileTypes types, 191a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) int url_count, 192a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) const base::FilePath& dst_dir) { 193a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (!base::CreateDirectory(dst_dir)) { 1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) PLOG(ERROR) << "Unable to create directory " << dst_dir.value().c_str(); 195a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return false; 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 198a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // We want this profile to be as deterministic as possible, so seed the 199a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // random number generator with the number of urls we're generating. 200a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) srand(static_cast<unsigned int>(url_count)); 201a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 202a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) printf("Creating profiles for testing...\n"); 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 204a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) TestingBrowserProcessInitializer initialize_browser_process; 205a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) base::MessageLoopForUI message_loop; 206a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); 207a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) content::TestBrowserThread db_thread(BrowserThread::DB, &message_loop); 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TestingProfile profile; 209bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch if (!profile.CreateHistoryService(false, false)) { 210bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch PLOG(ERROR) << "Creating history service failed"; 211bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch return false; 212bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch } 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (types & TOP_SITES) { 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile.CreateTopSites(); 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile.BlockUntilTopSitesLoaded(); 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The maximum number of URLs to insert into history in one batch. 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const int kBatchSize = 2000; 2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int page_id = 0; 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (page_id < url_count) { 2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const int batch_size = std::min(kBatchSize, url_count - page_id); 2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) InsertURLBatch(&profile, page_id, batch_size, types); 2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Run all pending messages to give TopSites a chance to catch up. 2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) message_loop.RunUntilIdle(); 2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) page_id += batch_size; 2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile.DestroyTopSites(); 2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile.DestroyHistoryService(); 2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) message_loop.RunUntilIdle(); 2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) base::FileEnumerator file_iterator(profile.GetPath(), false, 235868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) base::FileEnumerator::FILES); 2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath path = file_iterator.Next(); 2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (!path.empty()) { 2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath dst_file = dst_dir.Append(path.BaseName()); 2397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch base::DeleteFile(dst_file, false); 2407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!base::CopyFile(path, dst_file)) { 2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) PLOG(ERROR) << "Copying file failed"; 242a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return false; 2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) path = file_iterator.Next(); 2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 247a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) printf("Finished creating profiles for testing.\n"); 248a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 249a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) // Restore the random seed. 250a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) srand(static_cast<unsigned int>(Time::Now().ToInternalValue())); 251a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 252a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) return true; 2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 254