Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / common / manifest_handlers / kiosk_mode_info.cc
blobd265c232787237b35eb09cc1e6c1b7f1c658142a
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 "extensions/common/manifest_handlers/kiosk_mode_info.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "extensions/common/api/extensions_manifest_types.h"
13 #include "extensions/common/manifest_constants.h"
15 namespace extensions {
17 namespace keys = manifest_keys;
19 using api::extensions_manifest_types::KioskSecondaryAppsType;
21 KioskModeInfo::KioskModeInfo(KioskStatus kiosk_status,
22 const std::vector<std::string>& secondary_app_ids)
23 : kiosk_status(kiosk_status), secondary_app_ids(secondary_app_ids) {}
25 KioskModeInfo::~KioskModeInfo() {
28 // static
29 KioskModeInfo* KioskModeInfo::Get(const Extension* extension) {
30 return static_cast<KioskModeInfo*>(
31 extension->GetManifestData(keys::kKioskMode));
34 // static
35 bool KioskModeInfo::IsKioskEnabled(const Extension* extension) {
36 KioskModeInfo* info = Get(extension);
37 return info ? info->kiosk_status != NONE : false;
40 // static
41 bool KioskModeInfo::IsKioskOnly(const Extension* extension) {
42 KioskModeInfo* info = Get(extension);
43 return info ? info->kiosk_status == ONLY : false;
46 // static
47 bool KioskModeInfo::HasSecondaryApps(const Extension* extension) {
48 KioskModeInfo* info = Get(extension);
49 return info && !info->secondary_app_ids.empty();
52 KioskModeHandler::KioskModeHandler() {
53 supported_keys_.push_back(keys::kKioskEnabled);
54 supported_keys_.push_back(keys::kKioskOnly);
55 supported_keys_.push_back(keys::kKioskSecondaryApps);
58 KioskModeHandler::~KioskModeHandler() {
61 bool KioskModeHandler::Parse(Extension* extension, base::string16* error) {
62 const Manifest* manifest = extension->manifest();
63 DCHECK(manifest->HasKey(keys::kKioskEnabled) ||
64 manifest->HasKey(keys::kKioskOnly));
66 bool kiosk_enabled = false;
67 if (manifest->HasKey(keys::kKioskEnabled) &&
68 !manifest->GetBoolean(keys::kKioskEnabled, &kiosk_enabled)) {
69 *error = base::ASCIIToUTF16(manifest_errors::kInvalidKioskEnabled);
70 return false;
73 bool kiosk_only = false;
74 if (manifest->HasKey(keys::kKioskOnly) &&
75 !manifest->GetBoolean(keys::kKioskOnly, &kiosk_only)) {
76 *error = base::ASCIIToUTF16(manifest_errors::kInvalidKioskOnly);
77 return false;
80 if (kiosk_only && !kiosk_enabled) {
81 *error = base::ASCIIToUTF16(
82 manifest_errors::kInvalidKioskOnlyButNotEnabled);
83 return false;
86 // All other use cases should be already filtered out by manifest feature
87 // checks.
88 DCHECK(extension->is_platform_app());
90 KioskModeInfo::KioskStatus kiosk_status = KioskModeInfo::NONE;
91 if (kiosk_enabled)
92 kiosk_status = kiosk_only ? KioskModeInfo::ONLY : KioskModeInfo::ENABLED;
94 // Kiosk secondary apps key is optional.
95 std::vector<std::string> ids;
96 if (extension->manifest()->HasKey(keys::kKioskSecondaryApps)) {
97 const base::Value* secondary_apps = nullptr;
98 const base::ListValue* list = nullptr;
99 if (!extension->manifest()->Get(keys::kKioskSecondaryApps,
100 &secondary_apps) ||
101 !secondary_apps->GetAsList(&list)) {
102 *error = base::ASCIIToUTF16(manifest_errors::kInvalidKioskSecondaryApps);
103 return false;
106 for (const base::Value* value : *list) {
107 scoped_ptr<KioskSecondaryAppsType> app =
108 KioskSecondaryAppsType::FromValue(*value, error);
109 if (!app) {
110 *error = base::ASCIIToUTF16(
111 manifest_errors::kInvalidKioskSecondaryAppsBadAppId);
112 return false;
114 ids.push_back(app->id);
118 extension->SetManifestData(keys::kKioskMode,
119 new KioskModeInfo(kiosk_status, ids));
121 return true;
124 const std::vector<std::string> KioskModeHandler::Keys() const {
125 return supported_keys_;
128 } // namespace extensions