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 // An implementation of BrowserProcess for unit tests that fails for most 6 // services. By preventing creation of services, we reduce dependencies and 7 // keep the profile clean. Clients of this class must handle the NULL return 8 // value, however. 9 10 #ifndef CHROME_TEST_BASE_TESTING_BROWSER_PROCESS_H_ 11 #define CHROME_TEST_BASE_TESTING_BROWSER_PROCESS_H_ 12 13 #include <string> 14 15 #include "base/memory/ref_counted.h" 16 #include "base/memory/scoped_ptr.h" 17 #include "build/build_config.h" 18 #include "chrome/browser/browser_process.h" 19 #include "chrome/browser/browser_process_platform_part.h" 20 21 class BackgroundModeManager; 22 class CRLSetFetcher; 23 class IOThread; 24 class MHTMLGenerationManager; 25 class NotificationUIManager; 26 class PrefService; 27 class WatchDogThread; 28 29 namespace content { 30 class NotificationService; 31 } 32 33 namespace extensions { 34 class ExtensionsBrowserClient; 35 } 36 37 namespace policy { 38 class BrowserPolicyConnector; 39 class PolicyService; 40 } 41 42 namespace prerender { 43 class PrerenderTracker; 44 } 45 46 class TestingBrowserProcess : public BrowserProcess { 47 public: 48 // Initializes |g_browser_process| with a new TestingBrowserProcess. 49 static void CreateInstance(); 50 51 // Cleanly destroys |g_browser_process|, which has special deletion semantics. 52 static void DeleteInstance(); 53 54 // Convenience method to get g_browser_process as a TestingBrowserProcess*. 55 static TestingBrowserProcess* GetGlobal(); 56 57 virtual void ResourceDispatcherHostCreated() OVERRIDE; 58 virtual void EndSession() OVERRIDE; 59 virtual MetricsService* metrics_service() OVERRIDE; 60 virtual IOThread* io_thread() OVERRIDE; 61 virtual WatchDogThread* watchdog_thread() OVERRIDE; 62 virtual ProfileManager* profile_manager() OVERRIDE; 63 virtual PrefService* local_state() OVERRIDE; 64 virtual chrome_variations::VariationsService* variations_service() OVERRIDE; 65 virtual policy::BrowserPolicyConnector* browser_policy_connector() OVERRIDE; 66 virtual policy::PolicyService* policy_service() OVERRIDE; 67 virtual IconManager* icon_manager() OVERRIDE; 68 virtual GLStringManager* gl_string_manager() OVERRIDE; 69 virtual GpuModeManager* gpu_mode_manager() OVERRIDE; 70 virtual RenderWidgetSnapshotTaker* GetRenderWidgetSnapshotTaker() OVERRIDE; 71 virtual BackgroundModeManager* background_mode_manager() OVERRIDE; 72 virtual void set_background_mode_manager_for_test( 73 scoped_ptr<BackgroundModeManager> manager) OVERRIDE; 74 virtual StatusTray* status_tray() OVERRIDE; 75 virtual SafeBrowsingService* safe_browsing_service() OVERRIDE; 76 virtual safe_browsing::ClientSideDetectionService* 77 safe_browsing_detection_service() OVERRIDE; 78 virtual net::URLRequestContextGetter* system_request_context() OVERRIDE; 79 virtual BrowserProcessPlatformPart* platform_part() OVERRIDE; 80 81 virtual extensions::EventRouterForwarder* 82 extension_event_router_forwarder() OVERRIDE; 83 virtual NotificationUIManager* notification_ui_manager() OVERRIDE; 84 virtual message_center::MessageCenter* message_center() OVERRIDE; 85 virtual IntranetRedirectDetector* intranet_redirect_detector() OVERRIDE; 86 virtual AutomationProviderList* GetAutomationProviderList() OVERRIDE; 87 virtual void CreateDevToolsHttpProtocolHandler( 88 chrome::HostDesktopType host_desktop_type, 89 const std::string& ip, 90 int port, 91 const std::string& frontend_url) OVERRIDE; 92 virtual unsigned int AddRefModule() OVERRIDE; 93 virtual unsigned int ReleaseModule() OVERRIDE; 94 virtual bool IsShuttingDown() OVERRIDE; 95 virtual printing::PrintJobManager* print_job_manager() OVERRIDE; 96 virtual printing::PrintPreviewDialogController* 97 print_preview_dialog_controller() OVERRIDE; 98 virtual printing::BackgroundPrintingManager* 99 background_printing_manager() OVERRIDE; 100 virtual const std::string& GetApplicationLocale() OVERRIDE; 101 virtual void SetApplicationLocale(const std::string& app_locale) OVERRIDE; 102 virtual DownloadStatusUpdater* download_status_updater() OVERRIDE; 103 virtual DownloadRequestLimiter* download_request_limiter() OVERRIDE; 104 105 #if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS) 106 virtual void StartAutoupdateTimer() OVERRIDE {} 107 #endif 108 109 virtual ChromeNetLog* net_log() OVERRIDE; 110 virtual prerender::PrerenderTracker* prerender_tracker() OVERRIDE; 111 virtual ComponentUpdateService* component_updater() OVERRIDE; 112 virtual CRLSetFetcher* crl_set_fetcher() OVERRIDE; 113 virtual PnaclComponentInstaller* pnacl_component_installer() OVERRIDE; 114 virtual BookmarkPromptController* bookmark_prompt_controller() OVERRIDE; 115 virtual StorageMonitor* storage_monitor() OVERRIDE; 116 virtual MediaFileSystemRegistry* media_file_system_registry() OVERRIDE; 117 virtual bool created_local_state() const OVERRIDE; 118 119 #if defined(ENABLE_WEBRTC) 120 virtual WebRtcLogUploader* webrtc_log_uploader() OVERRIDE; 121 #endif 122 123 // Set the local state for tests. Consumer is responsible for cleaning it up 124 // afterwards (using ScopedTestingLocalState, for example). 125 void SetLocalState(PrefService* local_state); 126 void SetProfileManager(ProfileManager* profile_manager); 127 void SetIOThread(IOThread* io_thread); 128 void SetBrowserPolicyConnector(policy::BrowserPolicyConnector* connector); 129 void SetSafeBrowsingService(SafeBrowsingService* sb_service); 130 void SetBookmarkPromptController(BookmarkPromptController* controller); 131 void SetSystemRequestContext(net::URLRequestContextGetter* context_getter); 132 void SetStorageMonitor(scoped_ptr<StorageMonitor> storage_monitor); 133 134 private: 135 // See CreateInstance() and DestoryInstance() above. 136 TestingBrowserProcess(); 137 virtual ~TestingBrowserProcess(); 138 139 scoped_ptr<content::NotificationService> notification_service_; 140 unsigned int module_ref_count_; 141 std::string app_locale_; 142 143 // TODO(ios): Add back members as more code is compiled. 144 #if !defined(OS_IOS) 145 #if defined(ENABLE_CONFIGURATION_POLICY) 146 scoped_ptr<policy::BrowserPolicyConnector> browser_policy_connector_; 147 #else 148 scoped_ptr<policy::PolicyService> policy_service_; 149 #endif 150 scoped_ptr<ProfileManager> profile_manager_; 151 scoped_ptr<NotificationUIManager> notification_ui_manager_; 152 153 #if defined(ENABLE_FULL_PRINTING) 154 scoped_ptr<printing::PrintJobManager> print_job_manager_; 155 scoped_ptr<printing::BackgroundPrintingManager> background_printing_manager_; 156 scoped_refptr<printing::PrintPreviewDialogController> 157 print_preview_dialog_controller_; 158 #endif 159 160 scoped_ptr<prerender::PrerenderTracker> prerender_tracker_; 161 scoped_ptr<RenderWidgetSnapshotTaker> render_widget_snapshot_taker_; 162 scoped_refptr<SafeBrowsingService> sb_service_; 163 scoped_ptr<BookmarkPromptController> bookmark_prompt_controller_; 164 #endif // !defined(OS_IOS) 165 166 #if !defined(OS_IOS) && !defined(OS_ANDROID) 167 scoped_ptr<StorageMonitor> storage_monitor_; 168 scoped_ptr<MediaFileSystemRegistry> media_file_system_registry_; 169 #endif 170 171 // The following objects are not owned by TestingBrowserProcess: 172 PrefService* local_state_; 173 IOThread* io_thread_; 174 net::URLRequestContextGetter* system_request_context_; 175 176 scoped_ptr<BrowserProcessPlatformPart> platform_part_; 177 178 scoped_ptr<extensions::ExtensionsBrowserClient> extensions_browser_client_; 179 180 DISALLOW_COPY_AND_ASSIGN(TestingBrowserProcess); 181 }; 182 183 // RAII (resource acquisition is initialization) for TestingBrowserProcess. 184 // Allows you to initialize TestingBrowserProcess/NotificationService before 185 // other member variables. 186 // 187 // This can be helpful if you are running a unit test inside the browser_tests 188 // suite because browser_tests do not make a TestingBrowserProcess for you. 189 // 190 // class MyUnitTestRunningAsBrowserTest : public testing::Test { 191 // ...stuff... 192 // private: 193 // TestingBrowserProcessInitializer initializer_; 194 // LocalState local_state_; // Needs a BrowserProcess to initialize. 195 // NotificationRegistrar registar_; // Needs NotificationService. 196 // }; 197 class TestingBrowserProcessInitializer { 198 public: 199 TestingBrowserProcessInitializer(); 200 ~TestingBrowserProcessInitializer(); 201 202 private: 203 DISALLOW_COPY_AND_ASSIGN(TestingBrowserProcessInitializer); 204 }; 205 206 #endif // CHROME_TEST_BASE_TESTING_BROWSER_PROCESS_H_ 207