Chromium Code Reviews| Index: chrome/browser/extensions/api/tabs/tabs_api.cc |
| diff --git a/chrome/browser/extensions/api/tabs/tabs_api.cc b/chrome/browser/extensions/api/tabs/tabs_api.cc |
| index 082822b843de360c5af776ec9653ec3abb9dad99..39ea0422634a6f87a7dd9d59b9858a0ab83f6381 100644 |
| --- a/chrome/browser/extensions/api/tabs/tabs_api.cc |
| +++ b/chrome/browser/extensions/api/tabs/tabs_api.cc |
| @@ -2168,4 +2168,45 @@ bool TabsGetZoomSettingsFunction::RunAsync() { |
| return true; |
| } |
| +ExtensionFunction::ResponseAction TabsDiscardFunction::Run() { |
| + std::unique_ptr<tabs::Discard::Params> params( |
| + tabs::Discard::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params); |
| + |
| + WebContents* contents = nullptr; |
| + Profile* profile = Profile::FromBrowserContext(browser_context()); |
| + // If |tab_id| is given, find the web_contents respective to it. |
| + // Otherwise invoke discard function in TabManager with null web_contents |
| + // that will discard the least important tab. |
| + if (params->tab_id) { |
| + int tab_id = *params->tab_id; |
| + if (!GetTabById(tab_id, profile, include_incognito(), nullptr, nullptr, |
| + &contents, nullptr, &error_)) { |
| + return RespondNow(Error(error_)); |
| + } |
| + } |
| + // Discard the tab. |
| + contents = |
| + g_browser_process->GetTabManager()->DiscardTabByExtension(contents); |
| + |
| + // Create the Tab object and return it in case of success. |
| + if (contents) { |
| + return RespondNow(ArgumentList(tabs::Discard::Results::Create( |
| + *ExtensionTabUtil::CreateTabObject(contents)))); |
| + } |
| + |
| + // Return appropriate error message otherwise. |
| + if (params->tab_id) { |
| + error_ = ErrorUtils::FormatErrorMessage(keys::kCannotDiscardTab, |
|
Devlin
2016/07/26 16:13:45
nit: setting error_ is handled by returning an Err
Anderson Silva
2016/07/26 21:02:52
Done.
|
| + base::IntToString(*params->tab_id)); |
| + } else { |
| + error_ = keys::kCannotFindTabToDiscard; |
| + } |
| + |
| + return RespondNow(Error(error_)); |
| +} |
| + |
| +TabsDiscardFunction::TabsDiscardFunction() {} |
| +TabsDiscardFunction::~TabsDiscardFunction() {} |
| + |
| } // namespace extensions |