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 GetVisibleTabCaptureFunction::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 RenderViewHost* render_view_host = tab_contents->render_view_host(); | |
646 BackingStore* backing_store = render_view_host->GetBackingStore(true); | |
647 skia::PlatformCanvas temp_canvas; | |
648 if (!temp_canvas.initialize(backing_store->size().width(), | |
649 backing_store->size().height(), true)) { | |
650 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
651 keys::kInternalVisibleTabCaptureError, ""); | |
652 return false; | |
653 } | |
654 HDC temp_dc = temp_canvas.beginPlatformPaint(); | |
655 BitBlt(temp_dc, | |
656 0, 0, backing_store->size().width(), backing_store->size().height(), | |
657 backing_store->hdc(), 0, 0, SRCCOPY); | |
658 temp_canvas.endPlatformPaint(); | |
659 | |
660 screen_capture = temp_canvas.getTopPlatformDevice().accessBitmap(false); | |
661 #else | |
662 // TODO(adamhunter): TODO(port): Write this for other platforms. | |
Aaron Boodman
2009/07/30 18:15:12
What about the other platforms? What is the timeli
| |
663 NOTIMPLEMENTED(); | |
Aaron Boodman
2009/07/30 18:15:12
Also, I forget what the behavior of NOTIMPLEMENTED
| |
664 #endif | |
665 scoped_refptr<RefCountedBytes> jpeg_data(new RefCountedBytes); | |
666 SkAutoLockPixels screen_capture_lock(screen_capture); | |
667 bool encoded = JPEGCodec::Encode( | |
668 reinterpret_cast<unsigned char*>(screen_capture.getAddr32(0, 0)), | |
669 JPEGCodec::FORMAT_BGRA, screen_capture.width(), | |
670 screen_capture.height(), | |
671 static_cast<int>(screen_capture.rowBytes()), 90, | |
672 &jpeg_data->data); | |
673 if (!encoded) { | |
674 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
675 keys::kInternalVisibleTabCaptureError, ""); | |
676 return false; | |
677 } | |
678 | |
679 std::string base64_result; | |
680 std::string stream_as_string; | |
681 stream_as_string.resize(jpeg_data->data.size()); | |
682 memcpy(&stream_as_string[0], | |
683 reinterpret_cast<const char*>(&jpeg_data->data[0]), | |
684 jpeg_data->data.size()); | |
685 | |
686 net::Base64Encode(stream_as_string, &base64_result); | |
687 base64_result.insert(0, "data:image/jpg;base64,"); | |
688 result_.reset(new StringValue(base64_result)); | |
689 return true; | |
690 } | |
691 | |
618 bool GetTabLanguageFunction::RunImpl() { | 692 bool GetTabLanguageFunction::RunImpl() { |
619 int tab_id = 0; | 693 int tab_id = 0; |
620 Browser* browser = NULL; | 694 Browser* browser = NULL; |
621 TabContents* contents = NULL; | 695 TabContents* contents = NULL; |
622 | 696 |
623 // If |tab_id| is specified, look for it. Otherwise default to selected tab | 697 // If |tab_id| is specified, look for it. Otherwise default to selected tab |
624 // in the current window. | 698 // in the current window. |
625 if (!args_->IsType(Value::TYPE_NULL)) { | 699 if (!args_->IsType(Value::TYPE_NULL)) { |
626 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id)); | 700 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id)); |
627 if (!GetTabById(tab_id, profile(), &browser, NULL, &contents, NULL, | 701 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, | 804 if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip, |
731 contents, tab_index)) | 805 contents, tab_index)) |
732 return true; | 806 return true; |
733 | 807 |
734 if (error_message) | 808 if (error_message) |
735 *error_message = ExtensionErrorUtils::FormatErrorMessage( | 809 *error_message = ExtensionErrorUtils::FormatErrorMessage( |
736 keys::kTabNotFoundError, IntToString(tab_id)); | 810 keys::kTabNotFoundError, IntToString(tab_id)); |
737 | 811 |
738 return false; | 812 return false; |
739 } | 813 } |
OLD | NEW |