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() {
29 KioskModeInfo
* KioskModeInfo::Get(const Extension
* extension
) {
30 return static_cast<KioskModeInfo
*>(
31 extension
->GetManifestData(keys::kKioskMode
));
35 bool KioskModeInfo::IsKioskEnabled(const Extension
* extension
) {
36 KioskModeInfo
* info
= Get(extension
);
37 return info
? info
->kiosk_status
!= NONE
: false;
41 bool KioskModeInfo::IsKioskOnly(const Extension
* extension
) {
42 KioskModeInfo
* info
= Get(extension
);
43 return info
? info
->kiosk_status
== ONLY
: false;
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
);
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
);
80 if (kiosk_only
&& !kiosk_enabled
) {
81 *error
= base::ASCIIToUTF16(
82 manifest_errors::kInvalidKioskOnlyButNotEnabled
);
86 // All other use cases should be already filtered out by manifest feature
88 DCHECK(extension
->is_platform_app());
90 KioskModeInfo::KioskStatus kiosk_status
= KioskModeInfo::NONE
;
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
,
101 !secondary_apps
->GetAsList(&list
)) {
102 *error
= base::ASCIIToUTF16(manifest_errors::kInvalidKioskSecondaryApps
);
106 for (const base::Value
* value
: *list
) {
107 scoped_ptr
<KioskSecondaryAppsType
> app
=
108 KioskSecondaryAppsType::FromValue(*value
, error
);
110 *error
= base::ASCIIToUTF16(
111 manifest_errors::kInvalidKioskSecondaryAppsBadAppId
);
114 ids
.push_back(app
->id
);
118 extension
->SetManifestData(keys::kKioskMode
,
119 new KioskModeInfo(kiosk_status
, ids
));
124 const std::vector
<std::string
> KioskModeHandler::Keys() const {
125 return supported_keys_
;
128 } // namespace extensions