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

Side by Side Diff: chrome/browser/chromeos/policy/consumer_enrollment_handler_factory.cc

Issue 733613005: Move the notification part out of ConsumerEnrollmentHandler so that it can be reused for unenrollme… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@enroll
Patch Set: Added a check for UserManager. Created 6 years, 1 month 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/browser/chromeos/policy/consumer_enrollment_handler_factory.h" 5 #include "chrome/browser/chromeos/policy/consumer_enrollment_handler_factory.h"
6 6
7 #include "chrome/browser/browser_process.h" 7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/browser_process_platform_part.h" 8 #include "chrome/browser/browser_process_platform_part.h"
9 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" 9 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
10 #include "chrome/browser/chromeos/policy/consumer_enrollment_handler.h" 10 #include "chrome/browser/chromeos/policy/consumer_enrollment_handler.h"
11 #include "chrome/browser/chromeos/policy/consumer_management_service.h" 11 #include "chrome/browser/chromeos/policy/consumer_management_service.h"
12 #include "chrome/browser/chromeos/profiles/profile_helper.h" 12 #include "chrome/browser/chromeos/profiles/profile_helper.h"
13 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
15 #include "chrome/browser/signin/signin_manager_factory.h" 15 #include "chrome/browser/signin/signin_manager_factory.h"
16 #include "components/keyed_service/content/browser_context_dependency_manager.h" 16 #include "components/keyed_service/content/browser_context_dependency_manager.h"
17 #include "components/user_manager/user_manager.h"
17 18
18 namespace policy { 19 namespace policy {
19 20
20 // static 21 // static
21 ConsumerEnrollmentHandler* 22 ConsumerEnrollmentHandler*
22 ConsumerEnrollmentHandlerFactory::GetForBrowserContext( 23 ConsumerEnrollmentHandlerFactory::GetForBrowserContext(
23 content::BrowserContext* context) { 24 content::BrowserContext* context) {
24 return static_cast<ConsumerEnrollmentHandler*>( 25 return static_cast<ConsumerEnrollmentHandler*>(
25 GetInstance()->GetServiceForBrowserContext(context, true)); 26 GetInstance()->GetServiceForBrowserContext(context, true));
26 } 27 }
27 28
28 // static 29 // static
29 ConsumerEnrollmentHandlerFactory* 30 ConsumerEnrollmentHandlerFactory*
30 ConsumerEnrollmentHandlerFactory::GetInstance() { 31 ConsumerEnrollmentHandlerFactory::GetInstance() {
31 return Singleton<ConsumerEnrollmentHandlerFactory>::get(); 32 return Singleton<ConsumerEnrollmentHandlerFactory>::get();
32 } 33 }
33 34
34 ConsumerEnrollmentHandlerFactory::ConsumerEnrollmentHandlerFactory() 35 ConsumerEnrollmentHandlerFactory::ConsumerEnrollmentHandlerFactory()
35 : BrowserContextKeyedServiceFactory( 36 : BrowserContextKeyedServiceFactory(
36 "ConsumerEnrollmentHandler", 37 "ConsumerEnrollmentHandler",
37 BrowserContextDependencyManager::GetInstance()) { 38 BrowserContextDependencyManager::GetInstance()) {
38 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); 39 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
39 DependsOn(SigninManagerFactory::GetInstance()); 40 DependsOn(SigninManagerFactory::GetInstance());
40 } 41 }
41 42
42 ConsumerEnrollmentHandlerFactory::~ConsumerEnrollmentHandlerFactory() { 43 ConsumerEnrollmentHandlerFactory::~ConsumerEnrollmentHandlerFactory() {
43 } 44 }
44 45
45 bool ConsumerEnrollmentHandlerFactory::ShouldCreateHandler(
46 Profile* profile,
47 ConsumerManagementService* service) const {
48 if (!service)
49 return false;
50
51 // On a fresh device, the first time the owner signs in, IsOwnerProfile()
52 // will return false. But it is okay since there's no enrollment in progress
53 // so we don't need to create a handler.
54 if (!chromeos::ProfileHelper::IsOwnerProfile(profile))
55 return false;
56
57 return service->GetStatus() == ConsumerManagementService::STATUS_ENROLLING ||
58 service->HasPendingEnrollmentNotification();
59 }
60
61 KeyedService* ConsumerEnrollmentHandlerFactory::BuildServiceInstanceFor( 46 KeyedService* ConsumerEnrollmentHandlerFactory::BuildServiceInstanceFor(
62 content::BrowserContext* context) const { 47 content::BrowserContext* context) const {
48 // Some tests don't have a user manager and may crash at IsOwnerProfile().
49 if (!user_manager::UserManager::IsInitialized())
50 return nullptr;
51
52 // On a fresh device, the first time the owner signs in, IsOwnerProfile()
53 // will return false. But it is okay since there's no enrollment in progress.
63 Profile* profile = Profile::FromBrowserContext(context); 54 Profile* profile = Profile::FromBrowserContext(context);
55 if (!chromeos::ProfileHelper::IsOwnerProfile(profile))
56 return nullptr;
57
64 BrowserPolicyConnectorChromeOS* connector = 58 BrowserPolicyConnectorChromeOS* connector =
65 g_browser_process->platform_part()->browser_policy_connector_chromeos(); 59 g_browser_process->platform_part()->browser_policy_connector_chromeos();
66 ConsumerManagementService* service = 60 ConsumerManagementService* service =
67 connector->GetConsumerManagementService(); 61 connector->GetConsumerManagementService();
68 62 if (!service ||
69 if (ShouldCreateHandler(profile, service)) { 63 service->GetStatus() != ConsumerManagementService::STATUS_ENROLLING) {
70 return new ConsumerEnrollmentHandler(
71 profile,
72 service,
73 connector->GetDeviceManagementServiceForConsumer());
74 } else {
75 return nullptr; 64 return nullptr;
76 } 65 }
66
67 return new ConsumerEnrollmentHandler(
68 profile,
69 service,
70 connector->GetDeviceManagementServiceForConsumer());
77 } 71 }
78 72
79 bool ConsumerEnrollmentHandlerFactory::ServiceIsCreatedWithBrowserContext() 73 bool ConsumerEnrollmentHandlerFactory::ServiceIsCreatedWithBrowserContext()
80 const { 74 const {
81 return true; 75 return true;
82 } 76 }
83 77
84 } // namespace policy 78 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698