Chromium Code Reviews| 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 "openAdvancedFontSettings", | |
| 54 base::Bind(&FontHandler::HandleOpenAdvancedFontSettings, | |
| 55 base::Unretained(this))); | |
| 38 } | 56 } |
| 39 | 57 |
| 40 void FontHandler::HandleFetchFontsData(const base::ListValue* args) { | 58 void FontHandler::HandleFetchFontsData(const base::ListValue* args) { |
| 41 CHECK_EQ(1U, args->GetSize()); | 59 CHECK_EQ(1U, args->GetSize()); |
| 42 std::string callback_id; | 60 std::string callback_id; |
| 43 CHECK(args->GetString(0, &callback_id)); | 61 CHECK(args->GetString(0, &callback_id)); |
| 62 extensions::ExtensionRegistry* observer = | |
| 63 extensions::ExtensionRegistry::Get(profile_); | |
| 64 if (!extension_registry_observer_.IsObserving(observer)) | |
| 65 extension_registry_observer_.Add(observer); | |
| 66 NotifyAdvancedFontSettingsAvailability(); | |
|
dpapad
2016/04/11 21:27:30
The communication between JS and C++ starts to get
dschuyler
2016/04/11 22:53:35
Done.
| |
| 44 | 67 |
| 45 content::GetFontListAsync(base::Bind(&FontHandler::FontListHasLoaded, | 68 content::GetFontListAsync(base::Bind(&FontHandler::FontListHasLoaded, |
| 46 weak_ptr_factory_.GetWeakPtr(), | 69 weak_ptr_factory_.GetWeakPtr(), |
| 47 callback_id)); | 70 callback_id)); |
| 48 } | 71 } |
| 49 | 72 |
| 73 void FontHandler::HandleOpenAdvancedFontSettings( | |
| 74 const base::ListValue* /*args*/) { | |
| 75 const extensions::Extension* extension = GetAdvancedFontSettingsExtension(); | |
| 76 if (!extension) | |
| 77 return; | |
| 78 extensions::ExtensionTabUtil::OpenOptionsPage( | |
| 79 extension, | |
| 80 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents())); | |
| 81 } | |
| 82 | |
| 83 const extensions::Extension* FontHandler::GetAdvancedFontSettingsExtension() { | |
| 84 Profile* profile = Profile::FromWebUI(web_ui()); | |
|
dpapad
2016/04/11 21:27:30
There is already a profile_ member var.
dschuyler
2016/04/11 22:53:35
Done.
| |
| 85 ExtensionService* service = | |
| 86 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
| 87 if (!service->IsExtensionEnabled(kAdvancedFontSettingsExtensionId)) | |
| 88 return nullptr; | |
| 89 return service->GetInstalledExtension(kAdvancedFontSettingsExtensionId); | |
| 90 } | |
| 91 | |
| 92 void FontHandler::NotifyAdvancedFontSettingsAvailability() { | |
| 93 web_ui()->CallJavascriptFunction( | |
| 94 "cr.webUIListenerCallback", | |
| 95 base::StringValue("advanced-font-settings-installed"), | |
| 96 base::FundamentalValue(GetAdvancedFontSettingsExtension() != nullptr)); | |
| 97 } | |
| 98 | |
| 99 void FontHandler::OnExtensionLoaded( | |
| 100 content::BrowserContext* /*browser_context*/, | |
|
dpapad
2016/04/11 21:27:30
Nit: The "/* foo */" inlined comments here and bel
dschuyler
2016/04/11 22:53:35
Done.
| |
| 101 const extensions::Extension* /*extension*/) { | |
| 102 NotifyAdvancedFontSettingsAvailability(); | |
| 103 } | |
| 104 | |
| 105 void FontHandler::OnExtensionUnloaded( | |
| 106 content::BrowserContext* /*browser_context*/, | |
| 107 const extensions::Extension* /*extension*/, | |
| 108 extensions::UnloadedExtensionInfo::Reason /*reason*/) { | |
| 109 NotifyAdvancedFontSettingsAvailability(); | |
| 110 } | |
| 111 | |
| 50 void FontHandler::FontListHasLoaded(std::string callback_id, | 112 void FontHandler::FontListHasLoaded(std::string callback_id, |
| 51 std::unique_ptr<base::ListValue> list) { | 113 std::unique_ptr<base::ListValue> list) { |
| 52 // Font list. Selects the directionality for the fonts in the given list. | 114 // Font list. Selects the directionality for the fonts in the given list. |
| 53 for (size_t i = 0; i < list->GetSize(); i++) { | 115 for (size_t i = 0; i < list->GetSize(); i++) { |
| 54 base::ListValue* font; | 116 base::ListValue* font; |
| 55 bool has_font = list->GetList(i, &font); | 117 bool has_font = list->GetList(i, &font); |
| 56 DCHECK(has_font); | 118 DCHECK(has_font); |
| 57 | 119 |
| 58 base::string16 value; | 120 base::string16 value; |
| 59 bool has_value = font->GetString(1, &value); | 121 bool has_value = font->GetString(1, &value); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 84 base::i18n::StringContainsStrongRTLChars(it.encoding_display_name) | 146 base::i18n::StringContainsStrongRTLChars(it.encoding_display_name) |
| 85 ? "rtl" | 147 ? "rtl" |
| 86 : "ltr"); | 148 : "ltr"); |
| 87 } else { | 149 } else { |
| 88 // Add empty value to indicate a separator item. | 150 // Add empty value to indicate a separator item. |
| 89 option->AppendString(std::string()); | 151 option->AppendString(std::string()); |
| 90 } | 152 } |
| 91 encoding_list->Append(std::move(option)); | 153 encoding_list->Append(std::move(option)); |
| 92 } | 154 } |
| 93 | 155 |
| 156 GURL extension_url(extension_urls::GetWebstoreItemDetailURLPrefix()); | |
| 157 std::unique_ptr<base::StringValue> advancedExtensionUrl(new base::StringValue( | |
|
dpapad
2016/04/11 21:27:30
Is the use of a pointer necessary here? I think it
dschuyler
2016/04/11 22:53:35
Done.
| |
| 158 extension_url.Resolve(kAdvancedFontSettingsExtensionId).spec())); | |
| 159 | |
| 94 base::DictionaryValue response; | 160 base::DictionaryValue response; |
| 95 response.Set("fontList", std::move(list)); | 161 response.Set("fontList", std::move(list)); |
| 96 response.Set("encodingList", std::move(encoding_list)); | 162 response.Set("encodingList", std::move(encoding_list)); |
| 163 response.Set("extensionUrl", std::move(advancedExtensionUrl)); | |
|
dpapad
2016/04/11 21:27:30
1) How about using SetString() instead?
2) See exa
dschuyler
2016/04/11 22:53:35
Done.
| |
| 97 | 164 |
| 98 ResolveJavascriptCallback(base::StringValue(callback_id), response); | 165 ResolveJavascriptCallback(base::StringValue(callback_id), response); |
| 99 } | 166 } |
| 100 | 167 |
| 168 void FontHandler::RenderViewReused() { | |
| 169 extension_registry_observer_.RemoveAll(); | |
| 170 } | |
| 171 | |
| 101 } // namespace settings | 172 } // namespace settings |
| OLD | NEW |