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.h" | 5 #include "chrome/browser/extensions/api/tabs/tabs.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/base64.h" | 11 #include "base/base64.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/string16.h" | 17 #include "base/string16.h" |
18 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
20 #include "base/stringprintf.h" | 20 #include "base/stringprintf.h" |
21 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
22 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 22 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
23 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 23 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
24 #include "chrome/browser/extensions/extension_function_util.h" | 24 #include "chrome/browser/extensions/extension_function_util.h" |
25 #include "chrome/browser/extensions/extension_host.h" | 25 #include "chrome/browser/extensions/extension_host.h" |
26 #include "chrome/browser/extensions/extension_service.h" | 26 #include "chrome/browser/extensions/extension_service.h" |
27 #include "chrome/browser/extensions/tab_helper.h" | 27 #include "chrome/browser/extensions/tab_helper.h" |
28 #include "chrome/browser/extensions/extension_tab_util.h" | 28 #include "chrome/browser/extensions/extension_tab_util.h" |
29 #include "chrome/browser/extensions/extension_window_controller.h" | |
30 #include "chrome/browser/extensions/extension_window_list.h" | |
31 #include "chrome/browser/extensions/script_executor.h" | 29 #include "chrome/browser/extensions/script_executor.h" |
| 30 #include "chrome/browser/extensions/window_controller.h" |
| 31 #include "chrome/browser/extensions/window_controller_list.h" |
32 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 32 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
33 #include "chrome/browser/profiles/profile.h" | 33 #include "chrome/browser/profiles/profile.h" |
34 #include "chrome/browser/sessions/restore_tab_helper.h" | 34 #include "chrome/browser/sessions/restore_tab_helper.h" |
35 #include "chrome/browser/translate/translate_tab_helper.h" | 35 #include "chrome/browser/translate/translate_tab_helper.h" |
36 #include "chrome/browser/ui/browser.h" | 36 #include "chrome/browser/ui/browser.h" |
37 #include "chrome/browser/ui/browser_commands.h" | 37 #include "chrome/browser/ui/browser_commands.h" |
38 #include "chrome/browser/ui/browser_finder.h" | 38 #include "chrome/browser/ui/browser_finder.h" |
39 #include "chrome/browser/ui/browser_list.h" | 39 #include "chrome/browser/ui/browser_list.h" |
40 #include "chrome/browser/ui/browser_navigator.h" | 40 #include "chrome/browser/ui/browser_navigator.h" |
41 #include "chrome/browser/ui/browser_tabstrip.h" | 41 #include "chrome/browser/ui/browser_tabstrip.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 namespace errors = extension_manifest_errors; | 90 namespace errors = extension_manifest_errors; |
91 namespace keys = extensions::tabs_constants; | 91 namespace keys = extensions::tabs_constants; |
92 | 92 |
93 using content::NavigationController; | 93 using content::NavigationController; |
94 using content::NavigationEntry; | 94 using content::NavigationEntry; |
95 using content::OpenURLParams; | 95 using content::OpenURLParams; |
96 using content::Referrer; | 96 using content::Referrer; |
97 using content::RenderViewHost; | 97 using content::RenderViewHost; |
98 using content::WebContents; | 98 using content::WebContents; |
99 using extensions::ScriptExecutor; | 99 using extensions::ScriptExecutor; |
| 100 using extensions::WindowController; |
| 101 using extensions::WindowControllerList; |
100 | 102 |
101 const int CaptureVisibleTabFunction::kDefaultQuality = 90; | 103 const int CaptureVisibleTabFunction::kDefaultQuality = 90; |
102 | 104 |
103 namespace { | 105 namespace { |
104 | 106 |
105 // |error_message| can optionally be passed in a will be set with an appropriate | 107 // |error_message| can optionally be passed in a will be set with an appropriate |
106 // message if the window cannot be found by id. | 108 // message if the window cannot be found by id. |
107 Browser* GetBrowserInProfileWithId(Profile* profile, | 109 Browser* GetBrowserInProfileWithId(Profile* profile, |
108 const int window_id, | 110 const int window_id, |
109 bool include_incognito, | 111 bool include_incognito, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 if (!*browser) { | 144 if (!*browser) { |
143 function->SetError(error); | 145 function->SetError(error); |
144 return false; | 146 return false; |
145 } | 147 } |
146 } | 148 } |
147 return true; | 149 return true; |
148 } | 150 } |
149 | 151 |
150 bool GetWindowFromWindowID(UIThreadExtensionFunction* function, | 152 bool GetWindowFromWindowID(UIThreadExtensionFunction* function, |
151 int window_id, | 153 int window_id, |
152 ExtensionWindowController** controller) { | 154 WindowController** controller) { |
153 if (window_id == extension_misc::kCurrentWindowId) { | 155 if (window_id == extension_misc::kCurrentWindowId) { |
154 ExtensionWindowController* extension_window_controller = | 156 WindowController* extension_window_controller = |
155 function->dispatcher()->delegate()->GetExtensionWindowController(); | 157 function->dispatcher()->delegate()->GetExtensionWindowController(); |
156 // If there is a window controller associated with this extension, use that. | 158 // If there is a window controller associated with this extension, use that. |
157 if (extension_window_controller) { | 159 if (extension_window_controller) { |
158 *controller = extension_window_controller; | 160 *controller = extension_window_controller; |
159 } else { | 161 } else { |
160 // Otherwise get the focused or most recently added window. | 162 // Otherwise get the focused or most recently added window. |
161 *controller = ExtensionWindowList::GetInstance()-> | 163 *controller = WindowControllerList::GetInstance()-> |
162 CurrentWindowForFunction(function); | 164 CurrentWindowForFunction(function); |
163 } | 165 } |
164 if (!(*controller)) { | 166 if (!(*controller)) { |
165 function->SetError(keys::kNoCurrentWindowError); | 167 function->SetError(keys::kNoCurrentWindowError); |
166 return false; | 168 return false; |
167 } | 169 } |
168 } else { | 170 } else { |
169 *controller = ExtensionWindowList::GetInstance()->FindWindowForFunctionById( | 171 *controller = WindowControllerList::GetInstance()-> |
170 function, window_id); | 172 FindWindowForFunctionById(function, window_id); |
171 if (!(*controller)) { | 173 if (!(*controller)) { |
172 function->SetError(ExtensionErrorUtils::FormatErrorMessage( | 174 function->SetError(ExtensionErrorUtils::FormatErrorMessage( |
173 keys::kWindowNotFoundError, base::IntToString(window_id))); | 175 keys::kWindowNotFoundError, base::IntToString(window_id))); |
174 return false; | 176 return false; |
175 } | 177 } |
176 } | 178 } |
177 return true; | 179 return true; |
178 } | 180 } |
179 // |error_message| can optionally be passed in and will be set with an | 181 // |error_message| can optionally be passed in and will be set with an |
180 // appropriate message if the tab cannot be found by id. | 182 // appropriate message if the tab cannot be found by id. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 // Windows --------------------------------------------------------------------- | 251 // Windows --------------------------------------------------------------------- |
250 | 252 |
251 bool GetWindowFunction::RunImpl() { | 253 bool GetWindowFunction::RunImpl() { |
252 scoped_ptr<Get::Params> params(Get::Params::Create(*args_)); | 254 scoped_ptr<Get::Params> params(Get::Params::Create(*args_)); |
253 EXTENSION_FUNCTION_VALIDATE(params.get()); | 255 EXTENSION_FUNCTION_VALIDATE(params.get()); |
254 | 256 |
255 bool populate_tabs = false; | 257 bool populate_tabs = false; |
256 if (params->get_info.get() && params->get_info->populate.get()) | 258 if (params->get_info.get() && params->get_info->populate.get()) |
257 populate_tabs = *params->get_info->populate; | 259 populate_tabs = *params->get_info->populate; |
258 | 260 |
259 ExtensionWindowController* controller; | 261 WindowController* controller; |
260 if (!GetWindowFromWindowID(this, params->window_id, &controller)) | 262 if (!GetWindowFromWindowID(this, params->window_id, &controller)) |
261 return false; | 263 return false; |
262 | 264 |
263 if (populate_tabs) | 265 if (populate_tabs) |
264 SetResult(controller->CreateWindowValueWithTabs()); | 266 SetResult(controller->CreateWindowValueWithTabs()); |
265 else | 267 else |
266 SetResult(controller->CreateWindowValue()); | 268 SetResult(controller->CreateWindowValue()); |
267 return true; | 269 return true; |
268 } | 270 } |
269 | 271 |
270 bool GetCurrentWindowFunction::RunImpl() { | 272 bool GetCurrentWindowFunction::RunImpl() { |
271 scoped_ptr<GetCurrent::Params> params(GetCurrent::Params::Create(*args_)); | 273 scoped_ptr<GetCurrent::Params> params(GetCurrent::Params::Create(*args_)); |
272 EXTENSION_FUNCTION_VALIDATE(params.get()); | 274 EXTENSION_FUNCTION_VALIDATE(params.get()); |
273 | 275 |
274 bool populate_tabs = false; | 276 bool populate_tabs = false; |
275 if (params->get_info.get() && params->get_info->populate.get()) | 277 if (params->get_info.get() && params->get_info->populate.get()) |
276 populate_tabs = *params->get_info->populate; | 278 populate_tabs = *params->get_info->populate; |
277 | 279 |
278 ExtensionWindowController* controller; | 280 WindowController* controller; |
279 if (!GetWindowFromWindowID(this, | 281 if (!GetWindowFromWindowID(this, |
280 extension_misc::kCurrentWindowId, | 282 extension_misc::kCurrentWindowId, |
281 &controller)) { | 283 &controller)) { |
282 return false; | 284 return false; |
283 } | 285 } |
284 if (populate_tabs) | 286 if (populate_tabs) |
285 SetResult(controller->CreateWindowValueWithTabs()); | 287 SetResult(controller->CreateWindowValueWithTabs()); |
286 else | 288 else |
287 SetResult(controller->CreateWindowValue()); | 289 SetResult(controller->CreateWindowValue()); |
288 return true; | 290 return true; |
289 } | 291 } |
290 | 292 |
291 bool GetLastFocusedWindowFunction::RunImpl() { | 293 bool GetLastFocusedWindowFunction::RunImpl() { |
292 scoped_ptr<GetLastFocused::Params> params( | 294 scoped_ptr<GetLastFocused::Params> params( |
293 GetLastFocused::Params::Create(*args_)); | 295 GetLastFocused::Params::Create(*args_)); |
294 EXTENSION_FUNCTION_VALIDATE(params.get()); | 296 EXTENSION_FUNCTION_VALIDATE(params.get()); |
295 | 297 |
296 bool populate_tabs = false; | 298 bool populate_tabs = false; |
297 if (params->get_info.get() && params->get_info->populate.get()) | 299 if (params->get_info.get() && params->get_info->populate.get()) |
298 populate_tabs = *params->get_info->populate; | 300 populate_tabs = *params->get_info->populate; |
299 | 301 |
300 // Note: currently this returns the last active browser. If we decide to | 302 // Note: currently this returns the last active browser. If we decide to |
301 // include other window types (e.g. panels), we will need to add logic to | 303 // include other window types (e.g. panels), we will need to add logic to |
302 // ExtensionWindowList that mirrors the active behavior of BrowserList. | 304 // WindowControllerList that mirrors the active behavior of BrowserList. |
303 Browser* browser = browser::FindAnyBrowser( | 305 Browser* browser = browser::FindAnyBrowser( |
304 profile(), include_incognito()); | 306 profile(), include_incognito()); |
305 if (!browser || !browser->window()) { | 307 if (!browser || !browser->window()) { |
306 error_ = keys::kNoLastFocusedWindowError; | 308 error_ = keys::kNoLastFocusedWindowError; |
307 return false; | 309 return false; |
308 } | 310 } |
309 ExtensionWindowController* controller = | 311 WindowController* controller = |
310 browser->extension_window_controller(); | 312 browser->extension_window_controller(); |
311 if (populate_tabs) | 313 if (populate_tabs) |
312 SetResult(controller->CreateWindowValueWithTabs()); | 314 SetResult(controller->CreateWindowValueWithTabs()); |
313 else | 315 else |
314 SetResult(controller->CreateWindowValue()); | 316 SetResult(controller->CreateWindowValue()); |
315 return true; | 317 return true; |
316 } | 318 } |
317 | 319 |
318 bool GetAllWindowsFunction::RunImpl() { | 320 bool GetAllWindowsFunction::RunImpl() { |
319 scoped_ptr<GetAll::Params> params(GetAll::Params::Create(*args_)); | 321 scoped_ptr<GetAll::Params> params(GetAll::Params::Create(*args_)); |
320 EXTENSION_FUNCTION_VALIDATE(params.get()); | 322 EXTENSION_FUNCTION_VALIDATE(params.get()); |
321 | 323 |
322 bool populate_tabs = false; | 324 bool populate_tabs = false; |
323 if (params->get_info.get() && params->get_info->populate.get()) | 325 if (params->get_info.get() && params->get_info->populate.get()) |
324 populate_tabs = *params->get_info->populate; | 326 populate_tabs = *params->get_info->populate; |
325 | 327 |
326 ListValue* window_list = new ListValue(); | 328 ListValue* window_list = new ListValue(); |
327 const ExtensionWindowList::WindowList& windows = | 329 const WindowControllerList::ControllerList& windows = |
328 ExtensionWindowList::GetInstance()->windows(); | 330 WindowControllerList::GetInstance()->windows(); |
329 for (ExtensionWindowList::WindowList::const_iterator iter = | 331 for (WindowControllerList::ControllerList::const_iterator iter = |
330 windows.begin(); | 332 windows.begin(); |
331 iter != windows.end(); ++iter) { | 333 iter != windows.end(); ++iter) { |
332 if (!this->CanOperateOnWindow(*iter)) | 334 if (!this->CanOperateOnWindow(*iter)) |
333 continue; | 335 continue; |
334 if (populate_tabs) | 336 if (populate_tabs) |
335 window_list->Append((*iter)->CreateWindowValueWithTabs()); | 337 window_list->Append((*iter)->CreateWindowValueWithTabs()); |
336 else | 338 else |
337 window_list->Append((*iter)->CreateWindowValue()); | 339 window_list->Append((*iter)->CreateWindowValue()); |
338 } | 340 } |
339 SetResult(window_list); | 341 SetResult(window_list); |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 | 645 |
644 return true; | 646 return true; |
645 } | 647 } |
646 | 648 |
647 bool UpdateWindowFunction::RunImpl() { | 649 bool UpdateWindowFunction::RunImpl() { |
648 int window_id = extension_misc::kUnknownWindowId; | 650 int window_id = extension_misc::kUnknownWindowId; |
649 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 651 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
650 DictionaryValue* update_props; | 652 DictionaryValue* update_props; |
651 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); | 653 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); |
652 | 654 |
653 ExtensionWindowController* controller; | 655 WindowController* controller; |
654 if (!GetWindowFromWindowID(this, window_id, &controller)) | 656 if (!GetWindowFromWindowID(this, window_id, &controller)) |
655 return false; | 657 return false; |
656 | 658 |
657 ui::WindowShowState show_state = ui::SHOW_STATE_DEFAULT; // No change. | 659 ui::WindowShowState show_state = ui::SHOW_STATE_DEFAULT; // No change. |
658 std::string state_str; | 660 std::string state_str; |
659 if (update_props->HasKey(keys::kShowStateKey)) { | 661 if (update_props->HasKey(keys::kShowStateKey)) { |
660 EXTENSION_FUNCTION_VALIDATE(update_props->GetString(keys::kShowStateKey, | 662 EXTENSION_FUNCTION_VALIDATE(update_props->GetString(keys::kShowStateKey, |
661 &state_str)); | 663 &state_str)); |
662 if (state_str == keys::kShowStateValueNormal) { | 664 if (state_str == keys::kShowStateValueNormal) { |
663 show_state = ui::SHOW_STATE_NORMAL; | 665 show_state = ui::SHOW_STATE_NORMAL; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 | 779 |
778 SetResult(controller->CreateWindowValue()); | 780 SetResult(controller->CreateWindowValue()); |
779 | 781 |
780 return true; | 782 return true; |
781 } | 783 } |
782 | 784 |
783 bool RemoveWindowFunction::RunImpl() { | 785 bool RemoveWindowFunction::RunImpl() { |
784 int window_id = -1; | 786 int window_id = -1; |
785 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 787 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
786 | 788 |
787 ExtensionWindowController* controller; | 789 WindowController* controller; |
788 if (!GetWindowFromWindowID(this, window_id, &controller)) | 790 if (!GetWindowFromWindowID(this, window_id, &controller)) |
789 return false; | 791 return false; |
790 | 792 |
791 ExtensionWindowController::Reason reason; | 793 WindowController::Reason reason; |
792 if (!controller->CanClose(&reason)) { | 794 if (!controller->CanClose(&reason)) { |
793 if (reason == ExtensionWindowController::REASON_NOT_EDITABLE) | 795 if (reason == WindowController::REASON_NOT_EDITABLE) |
794 error_ = keys::kTabStripNotEditableError; | 796 error_ = keys::kTabStripNotEditableError; |
795 return false; | 797 return false; |
796 } | 798 } |
797 controller->window()->Close(); | 799 controller->window()->Close(); |
798 return true; | 800 return true; |
799 } | 801 } |
800 | 802 |
801 // Tabs ------------------------------------------------------------------------ | 803 // Tabs ------------------------------------------------------------------------ |
802 | 804 |
803 bool GetSelectedTabFunction::RunImpl() { | 805 bool GetSelectedTabFunction::RunImpl() { |
(...skipping 1005 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1809 // called for every API call the extension made. | 1811 // called for every API call the extension made. |
1810 GotLanguage(language); | 1812 GotLanguage(language); |
1811 } | 1813 } |
1812 | 1814 |
1813 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { | 1815 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { |
1814 SetResult(Value::CreateStringValue(language.c_str())); | 1816 SetResult(Value::CreateStringValue(language.c_str())); |
1815 SendResponse(true); | 1817 SendResponse(true); |
1816 | 1818 |
1817 Release(); // Balanced in Run() | 1819 Release(); // Balanced in Run() |
1818 } | 1820 } |
OLD | NEW |