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