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