Chromium Code Reviews| 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/extension_tabs_module.h" | 5 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 #include "chrome/common/extensions/extension_messages.h" | 50 #include "chrome/common/extensions/extension_messages.h" |
| 51 #include "chrome/common/extensions/user_script.h" | 51 #include "chrome/common/extensions/user_script.h" |
| 52 #include "chrome/common/pref_names.h" | 52 #include "chrome/common/pref_names.h" |
| 53 #include "chrome/common/url_constants.h" | 53 #include "chrome/common/url_constants.h" |
| 54 #include "content/public/browser/navigation_controller.h" | 54 #include "content/public/browser/navigation_controller.h" |
| 55 #include "content/public/browser/navigation_entry.h" | 55 #include "content/public/browser/navigation_entry.h" |
| 56 #include "content/public/browser/notification_details.h" | 56 #include "content/public/browser/notification_details.h" |
| 57 #include "content/public/browser/notification_source.h" | 57 #include "content/public/browser/notification_source.h" |
| 58 #include "content/public/browser/render_view_host.h" | 58 #include "content/public/browser/render_view_host.h" |
| 59 #include "content/public/browser/render_view_host_delegate.h" | 59 #include "content/public/browser/render_view_host_delegate.h" |
| 60 #include "content/public/browser/render_widget_host_view.h" | |
| 60 #include "content/public/browser/web_contents.h" | 61 #include "content/public/browser/web_contents.h" |
| 61 #include "content/public/browser/web_contents_view.h" | 62 #include "content/public/browser/web_contents_view.h" |
| 62 #include "content/public/common/url_constants.h" | 63 #include "content/public/common/url_constants.h" |
| 63 #include "skia/ext/image_operations.h" | 64 #include "skia/ext/image_operations.h" |
| 64 #include "skia/ext/platform_canvas.h" | 65 #include "skia/ext/platform_canvas.h" |
| 65 #include "third_party/skia/include/core/SkBitmap.h" | 66 #include "third_party/skia/include/core/SkBitmap.h" |
| 66 #include "ui/base/ui_base_types.h" | 67 #include "ui/base/ui_base_types.h" |
| 67 #include "ui/gfx/codec/jpeg_codec.h" | 68 #include "ui/gfx/codec/jpeg_codec.h" |
| 68 #include "ui/gfx/codec/png_codec.h" | 69 #include "ui/gfx/codec/png_codec.h" |
| 69 | 70 |
| (...skipping 1585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1655 } | 1656 } |
| 1656 } | 1657 } |
| 1657 | 1658 |
| 1658 // captureVisibleTab() can return an image containing sensitive information | 1659 // captureVisibleTab() can return an image containing sensitive information |
| 1659 // that the browser would otherwise protect. Ensure the extension has | 1660 // that the browser would otherwise protect. Ensure the extension has |
| 1660 // permission to do this. | 1661 // permission to do this. |
| 1661 if (!GetExtension()->CanCaptureVisiblePage(web_contents->GetURL(), &error_)) | 1662 if (!GetExtension()->CanCaptureVisiblePage(web_contents->GetURL(), &error_)) |
| 1662 return false; | 1663 return false; |
| 1663 | 1664 |
| 1664 RenderViewHost* render_view_host = web_contents->GetRenderViewHost(); | 1665 RenderViewHost* render_view_host = web_contents->GetRenderViewHost(); |
| 1665 | 1666 content::RenderWidgetHostView* view = render_view_host->GetView(); |
| 1666 // If a backing store is cached for the tab we want to capture, | 1667 if (!view) |
| 1667 // and it can be copied into a bitmap, then use it to generate the image. | 1668 return false; |
| 1668 // For example, some uncommon X11 visual modes are not supported by | 1669 skia::PlatformCanvas* temp_canvas = new skia::PlatformCanvas; |
| 1669 // CopyFromBackingStore(). | 1670 render_view_host->AsyncCopyFromBackingStore( |
| 1670 skia::PlatformCanvas temp_canvas; | 1671 gfx::Rect(), |
| 1671 if (render_view_host->CopyFromBackingStore( | 1672 view->GetViewBounds().size(), |
| 1672 gfx::Rect(), gfx::Size(), &temp_canvas)) { | 1673 temp_canvas, |
| 1673 VLOG(1) << "captureVisibleTab() got image from backing store."; | 1674 base::Bind(&CaptureVisibleTabFunction::CopyFromBackingStoreComplete, |
| 1674 SendResultFromBitmap(skia::GetTopDevice(temp_canvas)->accessBitmap(false)); | 1675 this, |
| 1675 return true; | 1676 base::Owned(temp_canvas))); |
| 1676 } | |
| 1677 | |
| 1678 // Ask the renderer for a snapshot of the tab. | |
| 1679 wrapper->snapshot_tab_helper()->CaptureSnapshot(); | |
|
mazda
2012/05/02 01:23:25
This line looks redundant and I copied the code wi
Sam Kerner (Chrome)
2012/05/02 03:26:09
It looks redundant to me, but the code has changed
| |
| 1680 registrar_.Add(this, | |
| 1681 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, | |
| 1682 content::Source<WebContents>(wrapper->web_contents())); | |
| 1683 AddRef(); // Balanced in CaptureVisibleTabFunction::Observe(). | |
| 1684 wrapper->snapshot_tab_helper()->CaptureSnapshot(); | |
| 1685 | |
| 1686 return true; | 1677 return true; |
| 1687 } | 1678 } |
| 1688 | 1679 |
| 1689 // If a backing store was not available in CaptureVisibleTabFunction::RunImpl, | 1680 // If a backing store was not available in CaptureVisibleTabFunction::RunImpl, |
| 1690 // than the renderer was asked for a snapshot. Listen for a notification | 1681 // than the renderer was asked for a snapshot. Listen for a notification |
| 1691 // that the snapshot is available. | 1682 // that the snapshot is available. |
| 1692 void CaptureVisibleTabFunction::Observe( | 1683 void CaptureVisibleTabFunction::Observe( |
| 1693 int type, | 1684 int type, |
| 1694 const content::NotificationSource& source, | 1685 const content::NotificationSource& source, |
| 1695 const content::NotificationDetails& details) { | 1686 const content::NotificationDetails& details) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1751 base::StringPiece stream_as_string( | 1742 base::StringPiece stream_as_string( |
| 1752 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); | 1743 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); |
| 1753 | 1744 |
| 1754 base::Base64Encode(stream_as_string, &base64_result); | 1745 base::Base64Encode(stream_as_string, &base64_result); |
| 1755 base64_result.insert(0, base::StringPrintf("data:%s;base64,", | 1746 base64_result.insert(0, base::StringPrintf("data:%s;base64,", |
| 1756 mime_type.c_str())); | 1747 mime_type.c_str())); |
| 1757 result_.reset(new StringValue(base64_result)); | 1748 result_.reset(new StringValue(base64_result)); |
| 1758 SendResponse(true); | 1749 SendResponse(true); |
| 1759 } | 1750 } |
| 1760 | 1751 |
| 1752 void CaptureVisibleTabFunction::CopyFromBackingStoreComplete( | |
|
Sam Kerner (Chrome)
2012/05/02 03:47:14
In this file, the functions are in the same order
mazda
2012/05/02 06:07:39
Done.
| |
| 1753 skia::PlatformCanvas* canvas, | |
| 1754 bool succeeded) { | |
| 1755 if (succeeded) { | |
| 1756 VLOG(1) << "captureVisibleTab() got image from backing store."; | |
| 1757 SendResultFromBitmap(skia::GetTopDevice(*canvas)->accessBitmap(false)); | |
| 1758 return; | |
| 1759 } | |
| 1760 | |
| 1761 // TODO(mazda): Remove the following code once AsyncCopyFromBackingStore is | |
|
Sam Kerner (Chrome)
2012/05/02 03:47:14
It has been two years since I looked at this in de
mazda
2012/05/02 06:07:39
Thanks for the useful comment.
I think your commen
| |
| 1762 // supported on all platforms. | |
| 1763 WebContents* web_contents = NULL; | |
| 1764 TabContentsWrapper* wrapper = NULL; | |
| 1765 if (!GetTabToCapture(&web_contents, &wrapper)) { | |
| 1766 error_ = keys::kInternalVisibleTabCaptureError; | |
| 1767 return; | |
| 1768 } | |
| 1769 | |
| 1770 // Ask the renderer for a snapshot of the tab. | |
| 1771 registrar_.Add(this, | |
| 1772 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, | |
| 1773 content::Source<WebContents>(web_contents)); | |
| 1774 AddRef(); // Balanced in CaptureVisibleTabFunction::Observe(). | |
| 1775 wrapper->snapshot_tab_helper()->CaptureSnapshot(); | |
| 1776 return; | |
| 1777 } | |
| 1778 | |
| 1761 bool DetectTabLanguageFunction::RunImpl() { | 1779 bool DetectTabLanguageFunction::RunImpl() { |
| 1762 int tab_id = 0; | 1780 int tab_id = 0; |
| 1763 Browser* browser = NULL; | 1781 Browser* browser = NULL; |
| 1764 TabContentsWrapper* contents = NULL; | 1782 TabContentsWrapper* contents = NULL; |
| 1765 | 1783 |
| 1766 // If |tab_id| is specified, look for it. Otherwise default to selected tab | 1784 // If |tab_id| is specified, look for it. Otherwise default to selected tab |
| 1767 // in the current window. | 1785 // in the current window. |
| 1768 if (HasOptionalArgument(0)) { | 1786 if (HasOptionalArgument(0)) { |
| 1769 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | 1787 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); |
| 1770 if (!GetTabById(tab_id, profile(), include_incognito(), | 1788 if (!GetTabById(tab_id, profile(), include_incognito(), |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1828 // called for every API call the extension made. | 1846 // called for every API call the extension made. |
| 1829 GotLanguage(language); | 1847 GotLanguage(language); |
| 1830 } | 1848 } |
| 1831 | 1849 |
| 1832 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { | 1850 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { |
| 1833 result_.reset(Value::CreateStringValue(language.c_str())); | 1851 result_.reset(Value::CreateStringValue(language.c_str())); |
| 1834 SendResponse(true); | 1852 SendResponse(true); |
| 1835 | 1853 |
| 1836 Release(); // Balanced in Run() | 1854 Release(); // Balanced in Run() |
| 1837 } | 1855 } |
| OLD | NEW |