| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/extension_tabs_module.h" | 5 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
| 9 #include "chrome/browser/browser_list.h" | 9 #include "chrome/browser/browser_list.h" |
| 10 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 10 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 int ExtensionTabUtil::GetWindowIdOfTab(const TabContents* tab_contents) { | 38 int ExtensionTabUtil::GetWindowIdOfTab(const TabContents* tab_contents) { |
| 39 return tab_contents->controller().window_id().id(); | 39 return tab_contents->controller().window_id().id(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 bool GetWindowsFunction::RunImpl() { | 42 bool GetWindowsFunction::RunImpl() { |
| 43 std::set<int> window_ids; | 43 std::set<int> window_ids; |
| 44 | 44 |
| 45 // Look for |ids| named parameter as list of id's to fetch. | 45 // Look for |ids| named parameter as list of id's to fetch. |
| 46 if (args_->IsType(Value::TYPE_DICTIONARY)) { | 46 if (args_->IsType(Value::TYPE_DICTIONARY)) { |
| 47 Value *ids_value; | 47 ListValue *window_id_list; |
| 48 if ((!static_cast<DictionaryValue*>(args_)->Get(L"ids", &ids_value)) || | 48 const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); |
| 49 (!ids_value->IsType(Value::TYPE_LIST))) { | 49 EXTENSION_FUNCTION_VALIDATE(args->GetList(L"ids", &window_id_list)); |
| 50 DCHECK(false); | |
| 51 return false; | |
| 52 } | |
| 53 | |
| 54 ListValue *window_id_list = static_cast<ListValue*>(ids_value); | |
| 55 for (ListValue::iterator id = window_id_list->begin(); | 50 for (ListValue::iterator id = window_id_list->begin(); |
| 56 id != window_id_list->end(); ++id) { | 51 id != window_id_list->end(); ++id) { |
| 57 int window_id; | 52 int window_id; |
| 58 if (!(*id)->GetAsInteger(&window_id)) { | 53 EXTENSION_FUNCTION_VALIDATE((*id)->GetAsInteger(&window_id)); |
| 59 DCHECK(false); | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 window_ids.insert(window_id); | 54 window_ids.insert(window_id); |
| 64 } | 55 } |
| 65 } | 56 } |
| 66 | 57 |
| 67 // Default to all windows. | 58 // Default to all windows. |
| 68 bool all_windows = (window_ids.size() == 0); | 59 bool all_windows = (window_ids.size() == 0); |
| 69 | 60 |
| 70 result_.reset(new ListValue()); | 61 result_.reset(new ListValue()); |
| 71 for (BrowserList::const_iterator browser = BrowserList::begin(); | 62 for (BrowserList::const_iterator browser = BrowserList::begin(); |
| 72 browser != BrowserList::end(); ++browser) { | 63 browser != BrowserList::end(); ++browser) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 new_window->window()->Show(); | 134 new_window->window()->Show(); |
| 144 | 135 |
| 145 // TODO(rafaelw): support |focused|, |zIndex| | 136 // TODO(rafaelw): support |focused|, |zIndex| |
| 146 | 137 |
| 147 result_.reset(CreateWindowValue(new_window)); | 138 result_.reset(CreateWindowValue(new_window)); |
| 148 | 139 |
| 149 return true; | 140 return true; |
| 150 } | 141 } |
| 151 | 142 |
| 152 bool RemoveWindowFunction::RunImpl() { | 143 bool RemoveWindowFunction::RunImpl() { |
| 153 if (!args_->IsType(Value::TYPE_INTEGER)) | |
| 154 return false; | |
| 155 | |
| 156 int window_id; | 144 int window_id; |
| 157 if (!args_->GetAsInteger(&window_id)) | 145 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id)); |
| 158 return false; | |
| 159 | 146 |
| 160 Browser* target = NULL; | 147 Browser* target = NULL; |
| 161 for (BrowserList::const_iterator browser = BrowserList::begin(); | 148 for (BrowserList::const_iterator browser = BrowserList::begin(); |
| 162 browser != BrowserList::end(); ++browser) { | 149 browser != BrowserList::end(); ++browser) { |
| 163 // Only examine browsers in the current profile. | 150 // Only examine browsers in the current profile. |
| 164 if ((*browser)->profile() == profile()) { | 151 if ((*browser)->profile() == profile()) { |
| 165 if (ExtensionTabUtil::GetWindowId(*browser) == window_id) { | 152 if (ExtensionTabUtil::GetWindowId(*browser) == window_id) { |
| 166 target = *browser; | 153 target = *browser; |
| 167 break; | 154 break; |
| 168 } | 155 } |
| 169 } | 156 } |
| 170 } | 157 } |
| 171 | 158 |
| 172 if (target == NULL) { | 159 if (target == NULL) { |
| 173 // TODO(rafaelw): need error message. | 160 // TODO(rafaelw): need error message. |
| 174 return false; | 161 return false; |
| 175 } | 162 } |
| 176 | 163 |
| 177 target->CloseWindow(); | 164 target->CloseWindow(); |
| 178 | 165 |
| 179 return true; | 166 return true; |
| 180 } | 167 } |
| 181 | 168 |
| 182 | 169 |
| 183 bool GetTabsForWindowFunction::RunImpl() { | 170 bool GetTabsForWindowFunction::RunImpl() { |
| 184 if (!args_->IsType(Value::TYPE_NULL)) | 171 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_NULL)); |
| 185 return false; | |
| 186 | 172 |
| 187 Browser* browser = dispatcher_->browser(); | 173 Browser* browser = dispatcher_->browser(); |
| 188 if (!browser) | 174 if (!browser) |
| 189 return false; | 175 return false; |
| 190 | 176 |
| 191 result_.reset(CreateTabList(browser)); | 177 result_.reset(CreateTabList(browser)); |
| 192 | 178 |
| 193 return true; | 179 return true; |
| 194 } | 180 } |
| 195 | 181 |
| 196 bool CreateTabFunction::RunImpl() { | 182 bool CreateTabFunction::RunImpl() { |
| 197 // TODO(aa): Do data-driven validation in JS. | 183 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 198 if (!args_->IsType(Value::TYPE_DICTIONARY)) | 184 const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); |
| 199 return false; | |
| 200 | 185 |
| 201 Browser* browser = BrowserList::GetLastActive(); | 186 Browser* browser = BrowserList::GetLastActive(); |
| 202 if (!browser) | 187 if (!browser) |
| 203 return false; | 188 return false; |
| 204 | 189 |
| 205 TabStripModel *tab_strip = browser->tabstrip_model(); | 190 TabStripModel *tab_strip = browser->tabstrip_model(); |
| 206 const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); | |
| 207 | 191 |
| 208 // TODO(rafaelw): handle setting remaining tab properties: | 192 // TODO(rafaelw): handle setting remaining tab properties: |
| 209 // -windowId | 193 // -windowId |
| 210 // -title | 194 // -title |
| 211 // -favIconUrl | 195 // -favIconUrl |
| 212 | 196 |
| 213 std::string url; | 197 std::string url; |
| 214 args->GetString(L"url", &url); | 198 args->GetString(L"url", &url); |
| 215 | 199 |
| 216 // Default to foreground for the new tab. The presence of 'selected' property | 200 // Default to foreground for the new tab. The presence of 'selected' property |
| (...skipping 18 matching lines...) Expand all Loading... |
| 235 index = tab_strip->GetIndexOfTabContents(contents); | 219 index = tab_strip->GetIndexOfTabContents(contents); |
| 236 | 220 |
| 237 // Return data about the newly created tab. | 221 // Return data about the newly created tab. |
| 238 if (has_callback()) | 222 if (has_callback()) |
| 239 result_.reset(CreateTabValue(tab_strip, index)); | 223 result_.reset(CreateTabValue(tab_strip, index)); |
| 240 | 224 |
| 241 return true; | 225 return true; |
| 242 } | 226 } |
| 243 | 227 |
| 244 bool GetTabFunction::RunImpl() { | 228 bool GetTabFunction::RunImpl() { |
| 245 if (!args_->IsType(Value::TYPE_INTEGER)) | 229 int tab_id; |
| 246 return false; | 230 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id)); |
| 247 | 231 |
| 248 Browser* browser = BrowserList::GetLastActive(); | 232 Browser* browser = BrowserList::GetLastActive(); |
| 249 if (!browser) | 233 if (!browser) |
| 250 return false; | 234 return false; |
| 251 | 235 |
| 252 int tab_id; | |
| 253 args_->GetAsInteger(&tab_id); | |
| 254 | |
| 255 int tab_index; | 236 int tab_index; |
| 256 TabStripModel* tab_strip = browser->tabstrip_model(); | 237 TabStripModel* tab_strip = browser->tabstrip_model(); |
| 257 // TODO(rafaelw): return an error if the tab is not found by |tab_id| | 238 // TODO(rafaelw): return an error if the tab is not found by |tab_id| |
| 258 if (!GetIndexOfTabId(tab_strip, tab_id, &tab_index)) | 239 if (!GetIndexOfTabId(tab_strip, tab_id, &tab_index)) |
| 259 return false; | 240 return false; |
| 260 | 241 |
| 261 result_.reset(CreateTabValue(tab_strip, tab_index)); | 242 result_.reset(CreateTabValue(tab_strip, tab_index)); |
| 262 return true; | 243 return true; |
| 263 } | 244 } |
| 264 | 245 |
| 265 bool UpdateTabFunction::RunImpl() { | 246 bool UpdateTabFunction::RunImpl() { |
| 266 // TODO(aa): Do data-driven validation in JS. | 247 int tab_id; |
| 267 if (!args_->IsType(Value::TYPE_DICTIONARY)) | 248 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 268 return false; | 249 const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); |
| 250 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"id", &tab_id)); |
| 269 | 251 |
| 270 Browser* browser = BrowserList::GetLastActive(); | 252 Browser* browser = BrowserList::GetLastActive(); |
| 271 if (!browser) | 253 if (!browser) |
| 272 return false; | 254 return false; |
| 273 | 255 |
| 274 int tab_id; | |
| 275 const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); | |
| 276 if (!args->GetInteger(L"id", &tab_id)) | |
| 277 return false; | |
| 278 | |
| 279 int tab_index; | 256 int tab_index; |
| 280 TabStripModel* tab_strip = browser->tabstrip_model(); | 257 TabStripModel* tab_strip = browser->tabstrip_model(); |
| 281 // TODO(rafaelw): return an error if the tab is not found by |tab_id| | 258 // TODO(rafaelw): return an error if the tab is not found by |tab_id| |
| 282 if (!GetIndexOfTabId(tab_strip, tab_id, &tab_index)) | 259 if (!GetIndexOfTabId(tab_strip, tab_id, &tab_index)) |
| 283 return false; | 260 return false; |
| 284 | 261 |
| 285 TabContents* tab_contents = tab_strip->GetTabContentsAt(tab_index); | 262 TabContents* tab_contents = tab_strip->GetTabContentsAt(tab_index); |
| 286 NavigationController& controller = tab_contents->controller(); | 263 NavigationController& controller = tab_contents->controller(); |
| 287 | 264 |
| 288 // TODO(rafaelw): handle setting remaining tab properties: | 265 // TODO(rafaelw): handle setting remaining tab properties: |
| (...skipping 17 matching lines...) Expand all Loading... |
| 306 if (args->GetBoolean(L"selected", &selected) && | 283 if (args->GetBoolean(L"selected", &selected) && |
| 307 selected && | 284 selected && |
| 308 tab_strip->selected_index() != tab_index) { | 285 tab_strip->selected_index() != tab_index) { |
| 309 tab_strip->SelectTabContentsAt(tab_index, false); | 286 tab_strip->SelectTabContentsAt(tab_index, false); |
| 310 } | 287 } |
| 311 | 288 |
| 312 return true; | 289 return true; |
| 313 } | 290 } |
| 314 | 291 |
| 315 bool MoveTabFunction::RunImpl() { | 292 bool MoveTabFunction::RunImpl() { |
| 316 if (!args_->IsType(Value::TYPE_DICTIONARY)) | 293 int tab_id; |
| 317 return false; | 294 int new_index; |
| 295 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 296 const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); |
| 297 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"id", &tab_id)); |
| 298 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"index", &new_index)); |
| 299 EXTENSION_FUNCTION_VALIDATE(new_index >= 0); |
| 318 | 300 |
| 319 Browser* browser = BrowserList::GetLastActive(); | 301 Browser* browser = BrowserList::GetLastActive(); |
| 320 if (!browser) | 302 if (!browser) |
| 321 return false; | 303 return false; |
| 322 | 304 |
| 323 int tab_id; | |
| 324 const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); | |
| 325 if (!args->GetInteger(L"id", &tab_id)) | |
| 326 return false; | |
| 327 | |
| 328 int tab_index; | 305 int tab_index; |
| 329 TabStripModel* tab_strip = browser->tabstrip_model(); | 306 TabStripModel* tab_strip = browser->tabstrip_model(); |
| 330 // TODO(rafaelw): return an error if the tab is not found by |tab_id| | 307 // TODO(rafaelw): return an error if the tab is not found by |tab_id| |
| 331 if (!GetIndexOfTabId(tab_strip, tab_id, &tab_index)) | 308 if (!GetIndexOfTabId(tab_strip, tab_id, &tab_index)) |
| 332 return false; | 309 return false; |
| 333 | 310 |
| 334 // TODO(rafaelw): support moving tabs between windows | 311 // TODO(rafaelw): support moving tabs between windows |
| 335 // -windowId | 312 // -windowId |
| 336 | 313 |
| 337 int new_index; | |
| 338 bool found_index = args->GetInteger(L"index", &new_index); | |
| 339 if (!found_index || new_index < 0) { | |
| 340 DCHECK(false); | |
| 341 return false; | |
| 342 } | |
| 343 | |
| 344 // Clamp move location to the last position. | 314 // Clamp move location to the last position. |
| 345 if (new_index >= tab_strip->count()) { | 315 if (new_index >= tab_strip->count()) { |
| 346 new_index = tab_strip->count() - 1; | 316 new_index = tab_strip->count() - 1; |
| 347 } | 317 } |
| 348 | 318 |
| 349 if (new_index != tab_index) { | 319 if (new_index != tab_index) { |
| 350 tab_strip->MoveTabContentsAt(tab_index, new_index, false); | 320 tab_strip->MoveTabContentsAt(tab_index, new_index, false); |
| 351 } | 321 } |
| 352 | 322 |
| 353 return true; | 323 return true; |
| 354 } | 324 } |
| 355 | 325 |
| 356 | 326 |
| 357 bool RemoveTabFunction::RunImpl() { | 327 bool RemoveTabFunction::RunImpl() { |
| 358 // TODO(rafaelw): This should have a callback, but it can't because it could | 328 // TODO(rafaelw): This should have a callback, but it can't because it could |
| 359 // close it's own tab. | 329 // close it's own tab. |
| 360 | 330 int tab_id; |
| 361 if (!args_->IsType(Value::TYPE_INTEGER)) | 331 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id)); |
| 362 return false; | |
| 363 | 332 |
| 364 Browser* browser = BrowserList::GetLastActive(); | 333 Browser* browser = BrowserList::GetLastActive(); |
| 365 if (!browser) | 334 if (!browser) |
| 366 return false; | 335 return false; |
| 367 | 336 |
| 368 int tab_id; | |
| 369 if (!args_->GetAsInteger(&tab_id)) { | |
| 370 return false; | |
| 371 } | |
| 372 | |
| 373 int tab_index; | 337 int tab_index; |
| 374 TabStripModel* tab_strip = browser->tabstrip_model(); | 338 TabStripModel* tab_strip = browser->tabstrip_model(); |
| 375 if (GetIndexOfTabId(tab_strip, tab_id, &tab_index)) { | 339 if (GetIndexOfTabId(tab_strip, tab_id, &tab_index)) { |
| 376 browser->CloseTabContents(tab_strip->GetTabContentsAt(tab_index)); | 340 browser->CloseTabContents(tab_strip->GetTabContentsAt(tab_index)); |
| 377 return true; | 341 return true; |
| 378 } | 342 } |
| 379 | 343 |
| 380 return false; | 344 return false; |
| 381 } | 345 } |
| 382 | 346 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 int* tab_index) { | 397 int* tab_index) { |
| 434 for (int i = 0; i < tab_strip->count(); ++i) { | 398 for (int i = 0; i < tab_strip->count(); ++i) { |
| 435 TabContents* tab_contents = tab_strip->GetTabContentsAt(i); | 399 TabContents* tab_contents = tab_strip->GetTabContentsAt(i); |
| 436 if (tab_contents->controller().session_id().id() == tab_id) { | 400 if (tab_contents->controller().session_id().id() == tab_id) { |
| 437 *tab_index = i; | 401 *tab_index = i; |
| 438 return true; | 402 return true; |
| 439 } | 403 } |
| 440 } | 404 } |
| 441 return false; | 405 return false; |
| 442 } | 406 } |
| OLD | NEW |