Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_font_settings_api.h" | 5 #include "chrome/browser/extensions/extension_font_settings_api.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_preference_helpers.h" | 11 #include "chrome/browser/extensions/extension_preference_helpers.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/extensions/extension_error_utils.h" | 14 #include "chrome/common/extensions/extension_error_utils.h" |
| 15 #include "chrome/common/pref_names.h" | |
| 15 #include "content/public/browser/font_list_async.h" | 16 #include "content/public/browser/font_list_async.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const char kGenericFamilyKey[] = "genericFamily"; | 20 const char kGenericFamilyKey[] = "genericFamily"; |
| 20 const char kFontNameKey[] = "fontName"; | 21 const char kFontNameKey[] = "fontName"; |
| 21 const char kLocalizedNameKey[] = "localizedName"; | 22 const char kLocalizedNameKey[] = "localizedName"; |
| 23 const char kPixelSizeKey[] = "pixelSize"; | |
| 22 const char kScriptKey[] = "script"; | 24 const char kScriptKey[] = "script"; |
| 23 | 25 |
| 24 // Format for per-script font preference keys. | 26 // Format for per-script font preference keys. |
| 25 // E.g., "webkit.webprefs.fonts.standard.Hrkt" | 27 // E.g., "webkit.webprefs.fonts.standard.Hrkt" |
| 26 const char kWebKitPerScriptFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; | 28 const char kWebKitPerScriptFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; |
| 27 | 29 |
| 28 // Format for global (non per-script) font preference keys. | 30 // Format for global (non per-script) font preference keys. |
| 29 // E.g., "webkit.webprefs.global.fixed_font_family" | 31 // E.g., "webkit.webprefs.global.fixed_font_family" |
| 30 // Note: there are two meanings of "global" here. The "Global" in the const name | 32 // Note: there are two meanings of "global" here. The "Global" in the const name |
| 31 // means "not per-script". The "global" in the key itself means "not per-tab" | 33 // means "not per-script". The "global" in the key itself means "not per-tab" |
| 32 // (per-profile). | 34 // (per-profile). |
| 33 const char kWebKitGlobalFontPrefFormat[] = | 35 const char kWebKitGlobalFontPrefFormat[] = |
| 34 "webkit.webprefs.global.%s_font_family"; | 36 "webkit.webprefs.global.%s_font_family"; |
| 35 | 37 |
| 36 // Gets the font name preference path from |details| which contains key | 38 // Gets the font name preference path from |details| which contains key |
| 37 // |kGenericFamilyKey| and optionally |kScriptKey|. | 39 // |kGenericFamilyKey| and optionally |kScriptKey|. |
| 38 bool GetFontNamePrefPath(DictionaryValue* details, std::string* pref_path) | 40 bool GetFontNamePrefPath(DictionaryValue* details, std::string* pref_path) { |
| 39 { | |
| 40 std::string generic_family; | 41 std::string generic_family; |
| 41 if (!details->GetString(kGenericFamilyKey, &generic_family)) | 42 if (!details->GetString(kGenericFamilyKey, &generic_family)) |
| 42 return false; | 43 return false; |
| 43 | 44 |
| 44 if (details->HasKey(kScriptKey)) { | 45 if (details->HasKey(kScriptKey)) { |
| 45 std::string script; | 46 std::string script; |
| 46 if (!details->GetString(kScriptKey, &script)) | 47 if (!details->GetString(kScriptKey, &script)) |
| 47 return false; | 48 return false; |
| 48 *pref_path = StringPrintf(kWebKitPerScriptFontPrefFormat, | 49 *pref_path = StringPrintf(kWebKitPerScriptFontPrefFormat, |
| 49 generic_family.c_str(), | 50 generic_family.c_str(), |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 content::GetFontListAsync( | 104 content::GetFontListAsync( |
| 104 Bind(&GetFontListFunction::FontListHasLoaded, this)); | 105 Bind(&GetFontListFunction::FontListHasLoaded, this)); |
| 105 return true; | 106 return true; |
| 106 } | 107 } |
| 107 | 108 |
| 108 void GetFontListFunction::FontListHasLoaded(scoped_ptr<ListValue> list) { | 109 void GetFontListFunction::FontListHasLoaded(scoped_ptr<ListValue> list) { |
| 109 bool success = CopyFontsToResult(list.get()); | 110 bool success = CopyFontsToResult(list.get()); |
| 110 SendResponse(success); | 111 SendResponse(success); |
| 111 } | 112 } |
| 112 | 113 |
| 113 bool GetFontListFunction::CopyFontsToResult(ListValue* fonts) | 114 bool GetFontListFunction::CopyFontsToResult(ListValue* fonts) { |
| 114 { | |
| 115 scoped_ptr<ListValue> result(new ListValue()); | 115 scoped_ptr<ListValue> result(new ListValue()); |
| 116 for (ListValue::iterator it = fonts->begin(); it != fonts->end(); ++it) { | 116 for (ListValue::iterator it = fonts->begin(); it != fonts->end(); ++it) { |
| 117 ListValue* font_list_value; | 117 ListValue* font_list_value; |
| 118 if (!(*it)->GetAsList(&font_list_value)) { | 118 if (!(*it)->GetAsList(&font_list_value)) { |
| 119 NOTREACHED(); | 119 NOTREACHED(); |
| 120 return false; | 120 return false; |
| 121 } | 121 } |
| 122 | 122 |
| 123 std::string name; | 123 std::string name; |
| 124 if (!font_list_value->GetString(0, &name)) { | 124 if (!font_list_value->GetString(0, &name)) { |
| 125 NOTREACHED(); | 125 NOTREACHED(); |
| 126 return false; | 126 return false; |
| 127 } | 127 } |
| 128 | 128 |
| 129 std::string localized_name; | 129 std::string localized_name; |
| 130 if (!font_list_value->GetString(1, &localized_name)) { | 130 if (!font_list_value->GetString(1, &localized_name)) { |
| 131 NOTREACHED(); | 131 NOTREACHED(); |
| 132 return false; | 132 return false; |
| 133 } | 133 } |
| 134 | 134 |
| 135 DictionaryValue* font_name = new DictionaryValue(); | 135 DictionaryValue* font_name = new DictionaryValue(); |
| 136 font_name->Set(kFontNameKey, Value::CreateStringValue(name)); | 136 font_name->Set(kFontNameKey, Value::CreateStringValue(name)); |
| 137 font_name->Set(kLocalizedNameKey, Value::CreateStringValue(localized_name)); | 137 font_name->Set(kLocalizedNameKey, Value::CreateStringValue(localized_name)); |
| 138 result->Append(font_name); | 138 result->Append(font_name); |
| 139 } | 139 } |
| 140 | 140 |
| 141 result_.reset(result.release()); | 141 result_.reset(result.release()); |
| 142 return true; | 142 return true; |
| 143 } | 143 } |
| 144 | |
| 145 bool GetDefaultFontSizeFunction::RunImpl() { | |
| 146 PrefService* prefs = profile_->GetPrefs(); | |
| 147 int size = prefs->GetInteger(prefs::kWebKitGlobalDefaultFontSize); | |
| 148 | |
| 149 DictionaryValue* result = new DictionaryValue(); | |
|
Matt Perry
2012/03/23 18:32:53
why not just result_.reset(new DictionaryValue())
falken
2012/03/24 04:27:13
That would require the next line to be rather unwi
Matt Perry
2012/03/26 18:31:18
Ah, I forgot result_ was not a DictionaryValue. Ne
| |
| 150 result->SetInteger(kPixelSizeKey, size); | |
| 151 result_.reset(result); | |
| 152 return true; | |
| 153 } | |
| 154 | |
| 155 bool SetDefaultFontSizeFunction::RunImpl() { | |
| 156 DictionaryValue* details = NULL; | |
| 157 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | |
| 158 | |
| 159 int size; | |
| 160 EXTENSION_FUNCTION_VALIDATE(details->GetInteger(kPixelSizeKey, &size)); | |
| 161 | |
| 162 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | |
| 163 prefs->SetExtensionControlledPref(extension_id(), | |
| 164 prefs::kWebKitGlobalDefaultFontSize, | |
| 165 kExtensionPrefsScopeRegular, | |
| 166 Value::CreateIntegerValue(size)); | |
| 167 return true; | |
| 168 } | |
| OLD | NEW |