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

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: Using callbacks instead of zoom IDs. 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"
57 #include "chrome/common/url_constants.h" 58 #include "chrome/common/url_constants.h"
58 #include "components/translate/core/common/language_detection_details.h" 59 #include "components/translate/core/common/language_detection_details.h"
59 #include "components/user_prefs/pref_registry_syncable.h" 60 #include "components/user_prefs/pref_registry_syncable.h"
60 #include "content/public/browser/navigation_controller.h" 61 #include "content/public/browser/navigation_controller.h"
61 #include "content/public/browser/navigation_entry.h" 62 #include "content/public/browser/navigation_entry.h"
62 #include "content/public/browser/notification_details.h" 63 #include "content/public/browser/notification_details.h"
64 #include "content/public/browser/notification_service.h"
63 #include "content/public/browser/notification_source.h" 65 #include "content/public/browser/notification_source.h"
64 #include "content/public/browser/render_process_host.h" 66 #include "content/public/browser/render_process_host.h"
65 #include "content/public/browser/render_view_host.h" 67 #include "content/public/browser/render_view_host.h"
66 #include "content/public/browser/render_widget_host_view.h" 68 #include "content/public/browser/render_widget_host_view.h"
67 #include "content/public/browser/web_contents.h" 69 #include "content/public/browser/web_contents.h"
68 #include "content/public/browser/web_contents_view.h" 70 #include "content/public/browser/web_contents_view.h"
69 #include "content/public/common/url_constants.h" 71 #include "content/public/common/url_constants.h"
70 #include "extensions/browser/extension_function_dispatcher.h" 72 #include "extensions/browser/extension_function_dispatcher.h"
71 #include "extensions/browser/extension_function_util.h" 73 #include "extensions/browser/extension_function_util.h"
72 #include "extensions/browser/extension_host.h" 74 #include "extensions/browser/extension_host.h"
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 new_window = chrome::FindTabbedBrowser(profile, false, 210 new_window = chrome::FindTabbedBrowser(profile, false,
209 params.host_desktop_type); 211 params.host_desktop_type);
210 212
211 if (!new_window) 213 if (!new_window)
212 new_window = new Browser(params); 214 new_window = new Browser(params);
213 return new_window; 215 return new_window;
214 } 216 }
215 217
216 } // namespace 218 } // namespace
217 219
220 void ZoomModeToZoomSettings(content::ZoomMode zoom_mode,
221 base::DictionaryValue* zoom_settings) {
222 std::string zoom_settings_mode;
223 std::string zoom_settings_scope;
not at google - send to devlin 2014/04/09 03:52:11 you could use const char* for both of these and av
paulmeyer 2014/04/11 03:01:02 Done.
224 switch (zoom_mode) {
225 case content::kZoomModeDefault:
226 zoom_settings_mode = "automatic";
227 zoom_settings_scope = "per-origin";
228 break;
229 case content::kZoomModeIsolated:
230 zoom_settings_mode = "automatic";
231 zoom_settings_scope = "per-tab";
232 break;
233 case content::kZoomModeManual:
234 zoom_settings_mode = "manual";
235 zoom_settings_scope = "per-tab";
236 break;
237 case content::kZoomModeDisabled:
238 zoom_settings_mode = "disabled";
239 zoom_settings_scope = "per-tab";
240 break;
241 default:
242 NOTREACHED();
not at google - send to devlin 2014/04/09 03:52:11 prefer not setting a default case so that you can
paulmeyer 2014/04/11 03:01:02 Done.
243 }
244
245 zoom_settings->SetString(keys::kZoomSettingsMode, zoom_settings_mode);
246 zoom_settings->SetString(keys::kZoomSettingsScope, zoom_settings_scope);
247 }
248
218 // Windows --------------------------------------------------------------------- 249 // Windows ---------------------------------------------------------------------
219 250
220 bool WindowsGetFunction::RunImpl() { 251 bool WindowsGetFunction::RunImpl() {
221 scoped_ptr<windows::Get::Params> params(windows::Get::Params::Create(*args_)); 252 scoped_ptr<windows::Get::Params> params(windows::Get::Params::Create(*args_));
222 EXTENSION_FUNCTION_VALIDATE(params.get()); 253 EXTENSION_FUNCTION_VALIDATE(params.get());
223 254
224 bool populate_tabs = false; 255 bool populate_tabs = false;
225 if (params->get_info.get() && params->get_info->populate.get()) 256 if (params->get_info.get() && params->get_info->populate.get())
226 populate_tabs = *params->get_info->populate; 257 populate_tabs = *params->get_info->populate;
227 258
(...skipping 1361 matching lines...) Expand 10 before | Expand all | Expand 10 after
1589 int tab_id = *params->tab_id; 1620 int tab_id = *params->tab_id;
1590 1621
1591 Browser* browser = NULL; 1622 Browser* browser = NULL;
1592 if (!GetTabById(tab_id, 1623 if (!GetTabById(tab_id,
1593 GetProfile(), 1624 GetProfile(),
1594 include_incognito(), 1625 include_incognito(),
1595 &browser, 1626 &browser,
1596 NULL, 1627 NULL,
1597 &web_contents, 1628 &web_contents,
1598 NULL, 1629 NULL,
1599 &error_)) 1630 &error_)) {
1600 return false; 1631 return false;
1632 }
1601 } 1633 }
1602 1634
1603 if (web_contents->ShowingInterstitialPage()) { 1635 if (web_contents->ShowingInterstitialPage()) {
1604 // This does as same as Browser::ReloadInternal. 1636 // This does as same as Browser::ReloadInternal.
1605 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); 1637 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry();
1606 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB, 1638 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB,
1607 content::PAGE_TRANSITION_RELOAD, false); 1639 content::PAGE_TRANSITION_RELOAD, false);
1608 GetCurrentBrowser()->OpenURL(params); 1640 GetCurrentBrowser()->OpenURL(params);
1609 } else if (bypass_cache) { 1641 } else if (bypass_cache) {
1610 web_contents->GetController().ReloadIgnoringCache(true); 1642 web_contents->GetController().ReloadIgnoringCache(true);
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
1910 1942
1911 execute_tab_id_ = tab_id; 1943 execute_tab_id_ = tab_id;
1912 details_ = details.Pass(); 1944 details_ = details.Pass();
1913 return true; 1945 return true;
1914 } 1946 }
1915 1947
1916 bool TabsInsertCSSFunction::ShouldInsertCSS() const { 1948 bool TabsInsertCSSFunction::ShouldInsertCSS() const {
1917 return true; 1949 return true;
1918 } 1950 }
1919 1951
1952 bool ZoomAPIFunction::GetWebContents(int* tab_id,
1953 content::WebContents** web_contents) {
not at google - send to devlin 2014/04/09 03:52:11 surely this functionality is already implemented f
paulmeyer 2014/04/11 03:01:02 Unfortunately it is implemented but not in a share
1954 if (tab_id) {
1955 if (!GetTabById(*tab_id,
1956 GetProfile(),
1957 include_incognito(),
1958 NULL,
1959 NULL,
1960 web_contents,
1961 NULL,
1962 &error_)) {
1963 return false;
1964 }
1965 } else {
1966 Browser* browser = GetCurrentBrowser();
1967 if (!browser) {
1968 error_ = keys::kNoCurrentWindowError;
1969 return false;
1970 }
1971 if (!ExtensionTabUtil::GetDefaultTab(browser, web_contents, NULL)) {
1972 error_ = keys::kNoCurrentTabError;
1973 return false;
1974 }
1975 }
1976 return true;
1977 }
1978
1979 bool TabsSetZoomFunction::RunImpl() {
1980 scoped_ptr<tabs::SetZoom::Params> params(
1981 tabs::SetZoom::Params::Create(*args_));
1982 EXTENSION_FUNCTION_VALIDATE(params.get());
1983
1984 WebContents* web_contents = NULL;
1985 if (!GetWebContents(params->tab_id.get(), &web_contents)) {
1986 SendResponse(false);
1987 return false;
1988 } else if (web_contents->GetURL().SchemeIs("chrome")) {
not at google - send to devlin 2014/04/09 03:52:11 use kChromeUIScheme not "chrome".
paulmeyer 2014/04/11 03:01:02 Done.
1989 error_ = keys::kCannotZoomChromePagesError;
1990 SendResponse(false);
1991 return false;
1992 }
1993
1994 ZoomController* zoom_controller =
1995 ZoomController::FromWebContents(web_contents);
1996 double zoom_level = content::ZoomFactorToZoomLevel(params->zoom_factor);
1997
1998 // Attempt to set the zoom level with a callback.
1999 scoped_refptr<TabsSetZoomFunction> this_ref = this;
not at google - send to devlin 2014/04/09 03:52:11 I don't think this makes any difference. just pass
paulmeyer 2014/04/11 03:01:02 I believe that the scoped_ptr is necessary to pres
not at google - send to devlin 2014/04/11 15:04:25 I looked at the documentation in callback.h. It re
paulmeyer 2014/04/11 19:09:49 Done.
2000 if (!zoom_controller->SetZoomLevelByExtension(
2001 zoom_level,
2002 GetExtension(),
2003 base::Bind(&TabsSetZoomFunction::Callback, this_ref))) {
not at google - send to devlin 2014/04/09 03:52:11 you can also bind to SendReseponse directly: base
paulmeyer 2014/04/11 03:01:02 Done.
2004 // Tried to zoom a tab in disabled mode.
2005 error_ = keys::kCannotZoomDisabledTabError;
2006 SendResponse(false);
2007 return false;
2008 }
2009
2010 return true;
2011 }
2012
2013 void TabsSetZoomFunction::Callback() {
2014 SendResponse(true);
2015 }
2016
2017 bool TabsGetZoomFunction::RunImpl() {
2018 scoped_ptr<tabs::GetZoom::Params> params(
2019 tabs::GetZoom::Params::Create(*args_));
2020 EXTENSION_FUNCTION_VALIDATE(params.get());
2021
2022 WebContents* web_contents = NULL;
2023 if (!GetWebContents(params->tab_id.get(), &web_contents)) {
2024 SendResponse(false);
2025 return false;
2026 }
2027
2028 double zoom_level = web_contents->GetZoomLevel();
2029 double zoom_factor = content::ZoomLevelToZoomFactor(zoom_level);
2030 SetResult(new base::FundamentalValue(zoom_factor));
not at google - send to devlin 2014/04/09 03:52:11 prefer result_ = tabs::getZoom::Result::Create(..
paulmeyer 2014/04/11 03:01:02 Done.
2031 SendResponse(true);
2032 return true;
2033 }
2034
2035 bool TabsSetZoomSettingsFunction::RunImpl() {
2036 scoped_ptr<tabs::SetZoomSettings::Params> params(
2037 tabs::SetZoomSettings::Params::Create(*args_));
2038 EXTENSION_FUNCTION_VALIDATE(params.get());
2039
2040 WebContents* web_contents = NULL;
2041 if (!GetWebContents(params->tab_id.get(), &web_contents)) {
2042 SendResponse(false);
2043 return false;
2044 } else if (web_contents->GetURL().SchemeIs("chrome")) {
2045 error_ = keys::kCannotChangeChromePageZoomSettingsError;
2046 SendResponse(false);
2047 return false;
2048 }
2049
2050 // Set default mode when omitted.
2051 if (params->zoom_settings.mode == api::tabs::ZoomSettings::MODE_NONE)
2052 params->zoom_settings.mode = api::tabs::ZoomSettings::MODE_AUTOMATIC;
2053
2054 // "per-origin" scope is only available in "automatic" mode.
2055 if (params->zoom_settings.scope ==
2056 api::tabs::ZoomSettings::SCOPE_PER_ORIGIN &&
2057 params->zoom_settings.mode != api::tabs::ZoomSettings::MODE_AUTOMATIC) {
2058 error_ = keys::kPerOriginOnlyInAutomaticError;
2059 SendResponse(false);
2060 return false;
2061 }
2062
2063 // Determine the correct internal zoom mode to set |web_contents| to from the
2064 // user-specified |zoom_settings|.
2065 content::ZoomMode zoom_mode = content::kZoomModeDefault;
2066 switch (params->zoom_settings.mode) {
2067 case api::tabs::ZoomSettings::MODE_AUTOMATIC:
2068 switch (params->zoom_settings.scope) {
2069 case api::tabs::ZoomSettings::SCOPE_NONE:
2070 case api::tabs::ZoomSettings::SCOPE_PER_ORIGIN:
2071 zoom_mode = content::kZoomModeDefault;
2072 break;
2073 case api::tabs::ZoomSettings::SCOPE_PER_TAB:
2074 zoom_mode = content::kZoomModeIsolated;
2075 break;
2076 default:
not at google - send to devlin 2014/04/09 03:52:11 don't specify a default case, then you will get co
paulmeyer 2014/04/11 03:01:02 Done.
2077 NOTREACHED();
2078 }
2079 break;
2080 case api::tabs::ZoomSettings::MODE_MANUAL:
2081 zoom_mode = content::kZoomModeManual;
2082 break;
2083 case api::tabs::ZoomSettings::MODE_DISABLED:
2084 zoom_mode = content::kZoomModeDisabled;
2085 break;
2086 default:
not at google - send to devlin 2014/04/09 03:52:11 ditto
paulmeyer 2014/04/11 03:01:02 Done.
2087 NOTREACHED();
2088 }
2089
2090 web_contents->SetZoomMode(zoom_mode);
2091
2092 SendResponse(true);
2093 return true;
2094 }
2095
2096 bool TabsGetZoomSettingsFunction::RunImpl() {
2097 scoped_ptr<tabs::GetZoomSettings::Params> params(
2098 tabs::GetZoomSettings::Params::Create(*args_));
2099 EXTENSION_FUNCTION_VALIDATE(params.get());
2100
2101 WebContents* web_contents = NULL;
2102 if (!GetWebContents(params->tab_id.get(), &web_contents)) {
2103 SendResponse(false);
2104 return false;
2105 }
2106
2107 content::ZoomMode zoom_mode = web_contents->GetZoomMode();
2108 base::DictionaryValue* zoom_settings = new base::DictionaryValue();
2109 ZoomModeToZoomSettings(zoom_mode, zoom_settings);
not at google - send to devlin 2014/04/09 03:52:11 my comments at the top aside - if this function is
paulmeyer 2014/04/11 03:01:02 It is also used in tabs_event_router.cc, but I imp
2110
2111 SetResult(zoom_settings);
2112 SendResponse(true);
2113 return true;
2114 }
2115
1920 } // namespace extensions 2116 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698