| 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/browser_window.h" | 10 #include "chrome/browser/browser_window.h" |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 Browser* browser = GetBrowserInProfileWithId(profile(), window_id, &error_); | 157 Browser* browser = GetBrowserInProfileWithId(profile(), window_id, &error_); |
| 158 if (!browser) | 158 if (!browser) |
| 159 return false; | 159 return false; |
| 160 | 160 |
| 161 result_.reset(CreateWindowValue(browser, false)); | 161 result_.reset(CreateWindowValue(browser, false)); |
| 162 return true; | 162 return true; |
| 163 } | 163 } |
| 164 | 164 |
| 165 bool GetCurrentWindowFunction::RunImpl() { | 165 bool GetCurrentWindowFunction::RunImpl() { |
| 166 Browser* browser = dispatcher_->GetBrowser(); | 166 Browser* browser = dispatcher_->GetBrowser(); |
| 167 if (!browser) { |
| 168 error_ = keys::kNoCurrentWindowError; |
| 169 return false; |
| 170 } |
| 167 result_.reset(CreateWindowValue(browser, false)); | 171 result_.reset(CreateWindowValue(browser, false)); |
| 168 return true; | 172 return true; |
| 169 } | 173 } |
| 170 | 174 |
| 171 bool GetLastFocusedWindowFunction::RunImpl() { | 175 bool GetLastFocusedWindowFunction::RunImpl() { |
| 172 Browser* browser = BrowserList::GetLastActiveWithProfile(profile()); | 176 Browser* browser = BrowserList::GetLastActiveWithProfile(profile()); |
| 177 if (!browser) { |
| 178 error_ = keys::kNoLastFocusedWindowError; |
| 179 return false; |
| 180 } |
| 173 result_.reset(CreateWindowValue(browser, false)); | 181 result_.reset(CreateWindowValue(browser, false)); |
| 174 return true; | 182 return true; |
| 175 } | 183 } |
| 176 | 184 |
| 177 bool GetAllWindowsFunction::RunImpl() { | 185 bool GetAllWindowsFunction::RunImpl() { |
| 178 bool populate_tabs = false; | 186 bool populate_tabs = false; |
| 179 if (!args_->IsType(Value::TYPE_NULL)) { | 187 if (!args_->IsType(Value::TYPE_NULL)) { |
| 180 EXTENSION_FUNCTION_VALIDATE(args_->GetAsBoolean(&populate_tabs)); | 188 EXTENSION_FUNCTION_VALIDATE(args_->GetAsBoolean(&populate_tabs)); |
| 181 } | 189 } |
| 182 | 190 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 211 return false; | 219 return false; |
| 212 } | 220 } |
| 213 } | 221 } |
| 214 } | 222 } |
| 215 | 223 |
| 216 // Try to position the new browser relative its originating browser window. | 224 // Try to position the new browser relative its originating browser window. |
| 217 gfx::Rect empty_bounds; | 225 gfx::Rect empty_bounds; |
| 218 gfx::Rect bounds; | 226 gfx::Rect bounds; |
| 219 bool maximized; | 227 bool maximized; |
| 220 // The call offsets the bounds by kWindowTilePixels (defined in WindowSizer to | 228 // The call offsets the bounds by kWindowTilePixels (defined in WindowSizer to |
| 221 // be 10). | 229 // be 10) |
| 230 // |
| 231 // NOTE(rafaelw): It's ok if dispatcher_->GetBrowser() returns NULL here. |
| 232 // GetBrowserWindowBounds will default to saved "default" values for the app. |
| 222 WindowSizer::GetBrowserWindowBounds(std::wstring(), empty_bounds, | 233 WindowSizer::GetBrowserWindowBounds(std::wstring(), empty_bounds, |
| 223 dispatcher_->GetBrowser(), &bounds, | 234 dispatcher_->GetBrowser(), &bounds, |
| 224 &maximized); | 235 &maximized); |
| 225 | 236 |
| 226 // Any part of the bounds can optionally be set by the caller. | 237 // Any part of the bounds can optionally be set by the caller. |
| 227 if (args_->IsType(Value::TYPE_DICTIONARY)) { | 238 if (args_->IsType(Value::TYPE_DICTIONARY)) { |
| 228 const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); | 239 const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); |
| 229 int bounds_val; | 240 int bounds_val; |
| 230 if (args->HasKey(keys::kLeftKey)) { | 241 if (args->HasKey(keys::kLeftKey)) { |
| 231 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kLeftKey, | 242 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kLeftKey, |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 bounds.set_width(bounds_val); | 313 bounds.set_width(bounds_val); |
| 303 } | 314 } |
| 304 | 315 |
| 305 if (update_props->HasKey(keys::kHeightKey)) { | 316 if (update_props->HasKey(keys::kHeightKey)) { |
| 306 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( | 317 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( |
| 307 keys::kHeightKey, | 318 keys::kHeightKey, |
| 308 &bounds_val)); | 319 &bounds_val)); |
| 309 bounds.set_height(bounds_val); | 320 bounds.set_height(bounds_val); |
| 310 } | 321 } |
| 311 | 322 |
| 312 // TODO(rafaelw): This call to SetBounds() ends up resulting in the target | |
| 313 // window being activated (pushed to the front). On win32, this appears to be | |
| 314 // the result of HWND event handling. | |
| 315 browser->window()->SetBounds(bounds); | 323 browser->window()->SetBounds(bounds); |
| 316 // TODO(rafaelw): Support |focused|. | 324 // TODO(rafaelw): Support |focused|. |
| 317 result_.reset(CreateWindowValue(browser, false)); | 325 result_.reset(CreateWindowValue(browser, false)); |
| 318 | 326 |
| 319 return true; | 327 return true; |
| 320 } | 328 } |
| 321 | 329 |
| 322 bool RemoveWindowFunction::RunImpl() { | 330 bool RemoveWindowFunction::RunImpl() { |
| 323 int window_id; | 331 int window_id; |
| 324 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id)); | 332 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 335 // Tabs ------------------------------------------------------------------------ | 343 // Tabs ------------------------------------------------------------------------ |
| 336 | 344 |
| 337 bool GetSelectedTabFunction::RunImpl() { | 345 bool GetSelectedTabFunction::RunImpl() { |
| 338 Browser* browser; | 346 Browser* browser; |
| 339 // windowId defaults to "current" window. | 347 // windowId defaults to "current" window. |
| 340 int window_id = -1; | 348 int window_id = -1; |
| 341 | 349 |
| 342 if (!args_->IsType(Value::TYPE_NULL)) { | 350 if (!args_->IsType(Value::TYPE_NULL)) { |
| 343 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id)); | 351 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id)); |
| 344 browser = GetBrowserInProfileWithId(profile(), window_id, &error_); | 352 browser = GetBrowserInProfileWithId(profile(), window_id, &error_); |
| 345 if (!browser) | |
| 346 return false; | |
| 347 } else { | 353 } else { |
| 348 browser = dispatcher_->GetBrowser(); | 354 browser = dispatcher_->GetBrowser(); |
| 355 if (!browser) |
| 356 error_ = keys::kNoCurrentWindowError; |
| 349 } | 357 } |
| 358 if (!browser) |
| 359 return false; |
| 350 | 360 |
| 351 TabStripModel* tab_strip = browser->tabstrip_model(); | 361 TabStripModel* tab_strip = browser->tabstrip_model(); |
| 352 result_.reset(ExtensionTabUtil::CreateTabValue( | 362 TabContents* contents = tab_strip->GetSelectedTabContents(); |
| 353 tab_strip->GetSelectedTabContents(), | 363 if (!contents) { |
| 354 tab_strip, | 364 error_ = keys::kNoSelectedTabError; |
| 365 return false; |
| 366 } |
| 367 result_.reset(ExtensionTabUtil::CreateTabValue(contents, tab_strip, |
| 355 tab_strip->selected_index())); | 368 tab_strip->selected_index())); |
| 356 return true; | 369 return true; |
| 357 } | 370 } |
| 358 | 371 |
| 359 bool GetAllTabsInWindowFunction::RunImpl() { | 372 bool GetAllTabsInWindowFunction::RunImpl() { |
| 360 Browser* browser; | 373 Browser* browser; |
| 361 // windowId defaults to "current" window. | 374 // windowId defaults to "current" window. |
| 362 int window_id = -1; | 375 int window_id = -1; |
| 363 if (!args_->IsType(Value::TYPE_NULL)) { | 376 if (!args_->IsType(Value::TYPE_NULL)) { |
| 364 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id)); | 377 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id)); |
| 365 browser = GetBrowserInProfileWithId(profile(), window_id, &error_); | 378 browser = GetBrowserInProfileWithId(profile(), window_id, &error_); |
| 366 if (!browser) | |
| 367 return false; | |
| 368 } else { | 379 } else { |
| 369 browser = dispatcher_->GetBrowser(); | 380 browser = dispatcher_->GetBrowser(); |
| 381 if (!browser) |
| 382 error_ = keys::kNoCurrentWindowError; |
| 370 } | 383 } |
| 384 if (!browser) |
| 385 return false; |
| 371 | 386 |
| 372 result_.reset(CreateTabList(browser)); | 387 result_.reset(CreateTabList(browser)); |
| 373 | 388 |
| 374 return true; | 389 return true; |
| 375 } | 390 } |
| 376 | 391 |
| 377 bool CreateTabFunction::RunImpl() { | 392 bool CreateTabFunction::RunImpl() { |
| 378 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); | 393 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 379 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); | 394 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); |
| 380 | 395 |
| 381 Browser *browser; | 396 Browser *browser; |
| 382 // windowId defaults to "current" window. | 397 // windowId defaults to "current" window. |
| 383 int window_id = -1; | 398 int window_id = -1; |
| 384 if (args->HasKey(keys::kWindowIdKey)) { | 399 if (args->HasKey(keys::kWindowIdKey)) { |
| 385 EXTENSION_FUNCTION_VALIDATE(args->GetInteger( | 400 EXTENSION_FUNCTION_VALIDATE(args->GetInteger( |
| 386 keys::kWindowIdKey, &window_id)); | 401 keys::kWindowIdKey, &window_id)); |
| 387 browser = GetBrowserInProfileWithId(profile(), window_id, &error_); | 402 browser = GetBrowserInProfileWithId(profile(), window_id, &error_); |
| 403 } else { |
| 404 browser = dispatcher_->GetBrowser(); |
| 388 if (!browser) | 405 if (!browser) |
| 389 return false; | 406 error_ = keys::kNoCurrentWindowError; |
| 390 } else { | |
| 391 browser = dispatcher_->GetBrowser(); | |
| 392 } | 407 } |
| 408 if (!browser) |
| 409 return false; |
| 393 | 410 |
| 394 TabStripModel* tab_strip = browser->tabstrip_model(); | 411 TabStripModel* tab_strip = browser->tabstrip_model(); |
| 395 | 412 |
| 396 // TODO(rafaelw): handle setting remaining tab properties: | 413 // TODO(rafaelw): handle setting remaining tab properties: |
| 397 // -title | 414 // -title |
| 398 // -favIconUrl | 415 // -favIconUrl |
| 399 | 416 |
| 400 std::string url_string; | 417 std::string url_string; |
| 401 scoped_ptr<GURL> url(new GURL()); | 418 scoped_ptr<GURL> url(new GURL()); |
| 402 if (args->HasKey(keys::kUrlKey)) { | 419 if (args->HasKey(keys::kUrlKey)) { |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip, | 683 if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip, |
| 667 contents, tab_index)) | 684 contents, tab_index)) |
| 668 return true; | 685 return true; |
| 669 | 686 |
| 670 if (error_message) | 687 if (error_message) |
| 671 *error_message = ExtensionErrorUtils::FormatErrorMessage( | 688 *error_message = ExtensionErrorUtils::FormatErrorMessage( |
| 672 keys::kTabNotFoundError, IntToString(tab_id)); | 689 keys::kTabNotFoundError, IntToString(tab_id)); |
| 673 | 690 |
| 674 return false; | 691 return false; |
| 675 } | 692 } |
| OLD | NEW |