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/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 Loading... | |
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; | |
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(); | |
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 1362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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; |
Fady Samuel
2014/04/07 20:01:14
Spacing looks wrong here.
paulmeyer
2014/04/07 20:56:10
Done.
| |
1601 } | 1632 } |
1602 | 1633 |
1603 if (web_contents->ShowingInterstitialPage()) { | 1634 if (web_contents->ShowingInterstitialPage()) { |
1604 // This does as same as Browser::ReloadInternal. | 1635 // This does as same as Browser::ReloadInternal. |
1605 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); | 1636 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry(); |
1606 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB, | 1637 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB, |
1607 content::PAGE_TRANSITION_RELOAD, false); | 1638 content::PAGE_TRANSITION_RELOAD, false); |
1608 GetCurrentBrowser()->OpenURL(params); | 1639 GetCurrentBrowser()->OpenURL(params); |
1609 } else if (bypass_cache) { | 1640 } else if (bypass_cache) { |
1610 web_contents->GetController().ReloadIgnoringCache(true); | 1641 web_contents->GetController().ReloadIgnoringCache(true); |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1910 | 1941 |
1911 execute_tab_id_ = tab_id; | 1942 execute_tab_id_ = tab_id; |
1912 details_ = details.Pass(); | 1943 details_ = details.Pass(); |
1913 return true; | 1944 return true; |
1914 } | 1945 } |
1915 | 1946 |
1916 bool TabsInsertCSSFunction::ShouldInsertCSS() const { | 1947 bool TabsInsertCSSFunction::ShouldInsertCSS() const { |
1917 return true; | 1948 return true; |
1918 } | 1949 } |
1919 | 1950 |
1951 bool ZoomAPIFunction::GetWebContents(int* tab_id, | |
1952 content::WebContents** web_contents) { | |
1953 if (tab_id) { | |
1954 if (!GetTabById(*tab_id, | |
1955 GetProfile(), | |
1956 include_incognito(), | |
1957 NULL, | |
1958 NULL, | |
1959 web_contents, | |
1960 NULL, | |
1961 &error_)) { | |
1962 return false; | |
1963 } | |
1964 } else { | |
1965 Browser* browser = GetCurrentBrowser(); | |
1966 if (!browser) { | |
1967 error_ = keys::kNoCurrentWindowError; | |
1968 return false; | |
1969 } | |
1970 if (!ExtensionTabUtil::GetDefaultTab(browser, web_contents, NULL)) { | |
1971 error_ = keys::kNoCurrentTabError; | |
1972 return false; | |
1973 } | |
1974 } | |
1975 return true; | |
1976 } | |
1977 | |
1978 TabsSetZoomFunction::TabsSetZoomFunction() : zoom_id_(0) {} | |
1979 | |
1980 bool TabsSetZoomFunction::RunImpl() { | |
1981 scoped_ptr<tabs::SetZoom::Params> params( | |
1982 tabs::SetZoom::Params::Create(*args_)); | |
1983 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
1984 | |
1985 WebContents* web_contents = NULL; | |
1986 if (!GetWebContents(params->tab_id.get(), &web_contents)) { | |
1987 SendResponse(false); | |
1988 return false; | |
1989 } else if (web_contents->GetURL().SchemeIs("chrome")) { | |
Fady Samuel
2014/04/07 20:01:14
Are we sure this API is also isolated from <webvie
paulmeyer
2014/04/07 20:56:10
Perhaps not for now due to the existing <webview>
| |
1990 error_ = keys::kCannotZoomChromePagesError; | |
1991 SendResponse(false); | |
1992 return false; | |
1993 } | |
1994 | |
1995 ZoomController* zoom_controller = | |
1996 ZoomController::FromWebContents(web_contents); | |
1997 double zoom_level = content::ZoomFactorToZoomLevel(params->zoom_factor); | |
1998 zoom_id_ = zoom_controller->SetZoomLevelByExtension(zoom_level, | |
1999 GetExtension()); | |
2000 | |
2001 if (!zoom_id_) { | |
2002 // Tried to zoom a tab in disabled mode. | |
2003 error_ = keys::kCannotZoomDisabledTabError; | |
2004 SendResponse(false); | |
2005 return false; | |
2006 } | |
2007 | |
2008 // Keep this function alive so that it can call its callback once the zoom | |
2009 // change has completed. | |
2010 AddRef(); | |
2011 registrar_.Add(this, chrome::NOTIFICATION_TAB_ZOOM_CHANGE_COMPLETE, | |
Fady Samuel
2014/04/07 20:04:53
So you don't need this notification if you have a
paulmeyer
2014/04/07 20:56:10
I believe that is true.
| |
2012 content::NotificationService::AllSources()); | |
2013 | |
2014 return true; | |
2015 } | |
2016 | |
2017 void TabsSetZoomFunction::Observe(int type, | |
2018 const content::NotificationSource& source, | |
2019 const content::NotificationDetails& details) { | |
2020 DCHECK(type == chrome::NOTIFICATION_TAB_ZOOM_CHANGE_COMPLETE); | |
Fady Samuel
2014/04/07 20:01:14
If we switch to a callback approach, would this no
paulmeyer
2014/04/07 20:56:10
I believe it can be taken out if callbacks are use
| |
2021 | |
2022 // Make sure it is this zoom change that has completed. | |
2023 int zoom_id = *content::Details<int>(details).ptr(); | |
2024 if (zoom_id != zoom_id_) { | |
2025 return; | |
2026 } | |
2027 | |
2028 // Release the function once it has called its callback. | |
2029 SendResponse(true); | |
2030 registrar_.RemoveAll(); | |
2031 Release(); | |
2032 } | |
2033 | |
2034 bool TabsGetZoomFunction::RunImpl() { | |
2035 scoped_ptr<tabs::GetZoom::Params> params( | |
2036 tabs::GetZoom::Params::Create(*args_)); | |
2037 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
2038 | |
2039 WebContents* web_contents = NULL; | |
2040 if (!GetWebContents(params->tab_id.get(), &web_contents)) { | |
2041 SendResponse(false); | |
2042 return false; | |
2043 } | |
2044 | |
2045 double zoom_level = web_contents->GetZoomLevel(); | |
2046 double zoom_factor = content::ZoomLevelToZoomFactor(zoom_level); | |
2047 SetResult(new base::FundamentalValue(zoom_factor)); | |
2048 SendResponse(true); | |
2049 return true; | |
2050 } | |
2051 | |
2052 bool TabsSetZoomSettingsFunction::RunImpl() { | |
2053 scoped_ptr<tabs::SetZoomSettings::Params> params( | |
2054 tabs::SetZoomSettings::Params::Create(*args_)); | |
2055 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
2056 | |
2057 WebContents* web_contents = NULL; | |
2058 if (!GetWebContents(params->tab_id.get(), &web_contents)) { | |
2059 SendResponse(false); | |
2060 return false; | |
2061 } else if (web_contents->GetURL().SchemeIs("chrome")) { | |
Fady Samuel
2014/04/07 20:01:14
What about <webview>?
paulmeyer
2014/04/07 20:56:10
Can be fixed in a separate patch.
| |
2062 error_ = keys::kCannotChangeChromePageZoomSettingsError; | |
2063 SendResponse(false); | |
2064 return false; | |
2065 } | |
2066 | |
2067 if (params->zoom_settings.scope == | |
Fady Samuel
2014/04/07 20:01:14
This is very confusing. I think it would be nice t
paulmeyer
2014/04/07 20:56:10
I made this clearer by setting the default mode an
| |
2068 api::tabs::ZoomSettings::SCOPE_PER_ORIGIN && | |
2069 params->zoom_settings.mode != api::tabs::ZoomSettings::MODE_NONE && | |
2070 params->zoom_settings.mode != api::tabs::ZoomSettings::MODE_AUTOMATIC) { | |
2071 error_ = keys::kPerOriginOnlyInAutomaticError; | |
2072 SendResponse(false); | |
2073 return false; | |
2074 } | |
2075 | |
2076 content::ZoomMode zoom_mode = content::kZoomModeDefault; | |
2077 switch (params->zoom_settings.mode) { | |
2078 case api::tabs::ZoomSettings::MODE_NONE: | |
2079 case api::tabs::ZoomSettings::MODE_AUTOMATIC: | |
2080 switch (params->zoom_settings.scope) { | |
2081 case api::tabs::ZoomSettings::SCOPE_NONE: | |
2082 case api::tabs::ZoomSettings::SCOPE_PER_ORIGIN: | |
2083 zoom_mode = content::kZoomModeDefault; | |
2084 break; | |
2085 case api::tabs::ZoomSettings::SCOPE_PER_TAB: | |
2086 zoom_mode = content::kZoomModeIsolated; | |
2087 break; | |
2088 default: | |
2089 NOTREACHED(); | |
2090 } | |
2091 break; | |
2092 case api::tabs::ZoomSettings::MODE_MANUAL: | |
2093 zoom_mode = content::kZoomModeManual; | |
2094 break; | |
2095 case api::tabs::ZoomSettings::MODE_DISABLED: | |
2096 zoom_mode = content::kZoomModeDisabled; | |
2097 break; | |
2098 default: | |
2099 NOTREACHED(); | |
2100 } | |
2101 | |
2102 web_contents->SetZoomMode(zoom_mode); | |
2103 | |
2104 SendResponse(true); | |
2105 return true; | |
2106 } | |
2107 | |
2108 bool TabsGetZoomSettingsFunction::RunImpl() { | |
2109 scoped_ptr<tabs::GetZoomSettings::Params> params( | |
2110 tabs::GetZoomSettings::Params::Create(*args_)); | |
2111 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
2112 | |
2113 WebContents* web_contents = NULL; | |
2114 if (!GetWebContents(params->tab_id.get(), &web_contents)) { | |
2115 SendResponse(false); | |
2116 return false; | |
2117 } | |
2118 | |
2119 content::ZoomMode zoom_mode = web_contents->GetZoomMode(); | |
2120 base::DictionaryValue* zoom_settings = new base::DictionaryValue(); | |
2121 ZoomModeToZoomSettings(zoom_mode, zoom_settings); | |
2122 | |
2123 SetResult(zoom_settings->DeepCopy()); | |
Fady Samuel
2014/04/07 20:01:14
You're leaking memory here. Just SetResult(zoom_se
paulmeyer
2014/04/07 20:56:10
Done.
| |
2124 SendResponse(true); | |
2125 return true; | |
2126 } | |
2127 | |
1920 } // namespace extensions | 2128 } // namespace extensions |
OLD | NEW |