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

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

Issue 225093019: Zoom Extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments. Created 6 years, 8 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/extensions/extension_file_util.h" 54 #include "chrome/common/extensions/extension_file_util.h"
54 #include "chrome/common/extensions/extension_l10n_util.h" 55 #include "chrome/common/extensions/extension_l10n_util.h"
55 #include "chrome/common/extensions/message_bundle.h" 56 #include "chrome/common/extensions/message_bundle.h"
56 #include "chrome/common/pref_names.h" 57 #include "chrome/common/pref_names.h"
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 new_window = chrome::FindTabbedBrowser(profile, false, 209 new_window = chrome::FindTabbedBrowser(profile, false,
209 params.host_desktop_type); 210 params.host_desktop_type);
210 211
211 if (!new_window) 212 if (!new_window)
212 new_window = new Browser(params); 213 new_window = new Browser(params);
213 return new_window; 214 return new_window;
214 } 215 }
215 216
216 } // namespace 217 } // namespace
217 218
219 void ZoomModeToZoomSettings(content::ZoomMode zoom_mode,
220 api::tabs::ZoomSettings* zoom_settings) {
221 DCHECK(zoom_settings);
222 switch (zoom_mode) {
223 case content::kZoomModeDefault:
224 zoom_settings->mode = api::tabs::ZoomSettings::MODE_AUTOMATIC;
225 zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_ORIGIN;
226 break;
227 case content::kZoomModeIsolated:
228 zoom_settings->mode = api::tabs::ZoomSettings::MODE_AUTOMATIC;
229 zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_TAB;
230 break;
231 case content::kZoomModeManual:
232 zoom_settings->mode = api::tabs::ZoomSettings::MODE_MANUAL;
233 zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_TAB;
234 break;
235 case content::kZoomModeDisabled:
236 zoom_settings->mode = api::tabs::ZoomSettings::MODE_DISABLED;
237 zoom_settings->scope = api::tabs::ZoomSettings::SCOPE_PER_TAB;
238 break;
239 }
240 }
241
218 // Windows --------------------------------------------------------------------- 242 // Windows ---------------------------------------------------------------------
219 243
220 bool WindowsGetFunction::RunImpl() { 244 bool WindowsGetFunction::RunImpl() {
221 scoped_ptr<windows::Get::Params> params(windows::Get::Params::Create(*args_)); 245 scoped_ptr<windows::Get::Params> params(windows::Get::Params::Create(*args_));
222 EXTENSION_FUNCTION_VALIDATE(params.get()); 246 EXTENSION_FUNCTION_VALIDATE(params.get());
223 247
224 bool populate_tabs = false; 248 bool populate_tabs = false;
225 if (params->get_info.get() && params->get_info->populate.get()) 249 if (params->get_info.get() && params->get_info->populate.get())
226 populate_tabs = *params->get_info->populate; 250 populate_tabs = *params->get_info->populate;
227 251
(...skipping 1361 matching lines...) Expand 10 before | Expand all | Expand 10 after
1589 int tab_id = *params->tab_id; 1613 int tab_id = *params->tab_id;
1590 1614
1591 Browser* browser = NULL; 1615 Browser* browser = NULL;
1592 if (!GetTabById(tab_id, 1616 if (!GetTabById(tab_id,
1593 GetProfile(), 1617 GetProfile(),
1594 include_incognito(), 1618 include_incognito(),
1595 &browser, 1619 &browser,
1596 NULL, 1620 NULL,
1597 &web_contents, 1621 &web_contents,
1598 NULL, 1622 NULL,
1599 &error_)) 1623 &error_)) {
1600 return false; 1624 return false;
1625 }
1601 } 1626 }
1602 1627
1603 if (web_contents->ShowingInterstitialPage()) { 1628 if (web_contents->ShowingInterstitialPage()) {
1604 // This does as same as Browser::ReloadInternal. 1629 // This does as same as Browser::ReloadInternal.
1605 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); 1630 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry();
1606 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB, 1631 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB,
1607 content::PAGE_TRANSITION_RELOAD, false); 1632 content::PAGE_TRANSITION_RELOAD, false);
1608 GetCurrentBrowser()->OpenURL(params); 1633 GetCurrentBrowser()->OpenURL(params);
1609 } else if (bypass_cache) { 1634 } else if (bypass_cache) {
1610 web_contents->GetController().ReloadIgnoringCache(true); 1635 web_contents->GetController().ReloadIgnoringCache(true);
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
1910 1935
1911 execute_tab_id_ = tab_id; 1936 execute_tab_id_ = tab_id;
1912 details_ = details.Pass(); 1937 details_ = details.Pass();
1913 return true; 1938 return true;
1914 } 1939 }
1915 1940
1916 bool TabsInsertCSSFunction::ShouldInsertCSS() const { 1941 bool TabsInsertCSSFunction::ShouldInsertCSS() const {
1917 return true; 1942 return true;
1918 } 1943 }
1919 1944
1945 bool ZoomAPIFunction::GetWebContents(int* tab_id,
1946 content::WebContents** web_contents) {
1947 if (tab_id) {
1948 if (!GetTabById(*tab_id,
1949 GetProfile(),
1950 include_incognito(),
1951 NULL,
1952 NULL,
1953 web_contents,
1954 NULL,
1955 &error_)) {
1956 return false;
1957 }
1958 } else {
1959 Browser* browser = GetCurrentBrowser();
1960 if (!browser) {
1961 error_ = keys::kNoCurrentWindowError;
1962 return false;
1963 }
1964 if (!ExtensionTabUtil::GetDefaultTab(browser, web_contents, NULL)) {
1965 error_ = keys::kNoSelectedTabError;
1966 return false;
1967 }
1968 }
1969 return true;
1970 }
1971
1972 bool TabsSetZoomFunction::RunImpl() {
1973 scoped_ptr<tabs::SetZoom::Params> params(
1974 tabs::SetZoom::Params::Create(*args_));
1975 EXTENSION_FUNCTION_VALIDATE(params.get());
1976
1977 WebContents* web_contents = NULL;
1978 if (!GetWebContents(params->tab_id.get(), &web_contents))
1979 return false;
1980 if (web_contents->GetURL().SchemeIs(content::kChromeUIScheme)) {
1981 error_ = keys::kCannotZoomChromePagesError;
1982 return false;
1983 }
1984
1985 ZoomController* zoom_controller =
1986 ZoomController::FromWebContents(web_contents);
1987 double zoom_level = content::ZoomFactorToZoomLevel(params->zoom_factor);
1988
1989 if (!zoom_controller->SetZoomLevelByExtension(
1990 zoom_level,
1991 GetExtension(),
1992 base::Bind(&TabsSetZoomFunction::SendResponse, this, true))) {
1993 // Tried to zoom a tab in disabled mode.
1994 error_ = keys::kCannotZoomDisabledTabError;
1995 return false;
1996 }
1997
1998 return true;
1999 }
2000
2001 bool TabsGetZoomFunction::RunImpl() {
2002 scoped_ptr<tabs::GetZoom::Params> params(
2003 tabs::GetZoom::Params::Create(*args_));
2004 EXTENSION_FUNCTION_VALIDATE(params.get());
2005
2006 WebContents* web_contents = NULL;
2007 if (!GetWebContents(params->tab_id.get(), &web_contents)) {
2008 return false;
2009 }
2010
2011 double zoom_level = web_contents->GetZoomLevel();
2012 double zoom_factor = content::ZoomLevelToZoomFactor(zoom_level);
2013 results_ = tabs::GetZoom::Results::Create(zoom_factor);
2014 SendResponse(true);
2015 return true;
2016 }
2017
2018 bool TabsSetZoomSettingsFunction::RunImpl() {
2019 using api::tabs::ZoomSettings;
2020
2021 scoped_ptr<tabs::SetZoomSettings::Params> params(
2022 tabs::SetZoomSettings::Params::Create(*args_));
2023 EXTENSION_FUNCTION_VALIDATE(params.get());
2024
2025 WebContents* web_contents = NULL;
2026 if (!GetWebContents(params->tab_id.get(), &web_contents))
2027 return false;
2028 if (web_contents->GetURL().SchemeIs(content::kChromeUIScheme)) {
2029 error_ = keys::kCannotChangeChromePageZoomSettingsError;
2030 return false;
2031 }
2032
2033 // "per-origin" scope is only available in "automatic" mode.
2034 if (params->zoom_settings.scope == ZoomSettings::SCOPE_PER_ORIGIN &&
2035 params->zoom_settings.mode != ZoomSettings::MODE_AUTOMATIC &&
2036 params->zoom_settings.mode != ZoomSettings::MODE_NONE) {
2037 error_ = keys::kPerOriginOnlyInAutomaticError;
2038 return false;
2039 }
2040
2041 // Determine the correct internal zoom mode to set |web_contents| to from the
2042 // user-specified |zoom_settings|.
2043 content::ZoomMode zoom_mode = content::kZoomModeDefault;
2044 switch (params->zoom_settings.mode) {
2045 case ZoomSettings::MODE_NONE:
2046 case ZoomSettings::MODE_AUTOMATIC:
2047 switch (params->zoom_settings.scope) {
2048 case ZoomSettings::SCOPE_NONE:
2049 case ZoomSettings::SCOPE_PER_ORIGIN:
2050 zoom_mode = content::kZoomModeDefault;
2051 break;
2052 case ZoomSettings::SCOPE_PER_TAB:
2053 zoom_mode = content::kZoomModeIsolated;
2054 break;
2055 }
2056 break;
2057 case ZoomSettings::MODE_MANUAL:
2058 zoom_mode = content::kZoomModeManual;
2059 break;
2060 case ZoomSettings::MODE_DISABLED:
2061 zoom_mode = content::kZoomModeDisabled;
2062 break;
2063 }
2064
2065 web_contents->SetZoomMode(zoom_mode);
2066
2067 SendResponse(true);
2068 return true;
2069 }
2070
2071 bool TabsGetZoomSettingsFunction::RunImpl() {
2072 scoped_ptr<tabs::GetZoomSettings::Params> params(
2073 tabs::GetZoomSettings::Params::Create(*args_));
2074 EXTENSION_FUNCTION_VALIDATE(params.get());
2075
2076 WebContents* web_contents = NULL;
2077 if (!GetWebContents(params->tab_id.get(), &web_contents))
2078 return false;
2079
2080 content::ZoomMode zoom_mode = web_contents->GetZoomMode();
2081 api::tabs::ZoomSettings zoom_settings;
2082 ZoomModeToZoomSettings(zoom_mode, &zoom_settings);
2083
2084 results_ = api::tabs::GetZoomSettings::Results::Create(zoom_settings);
2085 SendResponse(true);
2086 return true;
2087 }
2088
1920 } // namespace extensions 2089 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698