Home | History | Annotate | Download | only in base
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/test/base/testing_profile.h"
      6 
      7 #include "build/build_config.h"
      8 
      9 #include "base/base_paths.h"
     10 #include "base/command_line.h"
     11 #include "base/file_util.h"
     12 #include "base/message_loop/message_loop_proxy.h"
     13 #include "base/path_service.h"
     14 #include "base/prefs/testing_pref_store.h"
     15 #include "base/run_loop.h"
     16 #include "base/strings/string_number_conversions.h"
     17 #include "chrome/browser/autocomplete/autocomplete_classifier.h"
     18 #include "chrome/browser/bookmarks/bookmark_model.h"
     19 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
     20 #include "chrome/browser/browser_process.h"
     21 #include "chrome/browser/chrome_notification_types.h"
     22 #include "chrome/browser/content_settings/host_content_settings_map.h"
     23 #include "chrome/browser/extensions/extension_service.h"
     24 #include "chrome/browser/extensions/extension_special_storage_policy.h"
     25 #include "chrome/browser/extensions/extension_system.h"
     26 #include "chrome/browser/extensions/extension_system_factory.h"
     27 #include "chrome/browser/extensions/test_extension_system.h"
     28 #include "chrome/browser/favicon/favicon_service.h"
     29 #include "chrome/browser/favicon/favicon_service_factory.h"
     30 #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h"
     31 #include "chrome/browser/geolocation/chrome_geolocation_permission_context_factory.h"
     32 #include "chrome/browser/history/history_backend.h"
     33 #include "chrome/browser/history/history_db_task.h"
     34 #include "chrome/browser/history/history_service.h"
     35 #include "chrome/browser/history/history_service_factory.h"
     36 #include "chrome/browser/history/shortcuts_backend.h"
     37 #include "chrome/browser/history/shortcuts_backend_factory.h"
     38 #include "chrome/browser/history/top_sites.h"
     39 #include "chrome/browser/history/web_history_service_factory.h"
     40 #include "chrome/browser/net/pref_proxy_config_tracker.h"
     41 #include "chrome/browser/net/proxy_service_factory.h"
     42 #include "chrome/browser/notifications/desktop_notification_service.h"
     43 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
     44 #include "chrome/browser/policy/profile_policy_connector.h"
     45 #include "chrome/browser/policy/profile_policy_connector_factory.h"
     46 #include "chrome/browser/prefs/browser_prefs.h"
     47 #include "chrome/browser/prefs/pref_service_syncable.h"
     48 #include "chrome/browser/prerender/prerender_manager.h"
     49 #include "chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.h"
     50 #include "chrome/browser/profiles/profile_manager.h"
     51 #include "chrome/browser/profiles/storage_partition_descriptor.h"
     52 #include "chrome/browser/search_engines/template_url_fetcher_factory.h"
     53 #include "chrome/browser/webdata/web_data_service.h"
     54 #include "chrome/browser/webdata/web_data_service_factory.h"
     55 #include "chrome/common/chrome_constants.h"
     56 #include "chrome/common/chrome_switches.h"
     57 #include "chrome/common/pref_names.h"
     58 #include "chrome/common/url_constants.h"
     59 #include "chrome/test/base/history_index_restore_observer.h"
     60 #include "chrome/test/base/testing_pref_service_syncable.h"
     61 #include "chrome/test/base/ui_test_utils.h"
     62 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
     63 #include "components/policy/core/common/policy_service.h"
     64 #include "components/user_prefs/user_prefs.h"
     65 #include "content/public/browser/browser_thread.h"
     66 #include "content/public/browser/notification_service.h"
     67 #include "content/public/browser/render_process_host.h"
     68 #include "content/public/browser/storage_partition.h"
     69 #include "content/public/test/mock_resource_context.h"
     70 #include "content/public/test/test_utils.h"
     71 #include "extensions/common/constants.h"
     72 #include "net/cookies/cookie_monster.h"
     73 #include "net/url_request/url_request_context.h"
     74 #include "net/url_request/url_request_context_getter.h"
     75 #include "net/url_request/url_request_test_util.h"
     76 #include "testing/gmock/include/gmock/gmock.h"
     77 
     78 #if defined(ENABLE_CONFIGURATION_POLICY)
     79 #include "chrome/browser/policy/schema_registry_service.h"
     80 #include "chrome/browser/policy/schema_registry_service_factory.h"
     81 #include "components/policy/core/common/configuration_policy_provider.h"
     82 #include "components/policy/core/common/policy_service_impl.h"
     83 #include "components/policy/core/common/schema.h"
     84 #else
     85 #include "components/policy/core/common/policy_service_stub.h"
     86 #endif  // defined(ENABLE_CONFIGURATION_POLICY)
     87 
     88 #if defined(ENABLE_MANAGED_USERS)
     89 #include "chrome/browser/managed_mode/managed_user_settings_service.h"
     90 #include "chrome/browser/managed_mode/managed_user_settings_service_factory.h"
     91 #endif
     92 
     93 using base::Time;
     94 using content::BrowserThread;
     95 using content::DownloadManagerDelegate;
     96 using testing::NiceMock;
     97 using testing::Return;
     98 
     99 namespace {
    100 
    101 // Task used to make sure history has finished processing a request. Intended
    102 // for use with BlockUntilHistoryProcessesPendingRequests.
    103 
    104 class QuittingHistoryDBTask : public history::HistoryDBTask {
    105  public:
    106   QuittingHistoryDBTask() {}
    107 
    108   virtual bool RunOnDBThread(history::HistoryBackend* backend,
    109                              history::HistoryDatabase* db) OVERRIDE {
    110     return true;
    111   }
    112 
    113   virtual void DoneRunOnMainThread() OVERRIDE {
    114     base::MessageLoop::current()->Quit();
    115   }
    116 
    117  private:
    118   virtual ~QuittingHistoryDBTask() {}
    119 
    120   DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask);
    121 };
    122 
    123 class TestExtensionURLRequestContext : public net::URLRequestContext {
    124  public:
    125   TestExtensionURLRequestContext() {
    126     net::CookieMonster* cookie_monster = new net::CookieMonster(NULL, NULL);
    127     const char* schemes[] = {extensions::kExtensionScheme};
    128     cookie_monster->SetCookieableSchemes(schemes, 1);
    129     set_cookie_store(cookie_monster);
    130   }
    131 
    132   virtual ~TestExtensionURLRequestContext() {}
    133 };
    134 
    135 class TestExtensionURLRequestContextGetter
    136     : public net::URLRequestContextGetter {
    137  public:
    138   virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE {
    139     if (!context_.get())
    140       context_.reset(new TestExtensionURLRequestContext());
    141     return context_.get();
    142   }
    143   virtual scoped_refptr<base::SingleThreadTaskRunner>
    144       GetNetworkTaskRunner() const OVERRIDE {
    145     return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
    146   }
    147 
    148  protected:
    149   virtual ~TestExtensionURLRequestContextGetter() {}
    150 
    151  private:
    152   scoped_ptr<net::URLRequestContext> context_;
    153 };
    154 
    155 #if defined(ENABLE_NOTIFICATIONS)
    156 BrowserContextKeyedService* CreateTestDesktopNotificationService(
    157     content::BrowserContext* profile) {
    158   return new DesktopNotificationService(static_cast<Profile*>(profile), NULL);
    159 }
    160 #endif
    161 
    162 }  // namespace
    163 
    164 // static
    165 #if defined(OS_CHROMEOS)
    166 // Must be kept in sync with
    167 // ChromeBrowserMainPartsChromeos::PreEarlyInitialization.
    168 const char TestingProfile::kTestUserProfileDir[] = "test-user";
    169 #else
    170 const char TestingProfile::kTestUserProfileDir[] = "Default";
    171 #endif
    172 
    173 TestingProfile::TestingProfile()
    174     : start_time_(Time::Now()),
    175       testing_prefs_(NULL),
    176       incognito_(false),
    177       force_incognito_(false),
    178       original_profile_(NULL),
    179       guest_session_(false),
    180       last_session_exited_cleanly_(true),
    181       browser_context_dependency_manager_(
    182           BrowserContextDependencyManager::GetInstance()),
    183       resource_context_(NULL),
    184       delegate_(NULL) {
    185   CreateTempProfileDir();
    186   profile_path_ = temp_dir_.path();
    187 
    188   Init();
    189   FinishInit();
    190 }
    191 
    192 TestingProfile::TestingProfile(const base::FilePath& path)
    193     : start_time_(Time::Now()),
    194       testing_prefs_(NULL),
    195       incognito_(false),
    196       force_incognito_(false),
    197       original_profile_(NULL),
    198       guest_session_(false),
    199       last_session_exited_cleanly_(true),
    200       profile_path_(path),
    201       browser_context_dependency_manager_(
    202           BrowserContextDependencyManager::GetInstance()),
    203       resource_context_(NULL),
    204       delegate_(NULL) {
    205   Init();
    206   FinishInit();
    207 }
    208 
    209 TestingProfile::TestingProfile(const base::FilePath& path,
    210                                Delegate* delegate)
    211     : start_time_(Time::Now()),
    212       testing_prefs_(NULL),
    213       incognito_(false),
    214       force_incognito_(false),
    215       original_profile_(NULL),
    216       guest_session_(false),
    217       last_session_exited_cleanly_(true),
    218       profile_path_(path),
    219       browser_context_dependency_manager_(
    220           BrowserContextDependencyManager::GetInstance()),
    221       resource_context_(NULL),
    222       delegate_(delegate) {
    223   Init();
    224   if (delegate_) {
    225     base::MessageLoop::current()->PostTask(
    226         FROM_HERE,
    227         base::Bind(&TestingProfile::FinishInit, base::Unretained(this)));
    228   } else {
    229     FinishInit();
    230   }
    231 }
    232 
    233 TestingProfile::TestingProfile(
    234     const base::FilePath& path,
    235     Delegate* delegate,
    236     scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy,
    237     scoped_ptr<PrefServiceSyncable> prefs,
    238     bool incognito,
    239     bool guest_session,
    240     const std::string& managed_user_id,
    241     scoped_ptr<policy::PolicyService> policy_service,
    242     const TestingFactories& factories)
    243     : start_time_(Time::Now()),
    244       prefs_(prefs.release()),
    245       testing_prefs_(NULL),
    246       incognito_(incognito),
    247       force_incognito_(false),
    248       original_profile_(NULL),
    249       guest_session_(guest_session),
    250       managed_user_id_(managed_user_id),
    251       last_session_exited_cleanly_(true),
    252       extension_special_storage_policy_(extension_policy),
    253       profile_path_(path),
    254       browser_context_dependency_manager_(
    255           BrowserContextDependencyManager::GetInstance()),
    256       resource_context_(NULL),
    257       delegate_(delegate),
    258       policy_service_(policy_service.release()) {
    259 
    260   // If no profile path was supplied, create one.
    261   if (profile_path_.empty()) {
    262     CreateTempProfileDir();
    263     profile_path_ = temp_dir_.path();
    264   }
    265 
    266   // Set any testing factories prior to initializing the services.
    267   for (TestingFactories::const_iterator it = factories.begin();
    268        it != factories.end(); ++it) {
    269     it->first->SetTestingFactory(this, it->second);
    270   }
    271 
    272   Init();
    273   // If caller supplied a delegate, delay the FinishInit invocation until other
    274   // tasks have run.
    275   // TODO(atwilson): See if this is still required once we convert the current
    276   // users of the constructor that takes a Delegate* param.
    277   if (delegate_) {
    278     base::MessageLoop::current()->PostTask(
    279         FROM_HERE,
    280         base::Bind(&TestingProfile::FinishInit, base::Unretained(this)));
    281   } else {
    282     FinishInit();
    283   }
    284 }
    285 
    286 void TestingProfile::CreateTempProfileDir() {
    287   if (!temp_dir_.CreateUniqueTempDir()) {
    288     LOG(ERROR) << "Failed to create unique temporary directory.";
    289 
    290     // Fallback logic in case we fail to create unique temporary directory.
    291     base::FilePath system_tmp_dir;
    292     bool success = PathService::Get(base::DIR_TEMP, &system_tmp_dir);
    293 
    294     // We're severly screwed if we can't get the system temporary
    295     // directory. Die now to avoid writing to the filesystem root
    296     // or other bad places.
    297     CHECK(success);
    298 
    299     base::FilePath fallback_dir(
    300         system_tmp_dir.AppendASCII("TestingProfilePath"));
    301     base::DeleteFile(fallback_dir, true);
    302     base::CreateDirectory(fallback_dir);
    303     if (!temp_dir_.Set(fallback_dir)) {
    304       // That shouldn't happen, but if it does, try to recover.
    305       LOG(ERROR) << "Failed to use a fallback temporary directory.";
    306 
    307       // We're screwed if this fails, see CHECK above.
    308       CHECK(temp_dir_.Set(system_tmp_dir));
    309     }
    310   }
    311 }
    312 
    313 void TestingProfile::Init() {
    314   // If threads have been initialized, we should be on the UI thread.
    315   DCHECK(!content::BrowserThread::IsThreadInitialized(
    316              content::BrowserThread::UI) ||
    317          content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
    318 
    319   // Normally this would happen during browser startup, but for tests
    320   // we need to trigger creation of Profile-related services.
    321   ChromeBrowserMainExtraPartsProfiles::
    322       EnsureBrowserContextKeyedServiceFactoriesBuilt();
    323 
    324   if (prefs_.get())
    325     user_prefs::UserPrefs::Set(this, prefs_.get());
    326   else
    327     CreateTestingPrefService();
    328 
    329   if (!base::PathExists(profile_path_))
    330     base::CreateDirectory(profile_path_);
    331 
    332   // TODO(joaodasilva): remove this once this PKS isn't created in ProfileImpl
    333   // anymore, after converting the PrefService to a PKS. Until then it must
    334   // be associated with a TestingProfile too.
    335   if (!IsOffTheRecord())
    336     CreateProfilePolicyConnector();
    337 
    338   extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory(
    339       this, extensions::TestExtensionSystem::Build);
    340 
    341   // If no original profile was specified for this profile: register preferences
    342   // even if this is an incognito profile - this allows tests to create a
    343   // standalone incognito profile while still having prefs registered.
    344   if (!IsOffTheRecord() || !original_profile_) {
    345     user_prefs::PrefRegistrySyncable* pref_registry =
    346         static_cast<user_prefs::PrefRegistrySyncable*>(
    347             prefs_->DeprecatedGetPrefRegistry());
    348     browser_context_dependency_manager_->
    349         RegisterProfilePrefsForServices(this, pref_registry);
    350   }
    351 
    352   browser_context_dependency_manager_->CreateBrowserContextServicesForTest(
    353       this);
    354 
    355 #if defined(ENABLE_NOTIFICATIONS)
    356   // Install profile keyed service factory hooks for dummy/test services
    357   DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory(
    358       this, CreateTestDesktopNotificationService);
    359 #endif
    360 
    361 #if defined(ENABLE_MANAGED_USERS)
    362   ManagedUserSettingsService* settings_service =
    363       ManagedUserSettingsServiceFactory::GetForProfile(this);
    364   TestingPrefStore* store = new TestingPrefStore();
    365   settings_service->Init(store);
    366   store->SetInitializationCompleted();
    367 #endif
    368 
    369   profile_name_ = "testing_profile";
    370 }
    371 
    372 void TestingProfile::FinishInit() {
    373   DCHECK(content::NotificationService::current());
    374   content::NotificationService::current()->Notify(
    375       chrome::NOTIFICATION_PROFILE_CREATED,
    376       content::Source<Profile>(static_cast<Profile*>(this)),
    377       content::NotificationService::NoDetails());
    378 
    379   ProfileManager* profile_manager = g_browser_process->profile_manager();
    380   if (profile_manager)
    381     profile_manager->InitProfileUserPrefs(this);
    382 
    383   if (delegate_)
    384     delegate_->OnProfileCreated(this, true, false);
    385 }
    386 
    387 TestingProfile::~TestingProfile() {
    388   // Revert to non-incognito mode before shutdown.
    389   force_incognito_ = false;
    390 
    391   // Any objects holding live URLFetchers should be deleted before teardown.
    392   TemplateURLFetcherFactory::ShutdownForProfile(this);
    393 
    394   MaybeSendDestroyedNotification();
    395 
    396   browser_context_dependency_manager_->DestroyBrowserContextServices(this);
    397 
    398   if (host_content_settings_map_.get())
    399     host_content_settings_map_->ShutdownOnUIThread();
    400 
    401   DestroyTopSites();
    402 
    403   if (pref_proxy_config_tracker_.get())
    404     pref_proxy_config_tracker_->DetachFromPrefService();
    405   // Failing a post == leaks == heapcheck failure. Make that an immediate test
    406   // failure.
    407   if (resource_context_) {
    408     CHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
    409                                     resource_context_));
    410     resource_context_ = NULL;
    411     content::RunAllPendingInMessageLoop(BrowserThread::IO);
    412   }
    413 }
    414 
    415 static BrowserContextKeyedService* BuildFaviconService(
    416     content::BrowserContext* profile) {
    417   return new FaviconService(static_cast<Profile*>(profile));
    418 }
    419 
    420 void TestingProfile::CreateFaviconService() {
    421   // It is up to the caller to create the history service if one is needed.
    422   FaviconServiceFactory::GetInstance()->SetTestingFactory(
    423       this, BuildFaviconService);
    424 }
    425 
    426 static BrowserContextKeyedService* BuildHistoryService(
    427     content::BrowserContext* profile) {
    428   return new HistoryService(static_cast<Profile*>(profile));
    429 }
    430 
    431 bool TestingProfile::CreateHistoryService(bool delete_file, bool no_db) {
    432   DestroyHistoryService();
    433   if (delete_file) {
    434     base::FilePath path = GetPath();
    435     path = path.Append(chrome::kHistoryFilename);
    436     if (!base::DeleteFile(path, false) || base::PathExists(path))
    437       return false;
    438   }
    439   // This will create and init the history service.
    440   HistoryService* history_service = static_cast<HistoryService*>(
    441       HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(
    442           this, BuildHistoryService));
    443   if (!history_service->Init(this->GetPath(),
    444                              BookmarkModelFactory::GetForProfile(this),
    445                              no_db)) {
    446     HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(this, NULL);
    447   }
    448   // Disable WebHistoryService by default, since it makes network requests.
    449   WebHistoryServiceFactory::GetInstance()->SetTestingFactory(this, NULL);
    450   return true;
    451 }
    452 
    453 void TestingProfile::DestroyHistoryService() {
    454   HistoryService* history_service =
    455       HistoryServiceFactory::GetForProfileWithoutCreating(this);
    456   if (!history_service)
    457     return;
    458 
    459   history_service->NotifyRenderProcessHostDestruction(0);
    460   history_service->SetOnBackendDestroyTask(base::MessageLoop::QuitClosure());
    461   history_service->Cleanup();
    462   HistoryServiceFactory::ShutdownForProfile(this);
    463 
    464   // Wait for the backend class to terminate before deleting the files and
    465   // moving to the next test. Note: if this never terminates, somebody is
    466   // probably leaking a reference to the history backend, so it never calls
    467   // our destroy task.
    468   base::MessageLoop::current()->Run();
    469 
    470   // Make sure we don't have any event pending that could disrupt the next
    471   // test.
    472   base::MessageLoop::current()->PostTask(FROM_HERE,
    473                                          base::MessageLoop::QuitClosure());
    474   base::MessageLoop::current()->Run();
    475 }
    476 
    477 void TestingProfile::CreateTopSites() {
    478   DestroyTopSites();
    479   top_sites_ = history::TopSites::Create(
    480       this, GetPath().Append(chrome::kTopSitesFilename));
    481 }
    482 
    483 void TestingProfile::DestroyTopSites() {
    484   if (top_sites_.get()) {
    485     top_sites_->Shutdown();
    486     top_sites_ = NULL;
    487     // TopSitesImpl::Shutdown schedules some tasks (from TopSitesBackend) that
    488     // need to be run to properly shutdown. Run all pending tasks now. This is
    489     // normally handled by browser_process shutdown.
    490     if (base::MessageLoop::current())
    491       base::MessageLoop::current()->RunUntilIdle();
    492   }
    493 }
    494 
    495 static BrowserContextKeyedService* BuildBookmarkModel(
    496     content::BrowserContext* context) {
    497   Profile* profile = static_cast<Profile*>(context);
    498   BookmarkModel* bookmark_model = new BookmarkModel(profile);
    499   bookmark_model->Load(profile->GetIOTaskRunner());
    500   return bookmark_model;
    501 }
    502 
    503 void TestingProfile::CreateBookmarkModel(bool delete_file) {
    504   if (delete_file) {
    505     base::FilePath path = GetPath().Append(chrome::kBookmarksFileName);
    506     base::DeleteFile(path, false);
    507   }
    508   // This will create a bookmark model.
    509   BookmarkModel* bookmark_service = static_cast<BookmarkModel*>(
    510       BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse(
    511           this, BuildBookmarkModel));
    512 
    513   HistoryService* history_service =
    514       HistoryServiceFactory::GetForProfileWithoutCreating(this);
    515   if (history_service) {
    516     history_service->history_backend_->bookmark_service_ = bookmark_service;
    517     history_service->history_backend_->expirer_.bookmark_service_ =
    518         bookmark_service;
    519   }
    520 }
    521 
    522 static BrowserContextKeyedService* BuildWebDataService(
    523     content::BrowserContext* profile) {
    524   return new WebDataServiceWrapper(static_cast<Profile*>(profile));
    525 }
    526 
    527 void TestingProfile::CreateWebDataService() {
    528   WebDataServiceFactory::GetInstance()->SetTestingFactory(
    529       this, BuildWebDataService);
    530 }
    531 
    532 void TestingProfile::BlockUntilHistoryIndexIsRefreshed() {
    533   // Only get the history service if it actually exists since the caller of the
    534   // test should explicitly call CreateHistoryService to build it.
    535   HistoryService* history_service =
    536       HistoryServiceFactory::GetForProfileWithoutCreating(this);
    537   DCHECK(history_service);
    538   history::InMemoryURLIndex* index = history_service->InMemoryIndex();
    539   if (!index || index->restored())
    540     return;
    541   base::RunLoop run_loop;
    542   HistoryIndexRestoreObserver observer(
    543       content::GetQuitTaskForRunLoop(&run_loop));
    544   index->set_restore_cache_observer(&observer);
    545   run_loop.Run();
    546   index->set_restore_cache_observer(NULL);
    547   DCHECK(index->restored());
    548 }
    549 
    550 // TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded?
    551 void TestingProfile::BlockUntilTopSitesLoaded() {
    552   content::WindowedNotificationObserver top_sites_loaded_observer(
    553       chrome::NOTIFICATION_TOP_SITES_LOADED,
    554       content::NotificationService::AllSources());
    555   top_sites_loaded_observer.Wait();
    556 }
    557 
    558 base::FilePath TestingProfile::GetPath() const {
    559   return profile_path_;
    560 }
    561 
    562 scoped_refptr<base::SequencedTaskRunner> TestingProfile::GetIOTaskRunner() {
    563   return base::MessageLoop::current()->message_loop_proxy();
    564 }
    565 
    566 TestingPrefServiceSyncable* TestingProfile::GetTestingPrefService() {
    567   if (!prefs_.get())
    568     CreateTestingPrefService();
    569   DCHECK(testing_prefs_);
    570   return testing_prefs_;
    571 }
    572 
    573 TestingProfile* TestingProfile::AsTestingProfile() {
    574   return this;
    575 }
    576 
    577 std::string TestingProfile::GetProfileName() {
    578   return profile_name_;
    579 }
    580 
    581 bool TestingProfile::IsOffTheRecord() const {
    582   return force_incognito_ || incognito_;
    583 }
    584 
    585 void TestingProfile::SetOffTheRecordProfile(scoped_ptr<Profile> profile) {
    586   DCHECK(!IsOffTheRecord());
    587   incognito_profile_ = profile.Pass();
    588 }
    589 
    590 void TestingProfile::SetOriginalProfile(Profile* profile) {
    591   DCHECK(IsOffTheRecord());
    592   original_profile_ = profile;
    593 }
    594 
    595 Profile* TestingProfile::GetOffTheRecordProfile() {
    596   if (IsOffTheRecord())
    597     return this;
    598   return incognito_profile_.get();
    599 }
    600 
    601 bool TestingProfile::HasOffTheRecordProfile() {
    602   return incognito_profile_.get() != NULL;
    603 }
    604 
    605 Profile* TestingProfile::GetOriginalProfile() {
    606   if (original_profile_)
    607     return original_profile_;
    608   return this;
    609 }
    610 
    611 bool TestingProfile::IsManaged() {
    612   return !managed_user_id_.empty();
    613 }
    614 
    615 ExtensionService* TestingProfile::GetExtensionService() {
    616   return extensions::ExtensionSystem::Get(this)->extension_service();
    617 }
    618 
    619 void TestingProfile::SetExtensionSpecialStoragePolicy(
    620     ExtensionSpecialStoragePolicy* extension_special_storage_policy) {
    621   extension_special_storage_policy_ = extension_special_storage_policy;
    622 }
    623 
    624 ExtensionSpecialStoragePolicy*
    625 TestingProfile::GetExtensionSpecialStoragePolicy() {
    626   if (!extension_special_storage_policy_.get())
    627     extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL);
    628   return extension_special_storage_policy_.get();
    629 }
    630 
    631 net::CookieMonster* TestingProfile::GetCookieMonster() {
    632   if (!GetRequestContext())
    633     return NULL;
    634   return GetRequestContext()->GetURLRequestContext()->cookie_store()->
    635       GetCookieMonster();
    636 }
    637 
    638 void TestingProfile::CreateTestingPrefService() {
    639   DCHECK(!prefs_.get());
    640   testing_prefs_ = new TestingPrefServiceSyncable();
    641   prefs_.reset(testing_prefs_);
    642   user_prefs::UserPrefs::Set(this, prefs_.get());
    643   chrome::RegisterUserProfilePrefs(testing_prefs_->registry());
    644 }
    645 
    646 void TestingProfile::CreateProfilePolicyConnector() {
    647 #if defined(ENABLE_CONFIGURATION_POLICY)
    648   schema_registry_service_ =
    649       policy::SchemaRegistryServiceFactory::CreateForContext(
    650           this, policy::Schema(), NULL);
    651   CHECK_EQ(schema_registry_service_.get(),
    652            policy::SchemaRegistryServiceFactory::GetForContext(this));
    653 #endif  // defined(ENABLE_CONFIGURATION_POLICY)
    654 
    655 if (!policy_service_) {
    656 #if defined(ENABLE_CONFIGURATION_POLICY)
    657     std::vector<policy::ConfigurationPolicyProvider*> providers;
    658     policy_service_.reset(new policy::PolicyServiceImpl(providers));
    659 #else
    660     policy_service_.reset(new policy::PolicyServiceStub());
    661 #endif
    662   }
    663   profile_policy_connector_.reset(new policy::ProfilePolicyConnector());
    664   profile_policy_connector_->InitForTesting(policy_service_.Pass());
    665   policy::ProfilePolicyConnectorFactory::GetInstance()->SetServiceForTesting(
    666       this, profile_policy_connector_.get());
    667   CHECK_EQ(profile_policy_connector_.get(),
    668            policy::ProfilePolicyConnectorFactory::GetForProfile(this));
    669 }
    670 
    671 PrefService* TestingProfile::GetPrefs() {
    672   if (!prefs_.get()) {
    673     CreateTestingPrefService();
    674   }
    675   return prefs_.get();
    676 }
    677 
    678 history::TopSites* TestingProfile::GetTopSites() {
    679   return top_sites_.get();
    680 }
    681 
    682 history::TopSites* TestingProfile::GetTopSitesWithoutCreating() {
    683   return top_sites_.get();
    684 }
    685 
    686 DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() {
    687   return NULL;
    688 }
    689 
    690 net::URLRequestContextGetter* TestingProfile::GetRequestContext() {
    691   return GetDefaultStoragePartition(this)->GetURLRequestContext();
    692 }
    693 
    694 net::URLRequestContextGetter* TestingProfile::CreateRequestContext(
    695     content::ProtocolHandlerMap* protocol_handlers) {
    696   return new net::TestURLRequestContextGetter(
    697             BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
    698 }
    699 
    700 net::URLRequestContextGetter* TestingProfile::GetRequestContextForRenderProcess(
    701     int renderer_child_id) {
    702   content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
    703       renderer_child_id);
    704   return rph->GetStoragePartition()->GetURLRequestContext();
    705 }
    706 
    707 net::URLRequestContextGetter* TestingProfile::GetMediaRequestContext() {
    708   return NULL;
    709 }
    710 
    711 net::URLRequestContextGetter*
    712 TestingProfile::GetMediaRequestContextForRenderProcess(
    713     int renderer_child_id) {
    714   return NULL;
    715 }
    716 
    717 net::URLRequestContextGetter*
    718 TestingProfile::GetMediaRequestContextForStoragePartition(
    719     const base::FilePath& partition_path,
    720     bool in_memory) {
    721   return NULL;
    722 }
    723 
    724 void TestingProfile::RequestMIDISysExPermission(
    725       int render_process_id,
    726       int render_view_id,
    727       int bridge_id,
    728       const GURL& requesting_frame,
    729       const MIDISysExPermissionCallback& callback) {
    730   // Always reject requests for testing.
    731   callback.Run(false);
    732 }
    733 
    734 void TestingProfile::CancelMIDISysExPermissionRequest(
    735     int render_process_id,
    736     int render_view_id,
    737     int bridge_id,
    738     const GURL& requesting_frame) {
    739 }
    740 
    741 net::URLRequestContextGetter* TestingProfile::GetRequestContextForExtensions() {
    742   if (!extensions_request_context_.get())
    743     extensions_request_context_ = new TestExtensionURLRequestContextGetter();
    744   return extensions_request_context_.get();
    745 }
    746 
    747 net::SSLConfigService* TestingProfile::GetSSLConfigService() {
    748   if (!GetRequestContext())
    749     return NULL;
    750   return GetRequestContext()->GetURLRequestContext()->ssl_config_service();
    751 }
    752 
    753 net::URLRequestContextGetter*
    754 TestingProfile::CreateRequestContextForStoragePartition(
    755     const base::FilePath& partition_path,
    756     bool in_memory,
    757     content::ProtocolHandlerMap* protocol_handlers) {
    758   // We don't test storage partitions here yet, so returning the same dummy
    759   // context is sufficient for now.
    760   return GetRequestContext();
    761 }
    762 
    763 content::ResourceContext* TestingProfile::GetResourceContext() {
    764   if (!resource_context_)
    765     resource_context_ = new content::MockResourceContext();
    766   return resource_context_;
    767 }
    768 
    769 HostContentSettingsMap* TestingProfile::GetHostContentSettingsMap() {
    770   if (!host_content_settings_map_.get()) {
    771     host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), false);
    772 #if defined(ENABLE_EXTENSIONS)
    773     ExtensionService* extension_service = GetExtensionService();
    774     if (extension_service)
    775       host_content_settings_map_->RegisterExtensionService(extension_service);
    776 #endif
    777   }
    778   return host_content_settings_map_.get();
    779 }
    780 
    781 content::GeolocationPermissionContext*
    782 TestingProfile::GetGeolocationPermissionContext() {
    783   return ChromeGeolocationPermissionContextFactory::GetForProfile(this);
    784 }
    785 
    786 std::wstring TestingProfile::GetName() {
    787   return std::wstring();
    788 }
    789 
    790 std::wstring TestingProfile::GetID() {
    791   return id_;
    792 }
    793 
    794 void TestingProfile::SetID(const std::wstring& id) {
    795   id_ = id;
    796 }
    797 
    798 bool TestingProfile::IsSameProfile(Profile *p) {
    799   return this == p;
    800 }
    801 
    802 base::Time TestingProfile::GetStartTime() const {
    803   return start_time_;
    804 }
    805 
    806 base::FilePath TestingProfile::last_selected_directory() {
    807   return last_selected_directory_;
    808 }
    809 
    810 void TestingProfile::set_last_selected_directory(const base::FilePath& path) {
    811   last_selected_directory_ = path;
    812 }
    813 
    814 PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() {
    815   if (!pref_proxy_config_tracker_.get()) {
    816     // TestingProfile is used in unit tests, where local state is not available.
    817     pref_proxy_config_tracker_.reset(
    818         ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(GetPrefs(),
    819                                                                    NULL));
    820   }
    821   return pref_proxy_config_tracker_.get();
    822 }
    823 
    824 void TestingProfile::BlockUntilHistoryProcessesPendingRequests() {
    825   HistoryService* history_service =
    826       HistoryServiceFactory::GetForProfile(this, Profile::EXPLICIT_ACCESS);
    827   DCHECK(history_service);
    828   DCHECK(base::MessageLoop::current());
    829 
    830   CancelableRequestConsumer consumer;
    831   history_service->ScheduleDBTask(new QuittingHistoryDBTask(), &consumer);
    832   base::MessageLoop::current()->Run();
    833 }
    834 
    835 chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() {
    836   return NULL;
    837 }
    838 
    839 void TestingProfile::ClearNetworkingHistorySince(
    840     base::Time time,
    841     const base::Closure& completion) {
    842   if (!completion.is_null()) {
    843     BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
    844   }
    845 }
    846 
    847 GURL TestingProfile::GetHomePage() {
    848   return GURL(chrome::kChromeUINewTabURL);
    849 }
    850 
    851 PrefService* TestingProfile::GetOffTheRecordPrefs() {
    852   return NULL;
    853 }
    854 
    855 quota::SpecialStoragePolicy* TestingProfile::GetSpecialStoragePolicy() {
    856   return GetExtensionSpecialStoragePolicy();
    857 }
    858 
    859 bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) {
    860   return true;
    861 }
    862 
    863 bool TestingProfile::IsGuestSession() const {
    864   return guest_session_;
    865 }
    866 
    867 Profile::ExitType TestingProfile::GetLastSessionExitType() {
    868   return last_session_exited_cleanly_ ? EXIT_NORMAL : EXIT_CRASHED;
    869 }
    870 
    871 TestingProfile::Builder::Builder()
    872     : build_called_(false),
    873       delegate_(NULL),
    874       incognito_(false),
    875       guest_session_(false) {
    876 }
    877 
    878 TestingProfile::Builder::~Builder() {
    879 }
    880 
    881 void TestingProfile::Builder::SetPath(const base::FilePath& path) {
    882   path_ = path;
    883 }
    884 
    885 void TestingProfile::Builder::SetDelegate(Delegate* delegate) {
    886   delegate_ = delegate;
    887 }
    888 
    889 void TestingProfile::Builder::SetExtensionSpecialStoragePolicy(
    890     scoped_refptr<ExtensionSpecialStoragePolicy> policy) {
    891   extension_policy_ = policy;
    892 }
    893 
    894 void TestingProfile::Builder::SetPrefService(
    895     scoped_ptr<PrefServiceSyncable> prefs) {
    896   pref_service_ = prefs.Pass();
    897 }
    898 
    899 void TestingProfile::Builder::SetIncognito() {
    900   incognito_ = true;
    901 }
    902 
    903 void TestingProfile::Builder::SetGuestSession() {
    904   guest_session_ = true;
    905 }
    906 
    907 void TestingProfile::Builder::SetManagedUserId(
    908     const std::string& managed_user_id) {
    909   managed_user_id_ = managed_user_id;
    910 }
    911 
    912 void TestingProfile::Builder::SetPolicyService(
    913     scoped_ptr<policy::PolicyService> policy_service) {
    914   policy_service_ = policy_service.Pass();
    915 }
    916 
    917 void TestingProfile::Builder::AddTestingFactory(
    918     BrowserContextKeyedServiceFactory* service_factory,
    919     BrowserContextKeyedServiceFactory::TestingFactoryFunction callback) {
    920   testing_factories_.push_back(std::make_pair(service_factory, callback));
    921 }
    922 
    923 scoped_ptr<TestingProfile> TestingProfile::Builder::Build() {
    924   DCHECK(!build_called_);
    925   build_called_ = true;
    926 
    927   return scoped_ptr<TestingProfile>(new TestingProfile(
    928       path_,
    929       delegate_,
    930       extension_policy_,
    931       pref_service_.Pass(),
    932       incognito_,
    933       guest_session_,
    934       managed_user_id_,
    935       policy_service_.Pass(),
    936       testing_factories_));
    937 }
    938