Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Side by Side Diff: chrome/browser/ui/webui/settings/font_handler.cc

Issue 1896463003: WebUI: Add JavaScript lifecycle-control to WebUIMessageHandler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 web_ui()->RegisterMessageCallback( 52 web_ui()->RegisterMessageCallback(
53 "observeAdvancedFontExtensionAvailable", 53 "observeAdvancedFontExtensionAvailable",
54 base::Bind(&FontHandler::HandleObserveAdvancedFontExtensionAvailable, 54 base::Bind(&FontHandler::HandleObserveAdvancedFontExtensionAvailable,
55 base::Unretained(this))); 55 base::Unretained(this)));
56 web_ui()->RegisterMessageCallback( 56 web_ui()->RegisterMessageCallback(
57 "openAdvancedFontSettings", 57 "openAdvancedFontSettings",
58 base::Bind(&FontHandler::HandleOpenAdvancedFontSettings, 58 base::Bind(&FontHandler::HandleOpenAdvancedFontSettings,
59 base::Unretained(this))); 59 base::Unretained(this)));
60 } 60 }
61 61
62 void FontHandler::OnJavascriptAllowed() {
63 extension_registry_observer_.Add(
64 extensions::ExtensionRegistry::Get(profile_));
65 }
66
67 void FontHandler::OnJavascriptDisallowed() {
68 extension_registry_observer_.RemoveAll();
69 }
70
62 void FontHandler::HandleFetchFontsData(const base::ListValue* args) { 71 void FontHandler::HandleFetchFontsData(const base::ListValue* args) {
63 CHECK_EQ(1U, args->GetSize()); 72 CHECK_EQ(1U, args->GetSize());
64 std::string callback_id; 73 std::string callback_id;
65 CHECK(args->GetString(0, &callback_id)); 74 CHECK(args->GetString(0, &callback_id));
66 75
67 content::GetFontListAsync(base::Bind(&FontHandler::FontListHasLoaded, 76 content::GetFontListAsync(base::Bind(&FontHandler::FontListHasLoaded,
68 weak_ptr_factory_.GetWeakPtr(), 77 weak_ptr_factory_.GetWeakPtr(),
69 callback_id)); 78 callback_id));
70 } 79 }
71 80
72 void FontHandler::HandleObserveAdvancedFontExtensionAvailable( 81 void FontHandler::HandleObserveAdvancedFontExtensionAvailable(
73 const base::ListValue* /*args*/) { 82 const base::ListValue* /*args*/) {
74 extensions::ExtensionRegistry* observer = 83 AllowJavascript();
75 extensions::ExtensionRegistry::Get(profile_);
76 if (!extension_registry_observer_.IsObserving(observer))
77 extension_registry_observer_.Add(observer);
78 NotifyAdvancedFontSettingsAvailability(); 84 NotifyAdvancedFontSettingsAvailability();
79 } 85 }
80 86
81 void FontHandler::HandleOpenAdvancedFontSettings( 87 void FontHandler::HandleOpenAdvancedFontSettings(
82 const base::ListValue* /*args*/) { 88 const base::ListValue* /*args*/) {
83 const extensions::Extension* extension = GetAdvancedFontSettingsExtension(); 89 const extensions::Extension* extension = GetAdvancedFontSettingsExtension();
84 if (!extension) 90 if (!extension)
85 return; 91 return;
86 extensions::ExtensionTabUtil::OpenOptionsPage( 92 extensions::ExtensionTabUtil::OpenOptionsPage(
87 extension, 93 extension,
88 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents())); 94 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents()));
89 } 95 }
90 96
91 const extensions::Extension* FontHandler::GetAdvancedFontSettingsExtension() { 97 const extensions::Extension* FontHandler::GetAdvancedFontSettingsExtension() {
92 ExtensionService* service = 98 ExtensionService* service =
93 extensions::ExtensionSystem::Get(profile_)->extension_service(); 99 extensions::ExtensionSystem::Get(profile_)->extension_service();
94 if (!service->IsExtensionEnabled(kAdvancedFontSettingsExtensionId)) 100 if (!service->IsExtensionEnabled(kAdvancedFontSettingsExtensionId))
95 return nullptr; 101 return nullptr;
96 return service->GetInstalledExtension(kAdvancedFontSettingsExtensionId); 102 return service->GetInstalledExtension(kAdvancedFontSettingsExtensionId);
97 } 103 }
98 104
99 void FontHandler::NotifyAdvancedFontSettingsAvailability() { 105 void FontHandler::NotifyAdvancedFontSettingsAvailability() {
100 web_ui()->CallJavascriptFunction( 106 CallJavascriptFunction(
101 "cr.webUIListenerCallback", 107 "cr.webUIListenerCallback",
102 base::StringValue("advanced-font-settings-installed"), 108 base::StringValue("advanced-font-settings-installed"),
103 base::FundamentalValue(GetAdvancedFontSettingsExtension() != nullptr)); 109 base::FundamentalValue(GetAdvancedFontSettingsExtension() != nullptr));
104 } 110 }
105 111
106 void FontHandler::OnExtensionLoaded(content::BrowserContext*, 112 void FontHandler::OnExtensionLoaded(content::BrowserContext*,
107 const extensions::Extension*) { 113 const extensions::Extension*) {
108 NotifyAdvancedFontSettingsAvailability(); 114 NotifyAdvancedFontSettingsAvailability();
109 } 115 }
110 116
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 response.Set("encodingList", std::move(encoding_list)); 170 response.Set("encodingList", std::move(encoding_list));
165 171
166 GURL extension_url(extension_urls::GetWebstoreItemDetailURLPrefix()); 172 GURL extension_url(extension_urls::GetWebstoreItemDetailURLPrefix());
167 response.SetString( 173 response.SetString(
168 "extensionUrl", 174 "extensionUrl",
169 extension_url.Resolve(kAdvancedFontSettingsExtensionId).spec()); 175 extension_url.Resolve(kAdvancedFontSettingsExtensionId).spec());
170 176
171 ResolveJavascriptCallback(base::StringValue(callback_id), response); 177 ResolveJavascriptCallback(base::StringValue(callback_id), response);
172 } 178 }
173 179
174 void FontHandler::RenderViewReused() {
175 extension_registry_observer_.RemoveAll();
176 }
177
178 } // namespace settings 180 } // namespace settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698