Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: chrome/test/base/testing_profile.cc

Issue 10693022: Add support for loading user cloud policy on desktop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Tweaked some comments after self-review. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "chrome/test/base/testing_profile.h" 5 #include "chrome/test/base/testing_profile.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #include "base/base_paths.h" 9 #include "base/base_paths.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 16 matching lines...) Expand all
27 #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h" 27 #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h"
28 #include "chrome/browser/history/history.h" 28 #include "chrome/browser/history/history.h"
29 #include "chrome/browser/history/history_backend.h" 29 #include "chrome/browser/history/history_backend.h"
30 #include "chrome/browser/history/history_service_factory.h" 30 #include "chrome/browser/history/history_service_factory.h"
31 #include "chrome/browser/history/shortcuts_backend.h" 31 #include "chrome/browser/history/shortcuts_backend.h"
32 #include "chrome/browser/history/shortcuts_backend_factory.h" 32 #include "chrome/browser/history/shortcuts_backend_factory.h"
33 #include "chrome/browser/history/top_sites.h" 33 #include "chrome/browser/history/top_sites.h"
34 #include "chrome/browser/net/proxy_service_factory.h" 34 #include "chrome/browser/net/proxy_service_factory.h"
35 #include "chrome/browser/notifications/desktop_notification_service.h" 35 #include "chrome/browser/notifications/desktop_notification_service.h"
36 #include "chrome/browser/notifications/desktop_notification_service_factory.h" 36 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
37 #include "chrome/browser/policy/user_cloud_policy_manager.h"
37 #include "chrome/browser/prefs/browser_prefs.h" 38 #include "chrome/browser/prefs/browser_prefs.h"
38 #include "chrome/browser/prefs/testing_pref_store.h" 39 #include "chrome/browser/prefs/testing_pref_store.h"
39 #include "chrome/browser/prerender/prerender_manager.h" 40 #include "chrome/browser/prerender/prerender_manager.h"
40 #include "chrome/browser/profiles/profile_dependency_manager.h" 41 #include "chrome/browser/profiles/profile_dependency_manager.h"
41 #include "chrome/browser/protector/protector_service_factory.h" 42 #include "chrome/browser/protector/protector_service_factory.h"
42 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" 43 #include "chrome/browser/search_engines/template_url_fetcher_factory.h"
43 #include "chrome/browser/speech/chrome_speech_recognition_preferences.h" 44 #include "chrome/browser/speech/chrome_speech_recognition_preferences.h"
44 #include "chrome/browser/webdata/web_data_service.h" 45 #include "chrome/browser/webdata/web_data_service.h"
45 #include "chrome/browser/webdata/web_data_service_factory.h" 46 #include "chrome/browser/webdata/web_data_service_factory.h"
46 #include "chrome/common/chrome_constants.h" 47 #include "chrome/common/chrome_constants.h"
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 const char TestingProfile::kTestUserProfileDir[] = "Default"; 153 const char TestingProfile::kTestUserProfileDir[] = "Default";
153 #endif 154 #endif
154 155
155 TestingProfile::TestingProfile() 156 TestingProfile::TestingProfile()
156 : start_time_(Time::Now()), 157 : start_time_(Time::Now()),
157 testing_prefs_(NULL), 158 testing_prefs_(NULL),
158 incognito_(false), 159 incognito_(false),
159 last_session_exited_cleanly_(true), 160 last_session_exited_cleanly_(true),
160 profile_dependency_manager_(ProfileDependencyManager::GetInstance()), 161 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
161 delegate_(NULL) { 162 delegate_(NULL) {
162 if (!temp_dir_.CreateUniqueTempDir()) { 163 CreateTempProfileDir();
163 LOG(ERROR) << "Failed to create unique temporary directory.";
164
165 // Fallback logic in case we fail to create unique temporary directory.
166 FilePath system_tmp_dir;
167 bool success = PathService::Get(base::DIR_TEMP, &system_tmp_dir);
168
169 // We're severly screwed if we can't get the system temporary
170 // directory. Die now to avoid writing to the filesystem root
171 // or other bad places.
172 CHECK(success);
173
174 FilePath fallback_dir(system_tmp_dir.AppendASCII("TestingProfilePath"));
175 file_util::Delete(fallback_dir, true);
176 file_util::CreateDirectory(fallback_dir);
177 if (!temp_dir_.Set(fallback_dir)) {
178 // That shouldn't happen, but if it does, try to recover.
179 LOG(ERROR) << "Failed to use a fallback temporary directory.";
180
181 // We're screwed if this fails, see CHECK above.
182 CHECK(temp_dir_.Set(system_tmp_dir));
183 }
184 }
185
186 profile_path_ = temp_dir_.path(); 164 profile_path_ = temp_dir_.path();
187 165
188 Init(); 166 Init();
189 FinishInit(); 167 FinishInit();
190 } 168 }
191 169
192 TestingProfile::TestingProfile(const FilePath& path) 170 TestingProfile::TestingProfile(const FilePath& path)
193 : start_time_(Time::Now()), 171 : start_time_(Time::Now()),
194 testing_prefs_(NULL), 172 testing_prefs_(NULL),
195 incognito_(false), 173 incognito_(false),
(...skipping 17 matching lines...) Expand all
213 Init(); 191 Init();
214 if (delegate_) { 192 if (delegate_) {
215 MessageLoop::current()->PostTask(FROM_HERE, 193 MessageLoop::current()->PostTask(FROM_HERE,
216 base::Bind(&TestingProfile::FinishInit, 194 base::Bind(&TestingProfile::FinishInit,
217 base::Unretained(this))); 195 base::Unretained(this)));
218 } else { 196 } else {
219 FinishInit(); 197 FinishInit();
220 } 198 }
221 } 199 }
222 200
201 TestingProfile::TestingProfile(
202 const FilePath& path,
203 Delegate* delegate,
204 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy,
205 scoped_ptr<PrefService> prefs,
206 scoped_ptr<policy::UserCloudPolicyManager> user_cloud_policy_manager)
207 : start_time_(Time::Now()),
208 prefs_(prefs.release()),
209 testing_prefs_(NULL),
210 incognito_(false),
211 last_session_exited_cleanly_(true),
212 extension_special_storage_policy_(extension_policy),
213 user_cloud_policy_manager_(user_cloud_policy_manager.release()),
214 profile_path_(path),
215 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
216 delegate_(delegate) {
217
218 // If no profile path was supplied, create one.
219 if (profile_path_.empty()) {
220 CreateTempProfileDir();
221 profile_path_ = temp_dir_.path();
222 }
223
224 Init();
225 // If caller supplied a delegate, delay the FinishInit invocation until other
226 // tasks have run.
227 // TODO(atwilson): See if this is still required once we convert the current
228 // users of the constructor that takes a Delegate* param.
229 if (delegate_) {
230 MessageLoop::current()->PostTask(FROM_HERE,
231 base::Bind(&TestingProfile::FinishInit,
232 base::Unretained(this)));
233 } else {
234 FinishInit();
235 }
236 }
237
238 void TestingProfile::CreateTempProfileDir() {
239 if (!temp_dir_.CreateUniqueTempDir()) {
240 LOG(ERROR) << "Failed to create unique temporary directory.";
241
242 // Fallback logic in case we fail to create unique temporary directory.
243 FilePath system_tmp_dir;
244 bool success = PathService::Get(base::DIR_TEMP, &system_tmp_dir);
245
246 // We're severly screwed if we can't get the system temporary
247 // directory. Die now to avoid writing to the filesystem root
248 // or other bad places.
249 CHECK(success);
250
251 FilePath fallback_dir(system_tmp_dir.AppendASCII("TestingProfilePath"));
252 file_util::Delete(fallback_dir, true);
253 file_util::CreateDirectory(fallback_dir);
254 if (!temp_dir_.Set(fallback_dir)) {
255 // That shouldn't happen, but if it does, try to recover.
256 LOG(ERROR) << "Failed to use a fallback temporary directory.";
257
258 // We're screwed if this fails, see CHECK above.
259 CHECK(temp_dir_.Set(system_tmp_dir));
260 }
261 }
262 }
263
223 void TestingProfile::Init() { 264 void TestingProfile::Init() {
224 if (!file_util::PathExists(profile_path_)) 265 if (!file_util::PathExists(profile_path_))
225 file_util::CreateDirectory(profile_path_); 266 file_util::CreateDirectory(profile_path_);
226 267
227 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory( 268 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory(
228 this, extensions::TestExtensionSystem::Build); 269 this, extensions::TestExtensionSystem::Build);
229 270
230 profile_dependency_manager_->CreateProfileServices(this, true); 271 profile_dependency_manager_->CreateProfileServices(this, true);
231 272
232 #if defined(ENABLE_NOTIFICATIONS) 273 #if defined(ENABLE_NOTIFICATIONS)
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 return HistoryServiceFactory::GetForProfileWithoutCreating(this); 528 return HistoryServiceFactory::GetForProfileWithoutCreating(this);
488 } 529 }
489 530
490 net::CookieMonster* TestingProfile::GetCookieMonster() { 531 net::CookieMonster* TestingProfile::GetCookieMonster() {
491 if (!GetRequestContext()) 532 if (!GetRequestContext())
492 return NULL; 533 return NULL;
493 return GetRequestContext()->GetURLRequestContext()->cookie_store()-> 534 return GetRequestContext()->GetURLRequestContext()->cookie_store()->
494 GetCookieMonster(); 535 GetCookieMonster();
495 } 536 }
496 537
538 policy::UserCloudPolicyManager* TestingProfile::GetUserCloudPolicyManager() {
539 return user_cloud_policy_manager_.get();
540 }
541
497 policy::PolicyService* TestingProfile::GetPolicyService() { 542 policy::PolicyService* TestingProfile::GetPolicyService() {
498 if (!policy_service_.get()) { 543 if (!policy_service_.get()) {
499 #if defined(ENABLE_CONFIGURATION_POLICY) 544 #if defined(ENABLE_CONFIGURATION_POLICY)
500 policy::PolicyServiceImpl::Providers providers; 545 policy::PolicyServiceImpl::Providers providers;
501 policy_service_.reset(new policy::PolicyServiceImpl(providers)); 546 policy_service_.reset(new policy::PolicyServiceImpl(providers));
502 #else 547 #else
503 policy_service_.reset(new policy::PolicyServiceStub()); 548 policy_service_.reset(new policy::PolicyServiceStub());
504 #endif 549 #endif
505 } 550 }
506 return policy_service_.get(); 551 return policy_service_.get();
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 } 770 }
726 771
727 bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) { 772 bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) {
728 return true; 773 return true;
729 } 774 }
730 775
731 base::Callback<ChromeURLDataManagerBackend*(void)> 776 base::Callback<ChromeURLDataManagerBackend*(void)>
732 TestingProfile::GetChromeURLDataManagerBackendGetter() const { 777 TestingProfile::GetChromeURLDataManagerBackendGetter() const {
733 return base::Callback<ChromeURLDataManagerBackend*(void)>(); 778 return base::Callback<ChromeURLDataManagerBackend*(void)>();
734 } 779 }
780
781 TestingProfile::Builder::Builder()
782 : build_called_(false),
783 delegate_(NULL) {
784 }
785
786 TestingProfile::Builder::~Builder() {
787 }
788
789 void TestingProfile::Builder::SetPath(const FilePath& path) {
790 path_ = path;
791 }
792
793 void TestingProfile::Builder::SetDelegate(Delegate* delegate) {
794 delegate_ = delegate;
795 }
796
797 void TestingProfile::Builder::SetExtensionSpecialStoragePolicy(
798 scoped_refptr<ExtensionSpecialStoragePolicy> policy) {
799 extension_policy_ = policy;
800 }
801
802 void TestingProfile::Builder::SetPrefService(scoped_ptr<PrefService> prefs) {
803 pref_service_ = prefs.Pass();
804 }
805
806 void TestingProfile::Builder::SetUserCloudPolicyManager(
807 scoped_ptr<policy::UserCloudPolicyManager> manager) {
808 user_cloud_policy_manager_ = manager.Pass();
809 }
810
811 scoped_ptr<TestingProfile> TestingProfile::Builder::Build() {
812 DCHECK(!build_called_);
813 build_called_ = true;
814 return scoped_ptr<TestingProfile>(new TestingProfile(
815 path_,
816 delegate_,
817 extension_policy_,
818 pref_service_.Pass(),
819 user_cloud_policy_manager_.Pass()));
820 }
821
822
OLDNEW
« chrome/common/pref_names.cc ('K') | « chrome/test/base/testing_profile.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698