Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_management_internal.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/extension_management_internal.h"
6
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/extension_management_constants.h"
10 #include "extensions/common/url_pattern_set.h"
11 #include "url/gurl.h"
12
13 namespace extensions {
14
15 namespace internal {
16
17 namespace {
18 const char kMalformedPreferenceWarning[] =
19     "Malformed extension management preference.";
20 }  // namespace
21
22 IndividualSettings::IndividualSettings() {
23   Reset();
24 }
25
26 IndividualSettings::~IndividualSettings() {
27 }
28
29 bool IndividualSettings::Parse(const base::DictionaryValue* dict,
30                                ParsingScope scope) {
31   std::string installation_mode_str;
32   if (dict->GetStringWithoutPathExpansion(schema_constants::kInstallationMode,
33                                           &installation_mode_str)) {
34     if (installation_mode_str == schema_constants::kAllowed) {
35       installation_mode = ExtensionManagement::INSTALLATION_ALLOWED;
36     } else if (installation_mode_str == schema_constants::kBlocked) {
37       installation_mode = ExtensionManagement::INSTALLATION_BLOCKED;
38     } else if (installation_mode_str == schema_constants::kForceInstalled) {
39       installation_mode = ExtensionManagement::INSTALLATION_FORCED;
40     } else if (installation_mode_str == schema_constants::kNormalInstalled) {
41       installation_mode = ExtensionManagement::INSTALLATION_RECOMMENDED;
42     } else {
43       // Invalid value for 'installation_mode'.
44       LOG(WARNING) << kMalformedPreferenceWarning;
45       return false;
46     }
47
48     // Only proceed to fetch update url if force or recommended install mode
49     // is set.
50     if (installation_mode == ExtensionManagement::INSTALLATION_FORCED ||
51         installation_mode == ExtensionManagement::INSTALLATION_RECOMMENDED) {
52       if (scope != SCOPE_INDIVIDUAL) {
53         // Only individual extensions are allowed to be automatically installed.
54         LOG(WARNING) << kMalformedPreferenceWarning;
55         return false;
56       }
57       std::string update_url_str;
58       if (dict->GetStringWithoutPathExpansion(schema_constants::kUpdateUrl,
59                                               &update_url_str) &&
60           GURL(update_url_str).is_valid()) {
61         update_url = update_url_str;
62       } else {
63         // No valid update URL for extension.
64         LOG(WARNING) << kMalformedPreferenceWarning;
65         return false;
66       }
67     }
68   }
69
70   // Parses the blocked permission settings.
71   const base::ListValue* list_value = nullptr;
72   base::string16 error;
73
74   // If applicable, inherit from global block list and remove all explicitly
75   // allowed permissions.
76   if (scope != SCOPE_DEFAULT &&
77       dict->GetListWithoutPathExpansion(schema_constants::kAllowedPermissions,
78                                         &list_value)) {
79     // It is assumed that Parse() is already called for SCOPE_DEFAULT and
80     // settings specified for |this| is initialized by copying from default
81     // settings, including the |blocked_permissions| setting here.
82     // That is, |blocked_permissions| should be the default block permissions
83     // list settings here.
84     APIPermissionSet globally_blocked_permissions = blocked_permissions;
85     APIPermissionSet explicitly_allowed_permissions;
86     // Reuses code for parsing API permissions from manifest. But note that we
87     // only support list of strings type.
88     if (!APIPermissionSet::ParseFromJSON(
89             list_value,
90             APIPermissionSet::kDisallowInternalPermissions,
91             &explicitly_allowed_permissions,
92             &error,
93             nullptr)) {
94       // There might be unknown permissions, warn and just ignore them;
95       LOG(WARNING) << error;
96     }
97     APIPermissionSet::Difference(globally_blocked_permissions,
98                                  explicitly_allowed_permissions,
99                                  &blocked_permissions);
100   }
101
102   // Then add all newly blocked permissions to the list.
103   if (dict->GetListWithoutPathExpansion(schema_constants::kBlockedPermissions,
104                                         &list_value)) {
105     // The |blocked_permissions| might be the result of the routines above,
106     // or remains the same as default block permissions settings.
107     APIPermissionSet permissions_to_merge_from = blocked_permissions;
108     APIPermissionSet permissions_parsed;
109     if (!APIPermissionSet::ParseFromJSON(
110             list_value,
111             APIPermissionSet::kDisallowInternalPermissions,
112             &permissions_parsed,
113             &error,
114             nullptr)) {
115       LOG(WARNING) << error;
116     }
117     APIPermissionSet::Union(
118         permissions_to_merge_from, permissions_parsed, &blocked_permissions);
119   }
120
121   return true;
122 }
123
124 void IndividualSettings::Reset() {
125   installation_mode = ExtensionManagement::INSTALLATION_ALLOWED;
126   update_url.clear();
127   blocked_permissions.clear();
128 }
129
130 GlobalSettings::GlobalSettings() {
131   Reset();
132 }
133
134 GlobalSettings::~GlobalSettings() {
135 }
136
137 void GlobalSettings::Reset() {
138   has_restricted_install_sources = false;
139   install_sources.ClearPatterns();
140   has_restricted_allowed_types = false;
141   allowed_types.clear();
142 }
143
144 }  // namespace internal
145
146 }  // namespace extensions