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 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 std::string* error) { | 147 std::string* error) { |
148 Browser* result = nullptr; | 148 Browser* result = nullptr; |
149 result = ExtensionTabUtil::GetBrowserFromWindowID(details, window_id, error); | 149 result = ExtensionTabUtil::GetBrowserFromWindowID(details, window_id, error); |
150 if (!result) | 150 if (!result) |
151 return false; | 151 return false; |
152 | 152 |
153 *browser = result; | 153 *browser = result; |
154 return true; | 154 return true; |
155 } | 155 } |
156 | 156 |
157 bool GetBrowserFromWindowID(ChromeUIThreadExtensionFunction* function, | 157 bool GetBrowserFromWindowID(UIThreadExtensionFunction* function, |
158 int window_id, | 158 int window_id, |
159 Browser** browser, | 159 Browser** browser, |
160 std::string* error) { | 160 std::string* error) { |
161 return GetBrowserFromWindowID(ChromeExtensionFunctionDetails(function), | 161 return GetBrowserFromWindowID(ChromeExtensionFunctionDetails(function), |
162 window_id, browser, error); | 162 window_id, browser, error); |
163 } | 163 } |
164 | 164 |
165 // |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 |
166 // appropriate message if the tab cannot be found by id. | 166 // appropriate message if the tab cannot be found by id. |
167 bool GetTabById(int tab_id, | 167 bool GetTabById(int tab_id, |
168 Profile* profile, | 168 content::BrowserContext* context, |
169 bool include_incognito, | 169 bool include_incognito, |
170 Browser** browser, | 170 Browser** browser, |
171 TabStripModel** tab_strip, | 171 TabStripModel** tab_strip, |
172 content::WebContents** contents, | 172 content::WebContents** contents, |
173 int* tab_index, | 173 int* tab_index, |
174 std::string* error_message) { | 174 std::string* error_message) { |
175 if (ExtensionTabUtil::GetTabById(tab_id, profile, include_incognito, | 175 if (ExtensionTabUtil::GetTabById(tab_id, context, include_incognito, browser, |
176 browser, tab_strip, contents, tab_index)) { | 176 tab_strip, contents, tab_index)) { |
177 return true; | 177 return true; |
178 } | 178 } |
179 | 179 |
180 if (error_message) { | 180 if (error_message) { |
181 *error_message = ErrorUtils::FormatErrorMessage( | 181 *error_message = ErrorUtils::FormatErrorMessage( |
182 keys::kTabNotFoundError, base::IntToString(tab_id)); | 182 keys::kTabNotFoundError, base::IntToString(tab_id)); |
183 } | 183 } |
184 | 184 |
185 return false; | 185 return false; |
186 } | 186 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 break; | 268 break; |
269 case ZoomController::ZOOM_MODE_DISABLED: | 269 case ZoomController::ZOOM_MODE_DISABLED: |
270 zoom_settings->mode = api::tabs::ZOOM_SETTINGS_MODE_DISABLED; | 270 zoom_settings->mode = api::tabs::ZOOM_SETTINGS_MODE_DISABLED; |
271 zoom_settings->scope = api::tabs::ZOOM_SETTINGS_SCOPE_PER_TAB; | 271 zoom_settings->scope = api::tabs::ZOOM_SETTINGS_SCOPE_PER_TAB; |
272 break; | 272 break; |
273 } | 273 } |
274 } | 274 } |
275 | 275 |
276 // Windows --------------------------------------------------------------------- | 276 // Windows --------------------------------------------------------------------- |
277 | 277 |
278 bool WindowsGetFunction::RunSync() { | 278 ExtensionFunction::ResponseAction WindowsGetFunction::Run() { |
279 std::unique_ptr<windows::Get::Params> params( | 279 std::unique_ptr<windows::Get::Params> params( |
280 windows::Get::Params::Create(*args_)); | 280 windows::Get::Params::Create(*args_)); |
281 EXTENSION_FUNCTION_VALIDATE(params.get()); | 281 EXTENSION_FUNCTION_VALIDATE(params.get()); |
282 | 282 |
283 ApiParameterExtractor<windows::Get::Params> extractor(params.get()); | 283 ApiParameterExtractor<windows::Get::Params> extractor(params.get()); |
284 WindowController* controller; | 284 WindowController* controller = nullptr; |
| 285 std::string error; |
285 if (!windows_util::GetWindowFromWindowID(this, params->window_id, | 286 if (!windows_util::GetWindowFromWindowID(this, params->window_id, |
286 extractor.type_filters(), | 287 extractor.type_filters(), |
287 &controller, &error_)) { | 288 &controller, &error)) { |
288 return false; | 289 return RespondNow(Error(error)); |
289 } | 290 } |
290 | 291 |
291 if (extractor.populate_tabs()) | 292 std::unique_ptr<base::DictionaryValue> windows = |
292 SetResult(controller->CreateWindowValueWithTabs(extension())); | 293 extractor.populate_tabs() |
293 else | 294 ? controller->CreateWindowValueWithTabs(extension()) |
294 SetResult(controller->CreateWindowValue()); | 295 : controller->CreateWindowValue(); |
295 return true; | 296 return RespondNow(OneArgument(std::move(windows))); |
296 } | 297 } |
297 | 298 |
298 bool WindowsGetCurrentFunction::RunSync() { | 299 ExtensionFunction::ResponseAction WindowsGetCurrentFunction::Run() { |
299 std::unique_ptr<windows::GetCurrent::Params> params( | 300 std::unique_ptr<windows::GetCurrent::Params> params( |
300 windows::GetCurrent::Params::Create(*args_)); | 301 windows::GetCurrent::Params::Create(*args_)); |
301 EXTENSION_FUNCTION_VALIDATE(params.get()); | 302 EXTENSION_FUNCTION_VALIDATE(params.get()); |
302 | 303 |
303 ApiParameterExtractor<windows::GetCurrent::Params> extractor(params.get()); | 304 ApiParameterExtractor<windows::GetCurrent::Params> extractor(params.get()); |
304 WindowController* controller; | 305 WindowController* controller = nullptr; |
| 306 std::string error; |
305 if (!windows_util::GetWindowFromWindowID( | 307 if (!windows_util::GetWindowFromWindowID( |
306 this, extension_misc::kCurrentWindowId, extractor.type_filters(), | 308 this, extension_misc::kCurrentWindowId, extractor.type_filters(), |
307 &controller, &error_)) { | 309 &controller, &error)) { |
308 return false; | 310 return RespondNow(Error(error)); |
309 } | 311 } |
310 if (extractor.populate_tabs()) | 312 |
311 SetResult(controller->CreateWindowValueWithTabs(extension())); | 313 std::unique_ptr<base::DictionaryValue> windows = |
312 else | 314 extractor.populate_tabs() |
313 SetResult(controller->CreateWindowValue()); | 315 ? controller->CreateWindowValueWithTabs(extension()) |
314 return true; | 316 : controller->CreateWindowValue(); |
| 317 return RespondNow(OneArgument(std::move(windows))); |
315 } | 318 } |
316 | 319 |
317 bool WindowsGetLastFocusedFunction::RunSync() { | 320 ExtensionFunction::ResponseAction WindowsGetLastFocusedFunction::Run() { |
318 std::unique_ptr<windows::GetLastFocused::Params> params( | 321 std::unique_ptr<windows::GetLastFocused::Params> params( |
319 windows::GetLastFocused::Params::Create(*args_)); | 322 windows::GetLastFocused::Params::Create(*args_)); |
320 EXTENSION_FUNCTION_VALIDATE(params.get()); | 323 EXTENSION_FUNCTION_VALIDATE(params.get()); |
321 | 324 |
322 ApiParameterExtractor<windows::GetLastFocused::Params> extractor( | 325 ApiParameterExtractor<windows::GetLastFocused::Params> extractor( |
323 params.get()); | 326 params.get()); |
324 // The WindowControllerList should contain a list of application, | 327 // The WindowControllerList should contain a list of application, |
325 // browser and devtools windows. | 328 // browser and devtools windows. |
326 WindowController* controller = nullptr; | 329 WindowController* controller = nullptr; |
327 for (auto* iter : WindowControllerList::GetInstance()->windows()) { | 330 for (auto* iter : WindowControllerList::GetInstance()->windows()) { |
328 if (windows_util::CanOperateOnWindow(this, iter, | 331 if (windows_util::CanOperateOnWindow(this, iter, |
329 extractor.type_filters())) { | 332 extractor.type_filters())) { |
330 controller = iter; | 333 controller = iter; |
331 if (controller->window()->IsActive()) | 334 if (controller->window()->IsActive()) |
332 break; // Use focused window. | 335 break; // Use focused window. |
333 } | 336 } |
334 } | 337 } |
335 if (!controller) { | 338 if (!controller) |
336 error_ = keys::kNoLastFocusedWindowError; | 339 return RespondNow(Error(keys::kNoLastFocusedWindowError)); |
337 return false; | 340 |
338 } | 341 std::unique_ptr<base::DictionaryValue> windows = |
339 if (extractor.populate_tabs()) | 342 extractor.populate_tabs() |
340 SetResult(controller->CreateWindowValueWithTabs(extension())); | 343 ? controller->CreateWindowValueWithTabs(extension()) |
341 else | 344 : controller->CreateWindowValue(); |
342 SetResult(controller->CreateWindowValue()); | 345 return RespondNow(OneArgument(std::move(windows))); |
343 return true; | |
344 } | 346 } |
345 | 347 |
346 bool WindowsGetAllFunction::RunSync() { | 348 ExtensionFunction::ResponseAction WindowsGetAllFunction::Run() { |
347 std::unique_ptr<windows::GetAll::Params> params( | 349 std::unique_ptr<windows::GetAll::Params> params( |
348 windows::GetAll::Params::Create(*args_)); | 350 windows::GetAll::Params::Create(*args_)); |
349 EXTENSION_FUNCTION_VALIDATE(params.get()); | 351 EXTENSION_FUNCTION_VALIDATE(params.get()); |
350 | 352 |
351 ApiParameterExtractor<windows::GetAll::Params> extractor(params.get()); | 353 ApiParameterExtractor<windows::GetAll::Params> extractor(params.get()); |
352 std::unique_ptr<base::ListValue> window_list(new base::ListValue()); | 354 std::unique_ptr<base::ListValue> window_list(new base::ListValue()); |
353 const WindowControllerList::ControllerList& windows = | 355 const WindowControllerList::ControllerList& windows = |
354 WindowControllerList::GetInstance()->windows(); | 356 WindowControllerList::GetInstance()->windows(); |
355 for (WindowControllerList::ControllerList::const_iterator iter = | 357 for (WindowControllerList::ControllerList::const_iterator iter = |
356 windows.begin(); | 358 windows.begin(); |
357 iter != windows.end(); ++iter) { | 359 iter != windows.end(); ++iter) { |
358 if (!windows_util::CanOperateOnWindow(this, *iter, | 360 if (!windows_util::CanOperateOnWindow(this, *iter, |
359 extractor.type_filters())) | 361 extractor.type_filters())) |
360 continue; | 362 continue; |
361 if (extractor.populate_tabs()) | 363 if (extractor.populate_tabs()) |
362 window_list->Append((*iter)->CreateWindowValueWithTabs(extension())); | 364 window_list->Append((*iter)->CreateWindowValueWithTabs(extension())); |
363 else | 365 else |
364 window_list->Append((*iter)->CreateWindowValue()); | 366 window_list->Append((*iter)->CreateWindowValue()); |
365 } | 367 } |
366 SetResult(std::move(window_list)); | 368 |
367 return true; | 369 return RespondNow(OneArgument(std::move(window_list))); |
368 } | 370 } |
369 | 371 |
370 bool WindowsCreateFunction::ShouldOpenIncognitoWindow( | 372 bool WindowsCreateFunction::ShouldOpenIncognitoWindow( |
371 const windows::Create::Params::CreateData* create_data, | 373 const windows::Create::Params::CreateData* create_data, |
372 std::vector<GURL>* urls, bool* is_error) { | 374 std::vector<GURL>* urls, |
373 *is_error = false; | 375 std::string* error) { |
| 376 Profile* profile = Profile::FromBrowserContext(browser_context()); |
374 const IncognitoModePrefs::Availability incognito_availability = | 377 const IncognitoModePrefs::Availability incognito_availability = |
375 IncognitoModePrefs::GetAvailability(GetProfile()->GetPrefs()); | 378 IncognitoModePrefs::GetAvailability(profile->GetPrefs()); |
376 bool incognito = false; | 379 bool incognito = false; |
377 if (create_data && create_data->incognito) { | 380 if (create_data && create_data->incognito) { |
378 incognito = *create_data->incognito; | 381 incognito = *create_data->incognito; |
379 if (incognito && incognito_availability == IncognitoModePrefs::DISABLED) { | 382 if (incognito && incognito_availability == IncognitoModePrefs::DISABLED) { |
380 error_ = keys::kIncognitoModeIsDisabled; | 383 *error = keys::kIncognitoModeIsDisabled; |
381 *is_error = true; | |
382 return false; | 384 return false; |
383 } | 385 } |
384 if (!incognito && incognito_availability == IncognitoModePrefs::FORCED) { | 386 if (!incognito && incognito_availability == IncognitoModePrefs::FORCED) { |
385 error_ = keys::kIncognitoModeIsForced; | 387 *error = keys::kIncognitoModeIsForced; |
386 *is_error = true; | |
387 return false; | 388 return false; |
388 } | 389 } |
389 } else if (incognito_availability == IncognitoModePrefs::FORCED) { | 390 } else if (incognito_availability == IncognitoModePrefs::FORCED) { |
390 // If incognito argument is not specified explicitly, we default to | 391 // If incognito argument is not specified explicitly, we default to |
391 // incognito when forced so by policy. | 392 // incognito when forced so by policy. |
392 incognito = true; | 393 incognito = true; |
393 } | 394 } |
394 | 395 |
395 // Remove all URLs that are not allowed in an incognito session. Note that a | 396 // Remove all URLs that are not allowed in an incognito session. Note that a |
396 // ChromeOS guest session is not considered incognito in this case. | 397 // ChromeOS guest session is not considered incognito in this case. |
397 if (incognito && !GetProfile()->IsGuestSession()) { | 398 if (incognito && !profile->IsGuestSession()) { |
398 std::string first_url_erased; | 399 std::string first_url_erased; |
399 for (size_t i = 0; i < urls->size();) { | 400 for (size_t i = 0; i < urls->size();) { |
400 if (chrome::IsURLAllowedInIncognito((*urls)[i], GetProfile())) { | 401 if (chrome::IsURLAllowedInIncognito((*urls)[i], profile)) { |
401 i++; | 402 i++; |
402 } else { | 403 } else { |
403 if (first_url_erased.empty()) | 404 if (first_url_erased.empty()) |
404 first_url_erased = (*urls)[i].spec(); | 405 first_url_erased = (*urls)[i].spec(); |
405 urls->erase(urls->begin() + i); | 406 urls->erase(urls->begin() + i); |
406 } | 407 } |
407 } | 408 } |
408 if (urls->empty() && !first_url_erased.empty()) { | 409 if (urls->empty() && !first_url_erased.empty()) { |
409 error_ = ErrorUtils::FormatErrorMessage( | 410 *error = ErrorUtils::FormatErrorMessage( |
410 keys::kURLsNotAllowedInIncognitoError, first_url_erased); | 411 keys::kURLsNotAllowedInIncognitoError, first_url_erased); |
411 *is_error = true; | |
412 return false; | 412 return false; |
413 } | 413 } |
414 } | 414 } |
415 return incognito; | 415 return incognito; |
416 } | 416 } |
417 | 417 |
418 bool WindowsCreateFunction::RunSync() { | 418 ExtensionFunction::ResponseAction WindowsCreateFunction::Run() { |
419 std::unique_ptr<windows::Create::Params> params( | 419 std::unique_ptr<windows::Create::Params> params( |
420 windows::Create::Params::Create(*args_)); | 420 windows::Create::Params::Create(*args_)); |
421 EXTENSION_FUNCTION_VALIDATE(params); | 421 EXTENSION_FUNCTION_VALIDATE(params); |
422 std::vector<GURL> urls; | 422 std::vector<GURL> urls; |
423 TabStripModel* source_tab_strip = NULL; | 423 TabStripModel* source_tab_strip = NULL; |
424 int tab_index = -1; | 424 int tab_index = -1; |
425 | 425 |
426 windows::Create::Params::CreateData* create_data = params->create_data.get(); | 426 windows::Create::Params::CreateData* create_data = params->create_data.get(); |
427 | 427 |
428 // Look for optional url. | 428 // Look for optional url. |
429 if (create_data && create_data->url) { | 429 if (create_data && create_data->url) { |
430 std::vector<std::string> url_strings; | 430 std::vector<std::string> url_strings; |
431 // First, get all the URLs the client wants to open. | 431 // First, get all the URLs the client wants to open. |
432 if (create_data->url->as_string) | 432 if (create_data->url->as_string) |
433 url_strings.push_back(*create_data->url->as_string); | 433 url_strings.push_back(*create_data->url->as_string); |
434 else if (create_data->url->as_strings) | 434 else if (create_data->url->as_strings) |
435 url_strings.swap(*create_data->url->as_strings); | 435 url_strings.swap(*create_data->url->as_strings); |
436 | 436 |
437 // Second, resolve, validate and convert them to GURLs. | 437 // Second, resolve, validate and convert them to GURLs. |
438 for (std::vector<std::string>::iterator i = url_strings.begin(); | 438 for (std::vector<std::string>::iterator i = url_strings.begin(); |
439 i != url_strings.end(); ++i) { | 439 i != url_strings.end(); ++i) { |
440 GURL url = ExtensionTabUtil::ResolvePossiblyRelativeURL(*i, extension()); | 440 GURL url = ExtensionTabUtil::ResolvePossiblyRelativeURL(*i, extension()); |
441 if (!url.is_valid()) { | 441 if (!url.is_valid()) |
442 error_ = ErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, *i); | 442 return RespondNow(Error(keys::kInvalidUrlError, *i)); |
443 return false; | |
444 } | |
445 // Don't let the extension crash the browser or renderers. | 443 // Don't let the extension crash the browser or renderers. |
446 if (ExtensionTabUtil::IsKillURL(url)) { | 444 if (ExtensionTabUtil::IsKillURL(url)) |
447 error_ = keys::kNoCrashBrowserError; | 445 return RespondNow(Error(keys::kNoCrashBrowserError)); |
448 return false; | |
449 } | |
450 urls.push_back(url); | 446 urls.push_back(url); |
451 } | 447 } |
452 } | 448 } |
453 | 449 |
454 // Decide whether we are opening a normal window or an incognito window. | 450 // Decide whether we are opening a normal window or an incognito window. |
455 bool is_error = true; | 451 std::string error; |
456 bool open_incognito_window = | 452 bool open_incognito_window = |
457 ShouldOpenIncognitoWindow(create_data, &urls, &is_error); | 453 ShouldOpenIncognitoWindow(create_data, &urls, &error); |
458 if (is_error) | 454 if (!error.empty()) |
459 return false; // error_ member is set inside of ShouldOpenIncognitoWindow. | 455 return RespondNow(Error(error)); |
460 | 456 |
461 Profile* window_profile = GetProfile(); | 457 Profile* calling_profile = Profile::FromBrowserContext(browser_context()); |
462 if (open_incognito_window) | 458 Profile* window_profile = open_incognito_window |
463 window_profile = window_profile->GetOffTheRecordProfile(); | 459 ? calling_profile->GetOffTheRecordProfile() |
| 460 : calling_profile; |
464 | 461 |
465 // Look for optional tab id. | 462 // Look for optional tab id. |
466 if (create_data && create_data->tab_id) { | 463 if (create_data && create_data->tab_id) { |
467 // Find the tab. |source_tab_strip| and |tab_index| will later be used to | 464 // Find the tab. |source_tab_strip| and |tab_index| will later be used to |
468 // move the tab into the created window. | 465 // move the tab into the created window. |
469 Browser* source_browser = nullptr; | 466 Browser* source_browser = nullptr; |
470 if (!GetTabById(*create_data->tab_id, | 467 if (!GetTabById(*create_data->tab_id, calling_profile, include_incognito(), |
471 GetProfile(), | 468 &source_browser, &source_tab_strip, nullptr, &tab_index, |
472 include_incognito(), | 469 &error)) { |
473 &source_browser, | 470 return RespondNow(Error(error)); |
474 &source_tab_strip, | |
475 nullptr, | |
476 &tab_index, | |
477 &error_)) | |
478 return false; | |
479 | |
480 if (!source_browser->window()->IsTabStripEditable()) { | |
481 error_ = keys::kTabStripNotEditableError; | |
482 return false; | |
483 } | 471 } |
484 | 472 |
485 if (source_browser->profile() != window_profile) { | 473 if (!source_browser->window()->IsTabStripEditable()) |
486 error_ = keys::kCanOnlyMoveTabsWithinSameProfileError; | 474 return RespondNow(Error(keys::kTabStripNotEditableError)); |
487 return false; | 475 |
488 } | 476 if (source_browser->profile() != window_profile) |
| 477 return RespondNow(Error(keys::kCanOnlyMoveTabsWithinSameProfileError)); |
489 } | 478 } |
490 | 479 |
491 if (!IsValidStateForWindowsCreateFunction(create_data)) { | 480 if (!IsValidStateForWindowsCreateFunction(create_data)) |
492 error_ = keys::kInvalidWindowStateError; | 481 return RespondNow(Error(keys::kInvalidWindowStateError)); |
493 return false; | |
494 } | |
495 | 482 |
496 Browser::Type window_type = Browser::TYPE_TABBED; | 483 Browser::Type window_type = Browser::TYPE_TABBED; |
497 | 484 |
498 #if defined(USE_ASH) | 485 #if defined(USE_ASH) |
499 bool create_ash_panel = false; | 486 bool create_ash_panel = false; |
500 bool saw_focus_key = false; | 487 bool saw_focus_key = false; |
501 #endif // defined(USE_ASH) | 488 #endif // defined(USE_ASH) |
502 | 489 |
503 gfx::Rect window_bounds; | 490 gfx::Rect window_bounds; |
504 bool focused = true; | 491 bool focused = true; |
(...skipping 27 matching lines...) Expand all Loading... |
532 // window.create parameters. However, this is a more breaking change, so | 519 // window.create parameters. However, this is a more breaking change, so |
533 // for now simply treat it as a POPUP. | 520 // for now simply treat it as a POPUP. |
534 window_type = Browser::TYPE_POPUP; | 521 window_type = Browser::TYPE_POPUP; |
535 break; | 522 break; |
536 } | 523 } |
537 | 524 |
538 case windows::CREATE_TYPE_NONE: | 525 case windows::CREATE_TYPE_NONE: |
539 case windows::CREATE_TYPE_NORMAL: | 526 case windows::CREATE_TYPE_NORMAL: |
540 break; | 527 break; |
541 default: | 528 default: |
542 error_ = keys::kInvalidWindowTypeError; | 529 return RespondNow(Error(keys::kInvalidWindowTypeError)); |
543 return false; | |
544 } | 530 } |
545 | 531 |
546 // Initialize default window bounds according to window type. | 532 // Initialize default window bounds according to window type. |
547 if (window_type == Browser::TYPE_TABBED || | 533 if (window_type == Browser::TYPE_TABBED || |
548 window_type == Browser::TYPE_POPUP) { | 534 window_type == Browser::TYPE_POPUP) { |
549 // Try to position the new browser relative to its originating | 535 // Try to position the new browser relative to its originating |
550 // browser window. The call offsets the bounds by kWindowTilePixels | 536 // browser window. The call offsets the bounds by kWindowTilePixels |
551 // (defined in WindowSizer to be 10). | 537 // (defined in WindowSizer to be 10). |
552 // | 538 // |
553 // NOTE(rafaelw): It's ok if GetCurrentBrowser() returns NULL here. | 539 // NOTE(rafaelw): It's ok if GetCurrentBrowser() returns NULL here. |
554 // GetBrowserWindowBounds will default to saved "default" values for | 540 // GetBrowserWindowBounds will default to saved "default" values for |
555 // the app. | 541 // the app. |
556 ui::WindowShowState show_state = ui::SHOW_STATE_DEFAULT; | 542 ui::WindowShowState show_state = ui::SHOW_STATE_DEFAULT; |
557 WindowSizer::GetBrowserWindowBoundsAndShowState(std::string(), | 543 WindowSizer::GetBrowserWindowBoundsAndShowState( |
558 gfx::Rect(), | 544 std::string(), gfx::Rect(), |
559 GetCurrentBrowser(), | 545 ChromeExtensionFunctionDetails(this).GetCurrentBrowser(), |
560 &window_bounds, | 546 &window_bounds, &show_state); |
561 &show_state); | |
562 } | 547 } |
563 | 548 |
564 // Any part of the bounds can optionally be set by the caller. | 549 // Any part of the bounds can optionally be set by the caller. |
565 if (create_data->left) | 550 if (create_data->left) |
566 window_bounds.set_x(*create_data->left); | 551 window_bounds.set_x(*create_data->left); |
567 | 552 |
568 if (create_data->top) | 553 if (create_data->top) |
569 window_bounds.set_y(*create_data->top); | 554 window_bounds.set_y(*create_data->top); |
570 | 555 |
571 if (create_data->width) | 556 if (create_data->width) |
(...skipping 22 matching lines...) Expand all Loading... |
594 create_params.focused = saw_focus_key && focused; | 579 create_params.focused = saw_focus_key && focused; |
595 AppWindow* app_window = | 580 AppWindow* app_window = |
596 new AppWindow(window_profile, new ChromeAppDelegate(true), extension()); | 581 new AppWindow(window_profile, new ChromeAppDelegate(true), extension()); |
597 AshPanelContents* ash_panel_contents = new AshPanelContents(app_window); | 582 AshPanelContents* ash_panel_contents = new AshPanelContents(app_window); |
598 app_window->Init(urls[0], ash_panel_contents, render_frame_host(), | 583 app_window->Init(urls[0], ash_panel_contents, render_frame_host(), |
599 create_params); | 584 create_params); |
600 WindowController* window_controller = | 585 WindowController* window_controller = |
601 WindowControllerList::GetInstance()->FindWindowById( | 586 WindowControllerList::GetInstance()->FindWindowById( |
602 app_window->session_id().id()); | 587 app_window->session_id().id()); |
603 if (!window_controller) | 588 if (!window_controller) |
604 return false; | 589 return RespondNow(Error(kUnknownErrorDoNotUse)); |
605 SetResult(window_controller->CreateWindowValueWithTabs(extension())); | 590 return RespondNow( |
606 return true; | 591 OneArgument(window_controller->CreateWindowValueWithTabs(extension()))); |
607 } | 592 } |
608 #endif // defined(USE_ASH) | 593 #endif // defined(USE_ASH) |
609 | 594 |
610 // Create a new BrowserWindow. | 595 // Create a new BrowserWindow. |
611 Browser::CreateParams create_params(window_type, window_profile); | 596 Browser::CreateParams create_params(window_type, window_profile); |
612 if (extension_id.empty()) { | 597 if (extension_id.empty()) { |
613 create_params.initial_bounds = window_bounds; | 598 create_params.initial_bounds = window_bounds; |
614 } else { | 599 } else { |
615 create_params = Browser::CreateParams::CreateForApp( | 600 create_params = Browser::CreateParams::CreateForApp( |
616 web_app::GenerateApplicationNameFromExtensionId(extension_id), | 601 web_app::GenerateApplicationNameFromExtensionId(extension_id), |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 } | 638 } |
654 chrome::SelectNumberedTab(new_window, 0); | 639 chrome::SelectNumberedTab(new_window, 0); |
655 | 640 |
656 if (focused) | 641 if (focused) |
657 new_window->window()->Show(); | 642 new_window->window()->Show(); |
658 else | 643 else |
659 new_window->window()->ShowInactive(); | 644 new_window->window()->ShowInactive(); |
660 | 645 |
661 WindowController* controller = new_window->extension_window_controller(); | 646 WindowController* controller = new_window->extension_window_controller(); |
662 | 647 |
| 648 std::unique_ptr<base::Value> result; |
663 if (new_window->profile()->IsOffTheRecord() && | 649 if (new_window->profile()->IsOffTheRecord() && |
664 !GetProfile()->IsOffTheRecord() && !include_incognito()) { | 650 !browser_context()->IsOffTheRecord() && !include_incognito()) { |
665 // Don't expose incognito windows if extension itself works in non-incognito | 651 // Don't expose incognito windows if extension itself works in non-incognito |
666 // profile and CanCrossIncognito isn't allowed. | 652 // profile and CanCrossIncognito isn't allowed. |
667 SetResult(base::Value::CreateNullValue()); | 653 result = base::Value::CreateNullValue(); |
668 } else { | 654 } else { |
669 SetResult(controller->CreateWindowValueWithTabs(extension())); | 655 result = controller->CreateWindowValueWithTabs(extension()); |
670 } | 656 } |
671 | 657 |
672 return true; | 658 return RespondNow(OneArgument(std::move(result))); |
673 } | 659 } |
674 | 660 |
675 bool WindowsUpdateFunction::RunSync() { | 661 ExtensionFunction::ResponseAction WindowsUpdateFunction::Run() { |
676 std::unique_ptr<windows::Update::Params> params( | 662 std::unique_ptr<windows::Update::Params> params( |
677 windows::Update::Params::Create(*args_)); | 663 windows::Update::Params::Create(*args_)); |
678 EXTENSION_FUNCTION_VALIDATE(params); | 664 EXTENSION_FUNCTION_VALIDATE(params); |
679 | 665 |
680 WindowController* controller; | 666 WindowController* controller; |
| 667 std::string error; |
681 if (!windows_util::GetWindowFromWindowID( | 668 if (!windows_util::GetWindowFromWindowID( |
682 this, params->window_id, WindowController::GetAllWindowFilter(), | 669 this, params->window_id, WindowController::GetAllWindowFilter(), |
683 &controller, &error_)) { | 670 &controller, &error)) { |
684 return false; | 671 return RespondNow(Error(error)); |
685 } | 672 } |
686 | 673 |
687 ui::WindowShowState show_state = | 674 ui::WindowShowState show_state = |
688 ConvertToWindowShowState(params->update_info.state); | 675 ConvertToWindowShowState(params->update_info.state); |
689 | 676 |
690 if (show_state != ui::SHOW_STATE_FULLSCREEN && | 677 if (show_state != ui::SHOW_STATE_FULLSCREEN && |
691 show_state != ui::SHOW_STATE_DEFAULT) | 678 show_state != ui::SHOW_STATE_DEFAULT) |
692 controller->SetFullscreenMode(false, extension()->url()); | 679 controller->SetFullscreenMode(false, extension()->url()); |
693 | 680 |
694 switch (show_state) { | 681 switch (show_state) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
736 | 723 |
737 if (params->update_info.height) { | 724 if (params->update_info.height) { |
738 bounds.set_height(*params->update_info.height); | 725 bounds.set_height(*params->update_info.height); |
739 set_bounds = true; | 726 set_bounds = true; |
740 } | 727 } |
741 | 728 |
742 if (set_bounds) { | 729 if (set_bounds) { |
743 if (show_state == ui::SHOW_STATE_MINIMIZED || | 730 if (show_state == ui::SHOW_STATE_MINIMIZED || |
744 show_state == ui::SHOW_STATE_MAXIMIZED || | 731 show_state == ui::SHOW_STATE_MAXIMIZED || |
745 show_state == ui::SHOW_STATE_FULLSCREEN) { | 732 show_state == ui::SHOW_STATE_FULLSCREEN) { |
746 error_ = keys::kInvalidWindowStateError; | 733 return RespondNow(Error(keys::kInvalidWindowStateError)); |
747 return false; | |
748 } | 734 } |
749 // TODO(varkha): Updating bounds during a drag can cause problems and a more | 735 // TODO(varkha): Updating bounds during a drag can cause problems and a more |
750 // general solution is needed. See http://crbug.com/251813 . | 736 // general solution is needed. See http://crbug.com/251813 . |
751 controller->window()->SetBounds(bounds); | 737 controller->window()->SetBounds(bounds); |
752 } | 738 } |
753 | 739 |
754 if (params->update_info.focused) { | 740 if (params->update_info.focused) { |
755 if (*params->update_info.focused) { | 741 if (*params->update_info.focused) { |
756 if (show_state == ui::SHOW_STATE_MINIMIZED) { | 742 if (show_state == ui::SHOW_STATE_MINIMIZED) |
757 error_ = keys::kInvalidWindowStateError; | 743 return RespondNow(Error(keys::kInvalidWindowStateError)); |
758 return false; | |
759 } | |
760 controller->window()->Activate(); | 744 controller->window()->Activate(); |
761 } else { | 745 } else { |
762 if (show_state == ui::SHOW_STATE_MAXIMIZED || | 746 if (show_state == ui::SHOW_STATE_MAXIMIZED || |
763 show_state == ui::SHOW_STATE_FULLSCREEN) { | 747 show_state == ui::SHOW_STATE_FULLSCREEN) { |
764 error_ = keys::kInvalidWindowStateError; | 748 return RespondNow(Error(keys::kInvalidWindowStateError)); |
765 return false; | |
766 } | 749 } |
767 controller->window()->Deactivate(); | 750 controller->window()->Deactivate(); |
768 } | 751 } |
769 } | 752 } |
770 | 753 |
771 if (params->update_info.draw_attention) | 754 if (params->update_info.draw_attention) |
772 controller->window()->FlashFrame(*params->update_info.draw_attention); | 755 controller->window()->FlashFrame(*params->update_info.draw_attention); |
773 | 756 |
774 SetResult(controller->CreateWindowValue()); | 757 return RespondNow(OneArgument(controller->CreateWindowValue())); |
775 | |
776 return true; | |
777 } | 758 } |
778 | 759 |
779 bool WindowsRemoveFunction::RunSync() { | 760 ExtensionFunction::ResponseAction WindowsRemoveFunction::Run() { |
780 std::unique_ptr<windows::Remove::Params> params( | 761 std::unique_ptr<windows::Remove::Params> params( |
781 windows::Remove::Params::Create(*args_)); | 762 windows::Remove::Params::Create(*args_)); |
782 EXTENSION_FUNCTION_VALIDATE(params); | 763 EXTENSION_FUNCTION_VALIDATE(params); |
783 | 764 |
784 WindowController* controller; | 765 WindowController* controller = nullptr; |
| 766 std::string error; |
785 if (!windows_util::GetWindowFromWindowID(this, params->window_id, | 767 if (!windows_util::GetWindowFromWindowID(this, params->window_id, |
786 WindowController::kNoWindowFilter, | 768 WindowController::kNoWindowFilter, |
787 &controller, &error_)) { | 769 &controller, &error)) { |
788 return false; | 770 return RespondNow(Error(error)); |
789 } | 771 } |
790 | 772 |
791 WindowController::Reason reason; | 773 WindowController::Reason reason; |
792 if (!controller->CanClose(&reason)) { | 774 if (!controller->CanClose(&reason)) { |
793 if (reason == WindowController::REASON_NOT_EDITABLE) | 775 return RespondNow(Error(reason == WindowController::REASON_NOT_EDITABLE |
794 error_ = keys::kTabStripNotEditableError; | 776 ? keys::kTabStripNotEditableError |
795 return false; | 777 : kUnknownErrorDoNotUse)); |
796 } | 778 } |
797 controller->window()->Close(); | 779 controller->window()->Close(); |
798 return true; | 780 return RespondNow(NoArguments()); |
799 } | 781 } |
800 | 782 |
801 // Tabs ------------------------------------------------------------------------ | 783 // Tabs ------------------------------------------------------------------------ |
802 | 784 |
803 bool TabsGetSelectedFunction::RunSync() { | 785 ExtensionFunction::ResponseAction TabsGetSelectedFunction::Run() { |
804 // windowId defaults to "current" window. | 786 // windowId defaults to "current" window. |
805 int window_id = extension_misc::kCurrentWindowId; | 787 int window_id = extension_misc::kCurrentWindowId; |
806 | 788 |
807 std::unique_ptr<tabs::GetSelected::Params> params( | 789 std::unique_ptr<tabs::GetSelected::Params> params( |
808 tabs::GetSelected::Params::Create(*args_)); | 790 tabs::GetSelected::Params::Create(*args_)); |
809 EXTENSION_FUNCTION_VALIDATE(params.get()); | 791 EXTENSION_FUNCTION_VALIDATE(params.get()); |
810 if (params->window_id.get()) | 792 if (params->window_id.get()) |
811 window_id = *params->window_id; | 793 window_id = *params->window_id; |
812 | 794 |
813 Browser* browser = NULL; | 795 Browser* browser = NULL; |
814 if (!GetBrowserFromWindowID(this, window_id, &browser, &error_)) | 796 std::string error; |
815 return false; | 797 if (!GetBrowserFromWindowID(this, window_id, &browser, &error)) |
| 798 return RespondNow(Error(error)); |
816 | 799 |
817 TabStripModel* tab_strip = browser->tab_strip_model(); | 800 TabStripModel* tab_strip = browser->tab_strip_model(); |
818 WebContents* contents = tab_strip->GetActiveWebContents(); | 801 WebContents* contents = tab_strip->GetActiveWebContents(); |
819 if (!contents) { | 802 if (!contents) |
820 error_ = keys::kNoSelectedTabError; | 803 return RespondNow(Error(keys::kNoSelectedTabError)); |
821 return false; | 804 return RespondNow(ArgumentList( |
822 } | 805 tabs::Get::Results::Create(*ExtensionTabUtil::CreateTabObject( |
823 results_ = tabs::Get::Results::Create(*ExtensionTabUtil::CreateTabObject( | 806 contents, tab_strip, tab_strip->active_index(), extension())))); |
824 contents, tab_strip, tab_strip->active_index(), extension())); | |
825 return true; | |
826 } | 807 } |
827 | 808 |
828 bool TabsGetAllInWindowFunction::RunSync() { | 809 ExtensionFunction::ResponseAction TabsGetAllInWindowFunction::Run() { |
829 std::unique_ptr<tabs::GetAllInWindow::Params> params( | 810 std::unique_ptr<tabs::GetAllInWindow::Params> params( |
830 tabs::GetAllInWindow::Params::Create(*args_)); | 811 tabs::GetAllInWindow::Params::Create(*args_)); |
831 EXTENSION_FUNCTION_VALIDATE(params.get()); | 812 EXTENSION_FUNCTION_VALIDATE(params.get()); |
832 // windowId defaults to "current" window. | 813 // windowId defaults to "current" window. |
833 int window_id = extension_misc::kCurrentWindowId; | 814 int window_id = extension_misc::kCurrentWindowId; |
834 if (params->window_id.get()) | 815 if (params->window_id.get()) |
835 window_id = *params->window_id; | 816 window_id = *params->window_id; |
836 | 817 |
837 Browser* browser = NULL; | 818 Browser* browser = NULL; |
838 if (!GetBrowserFromWindowID(this, window_id, &browser, &error_)) | 819 std::string error; |
839 return false; | 820 if (!GetBrowserFromWindowID(this, window_id, &browser, &error)) |
| 821 return RespondNow(Error(error)); |
840 | 822 |
841 SetResult(ExtensionTabUtil::CreateTabList(browser, extension())); | 823 return RespondNow( |
842 | 824 OneArgument(ExtensionTabUtil::CreateTabList(browser, extension()))); |
843 return true; | |
844 } | 825 } |
845 | 826 |
846 bool TabsQueryFunction::RunSync() { | 827 ExtensionFunction::ResponseAction TabsQueryFunction::Run() { |
847 std::unique_ptr<tabs::Query::Params> params( | 828 std::unique_ptr<tabs::Query::Params> params( |
848 tabs::Query::Params::Create(*args_)); | 829 tabs::Query::Params::Create(*args_)); |
849 EXTENSION_FUNCTION_VALIDATE(params.get()); | 830 EXTENSION_FUNCTION_VALIDATE(params.get()); |
850 | 831 |
851 bool loading_status_set = params->query_info.status != tabs::TAB_STATUS_NONE; | 832 bool loading_status_set = params->query_info.status != tabs::TAB_STATUS_NONE; |
852 bool loading = params->query_info.status == tabs::TAB_STATUS_LOADING; | 833 bool loading = params->query_info.status == tabs::TAB_STATUS_LOADING; |
853 | 834 |
854 URLPatternSet url_patterns; | 835 URLPatternSet url_patterns; |
855 if (params->query_info.url.get()) { | 836 if (params->query_info.url.get()) { |
856 std::vector<std::string> url_pattern_strings; | 837 std::vector<std::string> url_pattern_strings; |
857 if (params->query_info.url->as_string) | 838 if (params->query_info.url->as_string) |
858 url_pattern_strings.push_back(*params->query_info.url->as_string); | 839 url_pattern_strings.push_back(*params->query_info.url->as_string); |
859 else if (params->query_info.url->as_strings) | 840 else if (params->query_info.url->as_strings) |
860 url_pattern_strings.swap(*params->query_info.url->as_strings); | 841 url_pattern_strings.swap(*params->query_info.url->as_strings); |
861 // It is o.k. to use URLPattern::SCHEME_ALL here because this function does | 842 // It is o.k. to use URLPattern::SCHEME_ALL here because this function does |
862 // not grant access to the content of the tabs, only to seeing their URLs | 843 // not grant access to the content of the tabs, only to seeing their URLs |
863 // and meta data. | 844 // and meta data. |
| 845 std::string error; |
864 if (!url_patterns.Populate(url_pattern_strings, URLPattern::SCHEME_ALL, | 846 if (!url_patterns.Populate(url_pattern_strings, URLPattern::SCHEME_ALL, |
865 true, &error_)) { | 847 true, &error)) { |
866 return false; | 848 return RespondNow(Error(error)); |
867 } | 849 } |
868 } | 850 } |
869 | 851 |
870 std::string title; | 852 std::string title; |
871 if (params->query_info.title.get()) | 853 if (params->query_info.title.get()) |
872 title = *params->query_info.title; | 854 title = *params->query_info.title; |
873 | 855 |
874 int window_id = extension_misc::kUnknownWindowId; | 856 int window_id = extension_misc::kUnknownWindowId; |
875 if (params->query_info.window_id.get()) | 857 if (params->query_info.window_id.get()) |
876 window_id = *params->query_info.window_id; | 858 window_id = *params->query_info.window_id; |
877 | 859 |
878 int index = -1; | 860 int index = -1; |
879 if (params->query_info.index.get()) | 861 if (params->query_info.index.get()) |
880 index = *params->query_info.index; | 862 index = *params->query_info.index; |
881 | 863 |
882 std::string window_type; | 864 std::string window_type; |
883 if (params->query_info.window_type != tabs::WINDOW_TYPE_NONE) | 865 if (params->query_info.window_type != tabs::WINDOW_TYPE_NONE) |
884 window_type = tabs::ToString(params->query_info.window_type); | 866 window_type = tabs::ToString(params->query_info.window_type); |
885 | 867 |
886 std::unique_ptr<base::ListValue> result(new base::ListValue()); | 868 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 869 Profile* profile = Profile::FromBrowserContext(browser_context()); |
887 Browser* last_active_browser = | 870 Browser* last_active_browser = |
888 chrome::FindAnyBrowser(GetProfile(), include_incognito()); | 871 chrome::FindAnyBrowser(profile, include_incognito()); |
889 Browser* current_browser = GetCurrentBrowser(); | 872 Browser* current_browser = |
| 873 ChromeExtensionFunctionDetails(this).GetCurrentBrowser(); |
890 for (auto* browser : *BrowserList::GetInstance()) { | 874 for (auto* browser : *BrowserList::GetInstance()) { |
891 if (!GetProfile()->IsSameProfile(browser->profile())) | 875 if (!profile->IsSameProfile(browser->profile())) |
892 continue; | 876 continue; |
893 | 877 |
894 if (!browser->window()) | 878 if (!browser->window()) |
895 continue; | 879 continue; |
896 | 880 |
897 if (!include_incognito() && GetProfile() != browser->profile()) | 881 if (!include_incognito() && profile != browser->profile()) |
898 continue; | 882 continue; |
899 | 883 |
900 if (!browser->extension_window_controller()->IsVisibleToExtension( | 884 if (!browser->extension_window_controller()->IsVisibleToExtension( |
901 extension())) { | 885 extension())) { |
902 continue; | 886 continue; |
903 } | 887 } |
904 | 888 |
905 if (window_id >= 0 && window_id != ExtensionTabUtil::GetWindowId(browser)) | 889 if (window_id >= 0 && window_id != ExtensionTabUtil::GetWindowId(browser)) |
906 continue; | 890 continue; |
907 | 891 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
996 | 980 |
997 if (loading_status_set && loading != web_contents->IsLoading()) | 981 if (loading_status_set && loading != web_contents->IsLoading()) |
998 continue; | 982 continue; |
999 | 983 |
1000 result->Append(ExtensionTabUtil::CreateTabObject(web_contents, tab_strip, | 984 result->Append(ExtensionTabUtil::CreateTabObject(web_contents, tab_strip, |
1001 i, extension()) | 985 i, extension()) |
1002 ->ToValue()); | 986 ->ToValue()); |
1003 } | 987 } |
1004 } | 988 } |
1005 | 989 |
1006 SetResult(std::move(result)); | 990 return RespondNow(OneArgument(std::move(result))); |
1007 return true; | |
1008 } | 991 } |
1009 | 992 |
1010 bool TabsCreateFunction::RunSync() { | 993 ExtensionFunction::ResponseAction TabsCreateFunction::Run() { |
1011 std::unique_ptr<tabs::Create::Params> params( | 994 std::unique_ptr<tabs::Create::Params> params( |
1012 tabs::Create::Params::Create(*args_)); | 995 tabs::Create::Params::Create(*args_)); |
1013 EXTENSION_FUNCTION_VALIDATE(params.get()); | 996 EXTENSION_FUNCTION_VALIDATE(params.get()); |
1014 | 997 |
1015 ExtensionTabUtil::OpenTabParams options; | 998 ExtensionTabUtil::OpenTabParams options; |
1016 AssignOptionalValue(params->create_properties.window_id, options.window_id); | 999 AssignOptionalValue(params->create_properties.window_id, options.window_id); |
1017 AssignOptionalValue(params->create_properties.opener_tab_id, | 1000 AssignOptionalValue(params->create_properties.opener_tab_id, |
1018 options.opener_tab_id); | 1001 options.opener_tab_id); |
1019 AssignOptionalValue(params->create_properties.selected, options.active); | 1002 AssignOptionalValue(params->create_properties.selected, options.active); |
1020 // The 'active' property has replaced the 'selected' property. | 1003 // The 'active' property has replaced the 'selected' property. |
1021 AssignOptionalValue(params->create_properties.active, options.active); | 1004 AssignOptionalValue(params->create_properties.active, options.active); |
1022 AssignOptionalValue(params->create_properties.pinned, options.pinned); | 1005 AssignOptionalValue(params->create_properties.pinned, options.pinned); |
1023 AssignOptionalValue(params->create_properties.index, options.index); | 1006 AssignOptionalValue(params->create_properties.index, options.index); |
1024 AssignOptionalValue(params->create_properties.url, options.url); | 1007 AssignOptionalValue(params->create_properties.url, options.url); |
1025 | 1008 |
1026 std::string error; | 1009 std::string error; |
1027 std::unique_ptr<base::DictionaryValue> result( | 1010 std::unique_ptr<base::DictionaryValue> result( |
1028 ExtensionTabUtil::OpenTab(this, options, &error)); | 1011 ExtensionTabUtil::OpenTab(this, options, &error)); |
1029 if (!result) { | 1012 if (!result) |
1030 SetError(error); | 1013 return RespondNow(Error(error)); |
1031 return false; | |
1032 } | |
1033 | 1014 |
1034 // Return data about the newly created tab. | 1015 // Return data about the newly created tab. |
1035 if (has_callback()) { | 1016 return RespondNow(has_callback() ? OneArgument(std::move(result)) |
1036 SetResult(std::move(result)); | 1017 : NoArguments()); |
1037 } | |
1038 return true; | |
1039 } | 1018 } |
1040 | 1019 |
1041 bool TabsDuplicateFunction::RunSync() { | 1020 ExtensionFunction::ResponseAction TabsDuplicateFunction::Run() { |
1042 std::unique_ptr<tabs::Duplicate::Params> params( | 1021 std::unique_ptr<tabs::Duplicate::Params> params( |
1043 tabs::Duplicate::Params::Create(*args_)); | 1022 tabs::Duplicate::Params::Create(*args_)); |
1044 EXTENSION_FUNCTION_VALIDATE(params.get()); | 1023 EXTENSION_FUNCTION_VALIDATE(params.get()); |
1045 int tab_id = params->tab_id; | 1024 int tab_id = params->tab_id; |
1046 | 1025 |
1047 Browser* browser = NULL; | 1026 Browser* browser = NULL; |
1048 TabStripModel* tab_strip = NULL; | 1027 TabStripModel* tab_strip = NULL; |
1049 int tab_index = -1; | 1028 int tab_index = -1; |
1050 if (!GetTabById(tab_id, | 1029 std::string error; |
1051 GetProfile(), | 1030 if (!GetTabById(tab_id, browser_context(), include_incognito(), &browser, |
1052 include_incognito(), | 1031 &tab_strip, NULL, &tab_index, &error)) { |
1053 &browser, | 1032 return RespondNow(Error(error)); |
1054 &tab_strip, | |
1055 NULL, | |
1056 &tab_index, | |
1057 &error_)) { | |
1058 return false; | |
1059 } | 1033 } |
1060 | 1034 |
1061 WebContents* new_contents = chrome::DuplicateTabAt(browser, tab_index); | 1035 WebContents* new_contents = chrome::DuplicateTabAt(browser, tab_index); |
1062 if (!has_callback()) | 1036 if (!has_callback()) |
1063 return true; | 1037 return RespondNow(NoArguments()); |
1064 | 1038 |
1065 // Duplicated tab may not be in the same window as the original, so find | 1039 // Duplicated tab may not be in the same window as the original, so find |
1066 // the window and the tab. | 1040 // the window and the tab. |
1067 TabStripModel* new_tab_strip = NULL; | 1041 TabStripModel* new_tab_strip = NULL; |
1068 int new_tab_index = -1; | 1042 int new_tab_index = -1; |
1069 ExtensionTabUtil::GetTabStripModel(new_contents, | 1043 ExtensionTabUtil::GetTabStripModel(new_contents, |
1070 &new_tab_strip, | 1044 &new_tab_strip, |
1071 &new_tab_index); | 1045 &new_tab_index); |
1072 if (!new_tab_strip || new_tab_index == -1) { | 1046 if (!new_tab_strip || new_tab_index == -1) { |
1073 return false; | 1047 return RespondNow(Error(kUnknownErrorDoNotUse)); |
1074 } | 1048 } |
1075 | 1049 |
1076 // Return data about the newly created tab. | 1050 return RespondNow(ArgumentList( |
1077 results_ = tabs::Get::Results::Create(*ExtensionTabUtil::CreateTabObject( | 1051 tabs::Get::Results::Create(*ExtensionTabUtil::CreateTabObject( |
1078 new_contents, new_tab_strip, new_tab_index, extension())); | 1052 new_contents, new_tab_strip, new_tab_index, extension())))); |
1079 | |
1080 return true; | |
1081 } | 1053 } |
1082 | 1054 |
1083 bool TabsGetFunction::RunSync() { | 1055 ExtensionFunction::ResponseAction TabsGetFunction::Run() { |
1084 std::unique_ptr<tabs::Get::Params> params(tabs::Get::Params::Create(*args_)); | 1056 std::unique_ptr<tabs::Get::Params> params(tabs::Get::Params::Create(*args_)); |
1085 EXTENSION_FUNCTION_VALIDATE(params.get()); | 1057 EXTENSION_FUNCTION_VALIDATE(params.get()); |
1086 int tab_id = params->tab_id; | 1058 int tab_id = params->tab_id; |
1087 | 1059 |
1088 TabStripModel* tab_strip = NULL; | 1060 TabStripModel* tab_strip = NULL; |
1089 WebContents* contents = NULL; | 1061 WebContents* contents = NULL; |
1090 int tab_index = -1; | 1062 int tab_index = -1; |
1091 if (!GetTabById(tab_id, | 1063 std::string error; |
1092 GetProfile(), | 1064 if (!GetTabById(tab_id, browser_context(), include_incognito(), NULL, |
1093 include_incognito(), | 1065 &tab_strip, &contents, &tab_index, &error)) { |
1094 NULL, | 1066 return RespondNow(Error(error)); |
1095 &tab_strip, | 1067 } |
1096 &contents, | |
1097 &tab_index, | |
1098 &error_)) | |
1099 return false; | |
1100 | 1068 |
1101 results_ = tabs::Get::Results::Create(*ExtensionTabUtil::CreateTabObject( | 1069 return RespondNow(ArgumentList( |
1102 contents, tab_strip, tab_index, extension())); | 1070 tabs::Get::Results::Create(*ExtensionTabUtil::CreateTabObject( |
1103 return true; | 1071 contents, tab_strip, tab_index, extension())))); |
1104 } | 1072 } |
1105 | 1073 |
1106 bool TabsGetCurrentFunction::RunSync() { | 1074 ExtensionFunction::ResponseAction TabsGetCurrentFunction::Run() { |
1107 DCHECK(dispatcher()); | 1075 DCHECK(dispatcher()); |
1108 | 1076 |
1109 // Return the caller, if it's a tab. If not the result isn't an error but an | 1077 // Return the caller, if it's a tab. If not the result isn't an error but an |
1110 // empty tab (hence returning true). | 1078 // empty tab (hence returning true). |
1111 WebContents* caller_contents = GetSenderWebContents(); | 1079 WebContents* caller_contents = GetSenderWebContents(); |
1112 if (caller_contents && ExtensionTabUtil::GetTabId(caller_contents) >= 0) | 1080 std::unique_ptr<base::ListValue> results; |
1113 results_ = tabs::Get::Results::Create( | 1081 if (caller_contents && ExtensionTabUtil::GetTabId(caller_contents) >= 0) { |
| 1082 results = tabs::Get::Results::Create( |
1114 *ExtensionTabUtil::CreateTabObject(caller_contents, extension())); | 1083 *ExtensionTabUtil::CreateTabObject(caller_contents, extension())); |
1115 | 1084 } |
1116 return true; | 1085 return RespondNow(results ? ArgumentList(std::move(results)) : NoArguments()); |
1117 } | 1086 } |
1118 | 1087 |
1119 bool TabsHighlightFunction::RunSync() { | 1088 ExtensionFunction::ResponseAction TabsHighlightFunction::Run() { |
1120 std::unique_ptr<tabs::Highlight::Params> params( | 1089 std::unique_ptr<tabs::Highlight::Params> params( |
1121 tabs::Highlight::Params::Create(*args_)); | 1090 tabs::Highlight::Params::Create(*args_)); |
1122 EXTENSION_FUNCTION_VALIDATE(params.get()); | 1091 EXTENSION_FUNCTION_VALIDATE(params.get()); |
1123 | 1092 |
1124 // Get the window id from the params; default to current window if omitted. | 1093 // Get the window id from the params; default to current window if omitted. |
1125 int window_id = extension_misc::kCurrentWindowId; | 1094 int window_id = extension_misc::kCurrentWindowId; |
1126 if (params->highlight_info.window_id.get()) | 1095 if (params->highlight_info.window_id.get()) |
1127 window_id = *params->highlight_info.window_id; | 1096 window_id = *params->highlight_info.window_id; |
1128 | 1097 |
1129 Browser* browser = NULL; | 1098 Browser* browser = NULL; |
1130 if (!GetBrowserFromWindowID(this, window_id, &browser, &error_)) | 1099 std::string error; |
1131 return false; | 1100 if (!GetBrowserFromWindowID(this, window_id, &browser, &error)) |
| 1101 return RespondNow(Error(error)); |
1132 | 1102 |
1133 TabStripModel* tabstrip = browser->tab_strip_model(); | 1103 TabStripModel* tabstrip = browser->tab_strip_model(); |
1134 ui::ListSelectionModel selection; | 1104 ui::ListSelectionModel selection; |
1135 int active_index = -1; | 1105 int active_index = -1; |
1136 | 1106 |
1137 if (params->highlight_info.tabs.as_integers) { | 1107 if (params->highlight_info.tabs.as_integers) { |
1138 std::vector<int>& tab_indices = *params->highlight_info.tabs.as_integers; | 1108 std::vector<int>& tab_indices = *params->highlight_info.tabs.as_integers; |
1139 // Create a new selection model as we read the list of tab indices. | 1109 // Create a new selection model as we read the list of tab indices. |
1140 for (size_t i = 0; i < tab_indices.size(); ++i) { | 1110 for (size_t i = 0; i < tab_indices.size(); ++i) { |
1141 if (!HighlightTab(tabstrip, &selection, &active_index, tab_indices[i])) | 1111 if (!HighlightTab(tabstrip, &selection, &active_index, tab_indices[i], |
1142 return false; | 1112 &error)) { |
| 1113 return RespondNow(Error(error)); |
| 1114 } |
1143 } | 1115 } |
1144 } else { | 1116 } else { |
1145 EXTENSION_FUNCTION_VALIDATE(params->highlight_info.tabs.as_integer); | 1117 EXTENSION_FUNCTION_VALIDATE(params->highlight_info.tabs.as_integer); |
1146 if (!HighlightTab(tabstrip, | 1118 if (!HighlightTab(tabstrip, &selection, &active_index, |
1147 &selection, | 1119 *params->highlight_info.tabs.as_integer, &error)) { |
1148 &active_index, | 1120 return RespondNow(Error(error)); |
1149 *params->highlight_info.tabs.as_integer)) { | |
1150 return false; | |
1151 } | 1121 } |
1152 } | 1122 } |
1153 | 1123 |
1154 // Make sure they actually specified tabs to select. | 1124 // Make sure they actually specified tabs to select. |
1155 if (selection.empty()) { | 1125 if (selection.empty()) |
1156 error_ = keys::kNoHighlightedTabError; | 1126 return RespondNow(Error(keys::kNoHighlightedTabError)); |
1157 return false; | |
1158 } | |
1159 | 1127 |
1160 selection.set_active(active_index); | 1128 selection.set_active(active_index); |
1161 browser->tab_strip_model()->SetSelectionFromModel(selection); | 1129 browser->tab_strip_model()->SetSelectionFromModel(selection); |
1162 SetResult(browser->extension_window_controller()->CreateWindowValueWithTabs( | 1130 return RespondNow(OneArgument( |
1163 extension())); | 1131 browser->extension_window_controller()->CreateWindowValueWithTabs( |
1164 return true; | 1132 extension()))); |
1165 } | 1133 } |
1166 | 1134 |
1167 bool TabsHighlightFunction::HighlightTab(TabStripModel* tabstrip, | 1135 bool TabsHighlightFunction::HighlightTab(TabStripModel* tabstrip, |
1168 ui::ListSelectionModel* selection, | 1136 ui::ListSelectionModel* selection, |
1169 int* active_index, | 1137 int* active_index, |
1170 int index) { | 1138 int index, |
| 1139 std::string* error) { |
1171 // Make sure the index is in range. | 1140 // Make sure the index is in range. |
1172 if (!tabstrip->ContainsIndex(index)) { | 1141 if (!tabstrip->ContainsIndex(index)) { |
1173 error_ = ErrorUtils::FormatErrorMessage( | 1142 *error = ErrorUtils::FormatErrorMessage(keys::kTabIndexNotFoundError, |
1174 keys::kTabIndexNotFoundError, base::IntToString(index)); | 1143 base::IntToString(index)); |
1175 return false; | 1144 return false; |
1176 } | 1145 } |
1177 | 1146 |
1178 // By default, we make the first tab in the list active. | 1147 // By default, we make the first tab in the list active. |
1179 if (*active_index == -1) | 1148 if (*active_index == -1) |
1180 *active_index = index; | 1149 *active_index = index; |
1181 | 1150 |
1182 selection->AddIndexToSelection(index); | 1151 selection->AddIndexToSelection(index); |
1183 return true; | 1152 return true; |
1184 } | 1153 } |
(...skipping 19 matching lines...) Expand all Loading... |
1204 error_ = keys::kNoSelectedTabError; | 1173 error_ = keys::kNoSelectedTabError; |
1205 return false; | 1174 return false; |
1206 } | 1175 } |
1207 tab_id = SessionTabHelper::IdForTab(contents); | 1176 tab_id = SessionTabHelper::IdForTab(contents); |
1208 } else { | 1177 } else { |
1209 tab_id = *params->tab_id; | 1178 tab_id = *params->tab_id; |
1210 } | 1179 } |
1211 | 1180 |
1212 int tab_index = -1; | 1181 int tab_index = -1; |
1213 TabStripModel* tab_strip = NULL; | 1182 TabStripModel* tab_strip = NULL; |
1214 if (!GetTabById(tab_id, | 1183 if (!GetTabById(tab_id, browser_context(), include_incognito(), NULL, |
1215 GetProfile(), | 1184 &tab_strip, &contents, &tab_index, &error_)) { |
1216 include_incognito(), | |
1217 NULL, | |
1218 &tab_strip, | |
1219 &contents, | |
1220 &tab_index, | |
1221 &error_)) { | |
1222 return false; | 1185 return false; |
1223 } | 1186 } |
1224 | 1187 |
1225 web_contents_ = contents; | 1188 web_contents_ = contents; |
1226 | 1189 |
1227 // TODO(rafaelw): handle setting remaining tab properties: | 1190 // TODO(rafaelw): handle setting remaining tab properties: |
1228 // -title | 1191 // -title |
1229 // -favIconUrl | 1192 // -favIconUrl |
1230 | 1193 |
1231 // Navigate the tab to a new location if the url is different. | 1194 // Navigate the tab to a new location if the url is different. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1283 error_ = ErrorUtils::FormatErrorMessage(keys::kCannotUpdateMuteCaptured, | 1246 error_ = ErrorUtils::FormatErrorMessage(keys::kCannotUpdateMuteCaptured, |
1284 base::IntToString(tab_id)); | 1247 base::IntToString(tab_id)); |
1285 return false; | 1248 return false; |
1286 } | 1249 } |
1287 } | 1250 } |
1288 | 1251 |
1289 if (params->update_properties.opener_tab_id.get()) { | 1252 if (params->update_properties.opener_tab_id.get()) { |
1290 int opener_id = *params->update_properties.opener_tab_id; | 1253 int opener_id = *params->update_properties.opener_tab_id; |
1291 | 1254 |
1292 WebContents* opener_contents = NULL; | 1255 WebContents* opener_contents = NULL; |
1293 if (!ExtensionTabUtil::GetTabById(opener_id, | 1256 if (!ExtensionTabUtil::GetTabById(opener_id, browser_context(), |
1294 GetProfile(), | 1257 include_incognito(), nullptr, nullptr, |
1295 include_incognito(), | 1258 &opener_contents, nullptr)) |
1296 NULL, | |
1297 NULL, | |
1298 &opener_contents, | |
1299 NULL)) | |
1300 return false; | 1259 return false; |
1301 | 1260 |
1302 tab_strip->SetOpenerOfWebContentsAt(tab_index, opener_contents); | 1261 tab_strip->SetOpenerOfWebContentsAt(tab_index, opener_contents); |
1303 } | 1262 } |
1304 | 1263 |
1305 if (params->update_properties.auto_discardable.get()) { | 1264 if (params->update_properties.auto_discardable.get()) { |
1306 bool state = *params->update_properties.auto_discardable; | 1265 bool state = *params->update_properties.auto_discardable; |
1307 g_browser_process->GetTabManager()->SetTabAutoDiscardableState(contents, | 1266 g_browser_process->GetTabManager()->SetTabAutoDiscardableState(contents, |
1308 state); | 1267 state); |
1309 } | 1268 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1387 const std::string& error, | 1346 const std::string& error, |
1388 const GURL& url, | 1347 const GURL& url, |
1389 const base::ListValue& script_result) { | 1348 const base::ListValue& script_result) { |
1390 if (error.empty()) | 1349 if (error.empty()) |
1391 PopulateResult(); | 1350 PopulateResult(); |
1392 else | 1351 else |
1393 error_ = error; | 1352 error_ = error; |
1394 SendResponse(error.empty()); | 1353 SendResponse(error.empty()); |
1395 } | 1354 } |
1396 | 1355 |
1397 bool TabsMoveFunction::RunSync() { | 1356 ExtensionFunction::ResponseAction TabsMoveFunction::Run() { |
1398 std::unique_ptr<tabs::Move::Params> params( | 1357 std::unique_ptr<tabs::Move::Params> params( |
1399 tabs::Move::Params::Create(*args_)); | 1358 tabs::Move::Params::Create(*args_)); |
1400 EXTENSION_FUNCTION_VALIDATE(params.get()); | 1359 EXTENSION_FUNCTION_VALIDATE(params.get()); |
1401 | 1360 |
1402 int new_index = params->move_properties.index; | 1361 int new_index = params->move_properties.index; |
1403 int* window_id = params->move_properties.window_id.get(); | 1362 int* window_id = params->move_properties.window_id.get(); |
1404 std::unique_ptr<base::ListValue> tab_values(new base::ListValue()); | 1363 std::unique_ptr<base::ListValue> tab_values(new base::ListValue()); |
1405 | 1364 |
1406 size_t num_tabs = 0; | 1365 size_t num_tabs = 0; |
| 1366 std::string error; |
1407 if (params->tab_ids.as_integers) { | 1367 if (params->tab_ids.as_integers) { |
1408 std::vector<int>& tab_ids = *params->tab_ids.as_integers; | 1368 std::vector<int>& tab_ids = *params->tab_ids.as_integers; |
1409 num_tabs = tab_ids.size(); | 1369 num_tabs = tab_ids.size(); |
1410 for (size_t i = 0; i < tab_ids.size(); ++i) { | 1370 for (size_t i = 0; i < tab_ids.size(); ++i) { |
1411 if (!MoveTab(tab_ids[i], &new_index, i, tab_values.get(), window_id)) | 1371 if (!MoveTab(tab_ids[i], &new_index, i, tab_values.get(), window_id, |
1412 return false; | 1372 &error)) { |
| 1373 return RespondNow(Error(error)); |
| 1374 } |
1413 } | 1375 } |
1414 } else { | 1376 } else { |
1415 EXTENSION_FUNCTION_VALIDATE(params->tab_ids.as_integer); | 1377 EXTENSION_FUNCTION_VALIDATE(params->tab_ids.as_integer); |
1416 num_tabs = 1; | 1378 num_tabs = 1; |
1417 if (!MoveTab(*params->tab_ids.as_integer, | 1379 if (!MoveTab(*params->tab_ids.as_integer, &new_index, 0, tab_values.get(), |
1418 &new_index, | 1380 window_id, &error)) { |
1419 0, | 1381 return RespondNow(Error(error)); |
1420 tab_values.get(), | |
1421 window_id)) { | |
1422 return false; | |
1423 } | 1382 } |
1424 } | 1383 } |
1425 | 1384 |
| 1385 // TODO(devlin): It's weird that whether or not the method provides a callback |
| 1386 // can determine its success (as we return errors below). |
1426 if (!has_callback()) | 1387 if (!has_callback()) |
1427 return true; | 1388 return RespondNow(NoArguments()); |
1428 | 1389 |
1429 if (num_tabs == 0) { | 1390 if (num_tabs == 0) |
1430 error_ = "No tabs given."; | 1391 return RespondNow(Error("No tabs given.")); |
1431 return false; | 1392 if (num_tabs == 1) { |
1432 } else if (num_tabs == 1) { | |
1433 std::unique_ptr<base::Value> value; | 1393 std::unique_ptr<base::Value> value; |
1434 CHECK(tab_values->Remove(0, &value)); | 1394 CHECK(tab_values->Remove(0, &value)); |
1435 SetResult(std::move(value)); | 1395 return RespondNow(OneArgument(std::move(value))); |
1436 } else { | |
1437 // Only return the results as an array if there are multiple tabs. | |
1438 SetResult(std::move(tab_values)); | |
1439 } | 1396 } |
1440 | 1397 |
1441 return true; | 1398 // Return the results as an array if there are multiple tabs. |
| 1399 return RespondNow(OneArgument(std::move(tab_values))); |
1442 } | 1400 } |
1443 | 1401 |
1444 bool TabsMoveFunction::MoveTab(int tab_id, | 1402 bool TabsMoveFunction::MoveTab(int tab_id, |
1445 int* new_index, | 1403 int* new_index, |
1446 int iteration, | 1404 int iteration, |
1447 base::ListValue* tab_values, | 1405 base::ListValue* tab_values, |
1448 int* window_id) { | 1406 int* window_id, |
| 1407 std::string* error) { |
1449 Browser* source_browser = NULL; | 1408 Browser* source_browser = NULL; |
1450 TabStripModel* source_tab_strip = NULL; | 1409 TabStripModel* source_tab_strip = NULL; |
1451 WebContents* contents = NULL; | 1410 WebContents* contents = NULL; |
1452 int tab_index = -1; | 1411 int tab_index = -1; |
1453 if (!GetTabById(tab_id, | 1412 if (!GetTabById(tab_id, browser_context(), include_incognito(), |
1454 GetProfile(), | 1413 &source_browser, &source_tab_strip, &contents, &tab_index, |
1455 include_incognito(), | 1414 error)) { |
1456 &source_browser, | |
1457 &source_tab_strip, | |
1458 &contents, | |
1459 &tab_index, | |
1460 &error_)) { | |
1461 return false; | 1415 return false; |
1462 } | 1416 } |
1463 | 1417 |
1464 // Don't let the extension move the tab if the user is dragging tabs. | 1418 // Don't let the extension move the tab if the user is dragging tabs. |
1465 if (!source_browser->window()->IsTabStripEditable()) { | 1419 if (!source_browser->window()->IsTabStripEditable()) { |
1466 error_ = keys::kTabStripNotEditableError; | 1420 *error = keys::kTabStripNotEditableError; |
1467 return false; | 1421 return false; |
1468 } | 1422 } |
1469 | 1423 |
1470 // Insert the tabs one after another. | 1424 // Insert the tabs one after another. |
1471 *new_index += iteration; | 1425 *new_index += iteration; |
1472 | 1426 |
1473 if (window_id) { | 1427 if (window_id) { |
1474 Browser* target_browser = NULL; | 1428 Browser* target_browser = NULL; |
1475 | 1429 |
1476 if (!GetBrowserFromWindowID(this, *window_id, &target_browser, &error_)) | 1430 if (!GetBrowserFromWindowID(this, *window_id, &target_browser, error)) |
1477 return false; | 1431 return false; |
1478 | 1432 |
1479 if (!target_browser->window()->IsTabStripEditable()) { | 1433 if (!target_browser->window()->IsTabStripEditable()) { |
1480 error_ = keys::kTabStripNotEditableError; | 1434 *error = keys::kTabStripNotEditableError; |
1481 return false; | 1435 return false; |
1482 } | 1436 } |
1483 | 1437 |
1484 if (!target_browser->is_type_tabbed()) { | 1438 if (!target_browser->is_type_tabbed()) { |
1485 error_ = keys::kCanOnlyMoveTabsWithinNormalWindowsError; | 1439 *error = keys::kCanOnlyMoveTabsWithinNormalWindowsError; |
1486 return false; | 1440 return false; |
1487 } | 1441 } |
1488 | 1442 |
1489 if (target_browser->profile() != source_browser->profile()) { | 1443 if (target_browser->profile() != source_browser->profile()) { |
1490 error_ = keys::kCanOnlyMoveTabsWithinSameProfileError; | 1444 *error = keys::kCanOnlyMoveTabsWithinSameProfileError; |
1491 return false; | 1445 return false; |
1492 } | 1446 } |
1493 | 1447 |
1494 // If windowId is different from the current window, move between windows. | 1448 // If windowId is different from the current window, move between windows. |
1495 if (ExtensionTabUtil::GetWindowId(target_browser) != | 1449 if (ExtensionTabUtil::GetWindowId(target_browser) != |
1496 ExtensionTabUtil::GetWindowId(source_browser)) { | 1450 ExtensionTabUtil::GetWindowId(source_browser)) { |
1497 TabStripModel* target_tab_strip = target_browser->tab_strip_model(); | 1451 TabStripModel* target_tab_strip = target_browser->tab_strip_model(); |
1498 WebContents* web_contents = | 1452 WebContents* web_contents = |
1499 source_tab_strip->DetachWebContentsAt(tab_index); | 1453 source_tab_strip->DetachWebContentsAt(tab_index); |
1500 if (!web_contents) { | 1454 if (!web_contents) { |
1501 error_ = ErrorUtils::FormatErrorMessage( | 1455 *error = ErrorUtils::FormatErrorMessage(keys::kTabNotFoundError, |
1502 keys::kTabNotFoundError, base::IntToString(tab_id)); | 1456 base::IntToString(tab_id)); |
1503 return false; | 1457 return false; |
1504 } | 1458 } |
1505 | 1459 |
1506 // Clamp move location to the last position. | 1460 // Clamp move location to the last position. |
1507 // This is ">" because it can append to a new index position. | 1461 // This is ">" because it can append to a new index position. |
1508 // -1 means set the move location to the last position. | 1462 // -1 means set the move location to the last position. |
1509 if (*new_index > target_tab_strip->count() || *new_index < 0) | 1463 if (*new_index > target_tab_strip->count() || *new_index < 0) |
1510 *new_index = target_tab_strip->count(); | 1464 *new_index = target_tab_strip->count(); |
1511 | 1465 |
1512 target_tab_strip->InsertWebContentsAt( | 1466 target_tab_strip->InsertWebContentsAt( |
(...skipping 22 matching lines...) Expand all Loading... |
1535 | 1489 |
1536 if (has_callback()) { | 1490 if (has_callback()) { |
1537 tab_values->Append(ExtensionTabUtil::CreateTabObject( | 1491 tab_values->Append(ExtensionTabUtil::CreateTabObject( |
1538 contents, source_tab_strip, *new_index, extension()) | 1492 contents, source_tab_strip, *new_index, extension()) |
1539 ->ToValue()); | 1493 ->ToValue()); |
1540 } | 1494 } |
1541 | 1495 |
1542 return true; | 1496 return true; |
1543 } | 1497 } |
1544 | 1498 |
1545 bool TabsReloadFunction::RunSync() { | 1499 ExtensionFunction::ResponseAction TabsReloadFunction::Run() { |
1546 std::unique_ptr<tabs::Reload::Params> params( | 1500 std::unique_ptr<tabs::Reload::Params> params( |
1547 tabs::Reload::Params::Create(*args_)); | 1501 tabs::Reload::Params::Create(*args_)); |
1548 EXTENSION_FUNCTION_VALIDATE(params.get()); | 1502 EXTENSION_FUNCTION_VALIDATE(params.get()); |
1549 | 1503 |
1550 bool bypass_cache = false; | 1504 bool bypass_cache = false; |
1551 if (params->reload_properties.get() && | 1505 if (params->reload_properties.get() && |
1552 params->reload_properties->bypass_cache.get()) { | 1506 params->reload_properties->bypass_cache.get()) { |
1553 bypass_cache = *params->reload_properties->bypass_cache; | 1507 bypass_cache = *params->reload_properties->bypass_cache; |
1554 } | 1508 } |
1555 | 1509 |
1556 content::WebContents* web_contents = NULL; | 1510 content::WebContents* web_contents = NULL; |
1557 | 1511 |
1558 // If |tab_id| is specified, look for it. Otherwise default to selected tab | 1512 // If |tab_id| is specified, look for it. Otherwise default to selected tab |
1559 // in the current window. | 1513 // in the current window. |
| 1514 Browser* current_browser = |
| 1515 ChromeExtensionFunctionDetails(this).GetCurrentBrowser(); |
1560 if (!params->tab_id.get()) { | 1516 if (!params->tab_id.get()) { |
1561 Browser* browser = GetCurrentBrowser(); | 1517 if (!current_browser) |
1562 if (!browser) { | 1518 return RespondNow(Error(keys::kNoCurrentWindowError)); |
1563 error_ = keys::kNoCurrentWindowError; | |
1564 return false; | |
1565 } | |
1566 | 1519 |
1567 if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, NULL)) | 1520 if (!ExtensionTabUtil::GetDefaultTab(current_browser, &web_contents, NULL)) |
1568 return false; | 1521 return RespondNow(Error(kUnknownErrorDoNotUse)); |
1569 } else { | 1522 } else { |
1570 int tab_id = *params->tab_id; | 1523 int tab_id = *params->tab_id; |
1571 | 1524 |
1572 Browser* browser = NULL; | 1525 Browser* browser = NULL; |
1573 if (!GetTabById(tab_id, | 1526 std::string error; |
1574 GetProfile(), | 1527 if (!GetTabById(tab_id, browser_context(), include_incognito(), &browser, |
1575 include_incognito(), | 1528 NULL, &web_contents, NULL, &error)) { |
1576 &browser, | 1529 return RespondNow(Error(error)); |
1577 NULL, | |
1578 &web_contents, | |
1579 NULL, | |
1580 &error_)) { | |
1581 return false; | |
1582 } | 1530 } |
1583 } | 1531 } |
1584 | 1532 |
1585 if (web_contents->ShowingInterstitialPage()) { | 1533 if (web_contents->ShowingInterstitialPage()) { |
1586 // This does as same as Browser::ReloadInternal. | 1534 // This does as same as Browser::ReloadInternal. |
1587 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); | 1535 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); |
1588 GURL reload_url = entry ? entry->GetURL() : GURL(url::kAboutBlankURL); | 1536 GURL reload_url = entry ? entry->GetURL() : GURL(url::kAboutBlankURL); |
1589 OpenURLParams params(reload_url, Referrer(), | 1537 OpenURLParams params(reload_url, Referrer(), |
1590 WindowOpenDisposition::CURRENT_TAB, | 1538 WindowOpenDisposition::CURRENT_TAB, |
1591 ui::PAGE_TRANSITION_RELOAD, false); | 1539 ui::PAGE_TRANSITION_RELOAD, false); |
1592 GetCurrentBrowser()->OpenURL(params); | 1540 current_browser->OpenURL(params); |
1593 } else if (bypass_cache) { | 1541 } else if (bypass_cache) { |
1594 web_contents->GetController().ReloadBypassingCache(true); | 1542 web_contents->GetController().ReloadBypassingCache(true); |
1595 } else { | 1543 } else { |
1596 web_contents->GetController().Reload(true); | 1544 web_contents->GetController().Reload(true); |
1597 } | 1545 } |
1598 | 1546 |
1599 return true; | 1547 return RespondNow(NoArguments()); |
1600 } | 1548 } |
1601 | 1549 |
1602 bool TabsRemoveFunction::RunSync() { | 1550 ExtensionFunction::ResponseAction TabsRemoveFunction::Run() { |
1603 std::unique_ptr<tabs::Remove::Params> params( | 1551 std::unique_ptr<tabs::Remove::Params> params( |
1604 tabs::Remove::Params::Create(*args_)); | 1552 tabs::Remove::Params::Create(*args_)); |
1605 EXTENSION_FUNCTION_VALIDATE(params.get()); | 1553 EXTENSION_FUNCTION_VALIDATE(params.get()); |
1606 | 1554 |
| 1555 std::string error; |
1607 if (params->tab_ids.as_integers) { | 1556 if (params->tab_ids.as_integers) { |
1608 std::vector<int>& tab_ids = *params->tab_ids.as_integers; | 1557 std::vector<int>& tab_ids = *params->tab_ids.as_integers; |
1609 for (size_t i = 0; i < tab_ids.size(); ++i) { | 1558 for (size_t i = 0; i < tab_ids.size(); ++i) { |
1610 if (!RemoveTab(tab_ids[i])) | 1559 if (!RemoveTab(tab_ids[i], &error)) |
1611 return false; | 1560 return RespondNow(Error(error)); |
1612 } | 1561 } |
1613 } else { | 1562 } else { |
1614 EXTENSION_FUNCTION_VALIDATE(params->tab_ids.as_integer); | 1563 EXTENSION_FUNCTION_VALIDATE(params->tab_ids.as_integer); |
1615 if (!RemoveTab(*params->tab_ids.as_integer)) | 1564 if (!RemoveTab(*params->tab_ids.as_integer, &error)) |
1616 return false; | 1565 return RespondNow(Error(error)); |
1617 } | 1566 } |
1618 return true; | 1567 return RespondNow(NoArguments()); |
1619 } | 1568 } |
1620 | 1569 |
1621 bool TabsRemoveFunction::RemoveTab(int tab_id) { | 1570 bool TabsRemoveFunction::RemoveTab(int tab_id, std::string* error) { |
1622 Browser* browser = NULL; | 1571 Browser* browser = NULL; |
1623 WebContents* contents = NULL; | 1572 WebContents* contents = NULL; |
1624 if (!GetTabById(tab_id, | 1573 if (!GetTabById(tab_id, browser_context(), include_incognito(), &browser, |
1625 GetProfile(), | 1574 nullptr, &contents, nullptr, error)) { |
1626 include_incognito(), | |
1627 &browser, | |
1628 NULL, | |
1629 &contents, | |
1630 NULL, | |
1631 &error_)) { | |
1632 return false; | 1575 return false; |
1633 } | 1576 } |
1634 | 1577 |
1635 // Don't let the extension remove a tab if the user is dragging tabs around. | 1578 // Don't let the extension remove a tab if the user is dragging tabs around. |
1636 if (!browser->window()->IsTabStripEditable()) { | 1579 if (!browser->window()->IsTabStripEditable()) { |
1637 error_ = keys::kTabStripNotEditableError; | 1580 *error = keys::kTabStripNotEditableError; |
1638 return false; | 1581 return false; |
1639 } | 1582 } |
1640 // There's a chance that the tab is being dragged, or we're in some other | 1583 // There's a chance that the tab is being dragged, or we're in some other |
1641 // nested event loop. This code path ensures that the tab is safely closed | 1584 // nested event loop. This code path ensures that the tab is safely closed |
1642 // under such circumstances, whereas |TabStripModel::CloseWebContentsAt()| | 1585 // under such circumstances, whereas |TabStripModel::CloseWebContentsAt()| |
1643 // does not. | 1586 // does not. |
1644 contents->Close(); | 1587 contents->Close(); |
1645 return true; | 1588 return true; |
1646 } | 1589 } |
1647 | 1590 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1747 EXTENSION_FUNCTION_VALIDATE(params.get()); | 1690 EXTENSION_FUNCTION_VALIDATE(params.get()); |
1748 | 1691 |
1749 int tab_id = 0; | 1692 int tab_id = 0; |
1750 Browser* browser = NULL; | 1693 Browser* browser = NULL; |
1751 WebContents* contents = NULL; | 1694 WebContents* contents = NULL; |
1752 | 1695 |
1753 // If |tab_id| is specified, look for it. Otherwise default to selected tab | 1696 // If |tab_id| is specified, look for it. Otherwise default to selected tab |
1754 // in the current window. | 1697 // in the current window. |
1755 if (params->tab_id.get()) { | 1698 if (params->tab_id.get()) { |
1756 tab_id = *params->tab_id; | 1699 tab_id = *params->tab_id; |
1757 if (!GetTabById(tab_id, | 1700 if (!GetTabById(tab_id, browser_context(), include_incognito(), &browser, |
1758 GetProfile(), | 1701 nullptr, &contents, nullptr, &error_)) { |
1759 include_incognito(), | |
1760 &browser, | |
1761 NULL, | |
1762 &contents, | |
1763 NULL, | |
1764 &error_)) { | |
1765 return false; | 1702 return false; |
1766 } | 1703 } |
1767 if (!browser || !contents) | 1704 if (!browser || !contents) |
1768 return false; | 1705 return false; |
1769 } else { | 1706 } else { |
1770 browser = GetCurrentBrowser(); | 1707 browser = GetCurrentBrowser(); |
1771 if (!browser) | 1708 if (!browser) |
1772 return false; | 1709 return false; |
1773 contents = browser->tab_strip_model()->GetActiveWebContents(); | 1710 contents = browser->tab_strip_model()->GetActiveWebContents(); |
1774 if (!contents) | 1711 if (!contents) |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1884 set_host_id(HostID(HostID::EXTENSIONS, extension()->id())); | 1821 set_host_id(HostID(HostID::EXTENSIONS, extension()->id())); |
1885 return true; | 1822 return true; |
1886 } | 1823 } |
1887 | 1824 |
1888 bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage() { | 1825 bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage() { |
1889 content::WebContents* contents = NULL; | 1826 content::WebContents* contents = NULL; |
1890 | 1827 |
1891 // If |tab_id| is specified, look for the tab. Otherwise default to selected | 1828 // If |tab_id| is specified, look for the tab. Otherwise default to selected |
1892 // tab in the current window. | 1829 // tab in the current window. |
1893 CHECK_GE(execute_tab_id_, 0); | 1830 CHECK_GE(execute_tab_id_, 0); |
1894 if (!GetTabById(execute_tab_id_, | 1831 if (!GetTabById(execute_tab_id_, browser_context(), include_incognito(), |
1895 chrome_details_.GetProfile(), | 1832 nullptr, nullptr, &contents, nullptr, &error_)) { |
1896 include_incognito(), | |
1897 NULL, | |
1898 NULL, | |
1899 &contents, | |
1900 NULL, | |
1901 &error_)) { | |
1902 return false; | 1833 return false; |
1903 } | 1834 } |
1904 | 1835 |
1905 CHECK(contents); | 1836 CHECK(contents); |
1906 | 1837 |
1907 int frame_id = details_->frame_id ? *details_->frame_id | 1838 int frame_id = details_->frame_id ? *details_->frame_id |
1908 : ExtensionApiFrameIdMap::kTopFrameId; | 1839 : ExtensionApiFrameIdMap::kTopFrameId; |
1909 content::RenderFrameHost* rfh = | 1840 content::RenderFrameHost* rfh = |
1910 ExtensionApiFrameIdMap::GetRenderFrameHostById(contents, frame_id); | 1841 ExtensionApiFrameIdMap::GetRenderFrameHostById(contents, frame_id); |
1911 if (!rfh) { | 1842 if (!rfh) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1946 return false; | 1877 return false; |
1947 } | 1878 } |
1948 | 1879 |
1949 return true; | 1880 return true; |
1950 } | 1881 } |
1951 | 1882 |
1952 ScriptExecutor* ExecuteCodeInTabFunction::GetScriptExecutor() { | 1883 ScriptExecutor* ExecuteCodeInTabFunction::GetScriptExecutor() { |
1953 Browser* browser = NULL; | 1884 Browser* browser = NULL; |
1954 content::WebContents* contents = NULL; | 1885 content::WebContents* contents = NULL; |
1955 | 1886 |
1956 bool success = GetTabById(execute_tab_id_, | 1887 bool success = |
1957 chrome_details_.GetProfile(), | 1888 GetTabById(execute_tab_id_, browser_context(), include_incognito(), |
1958 include_incognito(), | 1889 &browser, nullptr, &contents, nullptr, &error_) && |
1959 &browser, | 1890 contents && browser; |
1960 NULL, | |
1961 &contents, | |
1962 NULL, | |
1963 &error_) && | |
1964 contents && browser; | |
1965 | 1891 |
1966 if (!success) | 1892 if (!success) |
1967 return NULL; | 1893 return NULL; |
1968 | 1894 |
1969 return TabHelper::FromWebContents(contents)->script_executor(); | 1895 return TabHelper::FromWebContents(contents)->script_executor(); |
1970 } | 1896 } |
1971 | 1897 |
1972 bool ExecuteCodeInTabFunction::IsWebView() const { | 1898 bool ExecuteCodeInTabFunction::IsWebView() const { |
1973 return false; | 1899 return false; |
1974 } | 1900 } |
(...skipping 16 matching lines...) Expand all Loading... |
1991 } | 1917 } |
1992 | 1918 |
1993 bool TabsInsertCSSFunction::ShouldInsertCSS() const { | 1919 bool TabsInsertCSSFunction::ShouldInsertCSS() const { |
1994 return true; | 1920 return true; |
1995 } | 1921 } |
1996 | 1922 |
1997 content::WebContents* ZoomAPIFunction::GetWebContents(int tab_id) { | 1923 content::WebContents* ZoomAPIFunction::GetWebContents(int tab_id) { |
1998 content::WebContents* web_contents = NULL; | 1924 content::WebContents* web_contents = NULL; |
1999 if (tab_id != -1) { | 1925 if (tab_id != -1) { |
2000 // We assume this call leaves web_contents unchanged if it is unsuccessful. | 1926 // We assume this call leaves web_contents unchanged if it is unsuccessful. |
2001 GetTabById(tab_id, | 1927 GetTabById(tab_id, browser_context(), include_incognito(), |
2002 GetProfile(), | 1928 nullptr /* ignore Browser* output */, |
2003 include_incognito(), | 1929 nullptr /* ignore TabStripModel* output */, &web_contents, |
2004 NULL /* ignore Browser* output */, | 1930 nullptr /* ignore int tab_index output */, &error_); |
2005 NULL /* ignore TabStripModel* output */, | |
2006 &web_contents, | |
2007 NULL /* ignore int tab_index output */, | |
2008 &error_); | |
2009 } else { | 1931 } else { |
2010 Browser* browser = GetCurrentBrowser(); | 1932 Browser* browser = GetCurrentBrowser(); |
2011 if (!browser) | 1933 if (!browser) |
2012 error_ = keys::kNoCurrentWindowError; | 1934 error_ = keys::kNoCurrentWindowError; |
2013 else if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, NULL)) | 1935 else if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, NULL)) |
2014 error_ = keys::kNoSelectedTabError; | 1936 error_ = keys::kNoSelectedTabError; |
2015 } | 1937 } |
2016 return web_contents; | 1938 return web_contents; |
2017 } | 1939 } |
2018 | 1940 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2140 SendResponse(true); | 2062 SendResponse(true); |
2141 return true; | 2063 return true; |
2142 } | 2064 } |
2143 | 2065 |
2144 ExtensionFunction::ResponseAction TabsDiscardFunction::Run() { | 2066 ExtensionFunction::ResponseAction TabsDiscardFunction::Run() { |
2145 std::unique_ptr<tabs::Discard::Params> params( | 2067 std::unique_ptr<tabs::Discard::Params> params( |
2146 tabs::Discard::Params::Create(*args_)); | 2068 tabs::Discard::Params::Create(*args_)); |
2147 EXTENSION_FUNCTION_VALIDATE(params); | 2069 EXTENSION_FUNCTION_VALIDATE(params); |
2148 | 2070 |
2149 WebContents* contents = nullptr; | 2071 WebContents* contents = nullptr; |
2150 Profile* profile = Profile::FromBrowserContext(browser_context()); | |
2151 // If |tab_id| is given, find the web_contents respective to it. | 2072 // If |tab_id| is given, find the web_contents respective to it. |
2152 // Otherwise invoke discard function in TabManager with null web_contents | 2073 // Otherwise invoke discard function in TabManager with null web_contents |
2153 // that will discard the least important tab. | 2074 // that will discard the least important tab. |
2154 if (params->tab_id) { | 2075 if (params->tab_id) { |
2155 int tab_id = *params->tab_id; | 2076 int tab_id = *params->tab_id; |
2156 std::string error; | 2077 std::string error; |
2157 if (!GetTabById(tab_id, profile, include_incognito(), nullptr, nullptr, | 2078 if (!GetTabById(tab_id, browser_context(), include_incognito(), nullptr, |
2158 &contents, nullptr, &error)) { | 2079 nullptr, &contents, nullptr, &error)) { |
2159 return RespondNow(Error(error)); | 2080 return RespondNow(Error(error)); |
2160 } | 2081 } |
2161 } | 2082 } |
2162 // Discard the tab. | 2083 // Discard the tab. |
2163 contents = | 2084 contents = |
2164 g_browser_process->GetTabManager()->DiscardTabByExtension(contents); | 2085 g_browser_process->GetTabManager()->DiscardTabByExtension(contents); |
2165 | 2086 |
2166 // Create the Tab object and return it in case of success. | 2087 // Create the Tab object and return it in case of success. |
2167 if (contents) { | 2088 if (contents) { |
2168 return RespondNow(ArgumentList(tabs::Discard::Results::Create( | 2089 return RespondNow(ArgumentList(tabs::Discard::Results::Create( |
2169 *ExtensionTabUtil::CreateTabObject(contents)))); | 2090 *ExtensionTabUtil::CreateTabObject(contents)))); |
2170 } | 2091 } |
2171 | 2092 |
2172 // Return appropriate error message otherwise. | 2093 // Return appropriate error message otherwise. |
2173 return RespondNow(Error( | 2094 return RespondNow(Error( |
2174 params->tab_id | 2095 params->tab_id |
2175 ? ErrorUtils::FormatErrorMessage(keys::kCannotDiscardTab, | 2096 ? ErrorUtils::FormatErrorMessage(keys::kCannotDiscardTab, |
2176 base::IntToString(*params->tab_id)) | 2097 base::IntToString(*params->tab_id)) |
2177 : keys::kCannotFindTabToDiscard)); | 2098 : keys::kCannotFindTabToDiscard)); |
2178 } | 2099 } |
2179 | 2100 |
2180 TabsDiscardFunction::TabsDiscardFunction() {} | 2101 TabsDiscardFunction::TabsDiscardFunction() {} |
2181 TabsDiscardFunction::~TabsDiscardFunction() {} | 2102 TabsDiscardFunction::~TabsDiscardFunction() {} |
2182 | 2103 |
2183 } // namespace extensions | 2104 } // namespace extensions |
OLD | NEW |