| 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/gfx/jpeg_codec.h" |
| 7 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 8 #include "chrome/browser/browser.h" | 9 #include "chrome/browser/browser.h" |
| 9 #include "chrome/browser/browser_list.h" | 10 #include "chrome/browser/browser_list.h" |
| 10 #include "chrome/browser/browser_window.h" | 11 #include "chrome/browser/browser_window.h" |
| 11 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 12 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| 12 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 13 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
| 13 #include "chrome/browser/extensions/extensions_service.h" | 14 #include "chrome/browser/extensions/extensions_service.h" |
| 14 #include "chrome/browser/profile.h" | 15 #include "chrome/browser/profile.h" |
| 16 #include "chrome/browser/renderer_host/backing_store.h" |
| 17 #include "chrome/browser/renderer_host/render_view_host.h" |
| 15 #include "chrome/browser/renderer_host/render_view_host_delegate.h" | 18 #include "chrome/browser/renderer_host/render_view_host_delegate.h" |
| 16 #include "chrome/browser/tab_contents/navigation_entry.h" | 19 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 17 #include "chrome/browser/tab_contents/tab_contents.h" | 20 #include "chrome/browser/tab_contents/tab_contents.h" |
| 18 #include "chrome/browser/window_sizer.h" | 21 #include "chrome/browser/window_sizer.h" |
| 19 #include "chrome/common/extensions/extension.h" | 22 #include "chrome/common/extensions/extension.h" |
| 20 #include "chrome/common/extensions/extension_error_utils.h" | 23 #include "chrome/common/extensions/extension_error_utils.h" |
| 24 #include "net/base/base64.h" |
| 25 #include "skia/ext/image_operations.h" |
| 26 #include "skia/ext/platform_canvas.h" |
| 27 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 | 28 |
| 22 | 29 |
| 23 namespace keys = extension_tabs_module_constants; | 30 namespace keys = extension_tabs_module_constants; |
| 24 | 31 |
| 25 // Forward declare static helper functions defined below. | 32 // Forward declare static helper functions defined below. |
| 26 static DictionaryValue* CreateWindowValue(Browser* browser, bool populate_tabs); | 33 static DictionaryValue* CreateWindowValue(Browser* browser, bool populate_tabs); |
| 27 static ListValue* CreateTabList(Browser* browser); | 34 static ListValue* CreateTabList(Browser* browser); |
| 28 | 35 |
| 29 // |error_message| can optionally be passed in a will be set with an appropriate | 36 // |error_message| can optionally be passed in a will be set with an appropriate |
| 30 // message if the window cannot be found by id. | 37 // message if the window cannot be found by id. |
| (...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 | 615 |
| 609 Browser* browser = NULL; | 616 Browser* browser = NULL; |
| 610 TabContents* contents = NULL; | 617 TabContents* contents = NULL; |
| 611 if (!GetTabById(tab_id, profile(), &browser, NULL, &contents, NULL, &error_)) | 618 if (!GetTabById(tab_id, profile(), &browser, NULL, &contents, NULL, &error_)) |
| 612 return false; | 619 return false; |
| 613 | 620 |
| 614 browser->CloseTabContents(contents); | 621 browser->CloseTabContents(contents); |
| 615 return true; | 622 return true; |
| 616 } | 623 } |
| 617 | 624 |
| 625 bool CaptureVisibleTabFunction::RunImpl() { |
| 626 Browser* browser; |
| 627 // windowId defaults to "current" window. |
| 628 int window_id = -1; |
| 629 |
| 630 if (!args_->IsType(Value::TYPE_NULL)) { |
| 631 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id)); |
| 632 browser = GetBrowserInProfileWithId(profile(), window_id, &error_); |
| 633 } else { |
| 634 browser = dispatcher()->GetBrowser(); |
| 635 } |
| 636 |
| 637 if (!browser) { |
| 638 error_ = keys::kNoCurrentWindowError; |
| 639 return false; |
| 640 } |
| 641 |
| 642 SkBitmap screen_capture; |
| 643 #if defined(OS_WIN) |
| 644 TabContents* tab_contents = browser->GetSelectedTabContents(); |
| 645 if (!tab_contents) { |
| 646 error_ = keys::kInternalVisibleTabCaptureError; |
| 647 return false; |
| 648 } |
| 649 RenderViewHost* render_view_host = tab_contents->render_view_host(); |
| 650 BackingStore* backing_store = render_view_host->GetBackingStore(false); |
| 651 if (!backing_store) { |
| 652 error_ = keys::kInternalVisibleTabCaptureError; |
| 653 return false; |
| 654 } |
| 655 skia::PlatformCanvas temp_canvas; |
| 656 if (!temp_canvas.initialize(backing_store->size().width(), |
| 657 backing_store->size().height(), true)) { |
| 658 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 659 keys::kInternalVisibleTabCaptureError, ""); |
| 660 return false; |
| 661 } |
| 662 HDC temp_dc = temp_canvas.beginPlatformPaint(); |
| 663 BitBlt(temp_dc, |
| 664 0, 0, backing_store->size().width(), backing_store->size().height(), |
| 665 backing_store->hdc(), 0, 0, SRCCOPY); |
| 666 temp_canvas.endPlatformPaint(); |
| 667 |
| 668 screen_capture = temp_canvas.getTopPlatformDevice().accessBitmap(false); |
| 669 #else |
| 670 // TODO(port) |
| 671 error_ = keys::kNotImplementedError; |
| 672 return false; |
| 673 #endif |
| 674 scoped_refptr<RefCountedBytes> jpeg_data(new RefCountedBytes); |
| 675 SkAutoLockPixels screen_capture_lock(screen_capture); |
| 676 bool encoded = JPEGCodec::Encode( |
| 677 reinterpret_cast<unsigned char*>(screen_capture.getAddr32(0, 0)), |
| 678 JPEGCodec::FORMAT_BGRA, screen_capture.width(), |
| 679 screen_capture.height(), |
| 680 static_cast<int>(screen_capture.rowBytes()), 90, |
| 681 &jpeg_data->data); |
| 682 if (!encoded) { |
| 683 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 684 keys::kInternalVisibleTabCaptureError, ""); |
| 685 return false; |
| 686 } |
| 687 |
| 688 std::string base64_result; |
| 689 std::string stream_as_string; |
| 690 stream_as_string.resize(jpeg_data->data.size()); |
| 691 memcpy(&stream_as_string[0], |
| 692 reinterpret_cast<const char*>(&jpeg_data->data[0]), |
| 693 jpeg_data->data.size()); |
| 694 |
| 695 net::Base64Encode(stream_as_string, &base64_result); |
| 696 base64_result.insert(0, "data:image/jpg;base64,"); |
| 697 result_.reset(new StringValue(base64_result)); |
| 698 return true; |
| 699 } |
| 700 |
| 618 bool DetectTabLanguageFunction::RunImpl() { | 701 bool DetectTabLanguageFunction::RunImpl() { |
| 619 int tab_id = 0; | 702 int tab_id = 0; |
| 620 Browser* browser = NULL; | 703 Browser* browser = NULL; |
| 621 TabContents* contents = NULL; | 704 TabContents* contents = NULL; |
| 622 | 705 |
| 623 // If |tab_id| is specified, look for it. Otherwise default to selected tab | 706 // If |tab_id| is specified, look for it. Otherwise default to selected tab |
| 624 // in the current window. | 707 // in the current window. |
| 625 if (!args_->IsType(Value::TYPE_NULL)) { | 708 if (!args_->IsType(Value::TYPE_NULL)) { |
| 626 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id)); | 709 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id)); |
| 627 if (!GetTabById(tab_id, profile(), &browser, NULL, &contents, NULL, | 710 if (!GetTabById(tab_id, profile(), &browser, NULL, &contents, NULL, |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 730 if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip, | 813 if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip, |
| 731 contents, tab_index)) | 814 contents, tab_index)) |
| 732 return true; | 815 return true; |
| 733 | 816 |
| 734 if (error_message) | 817 if (error_message) |
| 735 *error_message = ExtensionErrorUtils::FormatErrorMessage( | 818 *error_message = ExtensionErrorUtils::FormatErrorMessage( |
| 736 keys::kTabNotFoundError, IntToString(tab_id)); | 819 keys::kTabNotFoundError, IntToString(tab_id)); |
| 737 | 820 |
| 738 return false; | 821 return false; |
| 739 } | 822 } |
| OLD | NEW |