| 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 <utility> | 10 #include <utility> |
| (...skipping 1633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1644 // under such circumstances, whereas |TabStripModel::CloseWebContentsAt()| | 1644 // under such circumstances, whereas |TabStripModel::CloseWebContentsAt()| |
| 1645 // does not. | 1645 // does not. |
| 1646 contents->Close(); | 1646 contents->Close(); |
| 1647 return true; | 1647 return true; |
| 1648 } | 1648 } |
| 1649 | 1649 |
| 1650 TabsCaptureVisibleTabFunction::TabsCaptureVisibleTabFunction() | 1650 TabsCaptureVisibleTabFunction::TabsCaptureVisibleTabFunction() |
| 1651 : chrome_details_(this) { | 1651 : chrome_details_(this) { |
| 1652 } | 1652 } |
| 1653 | 1653 |
| 1654 bool TabsCaptureVisibleTabFunction::HasPermission() { |
| 1655 return true; |
| 1656 } |
| 1657 |
| 1654 bool TabsCaptureVisibleTabFunction::IsScreenshotEnabled() { | 1658 bool TabsCaptureVisibleTabFunction::IsScreenshotEnabled() { |
| 1655 PrefService* service = chrome_details_.GetProfile()->GetPrefs(); | 1659 PrefService* service = chrome_details_.GetProfile()->GetPrefs(); |
| 1656 if (service->GetBoolean(prefs::kDisableScreenshots)) { | 1660 if (service->GetBoolean(prefs::kDisableScreenshots)) { |
| 1657 error_ = keys::kScreenshotsDisabled; | 1661 error_ = keys::kScreenshotsDisabled; |
| 1658 return false; | 1662 return false; |
| 1659 } | 1663 } |
| 1660 return true; | 1664 return true; |
| 1661 } | 1665 } |
| 1662 | 1666 |
| 1663 WebContents* TabsCaptureVisibleTabFunction::GetWebContentsForID(int window_id) { | 1667 WebContents* TabsCaptureVisibleTabFunction::GetWebContentsForID(int window_id) { |
| 1664 Browser* browser = NULL; | 1668 Browser* browser = NULL; |
| 1665 if (!GetBrowserFromWindowID(&chrome_details_, window_id, &browser)) | 1669 if (!GetBrowserFromWindowID(&chrome_details_, window_id, &browser)) |
| 1666 return NULL; | 1670 return NULL; |
| 1667 | 1671 |
| 1668 WebContents* contents = browser->tab_strip_model()->GetActiveWebContents(); | 1672 WebContents* contents = browser->tab_strip_model()->GetActiveWebContents(); |
| 1669 if (!contents) { | 1673 if (!contents) { |
| 1670 error_ = "No active web contents to capture"; | 1674 error_ = "No active web contents to capture"; |
| 1671 return NULL; | 1675 return NULL; |
| 1672 } | 1676 } |
| 1673 | 1677 |
| 1674 if (!extension()->permissions_data()->CanCaptureVisiblePage( | 1678 if (!extension()->permissions_data()->CanCaptureVisiblePage( |
| 1675 SessionTabHelper::IdForTab(contents), &error_)) { | 1679 SessionTabHelper::IdForTab(contents), &error_)) { |
| 1676 return NULL; | 1680 return NULL; |
| 1677 } | 1681 } |
| 1678 return contents; | 1682 return contents; |
| 1679 } | 1683 } |
| 1680 | 1684 |
| 1685 bool TabsCaptureVisibleTabFunction::RunAsync() { |
| 1686 using api::extension_types::ImageDetails; |
| 1687 |
| 1688 EXTENSION_FUNCTION_VALIDATE(args_); |
| 1689 |
| 1690 int context_id = extension_misc::kCurrentWindowId; |
| 1691 args_->GetInteger(0, &context_id); |
| 1692 |
| 1693 scoped_ptr<ImageDetails> image_details; |
| 1694 if (args_->GetSize() > 1) { |
| 1695 base::Value* spec = NULL; |
| 1696 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &spec) && spec); |
| 1697 image_details = ImageDetails::FromValue(*spec); |
| 1698 } |
| 1699 |
| 1700 WebContents* contents = GetWebContentsForID(context_id); |
| 1701 |
| 1702 return CaptureAsync( |
| 1703 contents, image_details.get(), |
| 1704 base::Bind(&TabsCaptureVisibleTabFunction::CopyFromBackingStoreComplete, |
| 1705 this)); |
| 1706 } |
| 1707 |
| 1708 void TabsCaptureVisibleTabFunction::OnCaptureSuccess(const SkBitmap& bitmap) { |
| 1709 std::string base64_result; |
| 1710 if (!EncodeBitmap(bitmap, &base64_result)) { |
| 1711 OnCaptureFailure(FAILURE_REASON_ENCODING_FAILED); |
| 1712 return; |
| 1713 } |
| 1714 |
| 1715 SetResult(new base::StringValue(base64_result)); |
| 1716 SendResponse(true); |
| 1717 } |
| 1718 |
| 1681 void TabsCaptureVisibleTabFunction::OnCaptureFailure(FailureReason reason) { | 1719 void TabsCaptureVisibleTabFunction::OnCaptureFailure(FailureReason reason) { |
| 1682 const char* reason_description = "internal error"; | 1720 const char* reason_description = "internal error"; |
| 1683 switch (reason) { | 1721 switch (reason) { |
| 1684 case FAILURE_REASON_UNKNOWN: | 1722 case FAILURE_REASON_UNKNOWN: |
| 1685 reason_description = "unknown error"; | 1723 reason_description = "unknown error"; |
| 1686 break; | 1724 break; |
| 1687 case FAILURE_REASON_ENCODING_FAILED: | 1725 case FAILURE_REASON_ENCODING_FAILED: |
| 1688 reason_description = "encoding failed"; | 1726 reason_description = "encoding failed"; |
| 1689 break; | 1727 break; |
| 1690 case FAILURE_REASON_VIEW_INVISIBLE: | 1728 case FAILURE_REASON_VIEW_INVISIBLE: |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2065 ZoomModeToZoomSettings(zoom_mode, &zoom_settings); | 2103 ZoomModeToZoomSettings(zoom_mode, &zoom_settings); |
| 2066 zoom_settings.default_zoom_factor.reset(new double( | 2104 zoom_settings.default_zoom_factor.reset(new double( |
| 2067 content::ZoomLevelToZoomFactor(zoom_controller->GetDefaultZoomLevel()))); | 2105 content::ZoomLevelToZoomFactor(zoom_controller->GetDefaultZoomLevel()))); |
| 2068 | 2106 |
| 2069 results_ = api::tabs::GetZoomSettings::Results::Create(zoom_settings); | 2107 results_ = api::tabs::GetZoomSettings::Results::Create(zoom_settings); |
| 2070 SendResponse(true); | 2108 SendResponse(true); |
| 2071 return true; | 2109 return true; |
| 2072 } | 2110 } |
| 2073 | 2111 |
| 2074 } // namespace extensions | 2112 } // namespace extensions |
| OLD | NEW |