| 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/extensions/api/developer_private/extension_info_generat
or.h" | 5 #include "chrome/browser/extensions/api/developer_private/extension_info_generat
or.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/callback_helpers.h" |
| 10 #include "base/location.h" | 11 #include "base/location.h" |
| 11 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 14 #include "chrome/browser/extensions/api/commands/command_service.h" | 15 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 15 #include "chrome/browser/extensions/api/developer_private/inspectable_views_find
er.h" | 16 #include "chrome/browser/extensions/api/developer_private/inspectable_views_find
er.h" |
| 16 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 17 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 17 #include "chrome/browser/extensions/error_console/error_console.h" | 18 #include "chrome/browser/extensions/error_console/error_console.h" |
| 18 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
| 19 #include "chrome/browser/extensions/extension_ui_util.h" | 20 #include "chrome/browser/extensions/extension_ui_util.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 developer::ERROR_TYPE_MANIFEST : developer::ERROR_TYPE_RUNTIME; | 97 developer::ERROR_TYPE_MANIFEST : developer::ERROR_TYPE_RUNTIME; |
| 97 out->extension_id = error.extension_id(); | 98 out->extension_id = error.extension_id(); |
| 98 out->from_incognito = error.from_incognito(); | 99 out->from_incognito = error.from_incognito(); |
| 99 out->source = base::UTF16ToUTF8(error.source()); | 100 out->source = base::UTF16ToUTF8(error.source()); |
| 100 out->message = base::UTF16ToUTF8(error.message()); | 101 out->message = base::UTF16ToUTF8(error.message()); |
| 101 out->id = error.id(); | 102 out->id = error.id(); |
| 102 } | 103 } |
| 103 | 104 |
| 104 // Given a ManifestError object, converts it into its developer_private | 105 // Given a ManifestError object, converts it into its developer_private |
| 105 // counterpart. | 106 // counterpart. |
| 106 linked_ptr<developer::ManifestError> ConstructManifestError( | 107 developer::ManifestError ConstructManifestError(const ManifestError& error) { |
| 107 const ManifestError& error) { | 108 developer::ManifestError result; |
| 108 linked_ptr<developer::ManifestError> result(new developer::ManifestError()); | 109 PopulateErrorBase(error, &result); |
| 109 PopulateErrorBase(error, result.get()); | 110 result.manifest_key = base::UTF16ToUTF8(error.manifest_key()); |
| 110 result->manifest_key = base::UTF16ToUTF8(error.manifest_key()); | |
| 111 if (!error.manifest_specific().empty()) { | 111 if (!error.manifest_specific().empty()) { |
| 112 result->manifest_specific.reset( | 112 result.manifest_specific.reset( |
| 113 new std::string(base::UTF16ToUTF8(error.manifest_specific()))); | 113 new std::string(base::UTF16ToUTF8(error.manifest_specific()))); |
| 114 } | 114 } |
| 115 return result; | 115 return result; |
| 116 } | 116 } |
| 117 | 117 |
| 118 // Given a RuntimeError object, converts it into its developer_private | 118 // Given a RuntimeError object, converts it into its developer_private |
| 119 // counterpart. | 119 // counterpart. |
| 120 linked_ptr<developer::RuntimeError> ConstructRuntimeError( | 120 developer::RuntimeError ConstructRuntimeError(const RuntimeError& error) { |
| 121 const RuntimeError& error) { | 121 developer::RuntimeError result; |
| 122 linked_ptr<developer::RuntimeError> result(new developer::RuntimeError()); | 122 PopulateErrorBase(error, &result); |
| 123 PopulateErrorBase(error, result.get()); | |
| 124 switch (error.level()) { | 123 switch (error.level()) { |
| 125 case logging::LOG_VERBOSE: | 124 case logging::LOG_VERBOSE: |
| 126 case logging::LOG_INFO: | 125 case logging::LOG_INFO: |
| 127 result->severity = developer::ERROR_LEVEL_LOG; | 126 result.severity = developer::ERROR_LEVEL_LOG; |
| 128 break; | 127 break; |
| 129 case logging::LOG_WARNING: | 128 case logging::LOG_WARNING: |
| 130 result->severity = developer::ERROR_LEVEL_WARN; | 129 result.severity = developer::ERROR_LEVEL_WARN; |
| 131 break; | 130 break; |
| 132 case logging::LOG_FATAL: | 131 case logging::LOG_FATAL: |
| 133 case logging::LOG_ERROR: | 132 case logging::LOG_ERROR: |
| 134 result->severity = developer::ERROR_LEVEL_ERROR; | 133 result.severity = developer::ERROR_LEVEL_ERROR; |
| 135 break; | 134 break; |
| 136 default: | 135 default: |
| 137 NOTREACHED(); | 136 NOTREACHED(); |
| 138 } | 137 } |
| 139 result->occurrences = error.occurrences(); | 138 result.occurrences = error.occurrences(); |
| 140 // NOTE(devlin): This is called "render_view_id" in the api for legacy | 139 // NOTE(devlin): This is called "render_view_id" in the api for legacy |
| 141 // reasons, but it's not a high priority to change. | 140 // reasons, but it's not a high priority to change. |
| 142 result->render_view_id = error.render_frame_id(); | 141 result.render_view_id = error.render_frame_id(); |
| 143 result->render_process_id = error.render_process_id(); | 142 result.render_process_id = error.render_process_id(); |
| 144 result->can_inspect = | 143 result.can_inspect = |
| 145 content::RenderFrameHost::FromID(error.render_process_id(), | 144 content::RenderFrameHost::FromID(error.render_process_id(), |
| 146 error.render_frame_id()) != nullptr; | 145 error.render_frame_id()) != nullptr; |
| 147 for (const StackFrame& f : error.stack_trace()) { | 146 for (const StackFrame& f : error.stack_trace()) { |
| 148 linked_ptr<developer::StackFrame> frame(new developer::StackFrame()); | 147 developer::StackFrame frame; |
| 149 frame->line_number = f.line_number; | 148 frame.line_number = f.line_number; |
| 150 frame->column_number = f.column_number; | 149 frame.column_number = f.column_number; |
| 151 frame->url = base::UTF16ToUTF8(f.source); | 150 frame.url = base::UTF16ToUTF8(f.source); |
| 152 frame->function_name = base::UTF16ToUTF8(f.function); | 151 frame.function_name = base::UTF16ToUTF8(f.function); |
| 153 result->stack_trace.push_back(frame); | 152 result.stack_trace.push_back(std::move(frame)); |
| 154 } | 153 } |
| 155 return result; | 154 return result; |
| 156 } | 155 } |
| 157 | 156 |
| 158 // Constructs any commands for the extension with the given |id|, and adds them | 157 // Constructs any commands for the extension with the given |id|, and adds them |
| 159 // to the list of |commands|. | 158 // to the list of |commands|. |
| 160 void ConstructCommands(CommandService* command_service, | 159 void ConstructCommands(CommandService* command_service, |
| 161 const std::string& extension_id, | 160 const std::string& extension_id, |
| 162 std::vector<linked_ptr<developer::Command>>* commands) { | 161 std::vector<developer::Command>* commands) { |
| 163 auto construct_command = [](const Command& command, | 162 auto construct_command = [](const Command& command, bool active, |
| 164 bool active, | |
| 165 bool is_extension_action) { | 163 bool is_extension_action) { |
| 166 developer::Command* command_value = new developer::Command(); | 164 developer::Command command_value; |
| 167 command_value->description = is_extension_action ? | 165 command_value.description = |
| 168 l10n_util::GetStringUTF8(IDS_EXTENSION_COMMANDS_GENERIC_ACTIVATE) : | 166 is_extension_action |
| 169 base::UTF16ToUTF8(command.description()); | 167 ? l10n_util::GetStringUTF8(IDS_EXTENSION_COMMANDS_GENERIC_ACTIVATE) |
| 170 command_value->keybinding = | 168 : base::UTF16ToUTF8(command.description()); |
| 169 command_value.keybinding = |
| 171 base::UTF16ToUTF8(command.accelerator().GetShortcutText()); | 170 base::UTF16ToUTF8(command.accelerator().GetShortcutText()); |
| 172 command_value->name = command.command_name(); | 171 command_value.name = command.command_name(); |
| 173 command_value->is_active = active; | 172 command_value.is_active = active; |
| 174 command_value->scope = command.global() ? developer::COMMAND_SCOPE_GLOBAL : | 173 command_value.scope = command.global() ? developer::COMMAND_SCOPE_GLOBAL |
| 175 developer::COMMAND_SCOPE_CHROME; | 174 : developer::COMMAND_SCOPE_CHROME; |
| 176 command_value->is_extension_action = is_extension_action; | 175 command_value.is_extension_action = is_extension_action; |
| 177 return command_value; | 176 return command_value; |
| 178 }; | 177 }; |
| 179 bool active = false; | 178 bool active = false; |
| 180 Command browser_action; | 179 Command browser_action; |
| 181 if (command_service->GetBrowserActionCommand(extension_id, | 180 if (command_service->GetBrowserActionCommand(extension_id, |
| 182 CommandService::ALL, | 181 CommandService::ALL, |
| 183 &browser_action, | 182 &browser_action, |
| 184 &active)) { | 183 &active)) { |
| 185 commands->push_back( | 184 commands->push_back(construct_command(browser_action, active, true)); |
| 186 make_linked_ptr(construct_command(browser_action, active, true))); | |
| 187 } | 185 } |
| 188 | 186 |
| 189 Command page_action; | 187 Command page_action; |
| 190 if (command_service->GetPageActionCommand(extension_id, | 188 if (command_service->GetPageActionCommand(extension_id, |
| 191 CommandService::ALL, | 189 CommandService::ALL, |
| 192 &page_action, | 190 &page_action, |
| 193 &active)) { | 191 &active)) { |
| 194 commands->push_back( | 192 commands->push_back(construct_command(page_action, active, true)); |
| 195 make_linked_ptr(construct_command(page_action, active, true))); | |
| 196 } | 193 } |
| 197 | 194 |
| 198 CommandMap named_commands; | 195 CommandMap named_commands; |
| 199 if (command_service->GetNamedCommands(extension_id, | 196 if (command_service->GetNamedCommands(extension_id, |
| 200 CommandService::ALL, | 197 CommandService::ALL, |
| 201 CommandService::ANY_SCOPE, | 198 CommandService::ANY_SCOPE, |
| 202 &named_commands)) { | 199 &named_commands)) { |
| 203 for (auto& pair : named_commands) { | 200 for (auto& pair : named_commands) { |
| 204 Command& command_to_use = pair.second; | 201 Command& command_to_use = pair.second; |
| 205 // TODO(devlin): For some reason beyond my knowledge, FindCommandByName | 202 // TODO(devlin): For some reason beyond my knowledge, FindCommandByName |
| 206 // returns different data than GetNamedCommands, including the | 203 // returns different data than GetNamedCommands, including the |
| 207 // accelerators, but not the descriptions - and even then, only if the | 204 // accelerators, but not the descriptions - and even then, only if the |
| 208 // command is active. | 205 // command is active. |
| 209 // Unfortunately, some systems may be relying on the other data (which | 206 // Unfortunately, some systems may be relying on the other data (which |
| 210 // more closely matches manifest data). | 207 // more closely matches manifest data). |
| 211 // Until we can sort all this out, we merge the two command structures. | 208 // Until we can sort all this out, we merge the two command structures. |
| 212 Command active_command = command_service->FindCommandByName( | 209 Command active_command = command_service->FindCommandByName( |
| 213 extension_id, command_to_use.command_name()); | 210 extension_id, command_to_use.command_name()); |
| 214 command_to_use.set_accelerator(active_command.accelerator()); | 211 command_to_use.set_accelerator(active_command.accelerator()); |
| 215 command_to_use.set_global(active_command.global()); | 212 command_to_use.set_global(active_command.global()); |
| 216 bool active = command_to_use.accelerator().key_code() != ui::VKEY_UNKNOWN; | 213 bool active = command_to_use.accelerator().key_code() != ui::VKEY_UNKNOWN; |
| 217 commands->push_back( | 214 commands->push_back(construct_command(command_to_use, active, false)); |
| 218 make_linked_ptr(construct_command(command_to_use, active, false))); | |
| 219 } | 215 } |
| 220 } | 216 } |
| 221 } | 217 } |
| 222 | 218 |
| 223 } // namespace | 219 } // namespace |
| 224 | 220 |
| 225 ExtensionInfoGenerator::ExtensionInfoGenerator( | 221 ExtensionInfoGenerator::ExtensionInfoGenerator( |
| 226 content::BrowserContext* browser_context) | 222 content::BrowserContext* browser_context) |
| 227 : browser_context_(browser_context), | 223 : browser_context_(browser_context), |
| 228 command_service_(CommandService::Get(browser_context)), | 224 command_service_(CommandService::Get(browser_context)), |
| (...skipping 24 matching lines...) Expand all Loading... |
| 253 else if ((ext = registry->disabled_extensions().GetByID(id)) != nullptr) | 249 else if ((ext = registry->disabled_extensions().GetByID(id)) != nullptr) |
| 254 state = developer::EXTENSION_STATE_DISABLED; | 250 state = developer::EXTENSION_STATE_DISABLED; |
| 255 else if ((ext = registry->terminated_extensions().GetByID(id)) != nullptr) | 251 else if ((ext = registry->terminated_extensions().GetByID(id)) != nullptr) |
| 256 state = developer::EXTENSION_STATE_TERMINATED; | 252 state = developer::EXTENSION_STATE_TERMINATED; |
| 257 | 253 |
| 258 if (ext && ui_util::ShouldDisplayInExtensionSettings(ext, browser_context_)) | 254 if (ext && ui_util::ShouldDisplayInExtensionSettings(ext, browser_context_)) |
| 259 CreateExtensionInfoHelper(*ext, state); | 255 CreateExtensionInfoHelper(*ext, state); |
| 260 | 256 |
| 261 if (pending_image_loads_ == 0) { | 257 if (pending_image_loads_ == 0) { |
| 262 // Don't call the callback re-entrantly. | 258 // Don't call the callback re-entrantly. |
| 263 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 259 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 264 base::Bind(callback, list_)); | 260 FROM_HERE, base::Bind(callback, base::Passed(&list_))); |
| 265 list_.clear(); | 261 list_.clear(); |
| 266 } else { | 262 } else { |
| 267 callback_ = callback; | 263 callback_ = callback; |
| 268 } | 264 } |
| 269 } | 265 } |
| 270 | 266 |
| 271 void ExtensionInfoGenerator::CreateExtensionsInfo( | 267 void ExtensionInfoGenerator::CreateExtensionsInfo( |
| 272 bool include_disabled, | 268 bool include_disabled, |
| 273 bool include_terminated, | 269 bool include_terminated, |
| 274 const ExtensionInfosCallback& callback) { | 270 const ExtensionInfosCallback& callback) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 289 add_to_list(registry->disabled_extensions(), | 285 add_to_list(registry->disabled_extensions(), |
| 290 developer::EXTENSION_STATE_DISABLED); | 286 developer::EXTENSION_STATE_DISABLED); |
| 291 } | 287 } |
| 292 if (include_terminated) { | 288 if (include_terminated) { |
| 293 add_to_list(registry->terminated_extensions(), | 289 add_to_list(registry->terminated_extensions(), |
| 294 developer::EXTENSION_STATE_TERMINATED); | 290 developer::EXTENSION_STATE_TERMINATED); |
| 295 } | 291 } |
| 296 | 292 |
| 297 if (pending_image_loads_ == 0) { | 293 if (pending_image_loads_ == 0) { |
| 298 // Don't call the callback re-entrantly. | 294 // Don't call the callback re-entrantly. |
| 299 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 295 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 300 base::Bind(callback, list_)); | 296 FROM_HERE, base::Bind(callback, base::Passed(&list_))); |
| 301 list_.clear(); | 297 list_.clear(); |
| 302 } else { | 298 } else { |
| 303 callback_ = callback; | 299 callback_ = callback; |
| 304 } | 300 } |
| 305 } | 301 } |
| 306 | 302 |
| 307 void ExtensionInfoGenerator::CreateExtensionInfoHelper( | 303 void ExtensionInfoGenerator::CreateExtensionInfoHelper( |
| 308 const Extension& extension, | 304 const Extension& extension, |
| 309 developer::ExtensionState state) { | 305 developer::ExtensionState state) { |
| 310 scoped_ptr<developer::ExtensionInfo> info(new developer::ExtensionInfo()); | 306 scoped_ptr<developer::ExtensionInfo> info(new developer::ExtensionInfo()); |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 GetViewsForExtension(extension, is_enabled); | 529 GetViewsForExtension(extension, is_enabled); |
| 534 } | 530 } |
| 535 | 531 |
| 536 // The icon. | 532 // The icon. |
| 537 ExtensionResource icon = | 533 ExtensionResource icon = |
| 538 IconsInfo::GetIconResource(&extension, | 534 IconsInfo::GetIconResource(&extension, |
| 539 extension_misc::EXTENSION_ICON_MEDIUM, | 535 extension_misc::EXTENSION_ICON_MEDIUM, |
| 540 ExtensionIconSet::MATCH_BIGGER); | 536 ExtensionIconSet::MATCH_BIGGER); |
| 541 if (icon.empty()) { | 537 if (icon.empty()) { |
| 542 info->icon_url = GetDefaultIconUrl(extension.is_app(), !is_enabled); | 538 info->icon_url = GetDefaultIconUrl(extension.is_app(), !is_enabled); |
| 543 list_.push_back(make_linked_ptr(info.release())); | 539 list_.push_back(std::move(*info)); |
| 544 } else { | 540 } else { |
| 545 ++pending_image_loads_; | 541 ++pending_image_loads_; |
| 546 // Max size of 128x128 is a random guess at a nice balance between being | 542 // Max size of 128x128 is a random guess at a nice balance between being |
| 547 // overly eager to resize and sending across gigantic data urls. (The icon | 543 // overly eager to resize and sending across gigantic data urls. (The icon |
| 548 // used by the url is 48x48). | 544 // used by the url is 48x48). |
| 549 gfx::Size max_size(128, 128); | 545 gfx::Size max_size(128, 128); |
| 550 image_loader_->LoadImageAsync( | 546 image_loader_->LoadImageAsync( |
| 551 &extension, icon, max_size, | 547 &extension, icon, max_size, |
| 552 base::Bind(&ExtensionInfoGenerator::OnImageLoaded, | 548 base::Bind(&ExtensionInfoGenerator::OnImageLoaded, |
| 553 weak_factory_.GetWeakPtr(), base::Passed(std::move(info)))); | 549 weak_factory_.GetWeakPtr(), base::Passed(&info))); |
| 554 } | 550 } |
| 555 } | 551 } |
| 556 | 552 |
| 557 const std::string& ExtensionInfoGenerator::GetDefaultIconUrl( | 553 const std::string& ExtensionInfoGenerator::GetDefaultIconUrl( |
| 558 bool is_app, | 554 bool is_app, |
| 559 bool is_greyscale) { | 555 bool is_greyscale) { |
| 560 std::string* str; | 556 std::string* str; |
| 561 if (is_app) { | 557 if (is_app) { |
| 562 str = is_greyscale ? &default_disabled_app_icon_url_ : | 558 str = is_greyscale ? &default_disabled_app_icon_url_ : |
| 563 &default_app_icon_url_; | 559 &default_app_icon_url_; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 icon, info->state != developer::EXTENSION_STATE_ENABLED); | 604 icon, info->state != developer::EXTENSION_STATE_ENABLED); |
| 609 } else { | 605 } else { |
| 610 bool is_app = | 606 bool is_app = |
| 611 info->type == developer::EXTENSION_TYPE_HOSTED_APP || | 607 info->type == developer::EXTENSION_TYPE_HOSTED_APP || |
| 612 info->type == developer::EXTENSION_TYPE_LEGACY_PACKAGED_APP || | 608 info->type == developer::EXTENSION_TYPE_LEGACY_PACKAGED_APP || |
| 613 info->type == developer::EXTENSION_TYPE_PLATFORM_APP; | 609 info->type == developer::EXTENSION_TYPE_PLATFORM_APP; |
| 614 info->icon_url = GetDefaultIconUrl( | 610 info->icon_url = GetDefaultIconUrl( |
| 615 is_app, info->state != developer::EXTENSION_STATE_ENABLED); | 611 is_app, info->state != developer::EXTENSION_STATE_ENABLED); |
| 616 } | 612 } |
| 617 | 613 |
| 618 list_.push_back(make_linked_ptr(info.release())); | 614 list_.push_back(std::move(*info)); |
| 619 | 615 |
| 620 --pending_image_loads_; | 616 --pending_image_loads_; |
| 621 | 617 |
| 622 if (pending_image_loads_ == 0) { // All done! | 618 if (pending_image_loads_ == 0) { // All done! |
| 623 // We assign to a temporary callback and list and reset the stored values so | 619 ExtensionInfoList list = std::move(list_); |
| 624 // that at the end of the method, any stored refs are destroyed. | 620 list_.clear(); |
| 625 ExtensionInfoList list; | 621 base::ResetAndReturn(&callback_).Run(std::move(list)); |
| 626 list.swap(list_); | 622 // WARNING: |this| is possibly deleted after this line! |
| 627 ExtensionInfosCallback callback = callback_; | |
| 628 callback_.Reset(); | |
| 629 callback.Run(list); // WARNING: |this| is possibly deleted after this line! | |
| 630 } | 623 } |
| 631 } | 624 } |
| 632 | 625 |
| 633 } // namespace extensions | 626 } // namespace extensions |
| OLD | NEW |