Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/components_ui.h" | 5 #include "chrome/browser/ui/webui/components_ui.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 virtual void RegisterMessages() OVERRIDE; | 69 virtual void RegisterMessages() OVERRIDE; |
| 70 | 70 |
| 71 // Callback for the "requestComponentsData" message. | 71 // Callback for the "requestComponentsData" message. |
| 72 void HandleRequestComponentsData(const base::ListValue* args); | 72 void HandleRequestComponentsData(const base::ListValue* args); |
| 73 | 73 |
| 74 // Callback for the "checkUpdate" message. | 74 // Callback for the "checkUpdate" message. |
| 75 void HandleCheckUpdate(const base::ListValue* args); | 75 void HandleCheckUpdate(const base::ListValue* args); |
| 76 | 76 |
| 77 private: | 77 private: |
| 78 void LoadComponents(); | 78 void LoadComponents(); |
| 79 void ComponentsDOMHandler::HandleRequestComponentStatus( | |
| 80 const base::ListValue* args); | |
| 79 | 81 |
| 80 content::NotificationRegistrar registrar_; | 82 content::NotificationRegistrar registrar_; |
| 81 | 83 |
| 82 DISALLOW_COPY_AND_ASSIGN(ComponentsDOMHandler); | 84 DISALLOW_COPY_AND_ASSIGN(ComponentsDOMHandler); |
| 83 }; | 85 }; |
| 84 | 86 |
| 85 ComponentsDOMHandler::ComponentsDOMHandler() { | 87 ComponentsDOMHandler::ComponentsDOMHandler() { |
| 86 } | 88 } |
| 87 | 89 |
| 88 void ComponentsDOMHandler::RegisterMessages() { | 90 void ComponentsDOMHandler::RegisterMessages() { |
| 89 web_ui()->RegisterMessageCallback("requestComponentsData", | 91 web_ui()->RegisterMessageCallback( |
| 92 "requestComponentsData", | |
| 90 base::Bind(&ComponentsDOMHandler::HandleRequestComponentsData, | 93 base::Bind(&ComponentsDOMHandler::HandleRequestComponentsData, |
| 91 base::Unretained(this))); | 94 base::Unretained(this))); |
| 92 | 95 |
| 93 web_ui()->RegisterMessageCallback("checkUpdate", | 96 web_ui()->RegisterMessageCallback( |
| 97 "checkUpdate", | |
| 94 base::Bind(&ComponentsDOMHandler::HandleCheckUpdate, | 98 base::Bind(&ComponentsDOMHandler::HandleCheckUpdate, |
| 95 base::Unretained(this))); | 99 base::Unretained(this))); |
| 100 | |
| 101 web_ui()->RegisterMessageCallback( | |
| 102 "requestComponentStatus", | |
| 103 base::Bind(&ComponentsDOMHandler::HandleRequestComponentStatus, | |
| 104 base::Unretained(this))); | |
| 96 } | 105 } |
| 97 | 106 |
| 98 void ComponentsDOMHandler::HandleRequestComponentsData( | 107 void ComponentsDOMHandler::HandleRequestComponentsData( |
| 99 const base::ListValue* args) { | 108 const base::ListValue* args) { |
| 100 LoadComponents(); | 109 LoadComponents(); |
| 101 } | 110 } |
| 102 | 111 |
| 103 // This function is called when user presses button from html UI. | 112 // This function is called when user presses button from html UI. |
| 104 // TODO(shrikant): We need to make this button available based on current | 113 // TODO(shrikant): We need to make this button available based on current |
| 105 // state e.g. If component state is currently updating then we need to disable | 114 // state e.g. If component state is currently updating then we need to disable |
| 106 // button. (https://code.google.com/p/chromium/issues/detail?id=272540) | 115 // button. (https://code.google.com/p/chromium/issues/detail?id=272540) |
| 107 void ComponentsDOMHandler::HandleCheckUpdate(const base::ListValue* args) { | 116 void ComponentsDOMHandler::HandleCheckUpdate(const base::ListValue* args) { |
| 108 if (args->GetSize() != 1) { | 117 if (args->GetSize() != 1) { |
| 109 NOTREACHED(); | 118 NOTREACHED(); |
| 110 return; | 119 return; |
| 111 } | 120 } |
| 112 | 121 |
| 113 std::string component_id; | 122 std::string component_id; |
| 114 if (!args->GetString(0, &component_id)) { | 123 if (!args->GetString(0, &component_id)) { |
| 115 NOTREACHED(); | 124 NOTREACHED(); |
| 116 return; | 125 return; |
| 117 } | 126 } |
| 118 | 127 |
| 119 ComponentsUI::OnDemandUpdate(component_id); | 128 ComponentsUI::OnDemandUpdate(component_id); |
| 120 } | 129 } |
| 121 | 130 |
| 131 void ComponentsDOMHandler::HandleRequestComponentStatus( | |
| 132 const base::ListValue* args) { | |
| 133 if (args->GetSize() != 1) { | |
| 134 NOTREACHED(); | |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 std::string component_id; | |
| 139 if (!args->GetString(0, &component_id)) { | |
| 140 NOTREACHED(); | |
| 141 return; | |
| 142 } | |
| 143 | |
| 144 component_updater::ComponentUpdateService* cus = | |
| 145 g_browser_process->component_updater(); | |
| 146 component_updater::ComponentUpdateService::Status status = | |
| 147 cus->GetComponentStatus(component_id); | |
| 148 | |
| 149 std::string string_status; | |
| 150 if (status == component_updater::ComponentUpdateService::kInProgress) | |
| 151 string_status = "in-progress"; | |
| 152 else if (status == component_updater::ComponentUpdateService::kOk) | |
| 153 string_status = "ok"; | |
| 154 else | |
| 155 string_status = "error"; | |
| 156 | |
| 157 web_ui()->CallJavascriptFunction("returnComponentStatus", | |
| 158 base::StringValue(component_id), | |
| 159 base::StringValue(string_status)); | |
| 160 } | |
| 161 | |
| 122 void ComponentsDOMHandler::LoadComponents() { | 162 void ComponentsDOMHandler::LoadComponents() { |
| 123 component_updater::ComponentUpdateService* cus = | 163 component_updater::ComponentUpdateService* cus = |
| 124 g_browser_process->component_updater(); | 164 g_browser_process->component_updater(); |
| 125 std::vector<component_updater::CrxComponentInfo> components; | 165 std::vector<component_updater::CrxComponentInfo> components; |
| 126 cus->GetComponents(&components); | 166 cus->GetComponents(&components); |
| 127 | 167 |
| 128 // Construct DictionaryValues to return to UI. | 168 // Construct DictionaryValues to return to UI. |
| 129 base::ListValue* component_list = new base::ListValue(); | 169 base::ListValue* component_list = new base::ListValue(); |
| 130 for (size_t j = 0; j < components.size(); ++j) { | 170 for (size_t j = 0; j < components.size(); ++j) { |
| 131 const component_updater::CrxComponentInfo& component = components[j]; | 171 const component_updater::CrxComponentInfo& component = components[j]; |
| 132 | 172 |
| 133 base::DictionaryValue* component_entry = new base::DictionaryValue(); | 173 base::DictionaryValue* component_entry = new base::DictionaryValue(); |
| 134 component_entry->SetString("id", component.id); | 174 component_entry->SetString("id", component.id); |
| 135 component_entry->SetString("name", component.name); | 175 component_entry->SetString("name", component.name); |
| 136 component_entry->SetString("version", component.version); | 176 component_entry->SetString("version", component.version); |
| 177 switch (component.status) { | |
| 178 case component_updater::ComponentUpdateService::kInProgress: | |
| 179 component_entry->SetString("status", "in-progress"); | |
| 180 break; | |
| 181 case component_updater::ComponentUpdateService::kOk: | |
| 182 component_entry->SetString("status", "ok"); | |
| 183 break; | |
| 184 case component_updater::ComponentUpdateService::kError: | |
| 185 component_entry->SetString("status", "error"); | |
| 186 break; | |
| 187 } | |
| 137 | 188 |
| 138 component_list->Append(component_entry); | 189 component_list->Append(component_entry); |
| 139 } | 190 } |
| 140 | 191 |
| 141 base::DictionaryValue results; | 192 base::DictionaryValue results; |
| 142 results.Set("components", component_list); | 193 results.Set("components", component_list); |
| 143 web_ui()->CallJavascriptFunction("returnComponentsData", results); | 194 web_ui()->CallJavascriptFunction("returnComponentsData", results); |
| 144 } | 195 } |
| 145 | 196 |
| 146 } // namespace | 197 } // namespace |
| 147 | 198 |
| 148 /////////////////////////////////////////////////////////////////////////////// | 199 /////////////////////////////////////////////////////////////////////////////// |
| 149 // | 200 // |
| 150 // ComponentsUI | 201 // ComponentsUI |
| 151 // | 202 // |
| 152 /////////////////////////////////////////////////////////////////////////////// | 203 /////////////////////////////////////////////////////////////////////////////// |
| 153 | 204 |
| 154 ComponentsUI::ComponentsUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 205 ComponentsUI::ComponentsUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| 155 web_ui->AddMessageHandler(new ComponentsDOMHandler()); | 206 web_ui->AddMessageHandler(new ComponentsDOMHandler()); |
| 156 | 207 |
| 157 // Set up the chrome://components/ source. | 208 // Set up the chrome://components/ source. |
| 158 Profile* profile = Profile::FromWebUI(web_ui); | 209 Profile* profile = Profile::FromWebUI(web_ui); |
| 159 content::WebUIDataSource::Add(profile, CreateComponentsUIHTMLSource(profile)); | 210 content::WebUIDataSource::Add(profile, CreateComponentsUIHTMLSource(profile)); |
| 211 component_updater::ComponentUpdateService* cus = | |
| 212 g_browser_process->component_updater(); | |
| 213 cus->AddObserver(this); | |
|
Sorin Jianu
2014/04/28 22:46:11
Are we removing the observer anywhere? Is there an
| |
| 160 } | 214 } |
| 161 | 215 |
| 162 // static | 216 // static |
| 163 void ComponentsUI::OnDemandUpdate(const std::string& component_id) { | 217 void ComponentsUI::OnDemandUpdate(const std::string& component_id) { |
| 164 component_updater::ComponentUpdateService* cus = | 218 component_updater::ComponentUpdateService* cus = |
| 165 g_browser_process->component_updater(); | 219 g_browser_process->component_updater(); |
| 166 cus->OnDemandUpdate(component_id); | 220 cus->OnDemandUpdate(component_id); |
| 167 } | 221 } |
| 168 | 222 |
| 169 // static | 223 // static |
| 170 base::RefCountedMemory* ComponentsUI::GetFaviconResourceBytes( | 224 base::RefCountedMemory* ComponentsUI::GetFaviconResourceBytes( |
| 171 ui::ScaleFactor scale_factor) { | 225 ui::ScaleFactor scale_factor) { |
| 172 return ResourceBundle::GetSharedInstance(). | 226 return ResourceBundle::GetSharedInstance(). |
| 173 LoadDataResourceBytesForScale(IDR_PLUGINS_FAVICON, scale_factor); | 227 LoadDataResourceBytesForScale(IDR_PLUGINS_FAVICON, scale_factor); |
| 174 } | 228 } |
| 229 | |
| 230 void ComponentsUI::ComponentEventToString(Events event, | |
| 231 std::string* converted_event) { | |
| 232 DCHECK(converted_event != NULL); | |
|
Sorin Jianu
2014/04/28 22:46:11
It seems converted_event is some kind of a an even
| |
| 233 switch (event) { | |
| 234 case COMPONENT_UPDATER_STARTED: | |
| 235 *converted_event = "UpdaterStarted"; | |
| 236 return; | |
| 237 case COMPONENT_UPDATER_SLEEPING: | |
| 238 *converted_event = "UpdaterSleeping"; | |
| 239 return; | |
| 240 case COMPONENT_UPDATE_FOUND: | |
| 241 *converted_event = "UpdateFound"; | |
| 242 return; | |
| 243 case COMPONENT_UPDATE_READY: | |
| 244 *converted_event = "UpdateReady"; | |
| 245 return; | |
| 246 case COMPONENT_UPDATED: | |
| 247 *converted_event = "ComponentUpdated"; | |
| 248 return; | |
| 249 case COMPONENT_NOT_UPDATED: | |
| 250 *converted_event = "ComponentNotUpdated"; | |
| 251 return; | |
| 252 default: | |
| 253 *converted_event = "Unknown"; | |
| 254 return; | |
| 255 } | |
| 256 } | |
| 257 | |
| 258 void ComponentsUI::OnEvent(Events event, const std::string& id) { | |
| 259 std::string converted_event; | |
| 260 ComponentEventToString(event, &converted_event); | |
| 261 base::DictionaryValue parameters; | |
| 262 parameters.SetString("event", converted_event.c_str()); | |
| 263 if (!id.empty()) | |
| 264 parameters.SetString("id", id.c_str()); | |
| 265 web_ui()->CallJavascriptFunction("onComponentEvent", parameters); | |
| 266 } | |
| OLD | NEW |