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

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, 6 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::kZoomModeDefault:
172 zoom_settings->mode = api::tabs::ZoomSettings::MODE_AUTOMATIC;
173 zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_ORIGIN;
174 break;
175 case ZoomController::kZoomModeIsolated:
176 zoom_settings->mode = api::tabs::ZoomSettings::MODE_AUTOMATIC;
177 zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_TAB;
178 break;
179 case ZoomController::kZoomModeManual:
180 zoom_settings->mode = api::tabs::ZoomSettings::MODE_MANUAL;
181 zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_TAB;
182 break;
183 case ZoomController::kZoomModeDisabled:
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 1237 matching lines...) Expand 10 before | Expand all | Expand 10 after
1413 int tab_id = *params->tab_id; 1437 int tab_id = *params->tab_id;
1414 1438
1415 Browser* browser = NULL; 1439 Browser* browser = NULL;
1416 if (!GetTabById(tab_id, 1440 if (!GetTabById(tab_id,
1417 GetProfile(), 1441 GetProfile(),
1418 include_incognito(), 1442 include_incognito(),
1419 &browser, 1443 &browser,
1420 NULL, 1444 NULL,
1421 &web_contents, 1445 &web_contents,
1422 NULL, 1446 NULL,
1423 &error_)) 1447 &error_)) {
1424 return false; 1448 return false;
1449 }
1425 } 1450 }
1426 1451
1427 if (web_contents->ShowingInterstitialPage()) { 1452 if (web_contents->ShowingInterstitialPage()) {
1428 // This does as same as Browser::ReloadInternal. 1453 // This does as same as Browser::ReloadInternal.
1429 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); 1454 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry();
1430 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB, 1455 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB,
1431 content::PAGE_TRANSITION_RELOAD, false); 1456 content::PAGE_TRANSITION_RELOAD, false);
1432 GetCurrentBrowser()->OpenURL(params); 1457 GetCurrentBrowser()->OpenURL(params);
1433 } else if (bypass_cache) { 1458 } else if (bypass_cache) {
1434 web_contents->GetController().ReloadIgnoringCache(true); 1459 web_contents->GetController().ReloadIgnoringCache(true);
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
1742 1767
1743 execute_tab_id_ = tab_id; 1768 execute_tab_id_ = tab_id;
1744 details_ = details.Pass(); 1769 details_ = details.Pass();
1745 return true; 1770 return true;
1746 } 1771 }
1747 1772
1748 bool TabsInsertCSSFunction::ShouldInsertCSS() const { 1773 bool TabsInsertCSSFunction::ShouldInsertCSS() const {
1749 return true; 1774 return true;
1750 } 1775 }
1751 1776
1777 content::WebContents* ZoomAPIFunction::GetWebContents(int* tab_id) {
Devlin 2014/06/19 21:15:28 nit: more common is to pass in -1 for "no tab id",
wjmaclean 2014/06/20 22:01:32 Done.
1778 content::WebContents* web_contents = NULL;
1779 if (tab_id) {
1780 // We assume this call leaves web_contents unchanged if it is unsuccessful.
1781 GetTabById(*tab_id,
1782 GetProfile(),
1783 include_incognito(),
1784 NULL,
Devlin 2014/06/19 21:15:28 Please document anonymous nulls.
wjmaclean 2014/06/20 22:01:32 Done.
1785 NULL,
1786 &web_contents,
1787 NULL,
1788 &error_);
1789 } else {
1790 Browser* browser = GetCurrentBrowser();
1791 if (!browser)
1792 error_ = keys::kNoCurrentWindowError;
1793 else if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, NULL))
1794 error_ = keys::kNoSelectedTabError;
1795 }
1796 return web_contents;
1797 }
1798
1799 bool TabsSetZoomFunction::RunAsync() {
1800 scoped_ptr<tabs::SetZoom::Params> params(
1801 tabs::SetZoom::Params::Create(*args_));
1802 EXTENSION_FUNCTION_VALIDATE(params.get());
1803
1804 WebContents* web_contents = GetWebContents(params->tab_id.get());
1805 if (!web_contents)
1806 return false;
1807
1808 GURL url(web_contents->GetURL());
Devlin 2014/06/19 21:15:28 GetURL is still deprecated. :)
wjmaclean 2014/06/20 22:01:32 Done.
1809 if (url.SchemeIs(content::kChromeUIScheme) ||
1810 !ExtensionsClient::Get()->IsScriptableURL(url, NULL)) {
Devlin 2014/06/19 21:15:28 Please document anonymous NULL. ...ExtensionsClien
wjmaclean 2014/06/20 22:01:32 Done.
1811 error_ = keys::kCannotZoomChromePagesError;
1812 return false;
1813 }
1814
1815 ZoomController* zoom_controller =
1816 ZoomController::FromWebContents(web_contents);
1817 double zoom_level = content::ZoomFactorToZoomLevel(params->zoom_factor);
1818
1819 if (!zoom_controller->SetZoomLevelByExtension(zoom_level, GetExtension())) {
1820 // Tried to zoom a tab in disabled mode.
1821 error_ = keys::kCannotZoomDisabledTabError;
1822 return false;
1823 }
1824
1825 SendResponse(true);
1826 return true;
1827 }
1828
1829 bool TabsGetZoomFunction::RunAsync() {
1830 scoped_ptr<tabs::GetZoom::Params> params(
1831 tabs::GetZoom::Params::Create(*args_));
1832 EXTENSION_FUNCTION_VALIDATE(params.get());
1833
1834 WebContents* web_contents = GetWebContents(params->tab_id.get());
1835 if (!web_contents)
1836 return false;
1837
1838 double zoom_level =
1839 ZoomController::FromWebContents(web_contents)->GetZoomLevel();
1840 double zoom_factor = content::ZoomLevelToZoomFactor(zoom_level);
1841 results_ = tabs::GetZoom::Results::Create(zoom_factor);
1842 SendResponse(true);
1843 return true;
1844 }
1845
1846 bool TabsSetZoomSettingsFunction::RunAsync() {
1847 using api::tabs::ZoomSettings;
1848
1849 scoped_ptr<tabs::SetZoomSettings::Params> params(
1850 tabs::SetZoomSettings::Params::Create(*args_));
1851 EXTENSION_FUNCTION_VALIDATE(params.get());
1852
1853 WebContents* web_contents = GetWebContents(params->tab_id.get());
1854 if (!web_contents)
1855 return false;
1856
1857 GURL url(web_contents->GetURL());
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::kZoomModeDefault;
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::kZoomModeDefault;
1882 break;
1883 case ZoomSettings::SCOPE_PER_TAB:
1884 zoom_mode = ZoomController::kZoomModeIsolated;
1885 }
1886 break;
1887 case ZoomSettings::MODE_MANUAL:
1888 zoom_mode = ZoomController::kZoomModeManual;
1889 break;
1890 case ZoomSettings::MODE_DISABLED:
1891 zoom_mode = ZoomController::kZoomModeDisabled;
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.get());
1904
1905 WebContents* web_contents = GetWebContents(params->tab_id.get());
1906 if (!web_contents)
1907 return false;
1908 ZoomController* zoom_controller =
1909 ZoomController::FromWebContents(web_contents);
1910
1911 ZoomController::ZoomMode zoom_mode = zoom_controller->zoom_mode();
1912 api::tabs::ZoomSettings zoom_settings;
1913 ZoomModeToZoomSettings(zoom_mode, &zoom_settings);
1914
1915 results_ = api::tabs::GetZoomSettings::Results::Create(zoom_settings);
1916 SendResponse(true);
1917 return true;
1918 }
1919
1752 } // namespace extensions 1920 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698