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