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

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 extensions::ExtensionRegistry* observer =
64 extensions::ExtensionRegistry::Get(profile_);
Dan Beam 2016/04/18 23:13:20 indent off
tommycli 2016/04/19 17:45:38 Done.
65 if (!extension_registry_observer_.IsObserving(observer))
Dan Beam 2016/04/18 23:13:20 why do you need this if?
tommycli 2016/04/19 17:45:37 Done.
66 extension_registry_observer_.Add(observer);
67 }
68
69 void FontHandler::OnJavascriptDisallowed() {
70 extension_registry_observer_.RemoveAll();
71 }
72
62 void FontHandler::HandleFetchFontsData(const base::ListValue* args) { 73 void FontHandler::HandleFetchFontsData(const base::ListValue* args) {
63 CHECK_EQ(1U, args->GetSize()); 74 CHECK_EQ(1U, args->GetSize());
64 std::string callback_id; 75 std::string callback_id;
65 CHECK(args->GetString(0, &callback_id)); 76 CHECK(args->GetString(0, &callback_id));
66 77
67 content::GetFontListAsync(base::Bind(&FontHandler::FontListHasLoaded, 78 content::GetFontListAsync(base::Bind(&FontHandler::FontListHasLoaded,
68 weak_ptr_factory_.GetWeakPtr(), 79 weak_ptr_factory_.GetWeakPtr(),
69 callback_id)); 80 callback_id));
70 } 81 }
71 82
72 void FontHandler::HandleObserveAdvancedFontExtensionAvailable( 83 void FontHandler::HandleObserveAdvancedFontExtensionAvailable(
73 const base::ListValue* /*args*/) { 84 const base::ListValue* /*args*/) {
74 extensions::ExtensionRegistry* observer = 85 AllowJavascript();
75 extensions::ExtensionRegistry::Get(profile_);
76 if (!extension_registry_observer_.IsObserving(observer))
77 extension_registry_observer_.Add(observer);
78 NotifyAdvancedFontSettingsAvailability(); 86 NotifyAdvancedFontSettingsAvailability();
79 } 87 }
80 88
81 void FontHandler::HandleOpenAdvancedFontSettings( 89 void FontHandler::HandleOpenAdvancedFontSettings(
82 const base::ListValue* /*args*/) { 90 const base::ListValue* /*args*/) {
83 const extensions::Extension* extension = GetAdvancedFontSettingsExtension(); 91 const extensions::Extension* extension = GetAdvancedFontSettingsExtension();
84 if (!extension) 92 if (!extension)
85 return; 93 return;
86 extensions::ExtensionTabUtil::OpenOptionsPage( 94 extensions::ExtensionTabUtil::OpenOptionsPage(
87 extension, 95 extension,
88 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents())); 96 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents()));
89 } 97 }
90 98
91 const extensions::Extension* FontHandler::GetAdvancedFontSettingsExtension() { 99 const extensions::Extension* FontHandler::GetAdvancedFontSettingsExtension() {
92 ExtensionService* service = 100 ExtensionService* service =
93 extensions::ExtensionSystem::Get(profile_)->extension_service(); 101 extensions::ExtensionSystem::Get(profile_)->extension_service();
94 if (!service->IsExtensionEnabled(kAdvancedFontSettingsExtensionId)) 102 if (!service->IsExtensionEnabled(kAdvancedFontSettingsExtensionId))
95 return nullptr; 103 return nullptr;
96 return service->GetInstalledExtension(kAdvancedFontSettingsExtensionId); 104 return service->GetInstalledExtension(kAdvancedFontSettingsExtensionId);
97 } 105 }
98 106
99 void FontHandler::NotifyAdvancedFontSettingsAvailability() { 107 void FontHandler::NotifyAdvancedFontSettingsAvailability() {
100 web_ui()->CallJavascriptFunction( 108 CallJavascriptFunction(
101 "cr.webUIListenerCallback", 109 "cr.webUIListenerCallback",
102 base::StringValue("advanced-font-settings-installed"), 110 base::StringValue("advanced-font-settings-installed"),
103 base::FundamentalValue(GetAdvancedFontSettingsExtension() != nullptr)); 111 base::FundamentalValue(GetAdvancedFontSettingsExtension() != nullptr));
104 } 112 }
105 113
106 void FontHandler::OnExtensionLoaded(content::BrowserContext*, 114 void FontHandler::OnExtensionLoaded(content::BrowserContext*,
107 const extensions::Extension*) { 115 const extensions::Extension*) {
108 NotifyAdvancedFontSettingsAvailability(); 116 NotifyAdvancedFontSettingsAvailability();
109 } 117 }
110 118
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 response.Set("encodingList", std::move(encoding_list)); 172 response.Set("encodingList", std::move(encoding_list));
165 173
166 GURL extension_url(extension_urls::GetWebstoreItemDetailURLPrefix()); 174 GURL extension_url(extension_urls::GetWebstoreItemDetailURLPrefix());
167 response.SetString( 175 response.SetString(
168 "extensionUrl", 176 "extensionUrl",
169 extension_url.Resolve(kAdvancedFontSettingsExtensionId).spec()); 177 extension_url.Resolve(kAdvancedFontSettingsExtensionId).spec());
170 178
171 ResolveJavascriptCallback(base::StringValue(callback_id), response); 179 ResolveJavascriptCallback(base::StringValue(callback_id), response);
172 } 180 }
173 181
174 void FontHandler::RenderViewReused() {
175 extension_registry_observer_.RemoveAll();
176 }
177
178 } // namespace settings 182 } // namespace settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698