| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/tabs/tabs_api.h" | 5 #include "chrome/browser/extensions/api/tabs/tabs_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 namespace extensions { | 109 namespace extensions { |
| 110 | 110 |
| 111 namespace windows = api::windows; | 111 namespace windows = api::windows; |
| 112 namespace keys = tabs_constants; | 112 namespace keys = tabs_constants; |
| 113 namespace tabs = api::tabs; | 113 namespace tabs = api::tabs; |
| 114 | 114 |
| 115 using api::extension_types::InjectDetails; | 115 using api::extension_types::InjectDetails; |
| 116 | 116 |
| 117 namespace { | 117 namespace { |
| 118 | 118 |
| 119 template <typename T> |
| 120 class ApiParameterExtractor { |
| 121 public: |
| 122 explicit ApiParameterExtractor(T* params) : params_(params) {} |
| 123 ~ApiParameterExtractor() {} |
| 124 |
| 125 bool populate_tabs() { |
| 126 if (params_->get_info.get() && params_->get_info->populate.get()) |
| 127 return *params_->get_info->populate; |
| 128 return false; |
| 129 } |
| 130 |
| 131 const WindowTypeFilter& type_filters() { |
| 132 if (params_->get_info.get() && params_->get_info->window_types.get()) |
| 133 return *params_->get_info->window_types.get(); |
| 134 return windows_util::GetDefaultWindowTypeFilters(); |
| 135 } |
| 136 |
| 137 private: |
| 138 T* params_; |
| 139 }; |
| 140 |
| 119 bool GetBrowserFromWindowID(ChromeUIThreadExtensionFunction* function, | 141 bool GetBrowserFromWindowID(ChromeUIThreadExtensionFunction* function, |
| 120 int window_id, | 142 int window_id, |
| 121 Browser** browser) { | 143 Browser** browser) { |
| 122 std::string error; | 144 std::string error; |
| 123 Browser* result; | 145 Browser* result; |
| 124 result = | 146 result = |
| 125 ExtensionTabUtil::GetBrowserFromWindowID(function, window_id, &error); | 147 ExtensionTabUtil::GetBrowserFromWindowID(function, window_id, &error); |
| 126 if (!result) { | 148 if (!result) { |
| 127 function->SetError(error); | 149 function->SetError(error); |
| 128 return false; | 150 return false; |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 break; | 280 break; |
| 259 } | 281 } |
| 260 } | 282 } |
| 261 | 283 |
| 262 // Windows --------------------------------------------------------------------- | 284 // Windows --------------------------------------------------------------------- |
| 263 | 285 |
| 264 bool WindowsGetFunction::RunSync() { | 286 bool WindowsGetFunction::RunSync() { |
| 265 scoped_ptr<windows::Get::Params> params(windows::Get::Params::Create(*args_)); | 287 scoped_ptr<windows::Get::Params> params(windows::Get::Params::Create(*args_)); |
| 266 EXTENSION_FUNCTION_VALIDATE(params.get()); | 288 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 267 | 289 |
| 268 bool populate_tabs = false; | 290 ApiParameterExtractor<windows::Get::Params> extractor(params.get()); |
| 269 if (params->get_info.get() && params->get_info->populate.get()) | |
| 270 populate_tabs = *params->get_info->populate; | |
| 271 | |
| 272 WindowController* controller; | 291 WindowController* controller; |
| 273 if (!windows_util::GetWindowFromWindowID(this, | 292 if (!windows_util::GetWindowFromWindowID( |
| 274 params->window_id, | 293 this, params->window_id, extractor.type_filters(), &controller)) { |
| 275 &controller)) { | |
| 276 return false; | 294 return false; |
| 277 } | 295 } |
| 278 | 296 |
| 279 if (populate_tabs) | 297 if (extractor.populate_tabs()) |
| 280 SetResult(controller->CreateWindowValueWithTabs(extension())); | 298 SetResult(controller->CreateWindowValueWithTabs(extension())); |
| 281 else | 299 else |
| 282 SetResult(controller->CreateWindowValue()); | 300 SetResult(controller->CreateWindowValue()); |
| 283 return true; | 301 return true; |
| 284 } | 302 } |
| 285 | 303 |
| 286 bool WindowsGetCurrentFunction::RunSync() { | 304 bool WindowsGetCurrentFunction::RunSync() { |
| 287 scoped_ptr<windows::GetCurrent::Params> params( | 305 scoped_ptr<windows::GetCurrent::Params> params( |
| 288 windows::GetCurrent::Params::Create(*args_)); | 306 windows::GetCurrent::Params::Create(*args_)); |
| 289 EXTENSION_FUNCTION_VALIDATE(params.get()); | 307 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 290 | 308 |
| 291 bool populate_tabs = false; | 309 ApiParameterExtractor<windows::GetCurrent::Params> extractor(params.get()); |
| 292 if (params->get_info.get() && params->get_info->populate.get()) | |
| 293 populate_tabs = *params->get_info->populate; | |
| 294 | |
| 295 WindowController* controller; | 310 WindowController* controller; |
| 296 if (!windows_util::GetWindowFromWindowID(this, | 311 if (!windows_util::GetWindowFromWindowID( |
| 297 extension_misc::kCurrentWindowId, | 312 this, extension_misc::kCurrentWindowId, extractor.type_filters(), |
| 298 &controller)) { | 313 &controller)) { |
| 299 return false; | 314 return false; |
| 300 } | 315 } |
| 301 if (populate_tabs) | 316 if (extractor.populate_tabs()) |
| 302 SetResult(controller->CreateWindowValueWithTabs(extension())); | 317 SetResult(controller->CreateWindowValueWithTabs(extension())); |
| 303 else | 318 else |
| 304 SetResult(controller->CreateWindowValue()); | 319 SetResult(controller->CreateWindowValue()); |
| 305 return true; | 320 return true; |
| 306 } | 321 } |
| 307 | 322 |
| 308 bool WindowsGetLastFocusedFunction::RunSync() { | 323 bool WindowsGetLastFocusedFunction::RunSync() { |
| 309 scoped_ptr<windows::GetLastFocused::Params> params( | 324 scoped_ptr<windows::GetLastFocused::Params> params( |
| 310 windows::GetLastFocused::Params::Create(*args_)); | 325 windows::GetLastFocused::Params::Create(*args_)); |
| 311 EXTENSION_FUNCTION_VALIDATE(params.get()); | 326 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 312 | 327 |
| 313 bool populate_tabs = false; | 328 ApiParameterExtractor<windows::GetLastFocused::Params> extractor( |
| 314 if (params->get_info.get() && params->get_info->populate.get()) | 329 params.get()); |
| 315 populate_tabs = *params->get_info->populate; | 330 // The WindowControllerList should contain a list of application, |
| 316 | 331 // browser and devtools windows. |
| 317 // Note: currently this returns the last active browser. If we decide to | 332 WindowController* controller = nullptr; |
| 318 // include other window types (e.g. panels), we will need to add logic to | 333 for (auto iter : WindowControllerList::GetInstance()->windows()) { |
| 319 // WindowControllerList that mirrors the active behavior of BrowserList. | 334 if (windows_util::CanOperateOnWindow(this, iter, |
| 320 Browser* browser = chrome::FindAnyBrowser( | 335 extractor.type_filters())) { |
| 321 GetProfile(), include_incognito(), chrome::GetActiveDesktop()); | 336 controller = iter; |
| 322 if (!browser || !browser->window()) { | 337 if (controller->window()->IsActive()) |
| 338 break; // Use focused window. |
| 339 } |
| 340 } |
| 341 if (!controller) { |
| 323 error_ = keys::kNoLastFocusedWindowError; | 342 error_ = keys::kNoLastFocusedWindowError; |
| 324 return false; | 343 return false; |
| 325 } | 344 } |
| 326 WindowController* controller = | 345 if (extractor.populate_tabs()) |
| 327 browser->extension_window_controller(); | |
| 328 if (populate_tabs) | |
| 329 SetResult(controller->CreateWindowValueWithTabs(extension())); | 346 SetResult(controller->CreateWindowValueWithTabs(extension())); |
| 330 else | 347 else |
| 331 SetResult(controller->CreateWindowValue()); | 348 SetResult(controller->CreateWindowValue()); |
| 332 return true; | 349 return true; |
| 333 } | 350 } |
| 334 | 351 |
| 335 bool WindowsGetAllFunction::RunSync() { | 352 bool WindowsGetAllFunction::RunSync() { |
| 336 scoped_ptr<windows::GetAll::Params> params( | 353 scoped_ptr<windows::GetAll::Params> params( |
| 337 windows::GetAll::Params::Create(*args_)); | 354 windows::GetAll::Params::Create(*args_)); |
| 338 EXTENSION_FUNCTION_VALIDATE(params.get()); | 355 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 339 | 356 |
| 340 bool populate_tabs = false; | 357 ApiParameterExtractor<windows::GetAll::Params> extractor(params.get()); |
| 341 if (params->get_info.get() && params->get_info->populate.get()) | |
| 342 populate_tabs = *params->get_info->populate; | |
| 343 | |
| 344 base::ListValue* window_list = new base::ListValue(); | 358 base::ListValue* window_list = new base::ListValue(); |
| 345 const WindowControllerList::ControllerList& windows = | 359 const WindowControllerList::ControllerList& windows = |
| 346 WindowControllerList::GetInstance()->windows(); | 360 WindowControllerList::GetInstance()->windows(); |
| 347 for (WindowControllerList::ControllerList::const_iterator iter = | 361 for (WindowControllerList::ControllerList::const_iterator iter = |
| 348 windows.begin(); | 362 windows.begin(); |
| 349 iter != windows.end(); ++iter) { | 363 iter != windows.end(); ++iter) { |
| 350 if (!windows_util::CanOperateOnWindow(this, *iter)) | 364 if (!windows_util::CanOperateOnWindow(this, *iter, |
| 365 extractor.type_filters())) |
| 351 continue; | 366 continue; |
| 352 if (populate_tabs) | 367 if (extractor.populate_tabs()) |
| 353 window_list->Append((*iter)->CreateWindowValueWithTabs(extension())); | 368 window_list->Append((*iter)->CreateWindowValueWithTabs(extension())); |
| 354 else | 369 else |
| 355 window_list->Append((*iter)->CreateWindowValue()); | 370 window_list->Append((*iter)->CreateWindowValue()); |
| 356 } | 371 } |
| 357 SetResult(window_list); | 372 SetResult(window_list); |
| 358 return true; | 373 return true; |
| 359 } | 374 } |
| 360 | 375 |
| 361 bool WindowsCreateFunction::ShouldOpenIncognitoWindow( | 376 bool WindowsCreateFunction::ShouldOpenIncognitoWindow( |
| 362 const windows::Create::Params::CreateData* create_data, | 377 const windows::Create::Params::CreateData* create_data, |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 686 | 701 |
| 687 return true; | 702 return true; |
| 688 } | 703 } |
| 689 | 704 |
| 690 bool WindowsUpdateFunction::RunSync() { | 705 bool WindowsUpdateFunction::RunSync() { |
| 691 scoped_ptr<windows::Update::Params> params( | 706 scoped_ptr<windows::Update::Params> params( |
| 692 windows::Update::Params::Create(*args_)); | 707 windows::Update::Params::Create(*args_)); |
| 693 EXTENSION_FUNCTION_VALIDATE(params); | 708 EXTENSION_FUNCTION_VALIDATE(params); |
| 694 | 709 |
| 695 WindowController* controller; | 710 WindowController* controller; |
| 696 if (!windows_util::GetWindowFromWindowID(this, params->window_id, | 711 if (!windows_util::GetWindowFromWindowID( |
| 697 &controller)) | 712 this, params->window_id, windows_util::GetNoWindowTypeFilters(), |
| 713 &controller)) |
| 698 return false; | 714 return false; |
| 699 | 715 |
| 700 ui::WindowShowState show_state = | 716 ui::WindowShowState show_state = |
| 701 ConvertToWindowShowState(params->update_info.state); | 717 ConvertToWindowShowState(params->update_info.state); |
| 702 | 718 |
| 703 if (show_state != ui::SHOW_STATE_FULLSCREEN && | 719 if (show_state != ui::SHOW_STATE_FULLSCREEN && |
| 704 show_state != ui::SHOW_STATE_DEFAULT) | 720 show_state != ui::SHOW_STATE_DEFAULT) |
| 705 controller->SetFullscreenMode(false, extension()->url()); | 721 controller->SetFullscreenMode(false, extension()->url()); |
| 706 | 722 |
| 707 switch (show_state) { | 723 switch (show_state) { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 788 | 804 |
| 789 return true; | 805 return true; |
| 790 } | 806 } |
| 791 | 807 |
| 792 bool WindowsRemoveFunction::RunSync() { | 808 bool WindowsRemoveFunction::RunSync() { |
| 793 scoped_ptr<windows::Remove::Params> params( | 809 scoped_ptr<windows::Remove::Params> params( |
| 794 windows::Remove::Params::Create(*args_)); | 810 windows::Remove::Params::Create(*args_)); |
| 795 EXTENSION_FUNCTION_VALIDATE(params); | 811 EXTENSION_FUNCTION_VALIDATE(params); |
| 796 | 812 |
| 797 WindowController* controller; | 813 WindowController* controller; |
| 798 if (!windows_util::GetWindowFromWindowID(this, params->window_id, | 814 if (!windows_util::GetWindowFromWindowID( |
| 799 &controller)) | 815 this, params->window_id, windows_util::GetDefaultWindowTypeFilters(), |
| 816 &controller)) |
| 800 return false; | 817 return false; |
| 801 | 818 |
| 802 WindowController::Reason reason; | 819 WindowController::Reason reason; |
| 803 if (!controller->CanClose(&reason)) { | 820 if (!controller->CanClose(&reason)) { |
| 804 if (reason == WindowController::REASON_NOT_EDITABLE) | 821 if (reason == WindowController::REASON_NOT_EDITABLE) |
| 805 error_ = keys::kTabStripNotEditableError; | 822 error_ = keys::kTabStripNotEditableError; |
| 806 return false; | 823 return false; |
| 807 } | 824 } |
| 808 controller->window()->Close(); | 825 controller->window()->Close(); |
| 809 return true; | 826 return true; |
| (...skipping 1218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2028 ZoomModeToZoomSettings(zoom_mode, &zoom_settings); | 2045 ZoomModeToZoomSettings(zoom_mode, &zoom_settings); |
| 2029 zoom_settings.default_zoom_factor.reset(new double( | 2046 zoom_settings.default_zoom_factor.reset(new double( |
| 2030 content::ZoomLevelToZoomFactor(zoom_controller->GetDefaultZoomLevel()))); | 2047 content::ZoomLevelToZoomFactor(zoom_controller->GetDefaultZoomLevel()))); |
| 2031 | 2048 |
| 2032 results_ = api::tabs::GetZoomSettings::Results::Create(zoom_settings); | 2049 results_ = api::tabs::GetZoomSettings::Results::Create(zoom_settings); |
| 2033 SendResponse(true); | 2050 SendResponse(true); |
| 2034 return true; | 2051 return true; |
| 2035 } | 2052 } |
| 2036 | 2053 |
| 2037 } // namespace extensions | 2054 } // namespace extensions |
| OLD | NEW |