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 <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 #include "chrome/browser/ui/browser_commands.h" | 37 #include "chrome/browser/ui/browser_commands.h" |
38 #include "chrome/browser/ui/browser_finder.h" | 38 #include "chrome/browser/ui/browser_finder.h" |
39 #include "chrome/browser/ui/browser_iterator.h" | 39 #include "chrome/browser/ui/browser_iterator.h" |
40 #include "chrome/browser/ui/browser_navigator.h" | 40 #include "chrome/browser/ui/browser_navigator.h" |
41 #include "chrome/browser/ui/browser_tabstrip.h" | 41 #include "chrome/browser/ui/browser_tabstrip.h" |
42 #include "chrome/browser/ui/browser_window.h" | 42 #include "chrome/browser/ui/browser_window.h" |
43 #include "chrome/browser/ui/host_desktop.h" | 43 #include "chrome/browser/ui/host_desktop.h" |
44 #include "chrome/browser/ui/panels/panel_manager.h" | 44 #include "chrome/browser/ui/panels/panel_manager.h" |
45 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 45 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
46 #include "chrome/browser/ui/window_sizer/window_sizer.h" | 46 #include "chrome/browser/ui/window_sizer/window_sizer.h" |
| 47 #include "chrome/browser/ui/zoom/zoom_controller.h" |
47 #include "chrome/browser/web_applications/web_app.h" | 48 #include "chrome/browser/web_applications/web_app.h" |
48 #include "chrome/common/chrome_switches.h" | 49 #include "chrome/common/chrome_switches.h" |
49 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" | 50 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" |
50 #include "chrome/common/extensions/api/tabs.h" | 51 #include "chrome/common/extensions/api/tabs.h" |
51 #include "chrome/common/extensions/api/windows.h" | 52 #include "chrome/common/extensions/api/windows.h" |
52 #include "chrome/common/extensions/extension_constants.h" | 53 #include "chrome/common/extensions/extension_constants.h" |
53 #include "chrome/common/pref_names.h" | 54 #include "chrome/common/pref_names.h" |
54 #include "chrome/common/url_constants.h" | 55 #include "chrome/common/url_constants.h" |
55 #include "components/pref_registry/pref_registry_syncable.h" | 56 #include "components/pref_registry/pref_registry_syncable.h" |
56 #include "components/translate/core/browser/language_state.h" | 57 #include "components/translate/core/browser/language_state.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 template <typename T> | 157 template <typename T> |
157 void AssignOptionalValue(const scoped_ptr<T>& source, | 158 void AssignOptionalValue(const scoped_ptr<T>& source, |
158 scoped_ptr<T>& destination) { | 159 scoped_ptr<T>& destination) { |
159 if (source.get()) { | 160 if (source.get()) { |
160 destination.reset(new T(*source.get())); | 161 destination.reset(new T(*source.get())); |
161 } | 162 } |
162 } | 163 } |
163 | 164 |
164 } // namespace | 165 } // namespace |
165 | 166 |
| 167 void ZoomModeToZoomSettings(ZoomController::ZoomMode zoom_mode, |
| 168 api::tabs::ZoomSettings* zoom_settings) { |
| 169 DCHECK(zoom_settings); |
| 170 switch (zoom_mode) { |
| 171 case ZoomController::ZOOM_MODE_DEFAULT: |
| 172 zoom_settings->mode = api::tabs::ZoomSettings::MODE_AUTOMATIC; |
| 173 zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_ORIGIN; |
| 174 break; |
| 175 case ZoomController::ZOOM_MODE_ISOLATED: |
| 176 zoom_settings->mode = api::tabs::ZoomSettings::MODE_AUTOMATIC; |
| 177 zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_TAB; |
| 178 break; |
| 179 case ZoomController::ZOOM_MODE_MANUAL: |
| 180 zoom_settings->mode = api::tabs::ZoomSettings::MODE_MANUAL; |
| 181 zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_TAB; |
| 182 break; |
| 183 case ZoomController::ZOOM_MODE_DISABLED: |
| 184 zoom_settings->mode = api::tabs::ZoomSettings::MODE_DISABLED; |
| 185 zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_TAB; |
| 186 break; |
| 187 } |
| 188 } |
| 189 |
166 // Windows --------------------------------------------------------------------- | 190 // Windows --------------------------------------------------------------------- |
167 | 191 |
168 bool WindowsGetFunction::RunSync() { | 192 bool WindowsGetFunction::RunSync() { |
169 scoped_ptr<windows::Get::Params> params(windows::Get::Params::Create(*args_)); | 193 scoped_ptr<windows::Get::Params> params(windows::Get::Params::Create(*args_)); |
170 EXTENSION_FUNCTION_VALIDATE(params.get()); | 194 EXTENSION_FUNCTION_VALIDATE(params.get()); |
171 | 195 |
172 bool populate_tabs = false; | 196 bool populate_tabs = false; |
173 if (params->get_info.get() && params->get_info->populate.get()) | 197 if (params->get_info.get() && params->get_info->populate.get()) |
174 populate_tabs = *params->get_info->populate; | 198 populate_tabs = *params->get_info->populate; |
175 | 199 |
(...skipping 1233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1409 int tab_id = *params->tab_id; | 1433 int tab_id = *params->tab_id; |
1410 | 1434 |
1411 Browser* browser = NULL; | 1435 Browser* browser = NULL; |
1412 if (!GetTabById(tab_id, | 1436 if (!GetTabById(tab_id, |
1413 GetProfile(), | 1437 GetProfile(), |
1414 include_incognito(), | 1438 include_incognito(), |
1415 &browser, | 1439 &browser, |
1416 NULL, | 1440 NULL, |
1417 &web_contents, | 1441 &web_contents, |
1418 NULL, | 1442 NULL, |
1419 &error_)) | 1443 &error_)) { |
1420 return false; | 1444 return false; |
| 1445 } |
1421 } | 1446 } |
1422 | 1447 |
1423 if (web_contents->ShowingInterstitialPage()) { | 1448 if (web_contents->ShowingInterstitialPage()) { |
1424 // This does as same as Browser::ReloadInternal. | 1449 // This does as same as Browser::ReloadInternal. |
1425 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); | 1450 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); |
1426 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB, | 1451 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB, |
1427 content::PAGE_TRANSITION_RELOAD, false); | 1452 content::PAGE_TRANSITION_RELOAD, false); |
1428 GetCurrentBrowser()->OpenURL(params); | 1453 GetCurrentBrowser()->OpenURL(params); |
1429 } else if (bypass_cache) { | 1454 } else if (bypass_cache) { |
1430 web_contents->GetController().ReloadIgnoringCache(true); | 1455 web_contents->GetController().ReloadIgnoringCache(true); |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1736 | 1761 |
1737 execute_tab_id_ = tab_id; | 1762 execute_tab_id_ = tab_id; |
1738 details_ = details.Pass(); | 1763 details_ = details.Pass(); |
1739 return true; | 1764 return true; |
1740 } | 1765 } |
1741 | 1766 |
1742 bool TabsInsertCSSFunction::ShouldInsertCSS() const { | 1767 bool TabsInsertCSSFunction::ShouldInsertCSS() const { |
1743 return true; | 1768 return true; |
1744 } | 1769 } |
1745 | 1770 |
| 1771 content::WebContents* ZoomAPIFunction::GetWebContents(int tab_id) { |
| 1772 content::WebContents* web_contents = NULL; |
| 1773 if (tab_id != -1) { |
| 1774 // We assume this call leaves web_contents unchanged if it is unsuccessful. |
| 1775 GetTabById(tab_id, |
| 1776 GetProfile(), |
| 1777 include_incognito(), |
| 1778 NULL /* ignore Browser* output */, |
| 1779 NULL /* ignore TabStripModel* output */, |
| 1780 &web_contents, |
| 1781 NULL /* ignore int tab_index output */, |
| 1782 &error_); |
| 1783 } else { |
| 1784 Browser* browser = GetCurrentBrowser(); |
| 1785 if (!browser) |
| 1786 error_ = keys::kNoCurrentWindowError; |
| 1787 else if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, NULL)) |
| 1788 error_ = keys::kNoSelectedTabError; |
| 1789 } |
| 1790 return web_contents; |
| 1791 } |
| 1792 |
| 1793 bool TabsSetZoomFunction::RunAsync() { |
| 1794 scoped_ptr<tabs::SetZoom::Params> params( |
| 1795 tabs::SetZoom::Params::Create(*args_)); |
| 1796 EXTENSION_FUNCTION_VALIDATE(params); |
| 1797 |
| 1798 int tab_id = params->tab_id ? *params->tab_id : -1; |
| 1799 WebContents* web_contents = GetWebContents(tab_id); |
| 1800 if (!web_contents) |
| 1801 return false; |
| 1802 |
| 1803 GURL url(web_contents->GetVisibleURL()); |
| 1804 if (PermissionsData::IsRestrictedUrl(url, url, GetExtension(), &error_)) |
| 1805 return false; |
| 1806 |
| 1807 ZoomController* zoom_controller = |
| 1808 ZoomController::FromWebContents(web_contents); |
| 1809 double zoom_level = content::ZoomFactorToZoomLevel(params->zoom_factor); |
| 1810 |
| 1811 if (!zoom_controller->SetZoomLevelByExtension(zoom_level, GetExtension())) { |
| 1812 // Tried to zoom a tab in disabled mode. |
| 1813 error_ = keys::kCannotZoomDisabledTabError; |
| 1814 return false; |
| 1815 } |
| 1816 |
| 1817 SendResponse(true); |
| 1818 return true; |
| 1819 } |
| 1820 |
| 1821 bool TabsGetZoomFunction::RunAsync() { |
| 1822 scoped_ptr<tabs::GetZoom::Params> params( |
| 1823 tabs::GetZoom::Params::Create(*args_)); |
| 1824 EXTENSION_FUNCTION_VALIDATE(params); |
| 1825 |
| 1826 int tab_id = params->tab_id ? *params->tab_id : -1; |
| 1827 WebContents* web_contents = GetWebContents(tab_id); |
| 1828 if (!web_contents) |
| 1829 return false; |
| 1830 |
| 1831 double zoom_level = |
| 1832 ZoomController::FromWebContents(web_contents)->GetZoomLevel(); |
| 1833 double zoom_factor = content::ZoomLevelToZoomFactor(zoom_level); |
| 1834 results_ = tabs::GetZoom::Results::Create(zoom_factor); |
| 1835 SendResponse(true); |
| 1836 return true; |
| 1837 } |
| 1838 |
| 1839 bool TabsSetZoomSettingsFunction::RunAsync() { |
| 1840 using api::tabs::ZoomSettings; |
| 1841 |
| 1842 scoped_ptr<tabs::SetZoomSettings::Params> params( |
| 1843 tabs::SetZoomSettings::Params::Create(*args_)); |
| 1844 EXTENSION_FUNCTION_VALIDATE(params); |
| 1845 |
| 1846 int tab_id = params->tab_id ? *params->tab_id : -1; |
| 1847 WebContents* web_contents = GetWebContents(tab_id); |
| 1848 if (!web_contents) |
| 1849 return false; |
| 1850 |
| 1851 GURL url(web_contents->GetVisibleURL()); |
| 1852 if (PermissionsData::IsRestrictedUrl(url, url, GetExtension(), &error_)) |
| 1853 return false; |
| 1854 |
| 1855 // "per-origin" scope is only available in "automatic" mode. |
| 1856 if (params->zoom_settings.scope == ZoomSettings::SCOPE_PER_ORIGIN && |
| 1857 params->zoom_settings.mode != ZoomSettings::MODE_AUTOMATIC && |
| 1858 params->zoom_settings.mode != ZoomSettings::MODE_NONE) { |
| 1859 error_ = keys::kPerOriginOnlyInAutomaticError; |
| 1860 return false; |
| 1861 } |
| 1862 |
| 1863 // Determine the correct internal zoom mode to set |web_contents| to from the |
| 1864 // user-specified |zoom_settings|. |
| 1865 ZoomController::ZoomMode zoom_mode = ZoomController::ZOOM_MODE_DEFAULT; |
| 1866 switch (params->zoom_settings.mode) { |
| 1867 case ZoomSettings::MODE_NONE: |
| 1868 case ZoomSettings::MODE_AUTOMATIC: |
| 1869 switch (params->zoom_settings.scope) { |
| 1870 case ZoomSettings::SCOPE_NONE: |
| 1871 case ZoomSettings::SCOPE_PER_ORIGIN: |
| 1872 zoom_mode = ZoomController::ZOOM_MODE_DEFAULT; |
| 1873 break; |
| 1874 case ZoomSettings::SCOPE_PER_TAB: |
| 1875 zoom_mode = ZoomController::ZOOM_MODE_ISOLATED; |
| 1876 } |
| 1877 break; |
| 1878 case ZoomSettings::MODE_MANUAL: |
| 1879 zoom_mode = ZoomController::ZOOM_MODE_MANUAL; |
| 1880 break; |
| 1881 case ZoomSettings::MODE_DISABLED: |
| 1882 zoom_mode = ZoomController::ZOOM_MODE_DISABLED; |
| 1883 } |
| 1884 |
| 1885 ZoomController::FromWebContents(web_contents)->SetZoomMode(zoom_mode); |
| 1886 |
| 1887 SendResponse(true); |
| 1888 return true; |
| 1889 } |
| 1890 |
| 1891 bool TabsGetZoomSettingsFunction::RunAsync() { |
| 1892 scoped_ptr<tabs::GetZoomSettings::Params> params( |
| 1893 tabs::GetZoomSettings::Params::Create(*args_)); |
| 1894 EXTENSION_FUNCTION_VALIDATE(params); |
| 1895 |
| 1896 int tab_id = params->tab_id ? *params->tab_id : -1; |
| 1897 WebContents* web_contents = GetWebContents(tab_id); |
| 1898 if (!web_contents) |
| 1899 return false; |
| 1900 ZoomController* zoom_controller = |
| 1901 ZoomController::FromWebContents(web_contents); |
| 1902 |
| 1903 ZoomController::ZoomMode zoom_mode = zoom_controller->zoom_mode(); |
| 1904 api::tabs::ZoomSettings zoom_settings; |
| 1905 ZoomModeToZoomSettings(zoom_mode, &zoom_settings); |
| 1906 |
| 1907 results_ = api::tabs::GetZoomSettings::Results::Create(zoom_settings); |
| 1908 SendResponse(true); |
| 1909 return true; |
| 1910 } |
| 1911 |
1746 } // namespace extensions | 1912 } // namespace extensions |
OLD | NEW |