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