Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sessions / restore_on_startup_policy_handler.cc
blobcd42ecc6e2a6722032f6d461e94a05a8223d13c1
1 // Copyright 2013 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.
5 #include "chrome/browser/sessions/restore_on_startup_policy_handler.h"
7 #include "base/prefs/pref_value_map.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/values.h"
10 #include "chrome/browser/prefs/session_startup_pref.h"
11 #include "chrome/common/pref_names.h"
12 #include "components/policy/core/browser/policy_error_map.h"
13 #include "components/policy/core/common/policy_map.h"
14 #include "grit/component_strings.h"
15 #include "policy/policy_constants.h"
17 namespace policy {
19 RestoreOnStartupPolicyHandler::RestoreOnStartupPolicyHandler()
20 : TypeCheckingPolicyHandler(key::kRestoreOnStartup,
21 base::Value::TYPE_INTEGER) {}
23 RestoreOnStartupPolicyHandler::~RestoreOnStartupPolicyHandler() {
26 void RestoreOnStartupPolicyHandler::ApplyPolicySettings(
27 const PolicyMap& policies,
28 PrefValueMap* prefs) {
29 const base::Value* restore_on_startup_value =
30 policies.GetValue(policy_name());
31 if (restore_on_startup_value) {
32 int restore_on_startup;
33 if (!restore_on_startup_value->GetAsInteger(&restore_on_startup))
34 return;
36 if (restore_on_startup == SessionStartupPref::kPrefValueHomePage)
37 ApplyPolicySettingsFromHomePage(policies, prefs);
38 else
39 prefs->SetInteger(prefs::kRestoreOnStartup, restore_on_startup);
43 void RestoreOnStartupPolicyHandler::ApplyPolicySettingsFromHomePage(
44 const PolicyMap& policies,
45 PrefValueMap* prefs) {
46 const base::Value* homepage_is_new_tab_page_value =
47 policies.GetValue(key::kHomepageIsNewTabPage);
48 if (!homepage_is_new_tab_page_value) {
49 // The policy is enforcing 'open the homepage on startup' but not
50 // enforcing what the homepage should be. Don't set any prefs.
51 return;
54 bool homepage_is_new_tab_page;
55 if (!homepage_is_new_tab_page_value->GetAsBoolean(&homepage_is_new_tab_page))
56 return;
58 if (homepage_is_new_tab_page) {
59 prefs->SetInteger(prefs::kRestoreOnStartup,
60 SessionStartupPref::kPrefValueNewTab);
61 } else {
62 const base::Value* homepage_value =
63 policies.GetValue(key::kHomepageLocation);
64 if (!homepage_value || !homepage_value->IsType(base::Value::TYPE_STRING)) {
65 // The policy is enforcing 'open the homepage on startup' but not
66 // enforcing what the homepage should be. Don't set any prefs.
67 return;
69 base::ListValue* url_list = new base::ListValue();
70 url_list->Append(homepage_value->DeepCopy());
71 prefs->SetInteger(prefs::kRestoreOnStartup,
72 SessionStartupPref::kPrefValueURLs);
73 prefs->SetValue(prefs::kURLsToRestoreOnStartup, url_list);
77 bool RestoreOnStartupPolicyHandler::CheckPolicySettings(
78 const PolicyMap& policies,
79 PolicyErrorMap* errors) {
80 if (!TypeCheckingPolicyHandler::CheckPolicySettings(policies, errors))
81 return false;
83 const base::Value* restore_policy = policies.GetValue(key::kRestoreOnStartup);
85 if (restore_policy) {
86 int restore_value;
87 CHECK(restore_policy->GetAsInteger(&restore_value)); // Passed type check.
88 switch (restore_value) {
89 case SessionStartupPref::kPrefValueHomePage:
90 errors->AddError(policy_name(), IDS_POLICY_VALUE_DEPRECATED);
91 break;
92 case SessionStartupPref::kPrefValueLast: {
93 // If the "restore last session" policy is set, session cookies are
94 // treated as permanent cookies and site data needed to restore the
95 // session is not cleared so we have to warn the user in that case.
96 const base::Value* cookies_policy =
97 policies.GetValue(key::kCookiesSessionOnlyForUrls);
98 const base::ListValue* cookies_value;
99 if (cookies_policy && cookies_policy->GetAsList(&cookies_value) &&
100 !cookies_value->empty()) {
101 errors->AddError(key::kCookiesSessionOnlyForUrls,
102 IDS_POLICY_OVERRIDDEN,
103 key::kRestoreOnStartup);
106 const base::Value* exit_policy =
107 policies.GetValue(key::kClearSiteDataOnExit);
108 bool exit_value;
109 if (exit_policy && exit_policy->GetAsBoolean(&exit_value) &&
110 exit_value) {
111 errors->AddError(key::kClearSiteDataOnExit,
112 IDS_POLICY_OVERRIDDEN,
113 key::kRestoreOnStartup);
115 break;
117 case SessionStartupPref::kPrefValueURLs:
118 case SessionStartupPref::kPrefValueNewTab:
119 // No error
120 break;
121 default:
122 errors->AddError(policy_name(),
123 IDS_POLICY_OUT_OF_RANGE_ERROR,
124 base::IntToString(restore_value));
127 return true;
130 } // namespace policy