| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ui/webui/settings/font_handler.h" | 5 #include "chrome/browser/ui/webui/settings/font_handler.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/i18n/rtl.h" | 13 #include "base/i18n/rtl.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/character_encoding.h" | 15 #include "chrome/browser/character_encoding.h" |
| 16 #include "chrome/browser/extensions/extension_service.h" |
| 17 #include "chrome/browser/extensions/extension_tab_util.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/browser_finder.h" |
| 17 #include "chrome/browser/ui/webui/options/font_settings_utils.h" | 20 #include "chrome/browser/ui/webui/options/font_settings_utils.h" |
| 18 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 19 #include "components/prefs/pref_service.h" | 22 #include "components/prefs/pref_service.h" |
| 20 #include "content/public/browser/font_list_async.h" | 23 #include "content/public/browser/font_list_async.h" |
| 21 #include "content/public/browser/web_ui.h" | 24 #include "content/public/browser/web_ui.h" |
| 25 #include "extensions/browser/extension_registry.h" |
| 26 #include "extensions/browser/extension_system.h" |
| 27 #include "extensions/common/extension_urls.h" |
| 28 |
| 29 namespace { |
| 30 |
| 31 const char kAdvancedFontSettingsExtensionId[] = |
| 32 "caclkomlalccbpcdllchkeecicepbmbm"; |
| 33 |
| 34 } // namespace |
| 22 | 35 |
| 23 namespace settings { | 36 namespace settings { |
| 24 | 37 |
| 25 FontHandler::FontHandler(content::WebUI* webui) | 38 FontHandler::FontHandler(content::WebUI* webui) |
| 26 : profile_(Profile::FromWebUI(webui)), | 39 : extension_registry_observer_(this), |
| 40 profile_(Profile::FromWebUI(webui)), |
| 27 weak_ptr_factory_(this) { | 41 weak_ptr_factory_(this) { |
| 28 // Perform validation for saved fonts. | 42 // Perform validation for saved fonts. |
| 29 options::FontSettingsUtilities::ValidateSavedFonts(profile_->GetPrefs()); | 43 options::FontSettingsUtilities::ValidateSavedFonts(profile_->GetPrefs()); |
| 30 } | 44 } |
| 31 | 45 |
| 32 FontHandler::~FontHandler() {} | 46 FontHandler::~FontHandler() {} |
| 33 | 47 |
| 34 void FontHandler::RegisterMessages() { | 48 void FontHandler::RegisterMessages() { |
| 35 web_ui()->RegisterMessageCallback( | 49 web_ui()->RegisterMessageCallback( |
| 36 "fetchFontsData", base::Bind(&FontHandler::HandleFetchFontsData, | 50 "fetchFontsData", base::Bind(&FontHandler::HandleFetchFontsData, |
| 37 base::Unretained(this))); | 51 base::Unretained(this))); |
| 52 web_ui()->RegisterMessageCallback( |
| 53 "observeAdvancedFontExtensionAvailable", |
| 54 base::Bind(&FontHandler::HandleObserveAdvancedFontExtensionAvailable, |
| 55 base::Unretained(this))); |
| 56 web_ui()->RegisterMessageCallback( |
| 57 "openAdvancedFontSettings", |
| 58 base::Bind(&FontHandler::HandleOpenAdvancedFontSettings, |
| 59 base::Unretained(this))); |
| 38 } | 60 } |
| 39 | 61 |
| 40 void FontHandler::HandleFetchFontsData(const base::ListValue* args) { | 62 void FontHandler::HandleFetchFontsData(const base::ListValue* args) { |
| 41 CHECK_EQ(1U, args->GetSize()); | 63 CHECK_EQ(1U, args->GetSize()); |
| 42 std::string callback_id; | 64 std::string callback_id; |
| 43 CHECK(args->GetString(0, &callback_id)); | 65 CHECK(args->GetString(0, &callback_id)); |
| 44 | 66 |
| 45 content::GetFontListAsync(base::Bind(&FontHandler::FontListHasLoaded, | 67 content::GetFontListAsync(base::Bind(&FontHandler::FontListHasLoaded, |
| 46 weak_ptr_factory_.GetWeakPtr(), | 68 weak_ptr_factory_.GetWeakPtr(), |
| 47 callback_id)); | 69 callback_id)); |
| 48 } | 70 } |
| 49 | 71 |
| 72 void FontHandler::HandleObserveAdvancedFontExtensionAvailable( |
| 73 const base::ListValue* /*args*/) { |
| 74 extensions::ExtensionRegistry* observer = |
| 75 extensions::ExtensionRegistry::Get(profile_); |
| 76 if (!extension_registry_observer_.IsObserving(observer)) |
| 77 extension_registry_observer_.Add(observer); |
| 78 NotifyAdvancedFontSettingsAvailability(); |
| 79 } |
| 80 |
| 81 void FontHandler::HandleOpenAdvancedFontSettings( |
| 82 const base::ListValue* /*args*/) { |
| 83 const extensions::Extension* extension = GetAdvancedFontSettingsExtension(); |
| 84 if (!extension) |
| 85 return; |
| 86 extensions::ExtensionTabUtil::OpenOptionsPage( |
| 87 extension, |
| 88 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents())); |
| 89 } |
| 90 |
| 91 const extensions::Extension* FontHandler::GetAdvancedFontSettingsExtension() { |
| 92 ExtensionService* service = |
| 93 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| 94 if (!service->IsExtensionEnabled(kAdvancedFontSettingsExtensionId)) |
| 95 return nullptr; |
| 96 return service->GetInstalledExtension(kAdvancedFontSettingsExtensionId); |
| 97 } |
| 98 |
| 99 void FontHandler::NotifyAdvancedFontSettingsAvailability() { |
| 100 web_ui()->CallJavascriptFunction( |
| 101 "cr.webUIListenerCallback", |
| 102 base::StringValue("advanced-font-settings-installed"), |
| 103 base::FundamentalValue(GetAdvancedFontSettingsExtension() != nullptr)); |
| 104 } |
| 105 |
| 106 void FontHandler::OnExtensionLoaded(content::BrowserContext*, |
| 107 const extensions::Extension*) { |
| 108 NotifyAdvancedFontSettingsAvailability(); |
| 109 } |
| 110 |
| 111 void FontHandler::OnExtensionUnloaded( |
| 112 content::BrowserContext*, |
| 113 const extensions::Extension*, |
| 114 extensions::UnloadedExtensionInfo::Reason) { |
| 115 NotifyAdvancedFontSettingsAvailability(); |
| 116 } |
| 117 |
| 50 void FontHandler::FontListHasLoaded(std::string callback_id, | 118 void FontHandler::FontListHasLoaded(std::string callback_id, |
| 51 std::unique_ptr<base::ListValue> list) { | 119 std::unique_ptr<base::ListValue> list) { |
| 52 // Font list. Selects the directionality for the fonts in the given list. | 120 // Font list. Selects the directionality for the fonts in the given list. |
| 53 for (size_t i = 0; i < list->GetSize(); i++) { | 121 for (size_t i = 0; i < list->GetSize(); i++) { |
| 54 base::ListValue* font; | 122 base::ListValue* font; |
| 55 bool has_font = list->GetList(i, &font); | 123 bool has_font = list->GetList(i, &font); |
| 56 DCHECK(has_font); | 124 DCHECK(has_font); |
| 57 | 125 |
| 58 base::string16 value; | 126 base::string16 value; |
| 59 bool has_value = font->GetString(1, &value); | 127 bool has_value = font->GetString(1, &value); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 88 // Add empty value to indicate a separator item. | 156 // Add empty value to indicate a separator item. |
| 89 option->AppendString(std::string()); | 157 option->AppendString(std::string()); |
| 90 } | 158 } |
| 91 encoding_list->Append(std::move(option)); | 159 encoding_list->Append(std::move(option)); |
| 92 } | 160 } |
| 93 | 161 |
| 94 base::DictionaryValue response; | 162 base::DictionaryValue response; |
| 95 response.Set("fontList", std::move(list)); | 163 response.Set("fontList", std::move(list)); |
| 96 response.Set("encodingList", std::move(encoding_list)); | 164 response.Set("encodingList", std::move(encoding_list)); |
| 97 | 165 |
| 166 GURL extension_url(extension_urls::GetWebstoreItemDetailURLPrefix()); |
| 167 response.SetString( |
| 168 "extensionUrl", |
| 169 extension_url.Resolve(kAdvancedFontSettingsExtensionId).spec()); |
| 170 |
| 98 ResolveJavascriptCallback(base::StringValue(callback_id), response); | 171 ResolveJavascriptCallback(base::StringValue(callback_id), response); |
| 99 } | 172 } |
| 100 | 173 |
| 174 void FontHandler::RenderViewReused() { |
| 175 extension_registry_observer_.RemoveAll(); |
| 176 } |
| 177 |
| 101 } // namespace settings | 178 } // namespace settings |
| OLD | NEW |