| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 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. | |
| 4 | |
| 5 #include "chrome/common/extensions/api/themes/theme_handler.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/common/extensions/extension_manifest_constants.h" | |
| 12 #include "chrome/common/extensions/manifest.h" | |
| 13 #include "grit/generated_resources.h" | |
| 14 #include "ui/base/l10n/l10n_util.h" | |
| 15 | |
| 16 namespace extensions { | |
| 17 | |
| 18 namespace keys = extension_manifest_keys; | |
| 19 namespace errors = extension_manifest_errors; | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 bool LoadThemeImages(const DictionaryValue* theme_value, | |
| 24 string16* error, | |
| 25 ThemeInfo* theme_info) { | |
| 26 const DictionaryValue* images_value = NULL; | |
| 27 if (theme_value->GetDictionary(keys::kThemeImages, &images_value)) { | |
| 28 // Validate that the images are all strings. | |
| 29 for (DictionaryValue::Iterator iter(*images_value); !iter.IsAtEnd(); | |
| 30 iter.Advance()) { | |
| 31 if (!iter.value().IsType(Value::TYPE_STRING)) { | |
| 32 *error = ASCIIToUTF16(errors::kInvalidThemeImages); | |
| 33 return false; | |
| 34 } | |
| 35 } | |
| 36 theme_info->theme_images_.reset(images_value->DeepCopy()); | |
| 37 } | |
| 38 return true; | |
| 39 } | |
| 40 | |
| 41 bool LoadThemeColors(const DictionaryValue* theme_value, | |
| 42 string16* error, | |
| 43 ThemeInfo* theme_info) { | |
| 44 const DictionaryValue* colors_value = NULL; | |
| 45 if (theme_value->GetDictionary(keys::kThemeColors, &colors_value)) { | |
| 46 // Validate that the colors are RGB or RGBA lists. | |
| 47 for (DictionaryValue::Iterator iter(*colors_value); !iter.IsAtEnd(); | |
| 48 iter.Advance()) { | |
| 49 const ListValue* color_list = NULL; | |
| 50 double alpha = 0.0; | |
| 51 int color = 0; | |
| 52 // The color must be a list... | |
| 53 if (!iter.value().GetAsList(&color_list) || | |
| 54 // ... and either 3 items (RGB) or 4 (RGBA). | |
| 55 ((color_list->GetSize() != 3) && | |
| 56 ((color_list->GetSize() != 4) || | |
| 57 // For RGBA, the fourth item must be a real or int alpha value. | |
| 58 // Note that GetDouble() can get an integer value. | |
| 59 !color_list->GetDouble(3, &alpha))) || | |
| 60 // For both RGB and RGBA, the first three items must be ints (R,G,B). | |
| 61 !color_list->GetInteger(0, &color) || | |
| 62 !color_list->GetInteger(1, &color) || | |
| 63 !color_list->GetInteger(2, &color)) { | |
| 64 *error = ASCIIToUTF16(errors::kInvalidThemeColors); | |
| 65 return false; | |
| 66 } | |
| 67 } | |
| 68 theme_info->theme_colors_.reset(colors_value->DeepCopy()); | |
| 69 } | |
| 70 return true; | |
| 71 } | |
| 72 | |
| 73 bool LoadThemeTints(const DictionaryValue* theme_value, | |
| 74 string16* error, | |
| 75 ThemeInfo* theme_info) { | |
| 76 const DictionaryValue* tints_value = NULL; | |
| 77 if (!theme_value->GetDictionary(keys::kThemeTints, &tints_value)) | |
| 78 return true; | |
| 79 | |
| 80 // Validate that the tints are all reals. | |
| 81 for (DictionaryValue::Iterator iter(*tints_value); !iter.IsAtEnd(); | |
| 82 iter.Advance()) { | |
| 83 const ListValue* tint_list = NULL; | |
| 84 double v = 0.0; | |
| 85 if (!iter.value().GetAsList(&tint_list) || | |
| 86 tint_list->GetSize() != 3 || | |
| 87 !tint_list->GetDouble(0, &v) || | |
| 88 !tint_list->GetDouble(1, &v) || | |
| 89 !tint_list->GetDouble(2, &v)) { | |
| 90 *error = ASCIIToUTF16(errors::kInvalidThemeTints); | |
| 91 return false; | |
| 92 } | |
| 93 } | |
| 94 theme_info->theme_tints_.reset(tints_value->DeepCopy()); | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 98 bool LoadThemeDisplayProperties(const DictionaryValue* theme_value, | |
| 99 string16* error, | |
| 100 ThemeInfo* theme_info) { | |
| 101 const DictionaryValue* display_properties_value = NULL; | |
| 102 if (theme_value->GetDictionary(keys::kThemeDisplayProperties, | |
| 103 &display_properties_value)) { | |
| 104 theme_info->theme_display_properties_.reset( | |
| 105 display_properties_value->DeepCopy()); | |
| 106 } | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 const ThemeInfo* GetThemeInfo(const Extension* extension) { | |
| 111 return static_cast<ThemeInfo*>(extension->GetManifestData(keys::kTheme)); | |
| 112 } | |
| 113 | |
| 114 } // namespace | |
| 115 | |
| 116 ThemeInfo::ThemeInfo() { | |
| 117 } | |
| 118 | |
| 119 ThemeInfo::~ThemeInfo() { | |
| 120 } | |
| 121 | |
| 122 // static | |
| 123 DictionaryValue* ThemeInfo::GetThemeImages(const Extension* extension) { | |
| 124 const ThemeInfo* theme_info = GetThemeInfo(extension); | |
| 125 return theme_info ? theme_info->theme_images_.get() : NULL; | |
| 126 } | |
| 127 | |
| 128 // static | |
| 129 DictionaryValue* ThemeInfo::GetThemeColors(const Extension* extension) { | |
| 130 const ThemeInfo* theme_info = GetThemeInfo(extension); | |
| 131 return theme_info ? theme_info->theme_colors_.get() : NULL; | |
| 132 } | |
| 133 | |
| 134 // static | |
| 135 DictionaryValue* ThemeInfo::GetThemeTints(const Extension* extension) { | |
| 136 const ThemeInfo* theme_info = GetThemeInfo(extension); | |
| 137 return theme_info ? theme_info->theme_tints_.get() : NULL; | |
| 138 } | |
| 139 | |
| 140 // static | |
| 141 DictionaryValue* ThemeInfo::GetThemeDisplayProperties( | |
| 142 const Extension* extension) { | |
| 143 const ThemeInfo* theme_info = GetThemeInfo(extension); | |
| 144 return theme_info ? theme_info->theme_display_properties_.get() : NULL; | |
| 145 } | |
| 146 | |
| 147 ThemeHandler::ThemeHandler() { | |
| 148 } | |
| 149 | |
| 150 ThemeHandler::~ThemeHandler() { | |
| 151 } | |
| 152 | |
| 153 bool ThemeHandler::Parse(Extension* extension, string16* error) { | |
| 154 const DictionaryValue* theme_value = NULL; | |
| 155 if (!extension->manifest()->GetDictionary(keys::kTheme, &theme_value)) { | |
| 156 *error = ASCIIToUTF16(errors::kInvalidTheme); | |
| 157 return false; | |
| 158 } | |
| 159 | |
| 160 scoped_ptr<ThemeInfo> theme_info(new ThemeInfo); | |
| 161 if (!LoadThemeImages(theme_value, error, theme_info.get())) | |
| 162 return false; | |
| 163 if (!LoadThemeColors(theme_value, error, theme_info.get())) | |
| 164 return false; | |
| 165 if (!LoadThemeTints(theme_value, error, theme_info.get())) | |
| 166 return false; | |
| 167 if (!LoadThemeDisplayProperties(theme_value, error, theme_info.get())) | |
| 168 return false; | |
| 169 | |
| 170 extension->SetManifestData(keys::kTheme, theme_info.release()); | |
| 171 return true; | |
| 172 } | |
| 173 | |
| 174 bool ThemeHandler::Validate(const Extension* extension, | |
| 175 std::string* error, | |
| 176 std::vector<InstallWarning>* warnings) const { | |
| 177 // Validate that theme images exist. | |
| 178 if (extension->is_theme()) { | |
| 179 DictionaryValue* images_value = | |
| 180 extensions::ThemeInfo::GetThemeImages(extension); | |
| 181 if (images_value) { | |
| 182 for (DictionaryValue::Iterator iter(*images_value); !iter.IsAtEnd(); | |
| 183 iter.Advance()) { | |
| 184 std::string val; | |
| 185 if (iter.value().GetAsString(&val)) { | |
| 186 base::FilePath image_path = extension->path().Append( | |
| 187 base::FilePath::FromUTF8Unsafe(val)); | |
| 188 if (!file_util::PathExists(image_path)) { | |
| 189 *error = | |
| 190 l10n_util::GetStringFUTF8(IDS_EXTENSION_INVALID_IMAGE_PATH, | |
| 191 image_path.LossyDisplayName()); | |
| 192 return false; | |
| 193 } | |
| 194 } | |
| 195 } | |
| 196 } | |
| 197 } | |
| 198 return true; | |
| 199 } | |
| 200 | |
| 201 const std::vector<std::string> ThemeHandler::Keys() const { | |
| 202 return SingleKey(keys::kTheme); | |
| 203 } | |
| 204 | |
| 205 } // namespace extensions | |
| OLD | NEW |