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 <stddef.h> | 7 #include <stddef.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <limits> | 9 #include <limits> |
10 #include <memory> | 10 #include <memory> |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 if (params_->get_info.get() && params_->get_info->window_types.get()) | 134 if (params_->get_info.get() && params_->get_info->window_types.get()) |
135 return WindowController::GetFilterFromWindowTypes( | 135 return WindowController::GetFilterFromWindowTypes( |
136 *params_->get_info->window_types); | 136 *params_->get_info->window_types); |
137 return WindowController::kNoWindowFilter; | 137 return WindowController::kNoWindowFilter; |
138 } | 138 } |
139 | 139 |
140 private: | 140 private: |
141 T* params_; | 141 T* params_; |
142 }; | 142 }; |
143 | 143 |
144 bool GetBrowserFromWindowID(ChromeUIThreadExtensionFunction* function, | 144 bool GetBrowserFromWindowID(const ChromeExtensionFunctionDetails& details, |
145 int window_id, | 145 int window_id, |
146 Browser** browser) { | 146 Browser** browser, |
147 std::string error; | 147 std::string* error) { |
148 Browser* result; | 148 Browser* result = nullptr; |
149 result = | 149 result = ExtensionTabUtil::GetBrowserFromWindowID(details, window_id, error); |
150 ExtensionTabUtil::GetBrowserFromWindowID(function, window_id, &error); | 150 if (!result) |
151 if (!result) { | |
152 function->SetError(error); | |
153 return false; | 151 return false; |
154 } | |
155 | 152 |
156 *browser = result; | 153 *browser = result; |
157 return true; | 154 return true; |
158 } | 155 } |
159 | 156 |
160 bool GetBrowserFromWindowID(ChromeExtensionFunctionDetails* details, | 157 bool GetBrowserFromWindowID(ChromeUIThreadExtensionFunction* function, |
161 int window_id, | 158 int window_id, |
162 Browser** browser) { | 159 Browser** browser, |
163 std::string error; | 160 std::string* error) { |
164 Browser* result; | 161 return GetBrowserFromWindowID(ChromeExtensionFunctionDetails(function), |
165 result = | 162 window_id, browser, error); |
166 ExtensionTabUtil::GetBrowserFromWindowID(*details, window_id, &error); | |
167 if (!result) { | |
168 details->function()->SetError(error); | |
169 return false; | |
170 } | |
171 | |
172 *browser = result; | |
173 return true; | |
174 } | 163 } |
175 | 164 |
176 // |error_message| can optionally be passed in and will be set with an | 165 // |error_message| can optionally be passed in and will be set with an |
177 // appropriate message if the tab cannot be found by id. | 166 // appropriate message if the tab cannot be found by id. |
178 bool GetTabById(int tab_id, | 167 bool GetTabById(int tab_id, |
179 Profile* profile, | 168 Profile* profile, |
180 bool include_incognito, | 169 bool include_incognito, |
181 Browser** browser, | 170 Browser** browser, |
182 TabStripModel** tab_strip, | 171 TabStripModel** tab_strip, |
183 content::WebContents** contents, | 172 content::WebContents** contents, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 | 275 |
287 // Windows --------------------------------------------------------------------- | 276 // Windows --------------------------------------------------------------------- |
288 | 277 |
289 bool WindowsGetFunction::RunSync() { | 278 bool WindowsGetFunction::RunSync() { |
290 std::unique_ptr<windows::Get::Params> params( | 279 std::unique_ptr<windows::Get::Params> params( |
291 windows::Get::Params::Create(*args_)); | 280 windows::Get::Params::Create(*args_)); |
292 EXTENSION_FUNCTION_VALIDATE(params.get()); | 281 EXTENSION_FUNCTION_VALIDATE(params.get()); |
293 | 282 |
294 ApiParameterExtractor<windows::Get::Params> extractor(params.get()); | 283 ApiParameterExtractor<windows::Get::Params> extractor(params.get()); |
295 WindowController* controller; | 284 WindowController* controller; |
296 if (!windows_util::GetWindowFromWindowID( | 285 if (!windows_util::GetWindowFromWindowID(this, params->window_id, |
297 this, params->window_id, extractor.type_filters(), &controller)) { | 286 extractor.type_filters(), |
| 287 &controller, &error_)) { |
298 return false; | 288 return false; |
299 } | 289 } |
300 | 290 |
301 if (extractor.populate_tabs()) | 291 if (extractor.populate_tabs()) |
302 SetResult(controller->CreateWindowValueWithTabs(extension())); | 292 SetResult(controller->CreateWindowValueWithTabs(extension())); |
303 else | 293 else |
304 SetResult(controller->CreateWindowValue()); | 294 SetResult(controller->CreateWindowValue()); |
305 return true; | 295 return true; |
306 } | 296 } |
307 | 297 |
308 bool WindowsGetCurrentFunction::RunSync() { | 298 bool WindowsGetCurrentFunction::RunSync() { |
309 std::unique_ptr<windows::GetCurrent::Params> params( | 299 std::unique_ptr<windows::GetCurrent::Params> params( |
310 windows::GetCurrent::Params::Create(*args_)); | 300 windows::GetCurrent::Params::Create(*args_)); |
311 EXTENSION_FUNCTION_VALIDATE(params.get()); | 301 EXTENSION_FUNCTION_VALIDATE(params.get()); |
312 | 302 |
313 ApiParameterExtractor<windows::GetCurrent::Params> extractor(params.get()); | 303 ApiParameterExtractor<windows::GetCurrent::Params> extractor(params.get()); |
314 WindowController* controller; | 304 WindowController* controller; |
315 if (!windows_util::GetWindowFromWindowID( | 305 if (!windows_util::GetWindowFromWindowID( |
316 this, extension_misc::kCurrentWindowId, extractor.type_filters(), | 306 this, extension_misc::kCurrentWindowId, extractor.type_filters(), |
317 &controller)) { | 307 &controller, &error_)) { |
318 return false; | 308 return false; |
319 } | 309 } |
320 if (extractor.populate_tabs()) | 310 if (extractor.populate_tabs()) |
321 SetResult(controller->CreateWindowValueWithTabs(extension())); | 311 SetResult(controller->CreateWindowValueWithTabs(extension())); |
322 else | 312 else |
323 SetResult(controller->CreateWindowValue()); | 313 SetResult(controller->CreateWindowValue()); |
324 return true; | 314 return true; |
325 } | 315 } |
326 | 316 |
327 bool WindowsGetLastFocusedFunction::RunSync() { | 317 bool WindowsGetLastFocusedFunction::RunSync() { |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 } | 673 } |
684 | 674 |
685 bool WindowsUpdateFunction::RunSync() { | 675 bool WindowsUpdateFunction::RunSync() { |
686 std::unique_ptr<windows::Update::Params> params( | 676 std::unique_ptr<windows::Update::Params> params( |
687 windows::Update::Params::Create(*args_)); | 677 windows::Update::Params::Create(*args_)); |
688 EXTENSION_FUNCTION_VALIDATE(params); | 678 EXTENSION_FUNCTION_VALIDATE(params); |
689 | 679 |
690 WindowController* controller; | 680 WindowController* controller; |
691 if (!windows_util::GetWindowFromWindowID( | 681 if (!windows_util::GetWindowFromWindowID( |
692 this, params->window_id, WindowController::GetAllWindowFilter(), | 682 this, params->window_id, WindowController::GetAllWindowFilter(), |
693 &controller)) { | 683 &controller, &error_)) { |
694 return false; | 684 return false; |
695 } | 685 } |
696 | 686 |
697 ui::WindowShowState show_state = | 687 ui::WindowShowState show_state = |
698 ConvertToWindowShowState(params->update_info.state); | 688 ConvertToWindowShowState(params->update_info.state); |
699 | 689 |
700 if (show_state != ui::SHOW_STATE_FULLSCREEN && | 690 if (show_state != ui::SHOW_STATE_FULLSCREEN && |
701 show_state != ui::SHOW_STATE_DEFAULT) | 691 show_state != ui::SHOW_STATE_DEFAULT) |
702 controller->SetFullscreenMode(false, extension()->url()); | 692 controller->SetFullscreenMode(false, extension()->url()); |
703 | 693 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
787 } | 777 } |
788 | 778 |
789 bool WindowsRemoveFunction::RunSync() { | 779 bool WindowsRemoveFunction::RunSync() { |
790 std::unique_ptr<windows::Remove::Params> params( | 780 std::unique_ptr<windows::Remove::Params> params( |
791 windows::Remove::Params::Create(*args_)); | 781 windows::Remove::Params::Create(*args_)); |
792 EXTENSION_FUNCTION_VALIDATE(params); | 782 EXTENSION_FUNCTION_VALIDATE(params); |
793 | 783 |
794 WindowController* controller; | 784 WindowController* controller; |
795 if (!windows_util::GetWindowFromWindowID(this, params->window_id, | 785 if (!windows_util::GetWindowFromWindowID(this, params->window_id, |
796 WindowController::kNoWindowFilter, | 786 WindowController::kNoWindowFilter, |
797 &controller)) { | 787 &controller, &error_)) { |
798 return false; | 788 return false; |
799 } | 789 } |
800 | 790 |
801 WindowController::Reason reason; | 791 WindowController::Reason reason; |
802 if (!controller->CanClose(&reason)) { | 792 if (!controller->CanClose(&reason)) { |
803 if (reason == WindowController::REASON_NOT_EDITABLE) | 793 if (reason == WindowController::REASON_NOT_EDITABLE) |
804 error_ = keys::kTabStripNotEditableError; | 794 error_ = keys::kTabStripNotEditableError; |
805 return false; | 795 return false; |
806 } | 796 } |
807 controller->window()->Close(); | 797 controller->window()->Close(); |
808 return true; | 798 return true; |
809 } | 799 } |
810 | 800 |
811 // Tabs ------------------------------------------------------------------------ | 801 // Tabs ------------------------------------------------------------------------ |
812 | 802 |
813 bool TabsGetSelectedFunction::RunSync() { | 803 bool TabsGetSelectedFunction::RunSync() { |
814 // windowId defaults to "current" window. | 804 // windowId defaults to "current" window. |
815 int window_id = extension_misc::kCurrentWindowId; | 805 int window_id = extension_misc::kCurrentWindowId; |
816 | 806 |
817 std::unique_ptr<tabs::GetSelected::Params> params( | 807 std::unique_ptr<tabs::GetSelected::Params> params( |
818 tabs::GetSelected::Params::Create(*args_)); | 808 tabs::GetSelected::Params::Create(*args_)); |
819 EXTENSION_FUNCTION_VALIDATE(params.get()); | 809 EXTENSION_FUNCTION_VALIDATE(params.get()); |
820 if (params->window_id.get()) | 810 if (params->window_id.get()) |
821 window_id = *params->window_id; | 811 window_id = *params->window_id; |
822 | 812 |
823 Browser* browser = NULL; | 813 Browser* browser = NULL; |
824 if (!GetBrowserFromWindowID(this, window_id, &browser)) | 814 if (!GetBrowserFromWindowID(this, window_id, &browser, &error_)) |
825 return false; | 815 return false; |
826 | 816 |
827 TabStripModel* tab_strip = browser->tab_strip_model(); | 817 TabStripModel* tab_strip = browser->tab_strip_model(); |
828 WebContents* contents = tab_strip->GetActiveWebContents(); | 818 WebContents* contents = tab_strip->GetActiveWebContents(); |
829 if (!contents) { | 819 if (!contents) { |
830 error_ = keys::kNoSelectedTabError; | 820 error_ = keys::kNoSelectedTabError; |
831 return false; | 821 return false; |
832 } | 822 } |
833 results_ = tabs::Get::Results::Create(*ExtensionTabUtil::CreateTabObject( | 823 results_ = tabs::Get::Results::Create(*ExtensionTabUtil::CreateTabObject( |
834 contents, tab_strip, tab_strip->active_index(), extension())); | 824 contents, tab_strip, tab_strip->active_index(), extension())); |
835 return true; | 825 return true; |
836 } | 826 } |
837 | 827 |
838 bool TabsGetAllInWindowFunction::RunSync() { | 828 bool TabsGetAllInWindowFunction::RunSync() { |
839 std::unique_ptr<tabs::GetAllInWindow::Params> params( | 829 std::unique_ptr<tabs::GetAllInWindow::Params> params( |
840 tabs::GetAllInWindow::Params::Create(*args_)); | 830 tabs::GetAllInWindow::Params::Create(*args_)); |
841 EXTENSION_FUNCTION_VALIDATE(params.get()); | 831 EXTENSION_FUNCTION_VALIDATE(params.get()); |
842 // windowId defaults to "current" window. | 832 // windowId defaults to "current" window. |
843 int window_id = extension_misc::kCurrentWindowId; | 833 int window_id = extension_misc::kCurrentWindowId; |
844 if (params->window_id.get()) | 834 if (params->window_id.get()) |
845 window_id = *params->window_id; | 835 window_id = *params->window_id; |
846 | 836 |
847 Browser* browser = NULL; | 837 Browser* browser = NULL; |
848 if (!GetBrowserFromWindowID(this, window_id, &browser)) | 838 if (!GetBrowserFromWindowID(this, window_id, &browser, &error_)) |
849 return false; | 839 return false; |
850 | 840 |
851 SetResult(ExtensionTabUtil::CreateTabList(browser, extension())); | 841 SetResult(ExtensionTabUtil::CreateTabList(browser, extension())); |
852 | 842 |
853 return true; | 843 return true; |
854 } | 844 } |
855 | 845 |
856 bool TabsQueryFunction::RunSync() { | 846 bool TabsQueryFunction::RunSync() { |
857 std::unique_ptr<tabs::Query::Params> params( | 847 std::unique_ptr<tabs::Query::Params> params( |
858 tabs::Query::Params::Create(*args_)); | 848 tabs::Query::Params::Create(*args_)); |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1130 std::unique_ptr<tabs::Highlight::Params> params( | 1120 std::unique_ptr<tabs::Highlight::Params> params( |
1131 tabs::Highlight::Params::Create(*args_)); | 1121 tabs::Highlight::Params::Create(*args_)); |
1132 EXTENSION_FUNCTION_VALIDATE(params.get()); | 1122 EXTENSION_FUNCTION_VALIDATE(params.get()); |
1133 | 1123 |
1134 // Get the window id from the params; default to current window if omitted. | 1124 // Get the window id from the params; default to current window if omitted. |
1135 int window_id = extension_misc::kCurrentWindowId; | 1125 int window_id = extension_misc::kCurrentWindowId; |
1136 if (params->highlight_info.window_id.get()) | 1126 if (params->highlight_info.window_id.get()) |
1137 window_id = *params->highlight_info.window_id; | 1127 window_id = *params->highlight_info.window_id; |
1138 | 1128 |
1139 Browser* browser = NULL; | 1129 Browser* browser = NULL; |
1140 if (!GetBrowserFromWindowID(this, window_id, &browser)) | 1130 if (!GetBrowserFromWindowID(this, window_id, &browser, &error_)) |
1141 return false; | 1131 return false; |
1142 | 1132 |
1143 TabStripModel* tabstrip = browser->tab_strip_model(); | 1133 TabStripModel* tabstrip = browser->tab_strip_model(); |
1144 ui::ListSelectionModel selection; | 1134 ui::ListSelectionModel selection; |
1145 int active_index = -1; | 1135 int active_index = -1; |
1146 | 1136 |
1147 if (params->highlight_info.tabs.as_integers) { | 1137 if (params->highlight_info.tabs.as_integers) { |
1148 std::vector<int>& tab_indices = *params->highlight_info.tabs.as_integers; | 1138 std::vector<int>& tab_indices = *params->highlight_info.tabs.as_integers; |
1149 // Create a new selection model as we read the list of tab indices. | 1139 // Create a new selection model as we read the list of tab indices. |
1150 for (size_t i = 0; i < tab_indices.size(); ++i) { | 1140 for (size_t i = 0; i < tab_indices.size(); ++i) { |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1476 error_ = keys::kTabStripNotEditableError; | 1466 error_ = keys::kTabStripNotEditableError; |
1477 return false; | 1467 return false; |
1478 } | 1468 } |
1479 | 1469 |
1480 // Insert the tabs one after another. | 1470 // Insert the tabs one after another. |
1481 *new_index += iteration; | 1471 *new_index += iteration; |
1482 | 1472 |
1483 if (window_id) { | 1473 if (window_id) { |
1484 Browser* target_browser = NULL; | 1474 Browser* target_browser = NULL; |
1485 | 1475 |
1486 if (!GetBrowserFromWindowID(this, *window_id, &target_browser)) | 1476 if (!GetBrowserFromWindowID(this, *window_id, &target_browser, &error_)) |
1487 return false; | 1477 return false; |
1488 | 1478 |
1489 if (!target_browser->window()->IsTabStripEditable()) { | 1479 if (!target_browser->window()->IsTabStripEditable()) { |
1490 error_ = keys::kTabStripNotEditableError; | 1480 error_ = keys::kTabStripNotEditableError; |
1491 return false; | 1481 return false; |
1492 } | 1482 } |
1493 | 1483 |
1494 if (!target_browser->is_type_tabbed()) { | 1484 if (!target_browser->is_type_tabbed()) { |
1495 error_ = keys::kCanOnlyMoveTabsWithinNormalWindowsError; | 1485 error_ = keys::kCanOnlyMoveTabsWithinNormalWindowsError; |
1496 return false; | 1486 return false; |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1671 } | 1661 } |
1672 return true; | 1662 return true; |
1673 } | 1663 } |
1674 | 1664 |
1675 bool TabsCaptureVisibleTabFunction::ClientAllowsTransparency() { | 1665 bool TabsCaptureVisibleTabFunction::ClientAllowsTransparency() { |
1676 return false; | 1666 return false; |
1677 } | 1667 } |
1678 | 1668 |
1679 WebContents* TabsCaptureVisibleTabFunction::GetWebContentsForID(int window_id) { | 1669 WebContents* TabsCaptureVisibleTabFunction::GetWebContentsForID(int window_id) { |
1680 Browser* browser = NULL; | 1670 Browser* browser = NULL; |
1681 if (!GetBrowserFromWindowID(&chrome_details_, window_id, &browser)) | 1671 if (!GetBrowserFromWindowID(chrome_details_, window_id, &browser, &error_)) |
1682 return NULL; | 1672 return NULL; |
1683 | 1673 |
1684 WebContents* contents = browser->tab_strip_model()->GetActiveWebContents(); | 1674 WebContents* contents = browser->tab_strip_model()->GetActiveWebContents(); |
1685 if (!contents) { | 1675 if (!contents) { |
1686 error_ = "No active web contents to capture"; | 1676 error_ = "No active web contents to capture"; |
1687 return NULL; | 1677 return NULL; |
1688 } | 1678 } |
1689 | 1679 |
1690 if (!extension()->permissions_data()->CanCaptureVisiblePage( | 1680 if (!extension()->permissions_data()->CanCaptureVisiblePage( |
1691 SessionTabHelper::IdForTab(contents), &error_)) { | 1681 SessionTabHelper::IdForTab(contents), &error_)) { |
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2184 params->tab_id | 2174 params->tab_id |
2185 ? ErrorUtils::FormatErrorMessage(keys::kCannotDiscardTab, | 2175 ? ErrorUtils::FormatErrorMessage(keys::kCannotDiscardTab, |
2186 base::IntToString(*params->tab_id)) | 2176 base::IntToString(*params->tab_id)) |
2187 : keys::kCannotFindTabToDiscard)); | 2177 : keys::kCannotFindTabToDiscard)); |
2188 } | 2178 } |
2189 | 2179 |
2190 TabsDiscardFunction::TabsDiscardFunction() {} | 2180 TabsDiscardFunction::TabsDiscardFunction() {} |
2191 TabsDiscardFunction::~TabsDiscardFunction() {} | 2181 TabsDiscardFunction::~TabsDiscardFunction() {} |
2192 | 2182 |
2193 } // namespace extensions | 2183 } // namespace extensions |
OLD | NEW |