Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Side by Side Diff: chrome/browser/extensions/api/tabs/tabs_api.cc

Issue 301733006: Zoom Extension API (chrome) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 1234 matching lines...) Expand 10 before | Expand all | Expand 10 after
1410 int tab_id = *params->tab_id; 1434 int tab_id = *params->tab_id;
1411 1435
1412 Browser* browser = NULL; 1436 Browser* browser = NULL;
1413 if (!GetTabById(tab_id, 1437 if (!GetTabById(tab_id,
1414 GetProfile(), 1438 GetProfile(),
1415 include_incognito(), 1439 include_incognito(),
1416 &browser, 1440 &browser,
1417 NULL, 1441 NULL,
1418 &web_contents, 1442 &web_contents,
1419 NULL, 1443 NULL,
1420 &error_)) 1444 &error_)) {
1421 return false; 1445 return false;
1446 }
1422 } 1447 }
1423 1448
1424 if (web_contents->ShowingInterstitialPage()) { 1449 if (web_contents->ShowingInterstitialPage()) {
1425 // This does as same as Browser::ReloadInternal. 1450 // This does as same as Browser::ReloadInternal.
1426 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); 1451 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry();
1427 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB, 1452 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB,
1428 content::PAGE_TRANSITION_RELOAD, false); 1453 content::PAGE_TRANSITION_RELOAD, false);
1429 GetCurrentBrowser()->OpenURL(params); 1454 GetCurrentBrowser()->OpenURL(params);
1430 } else if (bypass_cache) { 1455 } else if (bypass_cache) {
1431 web_contents->GetController().ReloadIgnoringCache(true); 1456 web_contents->GetController().ReloadIgnoringCache(true);
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
1739 1764
1740 execute_tab_id_ = tab_id; 1765 execute_tab_id_ = tab_id;
1741 details_ = details.Pass(); 1766 details_ = details.Pass();
1742 return true; 1767 return true;
1743 } 1768 }
1744 1769
1745 bool TabsInsertCSSFunction::ShouldInsertCSS() const { 1770 bool TabsInsertCSSFunction::ShouldInsertCSS() const {
1746 return true; 1771 return true;
1747 } 1772 }
1748 1773
1774 content::WebContents* ZoomAPIFunction::GetWebContents(int tab_id) {
1775 content::WebContents* web_contents = NULL;
1776 if (tab_id != -1) {
1777 // We assume this call leaves web_contents unchanged if it is unsuccessful.
1778 GetTabById(tab_id,
1779 GetProfile(),
1780 include_incognito(),
1781 NULL /* ignore Browser* output */,
1782 NULL /* ignore TabStripModel* output */,
1783 &web_contents,
1784 NULL /* ignore int tab_index output */,
1785 &error_);
1786 } else {
1787 Browser* browser = GetCurrentBrowser();
1788 if (!browser)
1789 error_ = keys::kNoCurrentWindowError;
1790 else if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, NULL))
1791 error_ = keys::kNoSelectedTabError;
1792 }
1793 return web_contents;
1794 }
1795
1796 bool TabsSetZoomFunction::RunAsync() {
1797 scoped_ptr<tabs::SetZoom::Params> params(
1798 tabs::SetZoom::Params::Create(*args_));
1799 EXTENSION_FUNCTION_VALIDATE(params);
1800
1801 int tab_id = params->tab_id ? *params->tab_id : -1;
1802 WebContents* web_contents = GetWebContents(tab_id);
1803 if (!web_contents)
1804 return false;
1805
1806 GURL url(web_contents->GetVisibleURL());
1807 if (url.SchemeIs(content::kChromeUIScheme) ||
Devlin 2014/07/03 14:55:37 Recently we added PermissionsData::IsRestrictedUrl
wjmaclean 2014/07/03 19:37:21 Recent indeed! I had to rebase ... ;-) Done.
1808 !ExtensionsClient::Get()->IsScriptableURL(url, NULL /* ignore error */)) {
1809 error_ = keys::kCannotZoomChromePagesError;
1810 return false;
1811 }
1812
1813 ZoomController* zoom_controller =
1814 ZoomController::FromWebContents(web_contents);
1815 double zoom_level = content::ZoomFactorToZoomLevel(params->zoom_factor);
1816
1817 if (!zoom_controller->SetZoomLevelByExtension(zoom_level, GetExtension())) {
1818 // Tried to zoom a tab in disabled mode.
1819 error_ = keys::kCannotZoomDisabledTabError;
1820 return false;
1821 }
1822
1823 SendResponse(true);
1824 return true;
1825 }
1826
1827 bool TabsGetZoomFunction::RunAsync() {
1828 scoped_ptr<tabs::GetZoom::Params> params(
1829 tabs::GetZoom::Params::Create(*args_));
1830 EXTENSION_FUNCTION_VALIDATE(params);
1831
1832 int tab_id = params->tab_id ? *params->tab_id : -1;
1833 WebContents* web_contents = GetWebContents(tab_id);
1834 if (!web_contents)
1835 return false;
1836
1837 double zoom_level =
1838 ZoomController::FromWebContents(web_contents)->GetZoomLevel();
1839 double zoom_factor = content::ZoomLevelToZoomFactor(zoom_level);
1840 results_ = tabs::GetZoom::Results::Create(zoom_factor);
1841 SendResponse(true);
1842 return true;
1843 }
1844
1845 bool TabsSetZoomSettingsFunction::RunAsync() {
1846 using api::tabs::ZoomSettings;
1847
1848 scoped_ptr<tabs::SetZoomSettings::Params> params(
1849 tabs::SetZoomSettings::Params::Create(*args_));
1850 EXTENSION_FUNCTION_VALIDATE(params);
1851
1852 int tab_id = params->tab_id ? *params->tab_id : -1;
1853 WebContents* web_contents = GetWebContents(tab_id);
1854 if (!web_contents)
1855 return false;
1856
1857 GURL url(web_contents->GetVisibleURL());
1858 if (url.SchemeIs(content::kChromeUIScheme) ||
1859 !ExtensionsClient::Get()->IsScriptableURL(url, NULL)) {
1860 error_ = keys::kCannotChangeChromePageZoomSettingsError;
1861 return false;
1862 }
1863
1864 // "per-origin" scope is only available in "automatic" mode.
1865 if (params->zoom_settings.scope == ZoomSettings::SCOPE_PER_ORIGIN &&
1866 params->zoom_settings.mode != ZoomSettings::MODE_AUTOMATIC &&
1867 params->zoom_settings.mode != ZoomSettings::MODE_NONE) {
1868 error_ = keys::kPerOriginOnlyInAutomaticError;
1869 return false;
1870 }
1871
1872 // Determine the correct internal zoom mode to set |web_contents| to from the
1873 // user-specified |zoom_settings|.
1874 ZoomController::ZoomMode zoom_mode = ZoomController::ZOOM_MODE_DEFAULT;
1875 switch (params->zoom_settings.mode) {
1876 case ZoomSettings::MODE_NONE:
1877 case ZoomSettings::MODE_AUTOMATIC:
1878 switch (params->zoom_settings.scope) {
1879 case ZoomSettings::SCOPE_NONE:
1880 case ZoomSettings::SCOPE_PER_ORIGIN:
1881 zoom_mode = ZoomController::ZOOM_MODE_DEFAULT;
1882 break;
1883 case ZoomSettings::SCOPE_PER_TAB:
1884 zoom_mode = ZoomController::ZOOM_MODE_ISOLATED;
1885 }
1886 break;
1887 case ZoomSettings::MODE_MANUAL:
1888 zoom_mode = ZoomController::ZOOM_MODE_MANUAL;
1889 break;
1890 case ZoomSettings::MODE_DISABLED:
1891 zoom_mode = ZoomController::ZOOM_MODE_DISABLED;
1892 }
1893
1894 ZoomController::FromWebContents(web_contents)->SetZoomMode(zoom_mode);
1895
1896 SendResponse(true);
1897 return true;
1898 }
1899
1900 bool TabsGetZoomSettingsFunction::RunAsync() {
1901 scoped_ptr<tabs::GetZoomSettings::Params> params(
1902 tabs::GetZoomSettings::Params::Create(*args_));
1903 EXTENSION_FUNCTION_VALIDATE(params);
1904
1905 int tab_id = params->tab_id ? *params->tab_id : -1;
1906 WebContents* web_contents = GetWebContents(tab_id);
1907 if (!web_contents)
1908 return false;
1909 ZoomController* zoom_controller =
1910 ZoomController::FromWebContents(web_contents);
1911
1912 ZoomController::ZoomMode zoom_mode = zoom_controller->zoom_mode();
1913 api::tabs::ZoomSettings zoom_settings;
1914 ZoomModeToZoomSettings(zoom_mode, &zoom_settings);
1915
1916 results_ = api::tabs::GetZoomSettings::Results::Create(zoom_settings);
1917 SendResponse(true);
1918 return true;
1919 }
1920
1749 } // namespace extensions 1921 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698