[email protected] | 4801ecc | 2009-04-05 04:52:58 | [diff] [blame] | 1 | // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | c61db1d | 2009-02-06 03:39:18 | [diff] [blame] | 5 | #include "chrome/browser/browser.h" |
| 6 | |
[email protected] | 6289beb | 2009-06-06 10:04:15 | [diff] [blame] | 7 | #include "app/animation.h" |
[email protected] | a92b864 | 2009-05-05 23:38:56 | [diff] [blame] | 8 | #include "app/l10n_util.h" |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 9 | #include "base/command_line.h" |
| 10 | #include "base/idle_timer.h" |
[email protected] | 22cdd93 | 2009-08-18 02:16:21 | [diff] [blame] | 11 | #include "base/keyboard_codes.h" |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 12 | #include "base/logging.h" |
| 13 | #include "base/string_util.h" |
[email protected] | b689fce7 | 2009-03-17 22:45:34 | [diff] [blame] | 14 | #include "base/thread.h" |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 15 | #include "chrome/app/chrome_dll_resource.h" |
[email protected] | a9afddb | 2009-02-12 17:49:42 | [diff] [blame] | 16 | #include "chrome/browser/bookmarks/bookmark_model.h" |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 17 | #include "chrome/browser/browser_list.h" |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame] | 18 | #include "chrome/browser/browser_process.h" |
[email protected] | c61db1d | 2009-02-06 03:39:18 | [diff] [blame] | 19 | #include "chrome/browser/browser_shutdown.h" |
[email protected] | c98fe6f | 2009-02-25 20:33:23 | [diff] [blame] | 20 | #include "chrome/browser/browser_window.h" |
[email protected] | 40d59ce5 | 2009-03-06 23:20:14 | [diff] [blame] | 21 | #include "chrome/browser/character_encoding.h" |
[email protected] | 40ecc90 | 2009-03-16 13:42:47 | [diff] [blame] | 22 | #include "chrome/browser/debugger/devtools_manager.h" |
[email protected] | 59560e0b | 2009-06-04 03:30:22 | [diff] [blame] | 23 | #include "chrome/browser/download/download_item_model.h" |
[email protected] | 69444cc | 2009-04-09 20:40:06 | [diff] [blame] | 24 | #include "chrome/browser/download/download_manager.h" |
[email protected] | 59560e0b | 2009-06-04 03:30:22 | [diff] [blame] | 25 | #include "chrome/browser/download/download_shelf.h" |
| 26 | #include "chrome/browser/download/download_started_animation.h" |
[email protected] | 371ed7a | 2009-08-25 15:22:46 | [diff] [blame^] | 27 | #include "chrome/browser/extensions/crashed_extension_infobar.h" |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 28 | #include "chrome/browser/extensions/extension_disabled_infobar_delegate.h" |
[email protected] | 4801ecc | 2009-04-05 04:52:58 | [diff] [blame] | 29 | #include "chrome/browser/find_bar.h" |
| 30 | #include "chrome/browser/find_bar_controller.h" |
[email protected] | 7745b82 | 2009-01-27 20:15:35 | [diff] [blame] | 31 | #include "chrome/browser/location_bar.h" |
[email protected] | d938aed9 | 2009-01-22 19:49:33 | [diff] [blame] | 32 | #include "chrome/browser/metrics/user_metrics.h" |
[email protected] | a239c3f | 2009-02-17 22:13:19 | [diff] [blame] | 33 | #include "chrome/browser/net/url_fixer_upper.h" |
[email protected] | 0e3e3e3 | 2009-05-14 01:00:53 | [diff] [blame] | 34 | #include "chrome/browser/options_window.h" |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 35 | #include "chrome/browser/profile.h" |
[email protected] | 14e60c8d | 2009-06-29 03:56:51 | [diff] [blame] | 36 | #include "chrome/browser/renderer_host/site_instance.h" |
[email protected] | 85e921fb8 | 2009-02-11 23:19:44 | [diff] [blame] | 37 | #include "chrome/browser/sessions/session_service.h" |
[email protected] | c61db1d | 2009-02-06 03:39:18 | [diff] [blame] | 38 | #include "chrome/browser/sessions/session_types.h" |
[email protected] | bd580a25 | 2009-02-12 01:16:30 | [diff] [blame] | 39 | #include "chrome/browser/sessions/tab_restore_service.h" |
[email protected] | abf6c7a | 2009-03-03 16:24:10 | [diff] [blame] | 40 | #include "chrome/browser/status_bubble.h" |
[email protected] | a165a05 | 2009-02-18 21:43:00 | [diff] [blame] | 41 | #include "chrome/browser/tab_contents/interstitial_page.h" |
[email protected] | c61db1d | 2009-02-06 03:39:18 | [diff] [blame] | 42 | #include "chrome/browser/tab_contents/navigation_controller.h" |
| 43 | #include "chrome/browser/tab_contents/navigation_entry.h" |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 44 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | 81af939 | 2009-04-21 02:37:45 | [diff] [blame] | 45 | #include "chrome/browser/tab_contents/tab_contents_view.h" |
[email protected] | 4d34e2e | 2009-05-26 22:55:28 | [diff] [blame] | 46 | #include "chrome/browser/window_sizer.h" |
[email protected] | d938aed9 | 2009-01-22 19:49:33 | [diff] [blame] | 47 | #include "chrome/common/chrome_constants.h" |
| 48 | #include "chrome/common/chrome_switches.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 49 | #include "chrome/common/extensions/extension.h" |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 50 | #include "chrome/common/notification_service.h" |
[email protected] | d938aed9 | 2009-01-22 19:49:33 | [diff] [blame] | 51 | #include "chrome/common/page_transition_types.h" |
[email protected] | 59560e0b | 2009-06-04 03:30:22 | [diff] [blame] | 52 | #include "chrome/common/platform_util.h" |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 53 | #include "chrome/common/pref_names.h" |
| 54 | #include "chrome/common/pref_service.h" |
[email protected] | b689fce7 | 2009-03-17 22:45:34 | [diff] [blame] | 55 | #include "chrome/common/url_constants.h" |
[email protected] | 026e34a | 2009-02-06 00:06:12 | [diff] [blame] | 56 | #ifdef CHROME_PERSONALIZATION |
[email protected] | ca38d8e | 2009-08-20 18:08:16 | [diff] [blame] | 57 | #include "chrome/browser/sync/profile_sync_service.h" |
[email protected] | 026e34a | 2009-02-06 00:06:12 | [diff] [blame] | 58 | #endif |
[email protected] | a3e18c4 | 2009-03-04 23:36:05 | [diff] [blame] | 59 | #include "grit/chromium_strings.h" |
| 60 | #include "grit/generated_resources.h" |
| 61 | #include "grit/locale_settings.h" |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 62 | #include "net/base/cookie_monster.h" |
| 63 | #include "net/base/cookie_policy.h" |
[email protected] | 2bfd94d | 2009-04-29 20:32:03 | [diff] [blame] | 64 | #include "net/base/mime_util.h" |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 65 | #include "net/base/net_util.h" |
| 66 | #include "net/base/registry_controlled_domain.h" |
| 67 | #include "net/url_request/url_request_context.h" |
[email protected] | d938aed9 | 2009-01-22 19:49:33 | [diff] [blame] | 68 | #include "webkit/glue/window_open_disposition.h" |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 69 | |
| 70 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | #include <windows.h> |
| 72 | #include <shellapi.h> |
| 73 | |
[email protected] | 4a0765a | 2009-05-08 23:12:25 | [diff] [blame] | 74 | #include "app/win_util.h" |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 75 | #include "chrome/browser/automation/ui_controls.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | #include "chrome/browser/browser_process.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | #include "chrome/browser/browser_url_handler.h" |
| 78 | #include "chrome/browser/cert_store.h" |
[email protected] | 37936ee | 2008-09-14 01:09:50 | [diff] [blame] | 79 | #include "chrome/browser/download/save_package.h" |
[email protected] | 3b073b2 | 2009-01-16 03:29:03 | [diff] [blame] | 80 | #include "chrome/browser/ssl/ssl_error_info.h" |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 81 | #include "chrome/browser/task_manager.h" |
[email protected] | f253006 | 2008-12-03 23:52:03 | [diff] [blame] | 82 | #include "chrome/browser/user_data_manager.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | #include "chrome/browser/view_ids.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | #include "chrome/browser/views/location_bar_view.h" |
[email protected] | 6dffde32 | 2009-02-18 03:47:48 | [diff] [blame] | 85 | #include "chrome/common/child_process_host.h" |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 86 | #endif // OS_WIN |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 87 | |
[email protected] | 33e5f44 | 2009-05-28 03:30:27 | [diff] [blame] | 88 | #if !defined(OS_MACOSX) |
[email protected] | 7d3feb91 | 2009-05-15 20:37:42 | [diff] [blame] | 89 | #include "chrome/browser/dock_info.h" |
| 90 | #endif |
| 91 | |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 92 | using base::TimeDelta; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | |
| 94 | // How long we wait before updating the browser chrome while loading a page. |
| 95 | static const int kUIUpdateCoalescingTimeMS = 200; |
| 96 | |
| 97 | // Idle time before helping prune memory consumption. |
| 98 | static const int kBrowserReleaseMemoryInterval = 30; // In seconds. |
| 99 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 100 | /////////////////////////////////////////////////////////////////////////////// |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | |
[email protected] | a436d92 | 2009-02-13 23:16:42 | [diff] [blame] | 102 | // A task to reduce the working set of the child processes that live on the IO |
| 103 | // thread (i.e. plugins, workers). |
| 104 | class ReduceChildProcessesWorkingSetTask : public Task { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | public: |
| 106 | virtual void Run() { |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 107 | #if defined(OS_WIN) |
[email protected] | 6dffde32 | 2009-02-18 03:47:48 | [diff] [blame] | 108 | for (ChildProcessHost::Iterator iter; !iter.Done(); ++iter) |
| 109 | iter->ReduceWorkingSet(); |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 110 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | } |
| 112 | }; |
| 113 | |
| 114 | // A browser task to run when the user is not using the browser. |
| 115 | // In our case, we're trying to be nice to the operating system and release |
| 116 | // memory not in use. |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 117 | class BrowserIdleTimer : public base::IdleTimer { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | public: |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 119 | BrowserIdleTimer() |
| 120 | : base::IdleTimer(TimeDelta::FromSeconds(kBrowserReleaseMemoryInterval), |
| 121 | false) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | virtual void OnIdle() { |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 125 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | // We're idle. Release browser and renderer unused pages. |
| 127 | |
| 128 | // Handle the Browser. |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 129 | base::Process process(GetCurrentProcess()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 130 | process.ReduceWorkingSet(); |
| 131 | |
| 132 | // Handle the Renderer(s). |
[email protected] | 9de09f8 | 2009-08-17 20:13:53 | [diff] [blame] | 133 | RenderProcessHost::iterator renderer_iter( |
| 134 | RenderProcessHost::AllHostsIterator()); |
| 135 | while (!renderer_iter.IsAtEnd()) { |
| 136 | base::Process process = renderer_iter.GetCurrentValue()->process(); |
| 137 | process.ReduceWorkingSet(); |
| 138 | renderer_iter.Advance(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | a436d92 | 2009-02-13 23:16:42 | [diff] [blame] | 141 | // Handle the child processe. We need to iterate through them on the IO |
| 142 | // thread because that thread manages the child process collection. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, |
[email protected] | a436d92 | 2009-02-13 23:16:42 | [diff] [blame] | 144 | new ReduceChildProcessesWorkingSetTask()); |
[email protected] | 8a53ee04 | 2009-01-21 16:41:33 | [diff] [blame] | 145 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | } |
| 147 | }; |
| 148 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 149 | /////////////////////////////////////////////////////////////////////////////// |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | |
| 151 | struct Browser::UIUpdate { |
| 152 | UIUpdate(const TabContents* src, unsigned flags) |
| 153 | : source(src), |
| 154 | changed_flags(flags) { |
| 155 | } |
| 156 | |
| 157 | // The source of the update. |
| 158 | const TabContents* source; |
| 159 | |
| 160 | // What changed in the UI. |
| 161 | unsigned changed_flags; |
| 162 | }; |
| 163 | |
[email protected] | 505323e2 | 2009-01-24 02:47:58 | [diff] [blame] | 164 | namespace { |
| 165 | |
| 166 | // Returns true if the specified TabContents has unload listeners registered. |
| 167 | bool TabHasUnloadListener(TabContents* contents) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 168 | return contents->notify_disconnection() && |
| 169 | !contents->showing_interstitial_page() && |
| 170 | !contents->render_view_host()->SuddenTerminationAllowed(); |
[email protected] | 505323e2 | 2009-01-24 02:47:58 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | } // namespace |
| 174 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 175 | /////////////////////////////////////////////////////////////////////////////// |
| 176 | // Browser, Constructors, Creation, Showing: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 177 | |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 178 | Browser::Browser(Type type, Profile* profile) |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 179 | : type_(type), |
| 180 | profile_(profile), |
[email protected] | f3e99e3 | 2008-07-30 04:48:39 | [diff] [blame] | 181 | window_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 182 | tabstrip_model_(this, profile), |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 183 | command_updater_(this), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | toolbar_model_(this), |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 185 | chrome_updater_factory_(this), |
| 186 | is_attempting_to_close_browser_(false), |
[email protected] | 69444cc | 2009-04-09 20:40:06 | [diff] [blame] | 187 | cancel_download_confirmation_state_(NOT_PROMPTED), |
[email protected] | 2e71662 | 2009-03-09 21:11:01 | [diff] [blame] | 188 | maximized_state_(MAXIMIZED_STATE_DEFAULT), |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 189 | method_factory_(this), |
[email protected] | ebdcf974 | 2009-01-23 05:25:28 | [diff] [blame] | 190 | idle_task_(new BrowserIdleTimer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 191 | tabstrip_model_.AddObserver(this); |
| 192 | |
[email protected] | 0cb9410 | 2009-05-22 19:51:21 | [diff] [blame] | 193 | registrar_.Add(this, NotificationType::SSL_VISIBLE_STATE_CHANGED, |
| 194 | NotificationService::AllSources()); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 195 | registrar_.Add(this, NotificationType::EXTENSION_UPDATE_DISABLED, |
| 196 | NotificationService::AllSources()); |
[email protected] | 0cb9410 | 2009-05-22 19:51:21 | [diff] [blame] | 197 | registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, |
| 198 | NotificationService::AllSources()); |
[email protected] | 371ed7a | 2009-08-25 15:22:46 | [diff] [blame^] | 199 | registrar_.Add(this, NotificationType::EXTENSION_PROCESS_CRASHED, |
| 200 | NotificationService::AllSources()); |
[email protected] | e001d41 | 2009-06-26 20:53:25 | [diff] [blame] | 201 | registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, |
| 202 | NotificationService::AllSources()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 203 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 204 | InitCommandState(); |
| 205 | BrowserList::AddBrowser(this); |
| 206 | |
| 207 | encoding_auto_detect_.Init(prefs::kWebKitUsesUniversalDetector, |
| 208 | profile_->GetPrefs(), NULL); |
| 209 | |
| 210 | // Trim browser memory on idle for low & medium memory models. |
| 211 | if (g_browser_process->memory_model() < BrowserProcess::HIGH_MEMORY_MODEL) |
| 212 | idle_task_->Start(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | Browser::~Browser() { |
| 216 | // The tab strip should be empty at this point. |
| 217 | DCHECK(tabstrip_model_.empty()); |
| 218 | tabstrip_model_.RemoveObserver(this); |
| 219 | |
| 220 | BrowserList::RemoveBrowser(this); |
| 221 | |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 222 | #if defined(OS_WIN) || defined(OS_LINUX) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | if (!BrowserList::HasBrowserWithProfile(profile_)) { |
| 224 | // We're the last browser window with this profile. We need to nuke the |
| 225 | // TabRestoreService, which will start the shutdown of the |
| 226 | // NavigationControllers and allow for proper shutdown. If we don't do this |
| 227 | // chrome won't shutdown cleanly, and may end up crashing when some |
| 228 | // thread tries to use the IO thread (or another thread) that is no longer |
| 229 | // valid. |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 230 | // This isn't a valid assumption for Mac OS, as it stays running after |
| 231 | // the last browser has closed. The Mac equivalent is in its app |
| 232 | // controller. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | profile_->ResetTabRestoreService(); |
| 234 | } |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 235 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | |
| 237 | SessionService* session_service = profile_->GetSessionService(); |
| 238 | if (session_service) |
| 239 | session_service->WindowClosed(session_id_); |
| 240 | |
[email protected] | d8375fd | 2008-11-25 22:45:39 | [diff] [blame] | 241 | TabRestoreService* tab_restore_service = profile()->GetTabRestoreService(); |
| 242 | if (tab_restore_service) |
| 243 | tab_restore_service->BrowserClosed(this); |
| 244 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | if (profile_->IsOffTheRecord() && |
| 246 | !BrowserList::IsOffTheRecordSessionActive()) { |
[email protected] | 860f5549 | 2009-03-27 19:50:59 | [diff] [blame] | 247 | // An off-the-record profile is no longer needed, this indirectly |
| 248 | // frees its cache and cookies. |
| 249 | profile_->GetOriginalProfile()->DestroyOffTheRecordProfile(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // There may be pending file dialogs, we need to tell them that we've gone |
| 253 | // away so they don't try and call back to us. |
| 254 | if (select_file_dialog_.get()) |
| 255 | select_file_dialog_->ListenerDestroyed(); |
| 256 | } |
| 257 | |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 258 | // static |
| 259 | Browser* Browser::Create(Profile* profile) { |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 260 | Browser* browser = new Browser(TYPE_NORMAL, profile); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 261 | browser->CreateBrowserWindow(); |
| 262 | return browser; |
| 263 | } |
[email protected] | 6104acf | 2008-11-11 22:27:34 | [diff] [blame] | 264 | |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 265 | // static |
| 266 | Browser* Browser::CreateForPopup(Profile* profile) { |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 267 | Browser* browser = new Browser(TYPE_POPUP, profile); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 268 | browser->CreateBrowserWindow(); |
| 269 | return browser; |
| 270 | } |
| 271 | |
| 272 | // static |
| 273 | Browser* Browser::CreateForApp(const std::wstring& app_name, |
[email protected] | d5fbc00 | 2009-02-27 22:12:58 | [diff] [blame] | 274 | Profile* profile, bool is_popup) { |
| 275 | Browser* browser = new Browser(is_popup? TYPE_APP_POPUP : TYPE_APP, profile); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 276 | browser->app_name_ = app_name; |
| 277 | browser->CreateBrowserWindow(); |
| 278 | return browser; |
| 279 | } |
| 280 | |
| 281 | void Browser::CreateBrowserWindow() { |
| 282 | DCHECK(!window_); |
[email protected] | 9c45b718 | 2009-08-04 16:44:43 | [diff] [blame] | 283 | |
| 284 | if (SupportsWindowFeature(FEATURE_EXTENSIONSHELF)) |
| 285 | extension_shelf_model_.reset(new ExtensionShelfModel(this)); |
| 286 | |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 287 | window_ = BrowserWindow::CreateBrowserWindow(this); |
| 288 | |
| 289 | // Show the First Run information bubble if we've been told to. |
| 290 | PrefService* local_state = g_browser_process->local_state(); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 291 | if (!local_state) |
| 292 | return; |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 293 | if (local_state->IsPrefRegistered(prefs::kShouldShowFirstRunBubble) && |
| 294 | local_state->GetBoolean(prefs::kShouldShowFirstRunBubble)) { |
[email protected] | b1390d0 | 2009-05-19 23:50:37 | [diff] [blame] | 295 | bool show_OEM_bubble = (local_state-> |
| 296 | IsPrefRegistered(prefs::kShouldUseOEMFirstRunBubble) && |
| 297 | local_state->GetBoolean(prefs::kShouldUseOEMFirstRunBubble)); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 298 | // Reset the preference so we don't show the bubble for subsequent windows. |
| 299 | local_state->ClearPref(prefs::kShouldShowFirstRunBubble); |
[email protected] | b1390d0 | 2009-05-19 23:50:37 | [diff] [blame] | 300 | window_->GetLocationBar()->ShowFirstRunBubble(show_OEM_bubble); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | } |
[email protected] | 4801ecc | 2009-04-05 04:52:58 | [diff] [blame] | 302 | |
[email protected] | d8730117 | 2009-05-31 04:28:18 | [diff] [blame] | 303 | #if !(defined(OS_LINUX) && defined(TOOLKIT_VIEWS)) |
[email protected] | 4801ecc | 2009-04-05 04:52:58 | [diff] [blame] | 304 | FindBar* find_bar = BrowserWindow::CreateFindBar(this); |
| 305 | find_bar_controller_.reset(new FindBarController(find_bar)); |
| 306 | find_bar->SetFindBarController(find_bar_controller_.get()); |
[email protected] | 21230ca | 2009-05-30 08:27:29 | [diff] [blame] | 307 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 308 | } |
| 309 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 310 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame] | 311 | // Getters & Setters |
| 312 | |
| 313 | const std::vector<std::wstring>& Browser::user_data_dir_profiles() const { |
| 314 | return g_browser_process->user_data_dir_profiles(); |
| 315 | } |
| 316 | |
| 317 | void Browser::set_user_data_dir_profiles( |
| 318 | const std::vector<std::wstring>& profiles) { |
| 319 | g_browser_process->user_data_dir_profiles() = profiles; |
| 320 | } |
| 321 | |
| 322 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 323 | // Browser, Creation Helpers: |
| 324 | |
| 325 | // static |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 326 | void Browser::OpenEmptyWindow(Profile* profile) { |
| 327 | Browser* browser = Browser::Create(profile); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 328 | browser->AddBlankTab(true); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 329 | browser->window()->Show(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | } |
| 331 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 332 | // static |
[email protected] | fbc947b | 2009-06-19 13:28:24 | [diff] [blame] | 333 | void Browser::OpenWindowWithRestoredTabs(Profile* profile) { |
| 334 | TabRestoreService* service = profile->GetTabRestoreService(); |
| 335 | if (service) |
| 336 | service->RestoreMostRecentEntry(NULL); |
| 337 | } |
| 338 | |
| 339 | // static |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 340 | void Browser::OpenURLOffTheRecord(Profile* profile, const GURL& url) { |
| 341 | Profile* off_the_record_profile = profile->GetOffTheRecordProfile(); |
| 342 | Browser* browser = BrowserList::FindBrowserWithType( |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 343 | off_the_record_profile, |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 344 | TYPE_NORMAL); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 345 | if (!browser) |
| 346 | browser = Browser::Create(off_the_record_profile); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 347 | // TODO(eroman): should we have referrer here? |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 348 | browser->AddTabWithURL(url, GURL(), PageTransition::LINK, true, -1, false, |
| 349 | NULL); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 350 | browser->window()->Show(); |
[email protected] | 2baf83d | 2008-07-30 05:58:17 | [diff] [blame] | 351 | } |
| 352 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 353 | // static |
[email protected] | 0303f31c | 2009-02-02 06:42:05 | [diff] [blame] | 354 | void Browser::OpenApplicationWindow(Profile* profile, const GURL& url) { |
| 355 | std::wstring app_name = ComputeApplicationNameFromURL(url); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 356 | RegisterAppPrefs(app_name); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 357 | |
[email protected] | d5fbc00 | 2009-02-27 22:12:58 | [diff] [blame] | 358 | Browser* browser = Browser::CreateForApp(app_name, profile, false); |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 359 | browser->AddTabWithURL(url, GURL(), PageTransition::START_PAGE, true, -1, |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 360 | false, NULL); |
[email protected] | 840b151 | 2009-07-21 16:53:46 | [diff] [blame] | 361 | |
[email protected] | 3df2aaf | 2009-07-25 00:24:57 | [diff] [blame] | 362 | #if defined(OS_WIN) |
| 363 | // Set the app user model id for this application to that of the application |
| 364 | // name. See http://crbug.com/7028. |
| 365 | win_util::SetAppIdForWindow(app_name, browser->window()->GetNativeHandle()); |
| 366 | #endif |
| 367 | |
[email protected] | 840b151 | 2009-07-21 16:53:46 | [diff] [blame] | 368 | TabContents* tab_contents = browser->GetSelectedTabContents(); |
| 369 | tab_contents->GetMutableRendererPrefs()->can_accept_load_drops = false; |
| 370 | tab_contents->render_view_host()->SyncRendererPrefs(); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 371 | browser->window()->Show(); |
[email protected] | 68f009f | 2009-02-26 19:43:35 | [diff] [blame] | 372 | // TODO(jcampan): http://crbug.com/8123 we should not need to set the initial |
| 373 | // focus explicitly. |
[email protected] | 840b151 | 2009-07-21 16:53:46 | [diff] [blame] | 374 | tab_contents->view()->SetInitialFocus(); |
[email protected] | d6a3c77 | 2009-01-27 19:41:20 | [diff] [blame] | 375 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 376 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 377 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 378 | // Browser, State Storage and Retrieval for UI: |
| 379 | |
[email protected] | bc9a515 | 2008-11-15 00:32:04 | [diff] [blame] | 380 | std::wstring Browser::GetWindowPlacementKey() const { |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 381 | std::wstring name(prefs::kBrowserWindowPlacement); |
| 382 | if (!app_name_.empty()) { |
| 383 | name.append(L"_"); |
| 384 | name.append(app_name_); |
| 385 | } |
[email protected] | bc9a515 | 2008-11-15 00:32:04 | [diff] [blame] | 386 | return name; |
| 387 | } |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 388 | |
[email protected] | bc9a515 | 2008-11-15 00:32:04 | [diff] [blame] | 389 | bool Browser::ShouldSaveWindowPlacement() const { |
| 390 | // We don't save window position for popups. |
[email protected] | d5fbc00 | 2009-02-27 22:12:58 | [diff] [blame] | 391 | return (type() & TYPE_POPUP) == 0; |
[email protected] | bc9a515 | 2008-11-15 00:32:04 | [diff] [blame] | 392 | } |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 393 | |
[email protected] | bc9a515 | 2008-11-15 00:32:04 | [diff] [blame] | 394 | void Browser::SaveWindowPlacement(const gfx::Rect& bounds, bool maximized) { |
| 395 | // Save to the session storage service, used when reloading a past session. |
| 396 | // Note that we don't want to be the ones who cause lazy initialization of |
| 397 | // the session service. This function gets called during initial window |
| 398 | // showing, and we don't want to bring in the session service this early. |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 399 | if (profile()->HasSessionService()) { |
| 400 | SessionService* session_service = profile()->GetSessionService(); |
| 401 | if (session_service) |
| 402 | session_service->SetWindowBounds(session_id_, bounds, maximized); |
| 403 | } |
| 404 | } |
| 405 | |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 406 | gfx::Rect Browser::GetSavedWindowBounds() const { |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 407 | const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 408 | bool record_mode = parsed_command_line.HasSwitch(switches::kRecordMode); |
| 409 | bool playback_mode = parsed_command_line.HasSwitch(switches::kPlaybackMode); |
| 410 | if (record_mode || playback_mode) { |
| 411 | // In playback/record mode we always fix the size of the browser and |
| 412 | // move it to (0,0). The reason for this is two reasons: First we want |
| 413 | // resize/moves in the playback to still work, and Second we want |
| 414 | // playbacks to work (as much as possible) on machines w/ different |
| 415 | // screen sizes. |
| 416 | return gfx::Rect(0, 0, 800, 600); |
| 417 | } |
| 418 | |
| 419 | gfx::Rect restored_bounds = override_bounds_; |
| 420 | bool maximized; |
[email protected] | 6dfed10 | 2009-04-28 03:09:53 | [diff] [blame] | 421 | WindowSizer::GetBrowserWindowBounds(app_name_, restored_bounds, NULL, |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 422 | &restored_bounds, &maximized); |
| 423 | return restored_bounds; |
| 424 | } |
| 425 | |
| 426 | // TODO(beng): obtain maximized state some other way so we don't need to go |
| 427 | // through all this hassle. |
| 428 | bool Browser::GetSavedMaximizedState() const { |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 429 | if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kStartMaximized)) |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 430 | return true; |
| 431 | |
[email protected] | 2e71662 | 2009-03-09 21:11:01 | [diff] [blame] | 432 | if (maximized_state_ == MAXIMIZED_STATE_MAXIMIZED) |
| 433 | return true; |
| 434 | if (maximized_state_ == MAXIMIZED_STATE_UNMAXIMIZED) |
| 435 | return false; |
| 436 | |
| 437 | // An explicit maximized state was not set. Query the window sizer. |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 438 | gfx::Rect restored_bounds; |
[email protected] | 2e71662 | 2009-03-09 21:11:01 | [diff] [blame] | 439 | bool maximized = false; |
[email protected] | 6dfed10 | 2009-04-28 03:09:53 | [diff] [blame] | 440 | WindowSizer::GetBrowserWindowBounds(app_name_, restored_bounds, NULL, |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 441 | &restored_bounds, &maximized); |
| 442 | return maximized; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | SkBitmap Browser::GetCurrentPageIcon() const { |
[email protected] | ce5348a8 | 2008-12-18 18:36:23 | [diff] [blame] | 446 | TabContents* contents = GetSelectedTabContents(); |
| 447 | // |contents| can be NULL since GetCurrentPageIcon() is called by the window |
| 448 | // during the window's creation (before tabs have been added). |
| 449 | return contents ? contents->GetFavIcon() : SkBitmap(); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 450 | } |
| 451 | |
[email protected] | 731f8a4 | 2009-07-20 22:07:32 | [diff] [blame] | 452 | string16 Browser::GetWindowTitleForCurrentTab() const { |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 453 | TabContents* contents = tabstrip_model_.GetSelectedTabContents(); |
[email protected] | dda5dfa3 | 2009-07-10 00:33:29 | [diff] [blame] | 454 | string16 title; |
[email protected] | c7c4233 | 2008-11-15 01:10:54 | [diff] [blame] | 455 | |
[email protected] | 731f8a4 | 2009-07-20 22:07:32 | [diff] [blame] | 456 | // |contents| can be NULL because GetWindowTitleForCurrentTab is called by the |
| 457 | // window during the window's creation (before tabs have been added). |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 458 | if (contents) { |
[email protected] | dda5dfa3 | 2009-07-10 00:33:29 | [diff] [blame] | 459 | title = contents->GetTitle(); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 460 | FormatTitleForDisplay(&title); |
| 461 | } |
| 462 | if (title.empty()) |
[email protected] | dda5dfa3 | 2009-07-10 00:33:29 | [diff] [blame] | 463 | title = l10n_util::GetStringUTF16(IDS_TAB_UNTITLED_TITLE); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 464 | |
[email protected] | 1677984 | 2009-07-08 23:45:29 | [diff] [blame] | 465 | #if defined(OS_MACOSX) || defined(OS_CHROMEOS) |
[email protected] | d904bc8 | 2009-07-07 00:41:04 | [diff] [blame] | 466 | // On Mac, we don't want to suffix the page title with the application name. |
| 467 | return title; |
| 468 | #elif defined(OS_WIN) || defined(OS_LINUX) |
[email protected] | edb5e6a | 2009-04-08 23:46:17 | [diff] [blame] | 469 | int string_id = IDS_BROWSER_WINDOW_TITLE_FORMAT; |
| 470 | // Don't append the app name to window titles when we're not displaying a |
| 471 | // distributor logo for the frame. |
| 472 | if (!ShouldShowDistributorLogo()) |
| 473 | string_id = IDS_BROWSER_WINDOW_TITLE_FORMAT_NO_LOGO; |
[email protected] | dda5dfa3 | 2009-07-10 00:33:29 | [diff] [blame] | 474 | return l10n_util::GetStringFUTF16(string_id, title); |
[email protected] | 57e68dba | 2009-05-15 21:30:50 | [diff] [blame] | 475 | #endif |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // static |
[email protected] | dda5dfa3 | 2009-07-10 00:33:29 | [diff] [blame] | 479 | void Browser::FormatTitleForDisplay(string16* title) { |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 480 | size_t current_index = 0; |
| 481 | size_t match_index; |
| 482 | while ((match_index = title->find(L'\n', current_index)) != |
| 483 | std::wstring::npos) { |
[email protected] | dda5dfa3 | 2009-07-10 00:33:29 | [diff] [blame] | 484 | title->replace(match_index, 1, EmptyString16()); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 485 | current_index = match_index; |
| 486 | } |
| 487 | } |
| 488 | |
[email protected] | edb5e6a | 2009-04-08 23:46:17 | [diff] [blame] | 489 | bool Browser::ShouldShowDistributorLogo() const { |
| 490 | // Don't show the distributor logo on app frames and app popups. |
| 491 | return !(type_ & TYPE_APP); |
| 492 | } |
[email protected] | c61db1d | 2009-02-06 03:39:18 | [diff] [blame] | 493 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 494 | /////////////////////////////////////////////////////////////////////////////// |
| 495 | // Browser, OnBeforeUnload handling: |
| 496 | |
| 497 | bool Browser::ShouldCloseWindow() { |
[email protected] | 69444cc | 2009-04-09 20:40:06 | [diff] [blame] | 498 | if (!CanCloseWithInProgressDownloads()) |
| 499 | return false; |
| 500 | |
| 501 | if (HasCompletedUnloadProcessing()) |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 502 | return true; |
[email protected] | 69444cc | 2009-04-09 20:40:06 | [diff] [blame] | 503 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 504 | is_attempting_to_close_browser_ = true; |
| 505 | |
| 506 | for (int i = 0; i < tab_count(); ++i) { |
[email protected] | 505323e2 | 2009-01-24 02:47:58 | [diff] [blame] | 507 | TabContents* contents = GetTabContentsAt(i); |
| 508 | if (TabHasUnloadListener(contents)) |
| 509 | tabs_needing_before_unload_fired_.insert(contents); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | if (tabs_needing_before_unload_fired_.empty()) |
| 513 | return true; |
| 514 | |
| 515 | ProcessPendingTabs(); |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | void Browser::OnWindowClosing() { |
| 520 | if (!ShouldCloseWindow()) |
| 521 | return; |
| 522 | |
[email protected] | a239c3f | 2009-02-17 22:13:19 | [diff] [blame] | 523 | #if defined(OS_WIN) || defined(OS_LINUX) |
| 524 | // We don't want to do this on Mac since closing all windows isn't a sign |
| 525 | // that the app is shutting down. |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 526 | if (BrowserList::size() == 1) |
| 527 | browser_shutdown::OnShutdownStarting(browser_shutdown::WINDOW_CLOSE); |
[email protected] | a239c3f | 2009-02-17 22:13:19 | [diff] [blame] | 528 | #endif |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 529 | |
| 530 | // Don't use HasSessionService here, we want to force creation of the |
| 531 | // session service so that user can restore what was open. |
| 532 | SessionService* session_service = profile()->GetSessionService(); |
| 533 | if (session_service) |
| 534 | session_service->WindowClosing(session_id()); |
| 535 | |
[email protected] | d8375fd | 2008-11-25 22:45:39 | [diff] [blame] | 536 | TabRestoreService* tab_restore_service = profile()->GetTabRestoreService(); |
| 537 | if (tab_restore_service) |
| 538 | tab_restore_service->BrowserClosing(this); |
| 539 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 540 | CloseAllTabs(); |
| 541 | } |
| 542 | |
[email protected] | 69444cc | 2009-04-09 20:40:06 | [diff] [blame] | 543 | //////////////////////////////////////////////////////////////////////////////// |
| 544 | // In-progress download termination handling: |
| 545 | |
| 546 | void Browser::InProgressDownloadResponse(bool cancel_downloads) { |
| 547 | if (cancel_downloads) { |
| 548 | cancel_download_confirmation_state_ = RESPONSE_RECEIVED; |
| 549 | CloseWindow(); |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | // Sets the confirmation state to NOT_PROMPTED so that if the user tries to |
| 554 | // close again we'll show the warning again. |
| 555 | cancel_download_confirmation_state_ = NOT_PROMPTED; |
| 556 | |
| 557 | // Show the download page so the user can figure-out what downloads are still |
| 558 | // in-progress. |
| 559 | ShowDownloadsTab(); |
| 560 | } |
| 561 | |
| 562 | |
| 563 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 564 | // Browser, Tab adding/showing functions: |
| 565 | |
| 566 | TabContents* Browser::AddTabWithURL( |
| 567 | const GURL& url, const GURL& referrer, PageTransition::Type transition, |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 568 | bool foreground, int index, bool force_index, |
| 569 | SiteInstance* instance) { |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 570 | TabContents* contents = NULL; |
| 571 | if (type_ == TYPE_NORMAL || tabstrip_model()->empty()) { |
| 572 | GURL url_to_load = url; |
| 573 | if (url_to_load.is_empty()) |
| 574 | url_to_load = GetHomePage(); |
| 575 | contents = CreateTabContentsForURL(url_to_load, referrer, profile_, |
| 576 | transition, false, instance); |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 577 | tabstrip_model_.AddTabContents(contents, index, force_index, |
| 578 | transition, foreground); |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 579 | // By default, content believes it is not hidden. When adding contents |
| 580 | // in the background, tell it that it's hidden. |
| 581 | if (!foreground) |
| 582 | contents->WasHidden(); |
| 583 | } else { |
| 584 | // We're in an app window or a popup window. Find an existing browser to |
| 585 | // open this URL in, creating one if none exists. |
[email protected] | 1a6b30a | 2009-08-19 03:52:11 | [diff] [blame] | 586 | Browser* b = GetOrCreateTabbedBrowser(profile_); |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 587 | contents = b->AddTabWithURL(url, referrer, transition, foreground, index, |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 588 | force_index, instance); |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 589 | b->window()->Show(); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 590 | } |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 591 | return contents; |
| 592 | } |
| 593 | |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 594 | // TODO(brettw) this should be just AddTab and it should take a TabContents. |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 595 | TabContents* Browser::AddTabWithNavigationController( |
| 596 | NavigationController* ctrl, PageTransition::Type type) { |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 597 | TabContents* tc = ctrl->tab_contents(); |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 598 | tabstrip_model_.AddTabContents(tc, -1, false, type, true); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 599 | return tc; |
| 600 | } |
| 601 | |
[email protected] | e0c7c26 | 2009-04-23 23:09:43 | [diff] [blame] | 602 | void Browser::AddTabContents(TabContents* new_contents, |
| 603 | WindowOpenDisposition disposition, |
| 604 | const gfx::Rect& initial_pos, |
| 605 | bool user_gesture) { |
| 606 | AddNewContents(NULL, new_contents, disposition, initial_pos, user_gesture); |
| 607 | } |
| 608 | |
| 609 | void Browser::CloseTabContents(TabContents* contents) { |
| 610 | CloseContents(contents); |
| 611 | } |
| 612 | |
| 613 | void Browser::BrowserShowHtmlDialog(HtmlDialogUIDelegate* delegate, |
[email protected] | 322f3ff | 2009-05-22 16:19:54 | [diff] [blame] | 614 | gfx::NativeWindow parent_window) { |
[email protected] | e0c7c26 | 2009-04-23 23:09:43 | [diff] [blame] | 615 | ShowHtmlDialog(delegate, parent_window); |
| 616 | } |
| 617 | |
| 618 | void Browser::BrowserRenderWidgetShowing() { |
| 619 | RenderWidgetShowing(); |
| 620 | } |
| 621 | |
| 622 | void Browser::ToolbarSizeChanged(bool is_animating) { |
| 623 | ToolbarSizeChanged(NULL, is_animating); |
| 624 | } |
| 625 | |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 626 | TabContents* Browser::AddRestoredTab( |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 627 | const std::vector<TabNavigation>& navigations, |
| 628 | int tab_index, |
| 629 | int selected_navigation, |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 630 | bool select, |
| 631 | bool pin) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 632 | TabContents* new_tab = new TabContents(profile(), NULL, |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 633 | MSG_ROUTING_NONE, NULL); |
| 634 | new_tab->controller().RestoreFromState(navigations, selected_navigation); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 635 | |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 636 | bool really_pin = |
| 637 | (pin && tab_index == tabstrip_model()->IndexOfFirstNonPinnedTab()); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 638 | tabstrip_model_.InsertTabContentsAt(tab_index, new_tab, select, false); |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 639 | if (really_pin) |
| 640 | tabstrip_model_.SetTabPinned(tab_index, true); |
[email protected] | 77bc673 | 2009-04-20 22:01:03 | [diff] [blame] | 641 | if (select) |
| 642 | window_->Activate(); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 643 | if (profile_->HasSessionService()) { |
| 644 | SessionService* session_service = profile_->GetSessionService(); |
| 645 | if (session_service) |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 646 | session_service->TabRestored(&new_tab->controller(), really_pin); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 647 | } |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 648 | return new_tab; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | void Browser::ReplaceRestoredTab( |
| 652 | const std::vector<TabNavigation>& navigations, |
| 653 | int selected_navigation) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 654 | TabContents* replacement = new TabContents(profile(), NULL, |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 655 | MSG_ROUTING_NONE, NULL); |
| 656 | replacement->controller().RestoreFromState(navigations, selected_navigation); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 657 | |
| 658 | tabstrip_model_.ReplaceNavigationControllerAt( |
| 659 | tabstrip_model_.selected_index(), |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 660 | &replacement->controller()); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 661 | } |
| 662 | |
[email protected] | a8eff10 | 2009-04-14 15:23:43 | [diff] [blame] | 663 | bool Browser::CanRestoreTab() { |
| 664 | TabRestoreService* service = profile_->GetTabRestoreService(); |
| 665 | return service && !service->entries().empty(); |
| 666 | } |
| 667 | |
[email protected] | 7a5f33d | 2009-03-03 04:35:36 | [diff] [blame] | 668 | void Browser::ShowSingleDOMUITab(const GURL& url) { |
[email protected] | b689fce7 | 2009-03-17 22:45:34 | [diff] [blame] | 669 | // See if we already have a tab with the given URL and select it if so. |
| 670 | for (int i = 0; i < tabstrip_model_.count(); i++) { |
| 671 | TabContents* tc = tabstrip_model_.GetTabContentsAt(i); |
| 672 | if (tc->GetURL() == url) { |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 673 | tabstrip_model_.SelectTabContentsAt(i, false); |
| 674 | return; |
| 675 | } |
| 676 | } |
[email protected] | b689fce7 | 2009-03-17 22:45:34 | [diff] [blame] | 677 | |
| 678 | // Otherwise, just create a new tab. |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 679 | AddTabWithURL(url, GURL(), PageTransition::AUTO_BOOKMARK, true, -1, |
| 680 | false, NULL); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 681 | } |
[email protected] | d938aed9 | 2009-01-22 19:49:33 | [diff] [blame] | 682 | |
[email protected] | ae2622c | 2009-07-30 23:47:58 | [diff] [blame] | 683 | void Browser::UpdateCommandsForFullscreenMode(bool is_fullscreen) { |
| 684 | const bool show_main_ui = (type() == TYPE_NORMAL) && !is_fullscreen; |
| 685 | |
| 686 | // Navigation commands |
| 687 | command_updater_.UpdateCommandEnabled(IDC_OPEN_CURRENT_URL, show_main_ui); |
| 688 | |
| 689 | // Window management commands |
| 690 | command_updater_.UpdateCommandEnabled(IDC_PROFILE_MENU, show_main_ui); |
| 691 | command_updater_.UpdateCommandEnabled(IDC_SHOW_AS_TAB, |
| 692 | (type() & TYPE_POPUP) && !is_fullscreen); |
| 693 | |
| 694 | // Focus various bits of UI |
| 695 | command_updater_.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR, show_main_ui); |
| 696 | command_updater_.UpdateCommandEnabled(IDC_FOCUS_LOCATION, show_main_ui); |
| 697 | command_updater_.UpdateCommandEnabled(IDC_FOCUS_SEARCH, show_main_ui); |
| 698 | |
| 699 | // Show various bits of UI |
| 700 | command_updater_.UpdateCommandEnabled(IDC_DEVELOPER_MENU, show_main_ui); |
| 701 | command_updater_.UpdateCommandEnabled(IDC_NEW_PROFILE, show_main_ui); |
| 702 | command_updater_.UpdateCommandEnabled(IDC_REPORT_BUG, show_main_ui); |
| 703 | command_updater_.UpdateCommandEnabled(IDC_SHOW_BOOKMARK_BAR, show_main_ui); |
| 704 | command_updater_.UpdateCommandEnabled(IDC_IMPORT_SETTINGS, show_main_ui); |
[email protected] | ca38d8e | 2009-08-20 18:08:16 | [diff] [blame] | 705 | command_updater_.UpdateCommandEnabled(IDC_SYNC_BOOKMARKS, show_main_ui); |
[email protected] | ae2622c | 2009-07-30 23:47:58 | [diff] [blame] | 706 | command_updater_.UpdateCommandEnabled(IDC_OPTIONS, show_main_ui); |
| 707 | command_updater_.UpdateCommandEnabled(IDC_EDIT_SEARCH_ENGINES, show_main_ui); |
| 708 | command_updater_.UpdateCommandEnabled(IDC_VIEW_PASSWORDS, show_main_ui); |
| 709 | command_updater_.UpdateCommandEnabled(IDC_ABOUT, show_main_ui); |
| 710 | } |
| 711 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 712 | /////////////////////////////////////////////////////////////////////////////// |
| 713 | // Browser, Assorted browser commands: |
| 714 | |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 715 | void Browser::GoBack(WindowOpenDisposition disposition) { |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 716 | UserMetrics::RecordAction(L"Back", profile_); |
| 717 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 718 | // If we are showing an interstitial, just hide it. |
| 719 | TabContents* current_tab = GetSelectedTabContents(); |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 720 | if (current_tab->interstitial_page()) { |
[email protected] | a3a1d14 | 2008-12-19 00:42:30 | [diff] [blame] | 721 | // The GoBack() case is a special case when an interstitial is shown because |
| 722 | // the "previous" page is still available, just hidden by the interstitial. |
| 723 | // We treat the back as a "Don't proceed", this hides the interstitial and |
| 724 | // reveals the previous page. |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 725 | current_tab->interstitial_page()->DontProceed(); |
[email protected] | c7c4233 | 2008-11-15 01:10:54 | [diff] [blame] | 726 | return; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 727 | } |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 728 | |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 729 | if (current_tab->controller().CanGoBack()) { |
| 730 | NavigationController* controller = NULL; |
| 731 | if (disposition == NEW_FOREGROUND_TAB || |
| 732 | disposition == NEW_BACKGROUND_TAB) { |
[email protected] | 90ba8dd | 2009-04-25 19:43:52 | [diff] [blame] | 733 | TabContents* cloned = GetSelectedTabContents()->Clone(); |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 734 | tabstrip_model_.AddTabContents(cloned, -1, false, |
[email protected] | 90ba8dd | 2009-04-25 19:43:52 | [diff] [blame] | 735 | PageTransition::LINK, |
| 736 | disposition == NEW_FOREGROUND_TAB); |
| 737 | controller = &cloned->controller(); |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 738 | } else { |
| 739 | // Default disposition is CURRENT_TAB. |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 740 | controller = ¤t_tab->controller(); |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 741 | } |
| 742 | controller->GoBack(); |
| 743 | } |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 744 | } |
| 745 | |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 746 | void Browser::GoForward(WindowOpenDisposition disp) { |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 747 | // TODO(brettw) this is mostly duplicated from GoBack, these should have a |
| 748 | // common backend or something. |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 749 | UserMetrics::RecordAction(L"Forward", profile_); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 750 | if (GetSelectedTabContents()->controller().CanGoForward()) { |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 751 | NavigationController* controller = 0; |
| 752 | if (disp == NEW_FOREGROUND_TAB || disp == NEW_BACKGROUND_TAB) { |
[email protected] | 90ba8dd | 2009-04-25 19:43:52 | [diff] [blame] | 753 | TabContents* cloned = GetSelectedTabContents()->Clone(); |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 754 | tabstrip_model_.AddTabContents(cloned, -1, false, |
[email protected] | 90ba8dd | 2009-04-25 19:43:52 | [diff] [blame] | 755 | PageTransition::LINK, |
| 756 | disp == NEW_FOREGROUND_TAB); |
| 757 | controller = &cloned->controller(); |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 758 | } else { |
| 759 | // Default disposition is CURRENT_TAB. |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 760 | controller = &GetSelectedTabContents()->controller(); |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 761 | } |
| 762 | controller->GoForward(); |
| 763 | } |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | void Browser::Reload() { |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 767 | UserMetrics::RecordAction(L"Reload", profile_); |
| 768 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 769 | // If we are showing an interstitial, treat this as an OpenURL. |
| 770 | TabContents* current_tab = GetSelectedTabContents(); |
| 771 | if (current_tab) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 772 | if (current_tab->showing_interstitial_page()) { |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 773 | NavigationEntry* entry = current_tab->controller().GetActiveEntry(); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 774 | DCHECK(entry); // Should exist if interstitial is showing. |
| 775 | OpenURL(entry->url(), GURL(), CURRENT_TAB, PageTransition::RELOAD); |
| 776 | return; |
| 777 | } |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 778 | |
[email protected] | 20142ba | 2009-05-04 16:58:47 | [diff] [blame] | 779 | // As this is caused by a user action, give the focus to the page. |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 780 | if (!current_tab->FocusLocationBarByDefault()) |
| 781 | current_tab->Focus(); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 782 | current_tab->controller().Reload(true); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 786 | void Browser::Home(WindowOpenDisposition disposition) { |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 787 | UserMetrics::RecordAction(L"Home", profile_); |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 788 | OpenURL(GetHomePage(), GURL(), disposition, PageTransition::AUTO_BOOKMARK); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 789 | } |
| 790 | |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 791 | void Browser::OpenCurrentURL() { |
| 792 | UserMetrics::RecordAction(L"LoadURL", profile_); |
[email protected] | 7745b82 | 2009-01-27 20:15:35 | [diff] [blame] | 793 | LocationBar* location_bar = window_->GetLocationBar(); |
[email protected] | 1f7d7e9 | 2009-06-02 20:55:04 | [diff] [blame] | 794 | OpenURLAtIndex(NULL, GURL(WideToUTF8(location_bar->GetInputString())), GURL(), |
| 795 | location_bar->GetWindowOpenDisposition(), |
| 796 | location_bar->GetPageTransition(), -1, true); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 797 | } |
| 798 | |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 799 | void Browser::Go(WindowOpenDisposition disposition) { |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 800 | UserMetrics::RecordAction(L"Go", profile_); |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 801 | window_->GetLocationBar()->AcceptInputWithDisposition(disposition); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | void Browser::Stop() { |
| 805 | UserMetrics::RecordAction(L"Stop", profile_); |
[email protected] | 0daf9473 | 2008-12-17 01:08:33 | [diff] [blame] | 806 | GetSelectedTabContents()->Stop(); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | void Browser::NewWindow() { |
| 810 | UserMetrics::RecordAction(L"NewWindow", profile_); |
| 811 | Browser::OpenEmptyWindow(profile_->GetOriginalProfile()); |
| 812 | } |
| 813 | |
| 814 | void Browser::NewIncognitoWindow() { |
| 815 | UserMetrics::RecordAction(L"NewIncognitoWindow", profile_); |
| 816 | Browser::OpenEmptyWindow(profile_->GetOffTheRecordProfile()); |
| 817 | } |
| 818 | |
| 819 | void Browser::NewProfileWindowByIndex(int index) { |
[email protected] | d938aed9 | 2009-01-22 19:49:33 | [diff] [blame] | 820 | #if defined(OS_WIN) |
[email protected] | ac92636 | 2009-02-26 01:33:18 | [diff] [blame] | 821 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 822 | if (!command_line.HasSwitch(switches::kEnableUserDataDirProfiles)) |
| 823 | return; |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 824 | UserMetrics::RecordAction(L"NewProfileWindowByIndex", profile_); |
| 825 | UserDataManager::Get()->LaunchChromeForProfile(index); |
[email protected] | d938aed9 | 2009-01-22 19:49:33 | [diff] [blame] | 826 | #endif |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | void Browser::CloseWindow() { |
| 830 | UserMetrics::RecordAction(L"CloseWindow", profile_); |
| 831 | window_->Close(); |
| 832 | } |
| 833 | |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 834 | void Browser::NewTab() { |
| 835 | UserMetrics::RecordAction(L"NewTab", profile_); |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 836 | if (type() == TYPE_NORMAL) { |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 837 | AddBlankTab(true); |
| 838 | } else { |
[email protected] | 1a6b30a | 2009-08-19 03:52:11 | [diff] [blame] | 839 | Browser* b = GetOrCreateTabbedBrowser(profile_); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 840 | b->AddBlankTab(true); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 841 | b->window()->Show(); |
[email protected] | e54f30d | 2009-01-14 21:17:11 | [diff] [blame] | 842 | // The call to AddBlankTab above did not set the focus to the tab as its |
| 843 | // window was not active, so we have to do it explicitly. |
| 844 | // See http://crbug.com/6380. |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 845 | b->GetSelectedTabContents()->view()->RestoreFocus(); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
| 849 | void Browser::CloseTab() { |
| 850 | UserMetrics::RecordAction(L"CloseTab_Accelerator", profile_); |
| 851 | tabstrip_model_.CloseTabContentsAt(tabstrip_model_.selected_index()); |
| 852 | } |
| 853 | |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 854 | void Browser::SelectNextTab() { |
| 855 | UserMetrics::RecordAction(L"SelectNextTab", profile_); |
| 856 | tabstrip_model_.SelectNextTab(); |
| 857 | } |
| 858 | |
| 859 | void Browser::SelectPreviousTab() { |
| 860 | UserMetrics::RecordAction(L"SelectPrevTab", profile_); |
| 861 | tabstrip_model_.SelectPreviousTab(); |
| 862 | } |
| 863 | |
| 864 | void Browser::SelectNumberedTab(int index) { |
| 865 | if (index < tab_count()) { |
| 866 | UserMetrics::RecordAction(L"SelectNumberedTab", profile_); |
| 867 | tabstrip_model_.SelectTabContentsAt(index, true); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | void Browser::SelectLastTab() { |
| 872 | UserMetrics::RecordAction(L"SelectLastTab", profile_); |
| 873 | tabstrip_model_.SelectLastTab(); |
| 874 | } |
| 875 | |
| 876 | void Browser::DuplicateTab() { |
| 877 | UserMetrics::RecordAction(L"Duplicate", profile_); |
| 878 | DuplicateContentsAt(selected_index()); |
| 879 | } |
| 880 | |
| 881 | void Browser::RestoreTab() { |
| 882 | UserMetrics::RecordAction(L"RestoreTab", profile_); |
| 883 | TabRestoreService* service = profile_->GetTabRestoreService(); |
| 884 | if (!service) |
| 885 | return; |
| 886 | |
[email protected] | d8375fd | 2008-11-25 22:45:39 | [diff] [blame] | 887 | service->RestoreMostRecentEntry(this); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | void Browser::ConvertPopupToTabbedBrowser() { |
| 891 | UserMetrics::RecordAction(L"ShowAsTab", profile_); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 892 | int tab_strip_index = tabstrip_model_.selected_index(); |
| 893 | TabContents* contents = tabstrip_model_.DetachTabContentsAt(tab_strip_index); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 894 | Browser* browser = Browser::Create(profile_); |
| 895 | browser->tabstrip_model()->AppendTabContents(contents, true); |
| 896 | browser->window()->Show(); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 897 | } |
| 898 | |
[email protected] | 9282cea | 2009-02-18 18:49:00 | [diff] [blame] | 899 | void Browser::ToggleFullscreenMode() { |
| 900 | UserMetrics::RecordAction(L"ToggleFullscreen", profile_); |
| 901 | window_->SetFullscreen(!window_->IsFullscreen()); |
[email protected] | ae2622c | 2009-07-30 23:47:58 | [diff] [blame] | 902 | // On Linux, setting fullscreen mode is an async call to the X server, which |
| 903 | // may or may not support fullscreen mode. |
| 904 | #if !defined(OS_LINUX) |
[email protected] | 9282cea | 2009-02-18 18:49:00 | [diff] [blame] | 905 | UpdateCommandsForFullscreenMode(window_->IsFullscreen()); |
[email protected] | ae2622c | 2009-07-30 23:47:58 | [diff] [blame] | 906 | #endif |
[email protected] | 9282cea | 2009-02-18 18:49:00 | [diff] [blame] | 907 | } |
| 908 | |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 909 | void Browser::Exit() { |
| 910 | UserMetrics::RecordAction(L"Exit", profile_); |
| 911 | BrowserList::CloseAllBrowsers(true); |
| 912 | } |
| 913 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 914 | void Browser::BookmarkCurrentPage() { |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 915 | UserMetrics::RecordAction(L"Star", profile_); |
| 916 | |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 917 | TabContents* contents = GetSelectedTabContents(); |
[email protected] | 85408b7c | 2009-06-03 19:11:54 | [diff] [blame] | 918 | if (!contents->ShouldDisplayURL()) |
| 919 | return; |
| 920 | const GURL& url = contents->GetURL(); |
| 921 | if (url.is_empty() || !url.is_valid()) |
| 922 | return; |
| 923 | std::wstring title = UTF16ToWideHack(contents->GetTitle()); |
| 924 | |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 925 | BookmarkModel* model = contents->profile()->GetBookmarkModel(); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 926 | if (!model || !model->IsLoaded()) |
| 927 | return; // Ignore requests until bookmarks are loaded. |
| 928 | |
[email protected] | c50d0d4 | 2009-03-03 17:37:44 | [diff] [blame] | 929 | bool was_bookmarked = model->IsBookmarked(url); |
[email protected] | 85408b7c | 2009-06-03 19:11:54 | [diff] [blame] | 930 | model->SetURLStarred(url, title, true); |
[email protected] | 5f2731c5 | 2009-02-28 00:41:27 | [diff] [blame] | 931 | if (window_->IsActive()) { |
| 932 | // Only show the bubble if the window is active, otherwise we may get into |
| 933 | // weird situations were the bubble is deleted as soon as it is shown. |
[email protected] | c50d0d4 | 2009-03-03 17:37:44 | [diff] [blame] | 934 | window_->ShowBookmarkBubble(url, was_bookmarked); |
[email protected] | 5f2731c5 | 2009-02-28 00:41:27 | [diff] [blame] | 935 | } |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 936 | } |
| 937 | |
[email protected] | 40bdb6d9 | 2009-02-25 00:22:27 | [diff] [blame] | 938 | void Browser::SavePage() { |
| 939 | UserMetrics::RecordAction(L"SavePage", profile_); |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 940 | GetSelectedTabContents()->OnSavePage(); |
[email protected] | 40bdb6d9 | 2009-02-25 00:22:27 | [diff] [blame] | 941 | } |
| 942 | |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 943 | void Browser::ViewSource() { |
| 944 | UserMetrics::RecordAction(L"ViewSource", profile_); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 945 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 946 | TabContents* current_tab = GetSelectedTabContents(); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 947 | NavigationEntry* entry = current_tab->controller().GetLastCommittedEntry(); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 948 | if (entry) { |
| 949 | GURL url("view-source:" + entry->url().spec()); |
[email protected] | 749eea04 | 2009-01-05 22:35:54 | [diff] [blame] | 950 | OpenURL(url, GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 951 | } |
| 952 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 953 | |
[email protected] | 4801ecc | 2009-04-05 04:52:58 | [diff] [blame] | 954 | void Browser::ShowFindBar() { |
| 955 | find_bar_controller_->Show(); |
| 956 | } |
| 957 | |
[email protected] | a3e18c4 | 2009-03-04 23:36:05 | [diff] [blame] | 958 | bool Browser::SupportsWindowFeature(WindowFeature feature) const { |
| 959 | unsigned int features = FEATURE_INFOBAR | FEATURE_DOWNLOADSHELF; |
[email protected] | 653c9ea | 2009-05-06 16:58:56 | [diff] [blame] | 960 | if (type() == TYPE_NORMAL) { |
| 961 | features |= FEATURE_BOOKMARKBAR; |
| 962 | features |= FEATURE_EXTENSIONSHELF; |
| 963 | } |
[email protected] | a3e18c4 | 2009-03-04 23:36:05 | [diff] [blame] | 964 | if (!window_ || !window_->IsFullscreen()) { |
| 965 | if (type() == TYPE_NORMAL) |
| 966 | features |= FEATURE_TABSTRIP | FEATURE_TOOLBAR; |
| 967 | else |
| 968 | features |= FEATURE_TITLEBAR; |
| 969 | if ((type() & Browser::TYPE_APP) == 0) |
| 970 | features |= FEATURE_LOCATIONBAR; |
| 971 | } |
| 972 | return !!(features & feature); |
| 973 | } |
| 974 | |
[email protected] | c61db1d | 2009-02-06 03:39:18 | [diff] [blame] | 975 | #if defined(OS_WIN) |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 976 | void Browser::ClosePopups() { |
| 977 | UserMetrics::RecordAction(L"CloseAllSuppressedPopups", profile_); |
| 978 | GetSelectedTabContents()->CloseAllSuppressedPopups(); |
| 979 | } |
[email protected] | 6862ac6c | 2009-08-05 20:26:41 | [diff] [blame] | 980 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 981 | |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 982 | void Browser::Print() { |
| 983 | UserMetrics::RecordAction(L"PrintPreview", profile_); |
[email protected] | 6862ac6c | 2009-08-05 20:26:41 | [diff] [blame] | 984 | #if defined(OS_WIN) || defined(OS_LINUX) |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 985 | GetSelectedTabContents()->PrintPreview(); |
[email protected] | 6862ac6c | 2009-08-05 20:26:41 | [diff] [blame] | 986 | #else |
| 987 | NOTIMPLEMENTED(); |
| 988 | #endif |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 989 | } |
| 990 | |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 991 | void Browser::ToggleEncodingAutoDetect() { |
| 992 | UserMetrics::RecordAction(L"AutoDetectChange", profile_); |
| 993 | encoding_auto_detect_.SetValue(!encoding_auto_detect_.GetValue()); |
| 994 | // Reload the page so we can try to auto-detect the charset. |
| 995 | Reload(); |
| 996 | } |
| 997 | |
| 998 | void Browser::OverrideEncoding(int encoding_id) { |
| 999 | UserMetrics::RecordAction(L"OverrideEncoding", profile_); |
| 1000 | const std::wstring selected_encoding = |
| 1001 | CharacterEncoding::GetCanonicalEncodingNameByCommandId(encoding_id); |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 1002 | TabContents* contents = GetSelectedTabContents(); |
| 1003 | if (!selected_encoding.empty() && contents) |
| 1004 | contents->override_encoding(selected_encoding); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 1005 | // Update the list of recently selected encodings. |
| 1006 | std::wstring new_selected_encoding_list; |
| 1007 | if (CharacterEncoding::UpdateRecentlySelectdEncoding( |
| 1008 | profile_->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding), |
| 1009 | encoding_id, |
| 1010 | &new_selected_encoding_list)) { |
| 1011 | profile_->GetPrefs()->SetString(prefs::kRecentlySelectedEncoding, |
| 1012 | new_selected_encoding_list); |
| 1013 | } |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1014 | } |
| 1015 | |
[email protected] | 40d59ce5 | 2009-03-06 23:20:14 | [diff] [blame] | 1016 | #if defined(OS_WIN) |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1017 | // TODO(devint): http://b/issue?id=1117225 Cut, Copy, and Paste are always |
| 1018 | // enabled in the page menu regardless of whether the command will do |
| 1019 | // anything. When someone selects the menu item, we just act as if they hit |
| 1020 | // the keyboard shortcut for the command by sending the associated key press |
| 1021 | // to windows. The real fix to this bug is to disable the commands when they |
| 1022 | // won't do anything. We'll need something like an overall clipboard command |
| 1023 | // manager to do that. |
| 1024 | |
| 1025 | void Browser::Cut() { |
| 1026 | UserMetrics::RecordAction(L"Cut", profile_); |
[email protected] | 22cdd93 | 2009-08-18 02:16:21 | [diff] [blame] | 1027 | ui_controls::SendKeyPress(window()->GetNativeHandle(), base::VKEY_X, true, |
| 1028 | false, false); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1029 | } |
| 1030 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1031 | void Browser::Copy() { |
| 1032 | UserMetrics::RecordAction(L"Copy", profile_); |
[email protected] | 22cdd93 | 2009-08-18 02:16:21 | [diff] [blame] | 1033 | ui_controls::SendKeyPress(window()->GetNativeHandle(), base::VKEY_C, true, |
| 1034 | false, false); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1035 | } |
| 1036 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1037 | void Browser::Paste() { |
| 1038 | UserMetrics::RecordAction(L"Paste", profile_); |
[email protected] | 22cdd93 | 2009-08-18 02:16:21 | [diff] [blame] | 1039 | ui_controls::SendKeyPress(window()->GetNativeHandle(), base::VKEY_V, true, |
| 1040 | false, false); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1041 | } |
[email protected] | 31b1bc3a | 2009-03-10 19:26:19 | [diff] [blame] | 1042 | #endif // #if defined(OS_WIN) |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1043 | |
| 1044 | void Browser::Find() { |
| 1045 | UserMetrics::RecordAction(L"Find", profile_); |
[email protected] | 4f3dc37 | 2009-02-24 00:10:29 | [diff] [blame] | 1046 | FindInPage(false, false); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | void Browser::FindNext() { |
| 1050 | UserMetrics::RecordAction(L"FindNext", profile_); |
[email protected] | 4f3dc37 | 2009-02-24 00:10:29 | [diff] [blame] | 1051 | FindInPage(true, true); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | void Browser::FindPrevious() { |
| 1055 | UserMetrics::RecordAction(L"FindPrevious", profile_); |
[email protected] | 4f3dc37 | 2009-02-24 00:10:29 | [diff] [blame] | 1056 | FindInPage(true, false); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | void Browser::ZoomIn() { |
| 1060 | UserMetrics::RecordAction(L"ZoomPlus", profile_); |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 1061 | GetSelectedTabContents()->render_view_host()->Zoom(PageZoom::LARGER); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | void Browser::ZoomReset() { |
| 1065 | UserMetrics::RecordAction(L"ZoomNormal", profile_); |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 1066 | GetSelectedTabContents()->render_view_host()->Zoom(PageZoom::STANDARD); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | void Browser::ZoomOut() { |
| 1070 | UserMetrics::RecordAction(L"ZoomMinus", profile_); |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 1071 | GetSelectedTabContents()->render_view_host()->Zoom(PageZoom::SMALLER); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | void Browser::FocusToolbar() { |
| 1075 | UserMetrics::RecordAction(L"FocusToolbar", profile_); |
| 1076 | window_->FocusToolbar(); |
| 1077 | } |
| 1078 | |
| 1079 | void Browser::FocusLocationBar() { |
| 1080 | UserMetrics::RecordAction(L"FocusLocation", profile_); |
[email protected] | d56bcd2 | 2009-03-16 19:51:56 | [diff] [blame] | 1081 | window_->SetFocusToLocationBar(); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | void Browser::FocusSearch() { |
| 1085 | // TODO(beng): replace this with FocusLocationBar |
| 1086 | UserMetrics::RecordAction(L"FocusSearch", profile_); |
[email protected] | 7745b82 | 2009-01-27 20:15:35 | [diff] [blame] | 1087 | window_->GetLocationBar()->FocusSearch(); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | void Browser::OpenFile() { |
| 1091 | UserMetrics::RecordAction(L"OpenFile", profile_); |
| 1092 | if (!select_file_dialog_.get()) |
| 1093 | select_file_dialog_ = SelectFileDialog::Create(this); |
| 1094 | |
| 1095 | // TODO(beng): figure out how to juggle this. |
[email protected] | d56bcd2 | 2009-03-16 19:51:56 | [diff] [blame] | 1096 | gfx::NativeWindow parent_window = window_->GetNativeHandle(); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1097 | select_file_dialog_->SelectFile(SelectFileDialog::SELECT_OPEN_FILE, |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 1098 | string16(), FilePath(), |
[email protected] | b949f111 | 2009-04-12 20:03:08 | [diff] [blame] | 1099 | NULL, 0, FILE_PATH_LITERAL(""), |
[email protected] | d56bcd2 | 2009-03-16 19:51:56 | [diff] [blame] | 1100 | parent_window, NULL); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | void Browser::OpenCreateShortcutsDialog() { |
| 1104 | UserMetrics::RecordAction(L"CreateShortcut", profile_); |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 1105 | #if defined(OS_WIN) || defined(OS_LINUX) |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 1106 | GetSelectedTabContents()->CreateShortcut(); |
[email protected] | 98f6e02 | 2009-06-05 22:49:53 | [diff] [blame] | 1107 | #else |
| 1108 | NOTIMPLEMENTED(); |
| 1109 | #endif |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1110 | } |
| 1111 | |
[email protected] | c630f16 | 2009-07-22 09:41:33 | [diff] [blame] | 1112 | void Browser::ToggleDevToolsWindow() { |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 1113 | UserMetrics::RecordAction(L"ShowJSConsole", profile_); |
[email protected] | 3061ccf3 | 2009-07-13 14:17:49 | [diff] [blame] | 1114 | DevToolsManager::GetInstance()->ToggleDevToolsWindow( |
[email protected] | d9f9b79 | 2009-06-24 13:17:12 | [diff] [blame] | 1115 | GetSelectedTabContents()->render_view_host()); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 1116 | } |
| 1117 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1118 | void Browser::OpenTaskManager() { |
| 1119 | UserMetrics::RecordAction(L"TaskManager", profile_); |
[email protected] | 8f90afd7 | 2009-06-22 22:44:38 | [diff] [blame] | 1120 | window_->ShowTaskManager(); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | void Browser::OpenSelectProfileDialog() { |
[email protected] | 12a6f036 | 2009-03-09 19:37:36 | [diff] [blame] | 1124 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 1125 | if (!command_line.HasSwitch(switches::kEnableUserDataDirProfiles)) |
| 1126 | return; |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1127 | UserMetrics::RecordAction(L"SelectProfile", profile_); |
[email protected] | 505323e2 | 2009-01-24 02:47:58 | [diff] [blame] | 1128 | window_->ShowSelectProfileDialog(); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | void Browser::OpenNewProfileDialog() { |
[email protected] | ac92636 | 2009-02-26 01:33:18 | [diff] [blame] | 1132 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 1133 | if (!command_line.HasSwitch(switches::kEnableUserDataDirProfiles)) |
| 1134 | return; |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1135 | UserMetrics::RecordAction(L"CreateProfile", profile_); |
[email protected] | 505323e2 | 2009-01-24 02:47:58 | [diff] [blame] | 1136 | window_->ShowNewProfileDialog(); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | void Browser::OpenBugReportDialog() { |
| 1140 | UserMetrics::RecordAction(L"ReportBug", profile_); |
| 1141 | window_->ShowReportBugDialog(); |
| 1142 | } |
[email protected] | 44b2c885 | 2009-03-18 00:57:49 | [diff] [blame] | 1143 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1144 | void Browser::ToggleBookmarkBar() { |
| 1145 | UserMetrics::RecordAction(L"ShowBookmarksBar", profile_); |
| 1146 | window_->ToggleBookmarkBar(); |
| 1147 | } |
| 1148 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1149 | void Browser::OpenBookmarkManager() { |
| 1150 | UserMetrics::RecordAction(L"ShowBookmarkManager", profile_); |
| 1151 | window_->ShowBookmarkManager(); |
| 1152 | } |
[email protected] | 8bf80e92 | 2009-03-09 20:56:36 | [diff] [blame] | 1153 | |
| 1154 | void Browser::ShowHistoryTab() { |
| 1155 | UserMetrics::RecordAction(L"ShowHistory", profile_); |
[email protected] | b689fce7 | 2009-03-17 22:45:34 | [diff] [blame] | 1156 | ShowSingleDOMUITab(GURL(chrome::kChromeUIHistoryURL)); |
[email protected] | 8bf80e92 | 2009-03-09 20:56:36 | [diff] [blame] | 1157 | } |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1158 | |
| 1159 | void Browser::ShowDownloadsTab() { |
| 1160 | UserMetrics::RecordAction(L"ShowDownloads", profile_); |
[email protected] | b689fce7 | 2009-03-17 22:45:34 | [diff] [blame] | 1161 | ShowSingleDOMUITab(GURL(chrome::kChromeUIDownloadsURL)); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | void Browser::OpenClearBrowsingDataDialog() { |
| 1165 | UserMetrics::RecordAction(L"ClearBrowsingData_ShowDlg", profile_); |
| 1166 | window_->ShowClearBrowsingDataDialog(); |
| 1167 | } |
| 1168 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1169 | void Browser::OpenOptionsDialog() { |
| 1170 | UserMetrics::RecordAction(L"ShowOptions", profile_); |
| 1171 | ShowOptionsWindow(OPTIONS_PAGE_DEFAULT, OPTIONS_GROUP_NONE, profile_); |
| 1172 | } |
| 1173 | |
| 1174 | void Browser::OpenKeywordEditor() { |
| 1175 | UserMetrics::RecordAction(L"EditSearchEngines", profile_); |
| 1176 | window_->ShowSearchEnginesDialog(); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | void Browser::OpenPasswordManager() { |
[email protected] | 2d46c84 | 2008-11-14 19:24:31 | [diff] [blame] | 1180 | window_->ShowPasswordManager(); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 1181 | } |
| 1182 | |
[email protected] | 1bdf29e | 2009-05-11 23:45:25 | [diff] [blame] | 1183 | void Browser::OpenImportSettingsDialog() { |
| 1184 | UserMetrics::RecordAction(L"Import_ShowDlg", profile_); |
| 1185 | window_->ShowImportDialog(); |
| 1186 | } |
[email protected] | 1bdf29e | 2009-05-11 23:45:25 | [diff] [blame] | 1187 | |
[email protected] | ca38d8e | 2009-08-20 18:08:16 | [diff] [blame] | 1188 | #ifdef CHROME_PERSONALIZATION |
| 1189 | void Browser::OpenSyncMyBookmarksDialog() { |
| 1190 | ProfileSyncService* service = profile_->GetProfileSyncService(); |
| 1191 | // TODO(timsteele): Incognito has no sync service for the time being, |
| 1192 | // so protect against this case. |
| 1193 | if (!service) |
| 1194 | return; |
| 1195 | if (service->HasSyncSetupCompleted()) { |
| 1196 | ShowOptionsWindow(OPTIONS_PAGE_CONTENT, OPTIONS_GROUP_NONE, profile_); |
| 1197 | } else { |
| 1198 | service->EnableForUser(); |
| 1199 | ProfileSyncService::SyncEvent(ProfileSyncService::START_FROM_WRENCH); |
| 1200 | } |
| 1201 | } |
| 1202 | #endif |
| 1203 | |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 1204 | void Browser::OpenAboutChromeDialog() { |
| 1205 | UserMetrics::RecordAction(L"AboutChrome", profile_); |
[email protected] | 2d46c84 | 2008-11-14 19:24:31 | [diff] [blame] | 1206 | window_->ShowAboutChromeDialog(); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 1207 | } |
| 1208 | |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 1209 | void Browser::OpenHelpTab() { |
[email protected] | 40d59ce5 | 2009-03-06 23:20:14 | [diff] [blame] | 1210 | GURL help_url(WideToASCII(l10n_util::GetString(IDS_HELP_CONTENT_URL))); |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 1211 | AddTabWithURL(help_url, GURL(), PageTransition::AUTO_BOOKMARK, true, -1, |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 1212 | false, NULL); |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 1213 | } |
| 1214 | |
[email protected] | 1677984 | 2009-07-08 23:45:29 | [diff] [blame] | 1215 | #if defined(OS_CHROMEOS) |
[email protected] | be71507 | 2009-07-07 15:43:20 | [diff] [blame] | 1216 | void Browser::ShowControlPanel() { |
| 1217 | GURL url("http://localhost:8080"); |
| 1218 | AddTabWithURL(url, GURL(), PageTransition::AUTO_BOOKMARK, true, -1, |
| 1219 | false, NULL); |
| 1220 | } |
| 1221 | #endif |
| 1222 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1223 | /////////////////////////////////////////////////////////////////////////////// |
| 1224 | |
| 1225 | // static |
| 1226 | void Browser::RegisterPrefs(PrefService* prefs) { |
| 1227 | prefs->RegisterDictionaryPref(prefs::kBrowserWindowPlacement); |
| 1228 | prefs->RegisterIntegerPref(prefs::kOptionsWindowLastTabIndex, 0); |
[email protected] | 749380bb | 2009-07-20 08:57:55 | [diff] [blame] | 1229 | prefs->RegisterIntegerPref(prefs::kDevToolsSplitLocation, -1); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | // static |
| 1233 | void Browser::RegisterUserPrefs(PrefService* prefs) { |
| 1234 | prefs->RegisterStringPref(prefs::kHomePage, L"chrome-internal:"); |
| 1235 | prefs->RegisterBooleanPref(prefs::kHomePageIsNewTabPage, true); |
| 1236 | prefs->RegisterIntegerPref(prefs::kCookieBehavior, |
| 1237 | net::CookiePolicy::ALLOW_ALL_COOKIES); |
| 1238 | prefs->RegisterBooleanPref(prefs::kShowHomeButton, false); |
[email protected] | 93a2c724 | 2009-07-07 18:43:09 | [diff] [blame] | 1239 | #if defined(OS_MACOSX) |
| 1240 | // This really belongs in platform code, but there's no good place to |
| 1241 | // initialize it between the time when the AppController is created |
| 1242 | // (where there's no profile) and the time the controller gets another |
| 1243 | // crack at the start of the main event loop. By that time, BrowserInit |
| 1244 | // has already created the browser window, and it's too late: we need the |
| 1245 | // pref to be already initialized. Doing it here also saves us from having |
| 1246 | // to hard-code pref registration in the several unit tests that use |
| 1247 | // this preference. |
| 1248 | prefs->RegisterBooleanPref(prefs::kShowPageOptionsButtons, false); |
| 1249 | #endif |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1250 | prefs->RegisterStringPref(prefs::kRecentlySelectedEncoding, L""); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1251 | prefs->RegisterBooleanPref(prefs::kDeleteBrowsingHistory, true); |
| 1252 | prefs->RegisterBooleanPref(prefs::kDeleteDownloadHistory, true); |
| 1253 | prefs->RegisterBooleanPref(prefs::kDeleteCache, true); |
| 1254 | prefs->RegisterBooleanPref(prefs::kDeleteCookies, true); |
| 1255 | prefs->RegisterBooleanPref(prefs::kDeletePasswords, false); |
| 1256 | prefs->RegisterBooleanPref(prefs::kDeleteFormData, true); |
| 1257 | prefs->RegisterIntegerPref(prefs::kDeleteTimePeriod, 0); |
[email protected] | 48b47ffa | 2009-05-02 07:18:30 | [diff] [blame] | 1258 | prefs->RegisterBooleanPref(prefs::kCheckDefaultBrowser, true); |
[email protected] | c886548 | 2009-07-23 20:40:10 | [diff] [blame] | 1259 | prefs->RegisterBooleanPref(prefs::kShowOmniboxSearchHint, true); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | // static |
| 1263 | Browser* Browser::GetBrowserForController( |
| 1264 | const NavigationController* controller, int* index_result) { |
| 1265 | BrowserList::const_iterator it; |
| 1266 | for (it = BrowserList::begin(); it != BrowserList::end(); ++it) { |
[email protected] | 902cdf77 | 2009-05-06 15:08:12 | [diff] [blame] | 1267 | int index = (*it)->tabstrip_model_.GetIndexOfController(controller); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1268 | if (index != TabStripModel::kNoTab) { |
| 1269 | if (index_result) |
| 1270 | *index_result = index; |
| 1271 | return *it; |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | return NULL; |
| 1276 | } |
| 1277 | |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 1278 | void Browser::ExecuteCommandWithDisposition( |
| 1279 | int id, WindowOpenDisposition disposition) { |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 1280 | // No commands are enabled if there is not yet any selected tab. |
| 1281 | // TODO(pkasting): It seems like we should not need this, because either |
| 1282 | // most/all commands should not have been enabled yet anyway or the ones that |
| 1283 | // are enabled should be global, or safe themselves against having no selected |
| 1284 | // tab. However, Ben says he tried removing this before and got lots of |
| 1285 | // crashes, e.g. from Windows sending WM_COMMANDs at random times during |
| 1286 | // window construction. This probably could use closer examination someday. |
| 1287 | if (!GetSelectedTabContents()) |
| 1288 | return; |
| 1289 | |
| 1290 | DCHECK(command_updater_.IsCommandEnabled(id)) << "Invalid/disabled command"; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1291 | |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 1292 | // The order of commands in this switch statement must match the function |
| 1293 | // declaration order in browser.h! |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1294 | switch (id) { |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1295 | // Navigation commands |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 1296 | case IDC_BACK: GoBack(disposition); break; |
| 1297 | case IDC_FORWARD: GoForward(disposition); break; |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1298 | case IDC_RELOAD: Reload(); break; |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 1299 | case IDC_HOME: Home(disposition); break; |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1300 | case IDC_OPEN_CURRENT_URL: OpenCurrentURL(); break; |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 1301 | case IDC_GO: Go(disposition); break; |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1302 | case IDC_STOP: Stop(); break; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1303 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1304 | // Window management commands |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1305 | case IDC_NEW_WINDOW: NewWindow(); break; |
| 1306 | case IDC_NEW_INCOGNITO_WINDOW: NewIncognitoWindow(); break; |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1307 | case IDC_NEW_WINDOW_PROFILE_0: |
| 1308 | case IDC_NEW_WINDOW_PROFILE_1: |
| 1309 | case IDC_NEW_WINDOW_PROFILE_2: |
| 1310 | case IDC_NEW_WINDOW_PROFILE_3: |
| 1311 | case IDC_NEW_WINDOW_PROFILE_4: |
| 1312 | case IDC_NEW_WINDOW_PROFILE_5: |
| 1313 | case IDC_NEW_WINDOW_PROFILE_6: |
| 1314 | case IDC_NEW_WINDOW_PROFILE_7: |
[email protected] | f21c613a | 2009-02-12 14:46:17 | [diff] [blame] | 1315 | case IDC_NEW_WINDOW_PROFILE_8: |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1316 | NewProfileWindowByIndex(id - IDC_NEW_WINDOW_PROFILE_0); break; |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1317 | case IDC_CLOSE_WINDOW: CloseWindow(); break; |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1318 | case IDC_NEW_TAB: NewTab(); break; |
| 1319 | case IDC_CLOSE_TAB: CloseTab(); break; |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1320 | case IDC_SELECT_NEXT_TAB: SelectNextTab(); break; |
| 1321 | case IDC_SELECT_PREVIOUS_TAB: SelectPreviousTab(); break; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1322 | case IDC_SELECT_TAB_0: |
| 1323 | case IDC_SELECT_TAB_1: |
| 1324 | case IDC_SELECT_TAB_2: |
| 1325 | case IDC_SELECT_TAB_3: |
| 1326 | case IDC_SELECT_TAB_4: |
| 1327 | case IDC_SELECT_TAB_5: |
| 1328 | case IDC_SELECT_TAB_6: |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1329 | case IDC_SELECT_TAB_7: SelectNumberedTab(id - IDC_SELECT_TAB_0); |
| 1330 | break; |
| 1331 | case IDC_SELECT_LAST_TAB: SelectLastTab(); break; |
| 1332 | case IDC_DUPLICATE_TAB: DuplicateTab(); break; |
| 1333 | case IDC_RESTORE_TAB: RestoreTab(); break; |
| 1334 | case IDC_SHOW_AS_TAB: ConvertPopupToTabbedBrowser(); break; |
[email protected] | 9282cea | 2009-02-18 18:49:00 | [diff] [blame] | 1335 | case IDC_FULLSCREEN: ToggleFullscreenMode(); break; |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1336 | case IDC_EXIT: Exit(); break; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1337 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1338 | // Page-related commands |
[email protected] | 40bdb6d9 | 2009-02-25 00:22:27 | [diff] [blame] | 1339 | case IDC_SAVE_PAGE: SavePage(); break; |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1340 | case IDC_STAR: BookmarkCurrentPage(); break; |
| 1341 | case IDC_VIEW_SOURCE: ViewSource(); break; |
[email protected] | c61db1d | 2009-02-06 03:39:18 | [diff] [blame] | 1342 | #if defined(OS_WIN) |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1343 | case IDC_CLOSE_POPUPS: ClosePopups(); break; |
[email protected] | 40d59ce5 | 2009-03-06 23:20:14 | [diff] [blame] | 1344 | #endif |
[email protected] | 6862ac6c | 2009-08-05 20:26:41 | [diff] [blame] | 1345 | case IDC_PRINT: Print(); break; |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1346 | case IDC_ENCODING_AUTO_DETECT: ToggleEncodingAutoDetect(); break; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1347 | case IDC_ENCODING_UTF8: |
| 1348 | case IDC_ENCODING_UTF16LE: |
| 1349 | case IDC_ENCODING_ISO88591: |
| 1350 | case IDC_ENCODING_WINDOWS1252: |
[email protected] | 1c5bf63 | 2008-12-11 20:30:49 | [diff] [blame] | 1351 | case IDC_ENCODING_GBK: |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1352 | case IDC_ENCODING_GB18030: |
| 1353 | case IDC_ENCODING_BIG5HKSCS: |
| 1354 | case IDC_ENCODING_BIG5: |
| 1355 | case IDC_ENCODING_KOREAN: |
| 1356 | case IDC_ENCODING_SHIFTJIS: |
| 1357 | case IDC_ENCODING_ISO2022JP: |
| 1358 | case IDC_ENCODING_EUCJP: |
| 1359 | case IDC_ENCODING_THAI: |
| 1360 | case IDC_ENCODING_ISO885915: |
| 1361 | case IDC_ENCODING_MACINTOSH: |
| 1362 | case IDC_ENCODING_ISO88592: |
| 1363 | case IDC_ENCODING_WINDOWS1250: |
| 1364 | case IDC_ENCODING_ISO88595: |
| 1365 | case IDC_ENCODING_WINDOWS1251: |
| 1366 | case IDC_ENCODING_KOI8R: |
| 1367 | case IDC_ENCODING_KOI8U: |
| 1368 | case IDC_ENCODING_ISO88597: |
| 1369 | case IDC_ENCODING_WINDOWS1253: |
| 1370 | case IDC_ENCODING_ISO88594: |
| 1371 | case IDC_ENCODING_ISO885913: |
| 1372 | case IDC_ENCODING_WINDOWS1257: |
| 1373 | case IDC_ENCODING_ISO88593: |
| 1374 | case IDC_ENCODING_ISO885910: |
| 1375 | case IDC_ENCODING_ISO885914: |
| 1376 | case IDC_ENCODING_ISO885916: |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1377 | case IDC_ENCODING_WINDOWS1254: |
| 1378 | case IDC_ENCODING_ISO88596: |
| 1379 | case IDC_ENCODING_WINDOWS1256: |
| 1380 | case IDC_ENCODING_ISO88598: |
[email protected] | e13271f | 2009-03-07 00:26:00 | [diff] [blame] | 1381 | case IDC_ENCODING_ISO88598I: |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1382 | case IDC_ENCODING_WINDOWS1255: |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1383 | case IDC_ENCODING_WINDOWS1258: OverrideEncoding(id); break; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1384 | |
[email protected] | 40d59ce5 | 2009-03-06 23:20:14 | [diff] [blame] | 1385 | #if defined(OS_WIN) |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1386 | // Clipboard commands |
| 1387 | case IDC_CUT: Cut(); break; |
| 1388 | case IDC_COPY: Copy(); break; |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1389 | case IDC_PASTE: Paste(); break; |
[email protected] | 31b1bc3a | 2009-03-10 19:26:19 | [diff] [blame] | 1390 | #endif |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1391 | |
| 1392 | // Find-in-page |
| 1393 | case IDC_FIND: Find(); break; |
| 1394 | case IDC_FIND_NEXT: FindNext(); break; |
| 1395 | case IDC_FIND_PREVIOUS: FindPrevious(); break; |
| 1396 | |
| 1397 | // Zoom |
| 1398 | case IDC_ZOOM_PLUS: ZoomIn(); break; |
| 1399 | case IDC_ZOOM_NORMAL: ZoomReset(); break; |
| 1400 | case IDC_ZOOM_MINUS: ZoomOut(); break; |
| 1401 | |
| 1402 | // Focus various bits of UI |
| 1403 | case IDC_FOCUS_TOOLBAR: FocusToolbar(); break; |
| 1404 | case IDC_FOCUS_LOCATION: FocusLocationBar(); break; |
| 1405 | case IDC_FOCUS_SEARCH: FocusSearch(); break; |
| 1406 | |
| 1407 | // Show various bits of UI |
| 1408 | case IDC_OPEN_FILE: OpenFile(); break; |
| 1409 | case IDC_CREATE_SHORTCUTS: OpenCreateShortcutsDialog(); break; |
[email protected] | c630f16 | 2009-07-22 09:41:33 | [diff] [blame] | 1410 | case IDC_DEV_TOOLS: ToggleDevToolsWindow(); break; |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1411 | case IDC_TASK_MANAGER: OpenTaskManager(); break; |
[email protected] | cb525c8 | 2008-12-08 23:04:54 | [diff] [blame] | 1412 | case IDC_SELECT_PROFILE: OpenSelectProfileDialog(); break; |
| 1413 | case IDC_NEW_PROFILE: OpenNewProfileDialog(); break; |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1414 | case IDC_REPORT_BUG: OpenBugReportDialog(); break; |
[email protected] | 44b2c885 | 2009-03-18 00:57:49 | [diff] [blame] | 1415 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1416 | case IDC_SHOW_BOOKMARK_BAR: ToggleBookmarkBar(); break; |
[email protected] | 44b2c885 | 2009-03-18 00:57:49 | [diff] [blame] | 1417 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1418 | case IDC_SHOW_BOOKMARK_MANAGER: OpenBookmarkManager(); break; |
[email protected] | 8bf80e92 | 2009-03-09 20:56:36 | [diff] [blame] | 1419 | case IDC_SHOW_HISTORY: ShowHistoryTab(); break; |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1420 | case IDC_SHOW_DOWNLOADS: ShowDownloadsTab(); break; |
[email protected] | 026e34a | 2009-02-06 00:06:12 | [diff] [blame] | 1421 | #ifdef CHROME_PERSONALIZATION |
[email protected] | ca38d8e | 2009-08-20 18:08:16 | [diff] [blame] | 1422 | case IDC_SYNC_BOOKMARKS: OpenSyncMyBookmarksDialog(); break; |
[email protected] | 0e3e3e3 | 2009-05-14 01:00:53 | [diff] [blame] | 1423 | #endif |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1424 | case IDC_OPTIONS: OpenOptionsDialog(); break; |
| 1425 | case IDC_EDIT_SEARCH_ENGINES: OpenKeywordEditor(); break; |
| 1426 | case IDC_VIEW_PASSWORDS: OpenPasswordManager(); break; |
[email protected] | 25364e1 | 2009-05-22 01:37:19 | [diff] [blame] | 1427 | case IDC_CLEAR_BROWSING_DATA: OpenClearBrowsingDataDialog(); break; |
[email protected] | 1bdf29e | 2009-05-11 23:45:25 | [diff] [blame] | 1428 | case IDC_IMPORT_SETTINGS: OpenImportSettingsDialog(); break; |
[email protected] | 4dbac17 | 2009-04-24 20:23:49 | [diff] [blame] | 1429 | case IDC_ABOUT: OpenAboutChromeDialog(); break; |
[email protected] | 40d59ce5 | 2009-03-06 23:20:14 | [diff] [blame] | 1430 | case IDC_HELP_PAGE: OpenHelpTab(); break; |
[email protected] | 1677984 | 2009-07-08 23:45:29 | [diff] [blame] | 1431 | #if defined(OS_CHROMEOS) |
[email protected] | be71507 | 2009-07-07 15:43:20 | [diff] [blame] | 1432 | case IDC_CONTROL_PANEL: ShowControlPanel(); break; |
| 1433 | #endif |
[email protected] | d938aed9 | 2009-01-22 19:49:33 | [diff] [blame] | 1434 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1435 | default: |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1436 | LOG(WARNING) << "Received Unimplemented Command: " << id; |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 1437 | break; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | 485fba4 | 2009-03-24 23:27:29 | [diff] [blame] | 1442 | // Browser, CommandUpdater::CommandUpdaterDelegate implementation: |
| 1443 | |
| 1444 | void Browser::ExecuteCommand(int id) { |
| 1445 | ExecuteCommandWithDisposition(id, CURRENT_TAB); |
| 1446 | } |
| 1447 | |
| 1448 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1449 | // Browser, TabStripModelDelegate implementation: |
| 1450 | |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 1451 | TabContents* Browser::AddBlankTab(bool foreground) { |
| 1452 | return AddBlankTabAt(-1, foreground); |
| 1453 | } |
| 1454 | |
| 1455 | TabContents* Browser::AddBlankTabAt(int index, bool foreground) { |
| 1456 | return AddTabWithURL(GURL(chrome::kChromeUINewTabURL), GURL(), |
[email protected] | 5a4940be | 2009-05-06 06:44:39 | [diff] [blame] | 1457 | PageTransition::TYPED, foreground, index, false, NULL); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 1458 | } |
| 1459 | |
[email protected] | 3d1104f | 2009-03-26 15:30:28 | [diff] [blame] | 1460 | Browser* Browser::CreateNewStripWithContents(TabContents* detached_contents, |
| 1461 | const gfx::Rect& window_bounds, |
| 1462 | const DockInfo& dock_info) { |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 1463 | DCHECK(type_ == TYPE_NORMAL); |
[email protected] | adf650f | 2008-12-09 16:10:06 | [diff] [blame] | 1464 | |
[email protected] | 5e49546 | 2008-11-20 23:07:41 | [diff] [blame] | 1465 | gfx::Rect new_window_bounds = window_bounds; |
| 1466 | bool maximize = false; |
| 1467 | if (dock_info.GetNewWindowBounds(&new_window_bounds, &maximize)) |
| 1468 | dock_info.AdjustOtherWindowBounds(); |
| 1469 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1470 | // Create an empty new browser window the same size as the old one. |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 1471 | Browser* browser = new Browser(TYPE_NORMAL, profile_); |
[email protected] | 5e49546 | 2008-11-20 23:07:41 | [diff] [blame] | 1472 | browser->set_override_bounds(new_window_bounds); |
[email protected] | 2e71662 | 2009-03-09 21:11:01 | [diff] [blame] | 1473 | browser->set_maximized_state( |
| 1474 | maximize ? MAXIMIZED_STATE_MAXIMIZED : MAXIMIZED_STATE_UNMAXIMIZED); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 1475 | browser->CreateBrowserWindow(); |
| 1476 | browser->tabstrip_model()->AppendTabContents(detached_contents, true); |
[email protected] | 159f776 | 2008-12-19 14:58:27 | [diff] [blame] | 1477 | // Make sure the loading state is updated correctly, otherwise the throbber |
| 1478 | // won't start if the page is loading. |
| 1479 | browser->LoadingStateChanged(detached_contents); |
[email protected] | 3d1104f | 2009-03-26 15:30:28 | [diff] [blame] | 1480 | return browser; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1481 | } |
| 1482 | |
[email protected] | 7c0560f | 2009-07-07 18:44:31 | [diff] [blame] | 1483 | void Browser::ContinueDraggingDetachedTab(TabContents* contents, |
| 1484 | const gfx::Rect& window_bounds, |
| 1485 | const gfx::Rect& tab_bounds) { |
| 1486 | Browser* browser = new Browser(TYPE_NORMAL, profile_); |
| 1487 | browser->set_override_bounds(window_bounds); |
| 1488 | browser->CreateBrowserWindow(); |
| 1489 | browser->tabstrip_model()->AppendTabContents(contents, true); |
| 1490 | browser->LoadingStateChanged(contents); |
| 1491 | browser->window()->Show(); |
| 1492 | browser->window()->ContinueDraggingDetachedTab(tab_bounds); |
| 1493 | } |
| 1494 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1495 | int Browser::GetDragActions() const { |
[email protected] | d54f6a00 | 2009-03-18 17:17:25 | [diff] [blame] | 1496 | return TAB_TEAROFF_ACTION | (tab_count() > 1 ? TAB_MOVE_ACTION : 0); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | TabContents* Browser::CreateTabContentsForURL( |
| 1500 | const GURL& url, const GURL& referrer, Profile* profile, |
| 1501 | PageTransition::Type transition, bool defer_load, |
| 1502 | SiteInstance* instance) const { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 1503 | TabContents* contents = new TabContents(profile, instance, |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 1504 | MSG_ROUTING_NONE, NULL); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1505 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1506 | if (!defer_load) { |
| 1507 | // Load the initial URL before adding the new tab contents to the tab strip |
| 1508 | // so that the tab contents has navigation state. |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 1509 | contents->controller().LoadURL(url, referrer, transition); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | return contents; |
| 1513 | } |
| 1514 | |
| 1515 | bool Browser::CanDuplicateContentsAt(int index) { |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 1516 | NavigationController& nc = GetTabContentsAt(index)->controller(); |
| 1517 | return nc.tab_contents() && nc.GetLastCommittedEntry(); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | void Browser::DuplicateContentsAt(int index) { |
| 1521 | TabContents* contents = GetTabContentsAt(index); |
| 1522 | TabContents* new_contents = NULL; |
| 1523 | DCHECK(contents); |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 1524 | bool pinned = false; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1525 | |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 1526 | if (type_ == TYPE_NORMAL) { |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1527 | // If this is a tabbed browser, just create a duplicate tab inside the same |
| 1528 | // window next to the tab being duplicated. |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 1529 | new_contents = contents->Clone(); |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 1530 | // Make sure we force the index, otherwise the duplicate tab may appear at |
| 1531 | // the wrong location. |
| 1532 | tabstrip_model_.AddTabContents(new_contents, index + 1, true, |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1533 | PageTransition::LINK, true); |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 1534 | if (tabstrip_model_.IsTabPinned(index)) { |
| 1535 | pinned = true; |
| 1536 | tabstrip_model_.SetTabPinned(index + 1, true); |
| 1537 | } |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1538 | } else { |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 1539 | Browser* browser = NULL; |
[email protected] | d5fbc00 | 2009-02-27 22:12:58 | [diff] [blame] | 1540 | if (type_ & TYPE_APP) { |
| 1541 | browser = Browser::CreateForApp(app_name_, profile_, type_ & TYPE_POPUP); |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 1542 | } else if (type_ == TYPE_POPUP) { |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 1543 | browser = Browser::CreateForPopup(profile_); |
| 1544 | } |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1545 | |
[email protected] | b1fed96 | 2008-12-18 00:54:08 | [diff] [blame] | 1546 | // Preserve the size of the original window. The new window has already |
| 1547 | // been given an offset by the OS, so we shouldn't copy the old bounds. |
| 1548 | BrowserWindow* new_window = browser->window(); |
[email protected] | dae12ee6 | 2009-07-29 20:02:05 | [diff] [blame] | 1549 | new_window->SetBounds(gfx::Rect(new_window->GetRestoredBounds().origin(), |
| 1550 | window()->GetRestoredBounds().size())); |
[email protected] | b1fed96 | 2008-12-18 00:54:08 | [diff] [blame] | 1551 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1552 | // We need to show the browser now. Otherwise ContainerWin assumes the |
| 1553 | // TabContents is invisible and won't size it. |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 1554 | browser->window()->Show(); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1555 | |
| 1556 | // The page transition below is only for the purpose of inserting the tab. |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 1557 | new_contents = browser->AddTabWithNavigationController( |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 1558 | &contents->Clone()->controller(), |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1559 | PageTransition::LINK); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | if (profile_->HasSessionService()) { |
| 1563 | SessionService* session_service = profile_->GetSessionService(); |
| 1564 | if (session_service) |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 1565 | session_service->TabRestored(&new_contents->controller(), pinned); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1566 | } |
| 1567 | } |
| 1568 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1569 | void Browser::CloseFrameAfterDragSession() { |
[email protected] | 9aefecc9 | 2009-06-10 21:31:32 | [diff] [blame] | 1570 | #if defined(OS_WIN) || defined(OS_LINUX) |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1571 | // This is scheduled to run after we return to the message loop because |
| 1572 | // otherwise the frame will think the drag session is still active and ignore |
| 1573 | // the request. |
[email protected] | d6a3c77 | 2009-01-27 19:41:20 | [diff] [blame] | 1574 | // TODO(port): figure out what is required here in a cross-platform world |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1575 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1576 | method_factory_.NewRunnableMethod(&Browser::CloseFrame)); |
[email protected] | d6a3c77 | 2009-01-27 19:41:20 | [diff] [blame] | 1577 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1578 | } |
| 1579 | |
[email protected] | 505323e2 | 2009-01-24 02:47:58 | [diff] [blame] | 1580 | void Browser::CreateHistoricalTab(TabContents* contents) { |
| 1581 | // We don't create historical tabs for incognito windows or windows without |
| 1582 | // profiles. |
| 1583 | if (!profile() || profile()->IsOffTheRecord() || |
| 1584 | !profile()->GetTabRestoreService()) { |
| 1585 | return; |
| 1586 | } |
| 1587 | |
| 1588 | // We only create historical tab entries for normal tabbed browser windows. |
| 1589 | if (type() == TYPE_NORMAL) { |
| 1590 | profile()->GetTabRestoreService()->CreateHistoricalTab( |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 1591 | &contents->controller()); |
[email protected] | 505323e2 | 2009-01-24 02:47:58 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | bool Browser::RunUnloadListenerBeforeClosing(TabContents* contents) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 1596 | // If the TabContents is not connected yet, then there's no unload |
| 1597 | // handler we can fire even if the TabContents has an unload listener. |
| 1598 | // One case where we hit this is in a tab that has an infinite loop |
| 1599 | // before load. |
| 1600 | if (TabHasUnloadListener(contents)) { |
| 1601 | // If the page has unload listeners, then we tell the renderer to fire |
| 1602 | // them. Once they have fired, we'll get a message back saying whether |
| 1603 | // to proceed closing the page or not, which sends us back to this method |
| 1604 | // with the HasUnloadListener bit cleared. |
[email protected] | ae23c27 | 2009-07-27 20:15:40 | [diff] [blame] | 1605 | contents->render_view_host()->FirePageBeforeUnload(false); |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 1606 | return true; |
[email protected] | 505323e2 | 2009-01-24 02:47:58 | [diff] [blame] | 1607 | } |
| 1608 | return false; |
| 1609 | } |
| 1610 | |
[email protected] | 940ccb2 | 2009-04-30 17:11:10 | [diff] [blame] | 1611 | bool Browser::CanCloseContentsAt(int index) { |
| 1612 | if (tabstrip_model_.count() > 1) |
| 1613 | return true; |
| 1614 | // We are closing the last tab for this browser. Make sure to check for |
| 1615 | // in-progress downloads. |
| 1616 | // Note that the next call when it returns false will ask the user for |
| 1617 | // confirmation before closing the browser if the user decides so. |
| 1618 | return CanCloseWithInProgressDownloads(); |
| 1619 | } |
[email protected] | d6a3c77 | 2009-01-27 19:41:20 | [diff] [blame] | 1620 | |
[email protected] | 2d46c84 | 2008-11-14 19:24:31 | [diff] [blame] | 1621 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1622 | // Browser, TabStripModelObserver implementation: |
| 1623 | |
| 1624 | void Browser::TabInsertedAt(TabContents* contents, |
| 1625 | int index, |
| 1626 | bool foreground) { |
| 1627 | contents->set_delegate(this); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 1628 | contents->controller().SetWindowID(session_id()); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1629 | |
| 1630 | SyncHistoryWithTabs(tabstrip_model_.GetIndexOfTabContents(contents)); |
| 1631 | |
[email protected] | 159f776 | 2008-12-19 14:58:27 | [diff] [blame] | 1632 | // Make sure the loading state is updated correctly, otherwise the throbber |
| 1633 | // won't start if the page is loading. |
| 1634 | LoadingStateChanged(contents); |
| 1635 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1636 | // If the tab crashes in the beforeunload or unload handler, it won't be |
| 1637 | // able to ack. But we know we can close it. |
[email protected] | 0cb9410 | 2009-05-22 19:51:21 | [diff] [blame] | 1638 | registrar_.Add(this, NotificationType::TAB_CONTENTS_DISCONNECTED, |
| 1639 | Source<TabContents>(contents)); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | void Browser::TabClosingAt(TabContents* contents, int index) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1643 | NotificationService::current()->Notify( |
| 1644 | NotificationType::TAB_CLOSING, |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 1645 | Source<NavigationController>(&contents->controller()), |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1646 | NotificationService::NoDetails()); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1647 | |
| 1648 | // Sever the TabContents' connection back to us. |
| 1649 | contents->set_delegate(NULL); |
| 1650 | } |
| 1651 | |
| 1652 | void Browser::TabDetachedAt(TabContents* contents, int index) { |
[email protected] | aacfcc7 | 2009-05-27 19:39:54 | [diff] [blame] | 1653 | // Save what the user's currently typing. |
| 1654 | window_->GetLocationBar()->SaveStateToContents(contents); |
| 1655 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1656 | contents->set_delegate(NULL); |
| 1657 | if (!tabstrip_model_.closing_all()) |
| 1658 | SyncHistoryWithTabs(0); |
| 1659 | |
| 1660 | RemoveScheduledUpdatesFor(contents); |
| 1661 | |
[email protected] | 4801ecc | 2009-04-05 04:52:58 | [diff] [blame] | 1662 | if (find_bar_controller_.get() && index == tabstrip_model_.selected_index()) |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 1663 | find_bar_controller_->ChangeTabContents(NULL); |
[email protected] | 4801ecc | 2009-04-05 04:52:58 | [diff] [blame] | 1664 | |
[email protected] | 0cb9410 | 2009-05-22 19:51:21 | [diff] [blame] | 1665 | registrar_.Remove(this, NotificationType::TAB_CONTENTS_DISCONNECTED, |
| 1666 | Source<TabContents>(contents)); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1667 | } |
| 1668 | |
[email protected] | 85ff2c4 | 2009-05-29 21:22:03 | [diff] [blame] | 1669 | void Browser::TabDeselectedAt(TabContents* contents, int index) { |
| 1670 | // Save what the user's currently typing, so it can be restored when we |
| 1671 | // switch back to this tab. |
| 1672 | window_->GetLocationBar()->SaveStateToContents(contents); |
| 1673 | } |
| 1674 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1675 | void Browser::TabSelectedAt(TabContents* old_contents, |
| 1676 | TabContents* new_contents, |
| 1677 | int index, |
| 1678 | bool user_gesture) { |
| 1679 | DCHECK(old_contents != new_contents); |
| 1680 | |
| 1681 | // If we have any update pending, do it now. |
| 1682 | if (!chrome_updater_factory_.empty() && old_contents) |
| 1683 | ProcessPendingUIUpdates(); |
| 1684 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1685 | // Propagate the profile to the location bar. |
| 1686 | UpdateToolbar(true); |
| 1687 | |
[email protected] | 64ff794 | 2008-12-17 18:11:23 | [diff] [blame] | 1688 | // Update stop/go state. |
[email protected] | c398981 | 2009-04-15 18:08:49 | [diff] [blame] | 1689 | UpdateStopGoState(new_contents->is_loading(), true); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1690 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1691 | // Update commands to reflect current state. |
| 1692 | UpdateCommandsForTabState(); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1693 | |
| 1694 | // Reset the status bubble. |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 1695 | StatusBubble* status_bubble = GetStatusBubble(); |
| 1696 | if (status_bubble) { |
| 1697 | status_bubble->Hide(); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1698 | |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 1699 | // Show the loading state (if any). |
| 1700 | status_bubble->SetStatus(GetSelectedTabContents()->GetStatusText()); |
| 1701 | } |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1702 | |
[email protected] | 4801ecc | 2009-04-05 04:52:58 | [diff] [blame] | 1703 | if (find_bar_controller_.get()) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 1704 | find_bar_controller_->ChangeTabContents(new_contents); |
[email protected] | 4801ecc | 2009-04-05 04:52:58 | [diff] [blame] | 1705 | find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect(), |
| 1706 | true); |
| 1707 | } |
| 1708 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1709 | // Update sessions. Don't force creation of sessions. If sessions doesn't |
| 1710 | // exist, the change will be picked up by sessions when created. |
| 1711 | if (profile_->HasSessionService()) { |
| 1712 | SessionService* session_service = profile_->GetSessionService(); |
| 1713 | if (session_service && !tabstrip_model_.closing_all()) { |
[email protected] | 2d46c84 | 2008-11-14 19:24:31 | [diff] [blame] | 1714 | session_service->SetSelectedTabInWindow( |
| 1715 | session_id(), tabstrip_model_.selected_index()); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1716 | } |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | void Browser::TabMoved(TabContents* contents, |
| 1721 | int from_index, |
[email protected] | c8db0916 | 2009-07-13 23:48:48 | [diff] [blame] | 1722 | int to_index, |
| 1723 | bool pinned_state_changed) { |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1724 | DCHECK(from_index >= 0 && to_index >= 0); |
| 1725 | // Notify the history service. |
| 1726 | SyncHistoryWithTabs(std::min(from_index, to_index)); |
| 1727 | } |
| 1728 | |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 1729 | void Browser::TabPinnedStateChanged(TabContents* contents, int index) { |
| 1730 | if (!profile()->HasSessionService()) |
| 1731 | return; |
| 1732 | SessionService* session_service = profile()->GetSessionService(); |
| 1733 | if (session_service) { |
| 1734 | session_service->SetPinnedState( |
| 1735 | session_id(), |
| 1736 | GetTabContentsAt(index)->controller().session_id(), |
| 1737 | tabstrip_model_.IsTabPinned(index)); |
| 1738 | } |
| 1739 | } |
| 1740 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1741 | void Browser::TabStripEmpty() { |
| 1742 | // Close the frame after we return to the message loop (not immediately, |
| 1743 | // otherwise it will destroy this object before the stack has a chance to |
| 1744 | // cleanly unwind.) |
| 1745 | // Note: This will be called several times if TabStripEmpty is called several |
| 1746 | // times. This is because it does not close the window if tabs are |
| 1747 | // still present. |
| 1748 | // NOTE: If you change to be immediate (no invokeLater) then you'll need to |
| 1749 | // update BrowserList::CloseAllBrowsers. |
| 1750 | MessageLoop::current()->PostTask(FROM_HERE, |
| 1751 | method_factory_.NewRunnableMethod(&Browser::CloseFrame)); |
| 1752 | } |
| 1753 | |
| 1754 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | e0c7c26 | 2009-04-23 23:09:43 | [diff] [blame] | 1755 | // Browser, PageNavigator implementation: |
| 1756 | void Browser::OpenURL(const GURL& url, const GURL& referrer, |
| 1757 | WindowOpenDisposition disposition, |
| 1758 | PageTransition::Type transition) { |
| 1759 | OpenURLFromTab(NULL, url, referrer, disposition, transition); |
| 1760 | } |
| 1761 | |
| 1762 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1763 | // Browser, TabContentsDelegate implementation: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1764 | |
| 1765 | void Browser::OpenURLFromTab(TabContents* source, |
[email protected] | 1f7d7e9 | 2009-06-02 20:55:04 | [diff] [blame] | 1766 | const GURL& url, |
| 1767 | const GURL& referrer, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1768 | WindowOpenDisposition disposition, |
[email protected] | e38f4015 | 2008-09-12 23:08:30 | [diff] [blame] | 1769 | PageTransition::Type transition) { |
[email protected] | 1f7d7e9 | 2009-06-02 20:55:04 | [diff] [blame] | 1770 | OpenURLAtIndex(source, url, referrer, disposition, transition, -1, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | void Browser::NavigationStateChanged(const TabContents* source, |
| 1774 | unsigned changed_flags) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1775 | // Only update the UI when something visible has changed. |
[email protected] | e83f168 | 2008-09-07 23:57:40 | [diff] [blame] | 1776 | if (changed_flags) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1777 | ScheduleUIUpdate(source, changed_flags); |
| 1778 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1779 | // We don't schedule updates to commands since they will only change once per |
| 1780 | // navigation, so we don't have to worry about flickering. |
[email protected] | e83f168 | 2008-09-07 23:57:40 | [diff] [blame] | 1781 | if (changed_flags & TabContents::INVALIDATE_URL) |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 1782 | UpdateCommandsForTabState(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1783 | } |
| 1784 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1785 | void Browser::AddNewContents(TabContents* source, |
| 1786 | TabContents* new_contents, |
| 1787 | WindowOpenDisposition disposition, |
| 1788 | const gfx::Rect& initial_pos, |
| 1789 | bool user_gesture) { |
| 1790 | DCHECK(disposition != SAVE_TO_DISK); // No code for this yet |
[email protected] | b680ad2 | 2009-04-15 23:19:42 | [diff] [blame] | 1791 | DCHECK(disposition != CURRENT_TAB); // Can't create a new contents for the |
| 1792 | // current tab. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1793 | |
| 1794 | // If this is an application we can only have one tab so we need to process |
[email protected] | ebdcf974 | 2009-01-23 05:25:28 | [diff] [blame] | 1795 | // this in tabbed browser window. |
| 1796 | if (tabstrip_model_.count() > 0 && |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1797 | disposition != NEW_WINDOW && disposition != NEW_POPUP && |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 1798 | type_ != TYPE_NORMAL) { |
[email protected] | 1a6b30a | 2009-08-19 03:52:11 | [diff] [blame] | 1799 | Browser* b = GetOrCreateTabbedBrowser(profile_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1800 | DCHECK(b); |
| 1801 | PageTransition::Type transition = PageTransition::LINK; |
| 1802 | // If we were called from an "installed webapp" we want to emulate the code |
| 1803 | // that is run from browser_init.cc for links from external applications. |
| 1804 | // This means we need to open the tab with the START PAGE transition. |
| 1805 | // AddNewContents doesn't support this but the TabStripModel's |
| 1806 | // AddTabContents method does. |
[email protected] | d5fbc00 | 2009-02-27 22:12:58 | [diff] [blame] | 1807 | if (type_ & TYPE_APP) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1808 | transition = PageTransition::START_PAGE; |
[email protected] | 4d34e2e | 2009-05-26 22:55:28 | [diff] [blame] | 1809 | b->tabstrip_model()->AddTabContents(new_contents, -1, false, transition, |
| 1810 | true); |
[email protected] | ebdcf974 | 2009-01-23 05:25:28 | [diff] [blame] | 1811 | b->window()->Show(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1812 | return; |
| 1813 | } |
| 1814 | |
| 1815 | if (disposition == NEW_POPUP) { |
| 1816 | BuildPopupWindow(source, new_contents, initial_pos); |
| 1817 | } else if (disposition == NEW_WINDOW) { |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 1818 | Browser* browser = Browser::Create(profile_); |
| 1819 | browser->AddNewContents(source, new_contents, NEW_FOREGROUND_TAB, |
| 1820 | initial_pos, user_gesture); |
| 1821 | browser->window()->Show(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1822 | } else if (disposition != SUPPRESS_OPEN) { |
[email protected] | 4d34e2e | 2009-05-26 22:55:28 | [diff] [blame] | 1823 | tabstrip_model_.AddTabContents(new_contents, -1, false, |
| 1824 | PageTransition::LINK, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1825 | disposition == NEW_FOREGROUND_TAB); |
| 1826 | } |
| 1827 | } |
| 1828 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1829 | void Browser::ActivateContents(TabContents* contents) { |
| 1830 | tabstrip_model_.SelectTabContentsAt( |
| 1831 | tabstrip_model_.GetIndexOfTabContents(contents), false); |
[email protected] | f3e99e3 | 2008-07-30 04:48:39 | [diff] [blame] | 1832 | window_->Activate(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1833 | } |
| 1834 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1835 | void Browser::LoadingStateChanged(TabContents* source) { |
[email protected] | afb7388 | 2008-11-14 22:40:44 | [diff] [blame] | 1836 | window_->UpdateLoadingAnimations(tabstrip_model_.TabsAreLoading()); |
[email protected] | f3e99e3 | 2008-07-30 04:48:39 | [diff] [blame] | 1837 | window_->UpdateTitleBar(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1838 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1839 | if (source == GetSelectedTabContents()) { |
[email protected] | c398981 | 2009-04-15 18:08:49 | [diff] [blame] | 1840 | UpdateStopGoState(source->is_loading(), false); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 1841 | if (GetStatusBubble()) |
| 1842 | GetStatusBubble()->SetStatus(GetSelectedTabContents()->GetStatusText()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | void Browser::CloseContents(TabContents* source) { |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 1847 | if (is_attempting_to_close_browser_) { |
| 1848 | // If we're trying to close the browser, just clear the state related to |
[email protected] | adf650f | 2008-12-09 16:10:06 | [diff] [blame] | 1849 | // waiting for unload to fire. Don't actually try to close the tab as it |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 1850 | // will go down the slow shutdown path instead of the fast path of killing |
| 1851 | // all the renderer processes. |
[email protected] | 8a2ce5a | 2008-08-11 23:43:08 | [diff] [blame] | 1852 | ClearUnloadState(source); |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 1853 | return; |
| 1854 | } |
| 1855 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1856 | int index = tabstrip_model_.GetIndexOfTabContents(source); |
| 1857 | if (index == TabStripModel::kNoTab) { |
| 1858 | NOTREACHED() << "CloseContents called for tab not in our strip"; |
| 1859 | return; |
| 1860 | } |
| 1861 | tabstrip_model_.CloseTabContentsAt(index); |
| 1862 | } |
| 1863 | |
[email protected] | 15e8abe | 2008-08-20 22:40:40 | [diff] [blame] | 1864 | void Browser::MoveContents(TabContents* source, const gfx::Rect& pos) { |
[email protected] | d5fbc00 | 2009-02-27 22:12:58 | [diff] [blame] | 1865 | if ((type() & TYPE_POPUP) == 0) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1866 | NOTREACHED() << "moving invalid browser type"; |
| 1867 | return; |
| 1868 | } |
[email protected] | 2d46c84 | 2008-11-14 19:24:31 | [diff] [blame] | 1869 | window_->SetBounds(pos); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1870 | } |
| 1871 | |
[email protected] | b6c87458 | 2009-05-08 19:38:31 | [diff] [blame] | 1872 | void Browser::DetachContents(TabContents* source) { |
| 1873 | int index = tabstrip_model_.GetIndexOfTabContents(source); |
| 1874 | if (index >= 0) |
| 1875 | tabstrip_model_.DetachTabContentsAt(index); |
| 1876 | } |
| 1877 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1878 | bool Browser::IsPopup(TabContents* source) { |
| 1879 | // A non-tabbed BROWSER is an unconstrained popup. |
[email protected] | d5fbc00 | 2009-02-27 22:12:58 | [diff] [blame] | 1880 | return (type() & TYPE_POPUP); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1881 | } |
| 1882 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1883 | void Browser::ToolbarSizeChanged(TabContents* source, bool is_animating) { |
| 1884 | if (source == GetSelectedTabContents() || source == NULL) { |
| 1885 | // This will refresh the shelf if needed. |
| 1886 | window_->SelectedTabToolbarSizeChanged(is_animating); |
| 1887 | } |
| 1888 | } |
| 1889 | |
| 1890 | void Browser::URLStarredChanged(TabContents* source, bool starred) { |
| 1891 | if (source == GetSelectedTabContents()) |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 1892 | window_->SetStarredState(starred); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1893 | } |
| 1894 | |
[email protected] | b640603 | 2009-03-19 15:10:15 | [diff] [blame] | 1895 | void Browser::ContentsMouseEvent(TabContents* source, bool motion) { |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 1896 | if (!GetStatusBubble()) |
| 1897 | return; |
| 1898 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1899 | if (source == GetSelectedTabContents()) { |
[email protected] | b640603 | 2009-03-19 15:10:15 | [diff] [blame] | 1900 | if (motion) { |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1901 | GetStatusBubble()->MouseMoved(); |
[email protected] | b640603 | 2009-03-19 15:10:15 | [diff] [blame] | 1902 | } else { |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1903 | GetStatusBubble()->SetURL(GURL(), std::wstring()); |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | void Browser::UpdateTargetURL(TabContents* source, const GURL& url) { |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 1909 | if (!GetStatusBubble()) |
| 1910 | return; |
| 1911 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1912 | if (source == GetSelectedTabContents()) { |
| 1913 | PrefService* prefs = profile_->GetPrefs(); |
| 1914 | GetStatusBubble()->SetURL(url, prefs->GetString(prefs::kAcceptLanguages)); |
| 1915 | } |
| 1916 | } |
| 1917 | |
[email protected] | 3a6a3b6 | 2009-05-27 21:36:20 | [diff] [blame] | 1918 | void Browser::UpdateDownloadShelfVisibility(bool visible) { |
[email protected] | 9b032bf | 2009-07-21 17:34:23 | [diff] [blame] | 1919 | if (GetStatusBubble()) |
| 1920 | GetStatusBubble()->UpdateDownloadShelfVisibility(visible); |
[email protected] | 3a6a3b6 | 2009-05-27 21:36:20 | [diff] [blame] | 1921 | } |
| 1922 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1923 | void Browser::ContentsZoomChange(bool zoom_in) { |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 1924 | ExecuteCommand(zoom_in ? IDC_ZOOM_PLUS : IDC_ZOOM_MINUS); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1925 | } |
| 1926 | |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 1927 | void Browser::TabContentsFocused(TabContents* tab_content) { |
| 1928 | window_->TabContentsFocused(tab_content); |
| 1929 | } |
| 1930 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1931 | bool Browser::IsApplication() const { |
[email protected] | d5fbc00 | 2009-02-27 22:12:58 | [diff] [blame] | 1932 | return (type_ & TYPE_APP) != 0; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | void Browser::ConvertContentsToApplication(TabContents* contents) { |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 1936 | const GURL& url = contents->controller().GetActiveEntry()->url(); |
[email protected] | 0303f31c | 2009-02-02 06:42:05 | [diff] [blame] | 1937 | std::wstring app_name = ComputeApplicationNameFromURL(url); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1938 | RegisterAppPrefs(app_name); |
| 1939 | |
[email protected] | b6c87458 | 2009-05-08 19:38:31 | [diff] [blame] | 1940 | DetachContents(contents); |
[email protected] | d5fbc00 | 2009-02-27 22:12:58 | [diff] [blame] | 1941 | Browser* browser = Browser::CreateForApp(app_name, profile_, false); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 1942 | browser->tabstrip_model()->AppendTabContents(contents, true); |
[email protected] | 840b151 | 2009-07-21 16:53:46 | [diff] [blame] | 1943 | TabContents* tab_contents = browser->GetSelectedTabContents(); |
| 1944 | tab_contents->GetMutableRendererPrefs()->can_accept_load_drops = false; |
| 1945 | tab_contents->render_view_host()->SyncRendererPrefs(); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 1946 | browser->window()->Show(); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1947 | } |
| 1948 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1949 | bool Browser::ShouldDisplayURLField() { |
| 1950 | return !IsApplication(); |
| 1951 | } |
| 1952 | |
| 1953 | void Browser::BeforeUnloadFired(TabContents* tab, |
| 1954 | bool proceed, |
| 1955 | bool* proceed_to_fire_unload) { |
| 1956 | if (!is_attempting_to_close_browser_) { |
| 1957 | *proceed_to_fire_unload = proceed; |
| 1958 | return; |
| 1959 | } |
| 1960 | |
| 1961 | if (!proceed) { |
| 1962 | CancelWindowClose(); |
| 1963 | *proceed_to_fire_unload = false; |
| 1964 | return; |
| 1965 | } |
| 1966 | |
[email protected] | 06b42f03 | 2008-12-03 18:43:05 | [diff] [blame] | 1967 | if (RemoveFromSet(&tabs_needing_before_unload_fired_, tab)) { |
[email protected] | 2d46c84 | 2008-11-14 19:24:31 | [diff] [blame] | 1968 | // Now that beforeunload has fired, put the tab on the queue to fire |
| 1969 | // unload. |
[email protected] | 06b42f03 | 2008-12-03 18:43:05 | [diff] [blame] | 1970 | tabs_needing_unload_fired_.insert(tab); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1971 | ProcessPendingTabs(); |
[email protected] | adf650f | 2008-12-09 16:10:06 | [diff] [blame] | 1972 | // We want to handle firing the unload event ourselves since we want to |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 1973 | // fire all the beforeunload events before attempting to fire the unload |
| 1974 | // events should the user cancel closing the browser. |
| 1975 | *proceed_to_fire_unload = false; |
| 1976 | return; |
| 1977 | } |
| 1978 | |
| 1979 | *proceed_to_fire_unload = true; |
| 1980 | } |
| 1981 | |
[email protected] | f21c613a | 2009-02-12 14:46:17 | [diff] [blame] | 1982 | gfx::Rect Browser::GetRootWindowResizerRect() const { |
| 1983 | return window_->GetRootWindowResizerRect(); |
| 1984 | } |
| 1985 | |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 1986 | void Browser::ShowHtmlDialog(HtmlDialogUIDelegate* delegate, |
[email protected] | 322f3ff | 2009-05-22 16:19:54 | [diff] [blame] | 1987 | gfx::NativeWindow parent_window) { |
[email protected] | 2d46c84 | 2008-11-14 19:24:31 | [diff] [blame] | 1988 | window_->ShowHTMLDialog(delegate, parent_window); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1989 | } |
| 1990 | |
[email protected] | 4bcefee | 2009-01-29 15:57:57 | [diff] [blame] | 1991 | void Browser::SetFocusToLocationBar() { |
[email protected] | 1a38062 | 2009-02-17 17:33:13 | [diff] [blame] | 1992 | // Two differences between this and FocusLocationBar(): |
| 1993 | // (1) This doesn't get recorded in user metrics, since it's called |
| 1994 | // internally. |
| 1995 | // (2) This checks whether the location bar can be focused, and if not, clears |
| 1996 | // the focus. FocusLocationBar() is only reached when the location bar is |
| 1997 | // focusable, but this may be reached at other times, e.g. while in |
| 1998 | // fullscreen mode, where we need to leave focus in a consistent state. |
| 1999 | window_->SetFocusToLocationBar(); |
[email protected] | 4bcefee | 2009-01-29 15:57:57 | [diff] [blame] | 2000 | } |
| 2001 | |
[email protected] | 7d5925a | 2009-03-03 02:59:12 | [diff] [blame] | 2002 | void Browser::RenderWidgetShowing() { |
| 2003 | window_->DisableInactiveFrame(); |
| 2004 | } |
[email protected] | a239c3f | 2009-02-17 22:13:19 | [diff] [blame] | 2005 | |
[email protected] | 0a2aeb8 | 2009-05-15 21:52:48 | [diff] [blame] | 2006 | int Browser::GetExtraRenderViewHeight() const { |
| 2007 | return window_->GetExtraRenderViewHeight(); |
| 2008 | } |
| 2009 | |
[email protected] | d0cc9fb5 | 2009-06-23 02:26:16 | [diff] [blame] | 2010 | void Browser::OnStartDownload(DownloadItem* download) { |
| 2011 | if (!window()) |
| 2012 | return; |
| 2013 | |
| 2014 | // GetDownloadShelf creates the download shelf if it was not yet created. |
| 2015 | window()->GetDownloadShelf()->AddDownload(new DownloadItemModel(download)); |
| 2016 | |
[email protected] | d0cc9fb5 | 2009-06-23 02:26:16 | [diff] [blame] | 2017 | // Don't show the animation for "Save file" downloads. |
| 2018 | if (download->total_bytes() > 0) { |
| 2019 | TabContents* current_tab = GetSelectedTabContents(); |
| 2020 | // We make this check for the case of minimized windows, unit tests, etc. |
| 2021 | if (platform_util::IsVisible(current_tab->GetNativeView()) && |
| 2022 | Animation::ShouldRenderRichAnimation()) |
| 2023 | DownloadStartedAnimation::Show(current_tab); |
| 2024 | } |
[email protected] | d0cc9fb5 | 2009-06-23 02:26:16 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | void Browser::ConfirmAddSearchProvider(const TemplateURL* template_url, |
| 2028 | Profile* profile) { |
| 2029 | window()->ConfirmAddSearchProvider(template_url, profile); |
| 2030 | } |
| 2031 | |
[email protected] | bb67833 | 2009-07-21 00:15:50 | [diff] [blame] | 2032 | void Browser::ShowPageInfo(Profile* profile, |
[email protected] | 4d67720 | 2009-07-19 07:37:12 | [diff] [blame] | 2033 | const GURL& url, |
| 2034 | const NavigationEntry::SSLStatus& ssl, |
| 2035 | bool show_history) { |
[email protected] | bb67833 | 2009-07-21 00:15:50 | [diff] [blame] | 2036 | window()->ShowPageInfo(profile, url, ssl, show_history); |
[email protected] | 4d67720 | 2009-07-19 07:37:12 | [diff] [blame] | 2037 | } |
| 2038 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2039 | /////////////////////////////////////////////////////////////////////////////// |
| 2040 | // Browser, SelectFileDialog::Listener implementation: |
| 2041 | |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 2042 | void Browser::FileSelected(const FilePath& path, int index, void* params) { |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 2043 | GURL file_url = net::FilePathToFileURL(path); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2044 | if (!file_url.is_empty()) |
| 2045 | OpenURL(file_url, GURL(), CURRENT_TAB, PageTransition::TYPED); |
| 2046 | } |
| 2047 | |
[email protected] | a239c3f | 2009-02-17 22:13:19 | [diff] [blame] | 2048 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2049 | /////////////////////////////////////////////////////////////////////////////// |
| 2050 | // Browser, NotificationObserver implementation: |
| 2051 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2052 | void Browser::Observe(NotificationType type, |
| 2053 | const NotificationSource& source, |
| 2054 | const NotificationDetails& details) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 2055 | switch (type.value) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 2056 | case NotificationType::TAB_CONTENTS_DISCONNECTED: |
[email protected] | e83f168 | 2008-09-07 23:57:40 | [diff] [blame] | 2057 | if (is_attempting_to_close_browser_) { |
| 2058 | // Need to do this asynchronously as it will close the tab, which is |
| 2059 | // currently on the call stack above us. |
| 2060 | MessageLoop::current()->PostTask(FROM_HERE, |
| 2061 | method_factory_.NewRunnableMethod(&Browser::ClearUnloadState, |
| 2062 | Source<TabContents>(source).ptr())); |
| 2063 | } |
| 2064 | break; |
| 2065 | |
[email protected] | b4a19ea | 2009-03-17 10:08:24 | [diff] [blame] | 2066 | case NotificationType::SSL_VISIBLE_STATE_CHANGED: |
[email protected] | e83f168 | 2008-09-07 23:57:40 | [diff] [blame] | 2067 | // When the current tab's SSL state changes, we need to update the URL |
[email protected] | 90e8d06 | 2008-09-08 02:26:32 | [diff] [blame] | 2068 | // bar to reflect the new state. Note that it's possible for the selected |
| 2069 | // tab contents to be NULL. This is because we listen for all sources |
| 2070 | // (NavigationControllers) for convenience, so the notification could |
| 2071 | // actually be for a different window while we're doing asynchronous |
| 2072 | // closing of this one. |
| 2073 | if (GetSelectedTabContents() && |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2074 | &GetSelectedTabContents()->controller() == |
[email protected] | e83f168 | 2008-09-07 23:57:40 | [diff] [blame] | 2075 | Source<NavigationController>(source).ptr()) |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2076 | UpdateToolbar(false); |
[email protected] | e83f168 | 2008-09-07 23:57:40 | [diff] [blame] | 2077 | break; |
| 2078 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 2079 | case NotificationType::EXTENSION_UPDATE_DISABLED: { |
| 2080 | // Show the UI. |
| 2081 | ExtensionsService* service = Source<ExtensionsService>(source).ptr(); |
| 2082 | Extension* extension = Details<Extension>(details).ptr(); |
| 2083 | ShowExtensionDisabledUI(service, profile_, extension); |
| 2084 | break; |
| 2085 | } |
| 2086 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 2087 | case NotificationType::EXTENSION_UNLOADED: { |
| 2088 | // Close any tabs from the unloaded extension. |
| 2089 | Extension* extension = Details<Extension>(details).ptr(); |
| 2090 | for (int i = 0; i < tabstrip_model_.count(); i++) { |
| 2091 | TabContents* tc = tabstrip_model_.GetTabContentsAt(i); |
| 2092 | if (tc->GetURL().SchemeIs(chrome::kExtensionScheme) && |
| 2093 | tc->GetURL().host() == extension->id()) { |
| 2094 | CloseTabContents(tc); |
| 2095 | return; |
| 2096 | } |
| 2097 | } |
| 2098 | break; |
| 2099 | } |
| 2100 | |
[email protected] | 371ed7a | 2009-08-25 15:22:46 | [diff] [blame^] | 2101 | case NotificationType::EXTENSION_PROCESS_CRASHED: { |
| 2102 | TabContents* tab_contents = GetSelectedTabContents(); |
| 2103 | if (!tab_contents) |
| 2104 | break; |
| 2105 | ExtensionsService* extensions_service = |
| 2106 | Source<ExtensionsService>(source).ptr(); |
| 2107 | ExtensionHost* extension_host = Details<ExtensionHost>(details).ptr(); |
| 2108 | tab_contents->AddInfoBar(new CrashedExtensionInfoBarDelegate( |
| 2109 | tab_contents, extensions_service, extension_host->extension())); |
| 2110 | break; |
| 2111 | } |
| 2112 | |
[email protected] | e001d41 | 2009-06-26 20:53:25 | [diff] [blame] | 2113 | case NotificationType::BROWSER_THEME_CHANGED: |
| 2114 | window()->UserChangedTheme(); |
| 2115 | break; |
| 2116 | |
[email protected] | e83f168 | 2008-09-07 23:57:40 | [diff] [blame] | 2117 | default: |
| 2118 | NOTREACHED() << "Got a notification we didn't register for."; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | |
[email protected] | 88d7494 | 2009-01-21 22:04:44 | [diff] [blame] | 2122 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2123 | /////////////////////////////////////////////////////////////////////////////// |
| 2124 | // Browser, Command and state updating (private): |
| 2125 | |
| 2126 | void Browser::InitCommandState() { |
| 2127 | // All browser commands whose state isn't set automagically some other way |
| 2128 | // (like Back & Forward with initial page load) must have their state |
| 2129 | // initialized here, otherwise they will be forever disabled. |
| 2130 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 2131 | // Navigation commands |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2132 | command_updater_.UpdateCommandEnabled(IDC_RELOAD, true); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 2133 | |
| 2134 | // Window management commands |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2135 | command_updater_.UpdateCommandEnabled(IDC_NEW_WINDOW, true); |
| 2136 | command_updater_.UpdateCommandEnabled(IDC_NEW_INCOGNITO_WINDOW, true); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 2137 | // TODO(pkasting): Perhaps the code that populates this submenu should do |
| 2138 | // this? |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2139 | command_updater_.UpdateCommandEnabled(IDC_NEW_WINDOW_PROFILE_0, true); |
| 2140 | command_updater_.UpdateCommandEnabled(IDC_NEW_WINDOW_PROFILE_1, true); |
| 2141 | command_updater_.UpdateCommandEnabled(IDC_NEW_WINDOW_PROFILE_2, true); |
| 2142 | command_updater_.UpdateCommandEnabled(IDC_NEW_WINDOW_PROFILE_3, true); |
| 2143 | command_updater_.UpdateCommandEnabled(IDC_NEW_WINDOW_PROFILE_4, true); |
| 2144 | command_updater_.UpdateCommandEnabled(IDC_NEW_WINDOW_PROFILE_5, true); |
| 2145 | command_updater_.UpdateCommandEnabled(IDC_NEW_WINDOW_PROFILE_6, true); |
| 2146 | command_updater_.UpdateCommandEnabled(IDC_NEW_WINDOW_PROFILE_7, true); |
| 2147 | command_updater_.UpdateCommandEnabled(IDC_NEW_WINDOW_PROFILE_8, true); |
| 2148 | command_updater_.UpdateCommandEnabled(IDC_CLOSE_WINDOW, true); |
| 2149 | command_updater_.UpdateCommandEnabled(IDC_NEW_TAB, true); |
| 2150 | command_updater_.UpdateCommandEnabled(IDC_CLOSE_TAB, true); |
| 2151 | command_updater_.UpdateCommandEnabled(IDC_DUPLICATE_TAB, true); |
[email protected] | 9282cea | 2009-02-18 18:49:00 | [diff] [blame] | 2152 | command_updater_.UpdateCommandEnabled(IDC_FULLSCREEN, true); |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2153 | command_updater_.UpdateCommandEnabled(IDC_EXIT, true); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 2154 | |
| 2155 | // Page-related commands |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2156 | command_updater_.UpdateCommandEnabled(IDC_CLOSE_POPUPS, true); |
[email protected] | e662ade | 2009-06-08 18:20:14 | [diff] [blame] | 2157 | command_updater_.UpdateCommandEnabled(IDC_PRINT, true); |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2158 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_AUTO_DETECT, true); |
| 2159 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_UTF8, true); |
| 2160 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_UTF16LE, true); |
| 2161 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88591, true); |
| 2162 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1252, true); |
| 2163 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_GBK, true); |
| 2164 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_GB18030, true); |
| 2165 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_BIG5HKSCS, true); |
| 2166 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_BIG5, true); |
| 2167 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_THAI, true); |
| 2168 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_KOREAN, true); |
| 2169 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_SHIFTJIS, true); |
| 2170 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO2022JP, true); |
| 2171 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_EUCJP, true); |
| 2172 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885915, true); |
| 2173 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_MACINTOSH, true); |
| 2174 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88592, true); |
| 2175 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1250, true); |
| 2176 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88595, true); |
| 2177 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1251, true); |
| 2178 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_KOI8R, true); |
| 2179 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_KOI8U, true); |
| 2180 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88597, true); |
| 2181 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1253, true); |
| 2182 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88594, true); |
| 2183 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885913, true); |
| 2184 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1257, true); |
| 2185 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88593, true); |
| 2186 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885910, true); |
| 2187 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885914, true); |
| 2188 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885916, true); |
| 2189 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1254, true); |
| 2190 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88596, true); |
| 2191 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1256, true); |
| 2192 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88598, true); |
[email protected] | e13271f | 2009-03-07 00:26:00 | [diff] [blame] | 2193 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88598I, true); |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2194 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1255, true); |
| 2195 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1258, true); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 2196 | |
| 2197 | // Clipboard commands |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2198 | command_updater_.UpdateCommandEnabled(IDC_CUT, true); |
| 2199 | command_updater_.UpdateCommandEnabled(IDC_COPY, true); |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2200 | command_updater_.UpdateCommandEnabled(IDC_PASTE, true); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 2201 | |
[email protected] | 63b0293 | 2009-06-05 09:40:51 | [diff] [blame] | 2202 | // Find-in-page |
| 2203 | command_updater_.UpdateCommandEnabled(IDC_FIND, true); |
| 2204 | command_updater_.UpdateCommandEnabled(IDC_FIND_NEXT, true); |
| 2205 | command_updater_.UpdateCommandEnabled(IDC_FIND_PREVIOUS, true); |
| 2206 | |
| 2207 | // Zoom |
| 2208 | command_updater_.UpdateCommandEnabled(IDC_ZOOM_MENU, true); |
| 2209 | command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, true); |
| 2210 | command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL, true); |
| 2211 | command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, true); |
| 2212 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 2213 | // Show various bits of UI |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2214 | command_updater_.UpdateCommandEnabled(IDC_OPEN_FILE, true); |
| 2215 | command_updater_.UpdateCommandEnabled(IDC_CREATE_SHORTCUTS, false); |
[email protected] | c630f16 | 2009-07-22 09:41:33 | [diff] [blame] | 2216 | command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS, true); |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2217 | command_updater_.UpdateCommandEnabled(IDC_TASK_MANAGER, true); |
| 2218 | command_updater_.UpdateCommandEnabled(IDC_SELECT_PROFILE, true); |
| 2219 | command_updater_.UpdateCommandEnabled(IDC_SHOW_HISTORY, true); |
| 2220 | command_updater_.UpdateCommandEnabled(IDC_SHOW_BOOKMARK_MANAGER, true); |
| 2221 | command_updater_.UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true); |
| 2222 | command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE, true); |
[email protected] | 1677984 | 2009-07-08 23:45:29 | [diff] [blame] | 2223 | #if defined(OS_CHROMEOS) |
[email protected] | be71507 | 2009-07-07 15:43:20 | [diff] [blame] | 2224 | command_updater_.UpdateCommandEnabled(IDC_CONTROL_PANEL, true); |
| 2225 | #endif |
[email protected] | 64ff794 | 2008-12-17 18:11:23 | [diff] [blame] | 2226 | |
| 2227 | // Initialize other commands based on the window type. |
| 2228 | { |
| 2229 | bool normal_window = type() == TYPE_NORMAL; |
| 2230 | |
| 2231 | // Navigation commands |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2232 | command_updater_.UpdateCommandEnabled(IDC_HOME, normal_window); |
[email protected] | 64ff794 | 2008-12-17 18:11:23 | [diff] [blame] | 2233 | |
| 2234 | // Window management commands |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2235 | command_updater_.UpdateCommandEnabled(IDC_SELECT_NEXT_TAB, normal_window); |
| 2236 | command_updater_.UpdateCommandEnabled(IDC_SELECT_PREVIOUS_TAB, |
| 2237 | normal_window); |
| 2238 | command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_0, normal_window); |
| 2239 | command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_1, normal_window); |
| 2240 | command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_2, normal_window); |
| 2241 | command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_3, normal_window); |
| 2242 | command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_4, normal_window); |
| 2243 | command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_5, normal_window); |
| 2244 | command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_6, normal_window); |
| 2245 | command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_7, normal_window); |
| 2246 | command_updater_.UpdateCommandEnabled(IDC_SELECT_LAST_TAB, normal_window); |
| 2247 | command_updater_.UpdateCommandEnabled(IDC_RESTORE_TAB, |
[email protected] | 64ff794 | 2008-12-17 18:11:23 | [diff] [blame] | 2248 | normal_window && !profile_->IsOffTheRecord()); |
[email protected] | e662ade | 2009-06-08 18:20:14 | [diff] [blame] | 2249 | |
| 2250 | // Page-related commands |
[email protected] | 63b0293 | 2009-06-05 09:40:51 | [diff] [blame] | 2251 | command_updater_.UpdateCommandEnabled(IDC_STAR, normal_window); |
[email protected] | e662ade | 2009-06-08 18:20:14 | [diff] [blame] | 2252 | |
| 2253 | // Clipboard commands |
[email protected] | 63b0293 | 2009-06-05 09:40:51 | [diff] [blame] | 2254 | command_updater_.UpdateCommandEnabled(IDC_COPY_URL, normal_window); |
[email protected] | d5c99b17 | 2009-02-25 17:09:21 | [diff] [blame] | 2255 | |
| 2256 | // Show various bits of UI |
[email protected] | 7fd4cc3 | 2009-04-15 23:17:37 | [diff] [blame] | 2257 | command_updater_.UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, |
| 2258 | normal_window); |
[email protected] | 64ff794 | 2008-12-17 18:11:23 | [diff] [blame] | 2259 | } |
[email protected] | 9282cea | 2009-02-18 18:49:00 | [diff] [blame] | 2260 | |
| 2261 | // Initialize other commands whose state changes based on fullscreen mode. |
| 2262 | UpdateCommandsForFullscreenMode(false); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2263 | } |
| 2264 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 2265 | void Browser::UpdateCommandsForTabState() { |
[email protected] | c7c4233 | 2008-11-15 01:10:54 | [diff] [blame] | 2266 | TabContents* current_tab = GetSelectedTabContents(); |
[email protected] | 64ff794 | 2008-12-17 18:11:23 | [diff] [blame] | 2267 | if (!current_tab) // May be NULL during tab restore. |
[email protected] | d8375fd | 2008-11-25 22:45:39 | [diff] [blame] | 2268 | return; |
[email protected] | d8375fd | 2008-11-25 22:45:39 | [diff] [blame] | 2269 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 2270 | // Navigation commands |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2271 | NavigationController& nc = current_tab->controller(); |
| 2272 | command_updater_.UpdateCommandEnabled(IDC_BACK, nc.CanGoBack()); |
| 2273 | command_updater_.UpdateCommandEnabled(IDC_FORWARD, nc.CanGoForward()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2274 | |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 2275 | // Window management commands |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2276 | command_updater_.UpdateCommandEnabled(IDC_DUPLICATE_TAB, |
[email protected] | 63b0293 | 2009-06-05 09:40:51 | [diff] [blame] | 2277 | !(type() & TYPE_APP) && CanDuplicateContentsAt(selected_index())); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 2278 | |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 2279 | // Current navigation entry, may be NULL. |
| 2280 | NavigationEntry* active_entry = current_tab->controller().GetActiveEntry(); |
[email protected] | 2bfd94d | 2009-04-29 20:32:03 | [diff] [blame] | 2281 | bool is_source_viewable = |
[email protected] | 2bfd94d | 2009-04-29 20:32:03 | [diff] [blame] | 2282 | net::IsSupportedNonImageMimeType( |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 2283 | current_tab->contents_mime_type().c_str()); |
[email protected] | 8421498 | 2008-12-10 18:49:10 | [diff] [blame] | 2284 | |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 2285 | // Page-related commands |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 2286 | window_->SetStarredState(current_tab->is_starred()); |
| 2287 | // View-source should not be enabled if already in view-source mode or |
| 2288 | // the source is not viewable. |
| 2289 | command_updater_.UpdateCommandEnabled(IDC_VIEW_SOURCE, |
| 2290 | active_entry && !active_entry->IsViewSourceMode() && |
| 2291 | is_source_viewable); |
[email protected] | 4d07cdc | 2009-08-22 16:09:39 | [diff] [blame] | 2292 | |
| 2293 | // Instead of using GetURL here, we use url() (which is the "real" url of the |
| 2294 | // page) from the NavigationEntry because its reflects their origin rather |
| 2295 | // than the display one (returned by GetURL) which may be different (like |
| 2296 | // having "view-source:" on the front). |
| 2297 | GURL savable_url = (active_entry) ? active_entry->url() : GURL::EmptyGURL(); |
| 2298 | |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 2299 | command_updater_.UpdateCommandEnabled(IDC_SAVE_PAGE, |
[email protected] | 4d07cdc | 2009-08-22 16:09:39 | [diff] [blame] | 2300 | SavePackage::IsSavableURL(savable_url)); |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 2301 | command_updater_.UpdateCommandEnabled(IDC_ENCODING_MENU, |
| 2302 | SavePackage::IsSavableContents(current_tab->contents_mime_type()) && |
[email protected] | 4d07cdc | 2009-08-22 16:09:39 | [diff] [blame] | 2303 | SavePackage::IsSavableURL(savable_url)); |
[email protected] | 6de7445 | 2009-02-25 18:04:59 | [diff] [blame] | 2304 | |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 2305 | // Show various bits of UI |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 2306 | command_updater_.UpdateCommandEnabled(IDC_CREATE_SHORTCUTS, |
| 2307 | !current_tab->GetFavIcon().isNull()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2308 | } |
| 2309 | |
[email protected] | c398981 | 2009-04-15 18:08:49 | [diff] [blame] | 2310 | void Browser::UpdateStopGoState(bool is_loading, bool force) { |
| 2311 | window_->UpdateStopGoState(is_loading, force); |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2312 | command_updater_.UpdateCommandEnabled(IDC_GO, !is_loading); |
| 2313 | command_updater_.UpdateCommandEnabled(IDC_STOP, is_loading); |
[email protected] | 64ff794 | 2008-12-17 18:11:23 | [diff] [blame] | 2314 | } |
| 2315 | |
[email protected] | d938aed9 | 2009-01-22 19:49:33 | [diff] [blame] | 2316 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2317 | /////////////////////////////////////////////////////////////////////////////// |
| 2318 | // Browser, UI update coalescing and handling (private): |
| 2319 | |
| 2320 | void Browser::UpdateToolbar(bool should_restore_state) { |
| 2321 | window_->UpdateToolbar(GetSelectedTabContents(), should_restore_state); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2322 | } |
| 2323 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2324 | void Browser::ScheduleUIUpdate(const TabContents* source, |
| 2325 | unsigned changed_flags) { |
[email protected] | 2b4355c | 2009-04-04 17:55:46 | [diff] [blame] | 2326 | // Do some synchronous updates. |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2327 | if (changed_flags & TabContents::INVALIDATE_URL && |
| 2328 | source == GetSelectedTabContents()) { |
| 2329 | // Only update the URL for the current tab. Note that we do not update |
| 2330 | // the navigation commands since those would have already been updated |
| 2331 | // synchronously by NavigationStateChanged. |
| 2332 | UpdateToolbar(false); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2333 | } |
[email protected] | 2b4355c | 2009-04-04 17:55:46 | [diff] [blame] | 2334 | if (changed_flags & TabContents::INVALIDATE_LOAD && source) { |
| 2335 | // Update the loading state synchronously. This is so the throbber will |
| 2336 | // immediately start/stop, which gives a more snappy feel. We want to do |
| 2337 | // this for any tab so they start & stop quickly, but the source can be |
| 2338 | // NULL, so we have to check for that. |
| 2339 | tabstrip_model_.UpdateTabContentsStateAt( |
[email protected] | 902cdf77 | 2009-05-06 15:08:12 | [diff] [blame] | 2340 | tabstrip_model_.GetIndexOfController(&source->controller()), true); |
[email protected] | 2b4355c | 2009-04-04 17:55:46 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | // If the only updates were synchronously handled above, we're done. |
| 2344 | if (changed_flags == |
| 2345 | (TabContents::INVALIDATE_URL | TabContents::INVALIDATE_LOAD)) |
| 2346 | return; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2347 | |
| 2348 | // Save the dirty bits. |
| 2349 | scheduled_updates_.push_back(UIUpdate(source, changed_flags)); |
| 2350 | |
| 2351 | if (chrome_updater_factory_.empty()) { |
| 2352 | // No task currently scheduled, start another. |
| 2353 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 2354 | chrome_updater_factory_.NewRunnableMethod( |
| 2355 | &Browser::ProcessPendingUIUpdates), |
| 2356 | kUIUpdateCoalescingTimeMS); |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | void Browser::ProcessPendingUIUpdates() { |
| 2361 | #ifndef NDEBUG |
| 2362 | // Validate that all tabs we have pending updates for exist. This is scary |
| 2363 | // because the pending list must be kept in sync with any detached or |
| 2364 | // deleted tabs. This code does not dereference any TabContents pointers. |
| 2365 | for (size_t i = 0; i < scheduled_updates_.size(); i++) { |
| 2366 | bool found = false; |
| 2367 | for (int tab = 0; tab < tab_count(); tab++) { |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2368 | if (&GetTabContentsAt(tab)->controller() == |
| 2369 | &scheduled_updates_[i].source->controller()) { |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2370 | found = true; |
| 2371 | break; |
| 2372 | } |
| 2373 | } |
| 2374 | DCHECK(found); |
| 2375 | } |
| 2376 | #endif |
| 2377 | |
| 2378 | chrome_updater_factory_.RevokeAll(); |
| 2379 | |
[email protected] | 2d46c84 | 2008-11-14 19:24:31 | [diff] [blame] | 2380 | // We could have many updates for the same thing in the queue. This map |
| 2381 | // tracks the bits of the stuff we've already updated for each TabContents so |
| 2382 | // we don't update again. |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2383 | typedef std::map<const TabContents*, unsigned> UpdateTracker; |
| 2384 | UpdateTracker updated_stuff; |
| 2385 | |
| 2386 | for (size_t i = 0; i < scheduled_updates_.size(); i++) { |
| 2387 | // Do not dereference |contents|, it may be out-of-date! |
| 2388 | const TabContents* contents = scheduled_updates_[i].source; |
| 2389 | unsigned flags = scheduled_updates_[i].changed_flags; |
| 2390 | |
| 2391 | // Remove any bits we have already updated, and save the new bits. |
| 2392 | UpdateTracker::iterator updated = updated_stuff.find(contents); |
| 2393 | if (updated != updated_stuff.end()) { |
| 2394 | // Turn off bits already set. |
| 2395 | flags &= ~updated->second; |
| 2396 | if (!flags) |
| 2397 | continue; |
| 2398 | |
| 2399 | updated->second |= flags; |
| 2400 | } else { |
| 2401 | updated_stuff[contents] = flags; |
| 2402 | } |
| 2403 | |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 2404 | if (flags & TabContents::INVALIDATE_PAGE_ACTIONS) |
| 2405 | window()->GetLocationBar()->UpdatePageActions(); |
| 2406 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2407 | // Updating the URL happens synchronously in ScheduleUIUpdate. |
[email protected] | ba8c4821 | 2009-05-12 21:20:40 | [diff] [blame] | 2408 | TabContents* selected_tab = GetSelectedTabContents(); |
[email protected] | 4d34e2e | 2009-05-26 22:55:28 | [diff] [blame] | 2409 | if (selected_tab && |
| 2410 | flags & TabContents::INVALIDATE_LOAD && GetStatusBubble()) { |
[email protected] | ba8c4821 | 2009-05-12 21:20:40 | [diff] [blame] | 2411 | GetStatusBubble()->SetStatus(selected_tab->GetStatusText()); |
[email protected] | 4d34e2e | 2009-05-26 22:55:28 | [diff] [blame] | 2412 | } |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2413 | |
[email protected] | c9cd222 | 2009-05-06 05:16:50 | [diff] [blame] | 2414 | if (flags & TabContents::INVALIDATE_TAB) { |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2415 | tabstrip_model_.UpdateTabContentsStateAt( |
[email protected] | 902cdf77 | 2009-05-06 15:08:12 | [diff] [blame] | 2416 | tabstrip_model_.GetIndexOfController(&contents->controller()), false); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2417 | window_->UpdateTitleBar(); |
| 2418 | |
[email protected] | ba8c4821 | 2009-05-12 21:20:40 | [diff] [blame] | 2419 | if (selected_tab && contents == selected_tab) { |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 2420 | command_updater_.UpdateCommandEnabled(IDC_CREATE_SHORTCUTS, |
[email protected] | ba8c4821 | 2009-05-12 21:20:40 | [diff] [blame] | 2421 | !selected_tab->GetFavIcon().isNull()); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | // We don't need to process INVALIDATE_STATE, since that's not visible. |
| 2426 | } |
| 2427 | |
| 2428 | scheduled_updates_.clear(); |
| 2429 | } |
| 2430 | |
| 2431 | void Browser::RemoveScheduledUpdatesFor(TabContents* contents) { |
| 2432 | if (!contents) |
| 2433 | return; |
| 2434 | |
| 2435 | // Remove any pending UI updates for the detached tab. |
| 2436 | UpdateVector::iterator cur_update = scheduled_updates_.begin(); |
| 2437 | while (cur_update != scheduled_updates_.end()) { |
| 2438 | if (cur_update->source == contents) { |
| 2439 | cur_update = scheduled_updates_.erase(cur_update); |
| 2440 | } else { |
| 2441 | ++cur_update; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2442 | } |
| 2443 | } |
| 2444 | } |
| 2445 | |
[email protected] | d938aed9 | 2009-01-22 19:49:33 | [diff] [blame] | 2446 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2447 | /////////////////////////////////////////////////////////////////////////////// |
| 2448 | // Browser, Getters for UI (private): |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2449 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2450 | StatusBubble* Browser::GetStatusBubble() { |
[email protected] | 9b032bf | 2009-07-21 17:34:23 | [diff] [blame] | 2451 | return window_ ? window_->GetStatusBubble() : NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2452 | } |
| 2453 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2454 | /////////////////////////////////////////////////////////////////////////////// |
| 2455 | // Browser, Session restore functions (private): |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2456 | |
| 2457 | void Browser::SyncHistoryWithTabs(int index) { |
| 2458 | if (!profile()->HasSessionService()) |
| 2459 | return; |
| 2460 | SessionService* session_service = profile()->GetSessionService(); |
| 2461 | if (session_service) { |
| 2462 | for (int i = index; i < tab_count(); ++i) { |
| 2463 | TabContents* contents = GetTabContentsAt(i); |
| 2464 | if (contents) { |
| 2465 | session_service->SetTabIndexInWindow( |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2466 | session_id(), contents->controller().session_id(), i); |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 2467 | session_service->SetPinnedState(session_id(), |
| 2468 | contents->controller().session_id(), |
| 2469 | tabstrip_model_.IsTabPinned(i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2470 | } |
| 2471 | } |
| 2472 | } |
| 2473 | } |
| 2474 | |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2475 | TabContents* Browser::BuildRestoredTab( |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2476 | const std::vector<TabNavigation>& navigations, |
| 2477 | int selected_navigation) { |
| 2478 | if (!navigations.empty()) { |
| 2479 | DCHECK(selected_navigation >= 0 && |
| 2480 | selected_navigation < static_cast<int>(navigations.size())); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2481 | // Create a NavigationController. This constructor creates the appropriate |
| 2482 | // set of TabContents. |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 2483 | TabContents* new_tab = new TabContents(profile_, NULL, |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2484 | MSG_ROUTING_NONE, NULL); |
| 2485 | new_tab->controller().RestoreFromState(navigations, selected_navigation); |
| 2486 | return new_tab; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2487 | } else { |
| 2488 | // No navigations. Create a tab with about:blank. |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 2489 | return CreateTabContentsForURL(GURL("about:blank"), GURL(), profile_, |
| 2490 | PageTransition::START_PAGE, false, NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2491 | } |
| 2492 | } |
| 2493 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2494 | /////////////////////////////////////////////////////////////////////////////// |
| 2495 | // Browser, OnBeforeUnload handling (private): |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2496 | |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2497 | void Browser::ProcessPendingTabs() { |
| 2498 | DCHECK(is_attempting_to_close_browser_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2499 | |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2500 | if (HasCompletedUnloadProcessing()) { |
| 2501 | // We've finished all the unload events and can proceed to close the |
| 2502 | // browser. |
| 2503 | OnWindowClosing(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2504 | return; |
| 2505 | } |
| 2506 | |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2507 | // Process beforeunload tabs first. When that queue is empty, process |
| 2508 | // unload tabs. |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2509 | if (!tabs_needing_before_unload_fired_.empty()) { |
[email protected] | 06b42f03 | 2008-12-03 18:43:05 | [diff] [blame] | 2510 | TabContents* tab = *(tabs_needing_before_unload_fired_.begin()); |
[email protected] | 2248462 | 2009-06-10 21:17:24 | [diff] [blame] | 2511 | // Null check render_view_host here as this gets called on a PostTask and |
| 2512 | // the tab's render_view_host may have been nulled out. |
| 2513 | if (tab->render_view_host()) { |
[email protected] | ae23c27 | 2009-07-27 20:15:40 | [diff] [blame] | 2514 | tab->render_view_host()->FirePageBeforeUnload(false); |
[email protected] | 2248462 | 2009-06-10 21:17:24 | [diff] [blame] | 2515 | } else { |
| 2516 | ClearUnloadState(tab); |
| 2517 | } |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2518 | } else if (!tabs_needing_unload_fired_.empty()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2519 | // We've finished firing all beforeunload events and can proceed with unload |
| 2520 | // events. |
| 2521 | // TODO(ojan): We should add a call to browser_shutdown::OnShutdownStarting |
| 2522 | // somewhere around here so that we have accurate measurements of shutdown |
| 2523 | // time. |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2524 | // TODO(ojan): We can probably fire all the unload events in parallel and |
| 2525 | // get a perf benefit from that in the cases where the tab hangs in it's |
| 2526 | // unload handler or takes a long time to page in. |
[email protected] | 06b42f03 | 2008-12-03 18:43:05 | [diff] [blame] | 2527 | TabContents* tab = *(tabs_needing_unload_fired_.begin()); |
[email protected] | 2248462 | 2009-06-10 21:17:24 | [diff] [blame] | 2528 | // Null check render_view_host here as this gets called on a PostTask and |
| 2529 | // the tab's render_view_host may have been nulled out. |
| 2530 | if (tab->render_view_host()) { |
[email protected] | eb6b87a | 2009-07-24 15:57:39 | [diff] [blame] | 2531 | tab->render_view_host()->ClosePage(false, -1, -1); |
[email protected] | 2248462 | 2009-06-10 21:17:24 | [diff] [blame] | 2532 | } else { |
| 2533 | ClearUnloadState(tab); |
| 2534 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2535 | } else { |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2536 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2537 | } |
| 2538 | } |
| 2539 | |
[email protected] | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 2540 | bool Browser::HasCompletedUnloadProcessing() const { |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2541 | return is_attempting_to_close_browser_ && |
| 2542 | tabs_needing_before_unload_fired_.empty() && |
| 2543 | tabs_needing_unload_fired_.empty(); |
| 2544 | } |
| 2545 | |
| 2546 | void Browser::CancelWindowClose() { |
| 2547 | DCHECK(is_attempting_to_close_browser_); |
| 2548 | // Only cancelling beforeunload should be able to cancel the window's close. |
| 2549 | // So there had better be a tab that we think needs beforeunload fired. |
| 2550 | DCHECK(!tabs_needing_before_unload_fired_.empty()); |
| 2551 | |
[email protected] | 8f673f3a | 2008-08-05 22:34:28 | [diff] [blame] | 2552 | tabs_needing_before_unload_fired_.clear(); |
| 2553 | tabs_needing_unload_fired_.clear(); |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2554 | is_attempting_to_close_browser_ = false; |
| 2555 | } |
| 2556 | |
[email protected] | 06b42f03 | 2008-12-03 18:43:05 | [diff] [blame] | 2557 | bool Browser::RemoveFromSet(UnloadListenerSet* set, TabContents* tab) { |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2558 | DCHECK(is_attempting_to_close_browser_); |
| 2559 | |
[email protected] | 06b42f03 | 2008-12-03 18:43:05 | [diff] [blame] | 2560 | UnloadListenerSet::iterator iter = std::find(set->begin(), set->end(), tab); |
| 2561 | if (iter != set->end()) { |
| 2562 | set->erase(iter); |
| 2563 | return true; |
[email protected] | 04b4a6c | 2008-08-02 00:44:47 | [diff] [blame] | 2564 | } |
| 2565 | return false; |
| 2566 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2567 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2568 | void Browser::ClearUnloadState(TabContents* tab) { |
| 2569 | DCHECK(is_attempting_to_close_browser_); |
[email protected] | 06b42f03 | 2008-12-03 18:43:05 | [diff] [blame] | 2570 | RemoveFromSet(&tabs_needing_before_unload_fired_, tab); |
| 2571 | RemoveFromSet(&tabs_needing_unload_fired_, tab); |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2572 | ProcessPendingTabs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2573 | } |
| 2574 | |
[email protected] | f745489 | 2009-01-29 19:24:21 | [diff] [blame] | 2575 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2576 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | 69444cc | 2009-04-09 20:40:06 | [diff] [blame] | 2577 | // Browser, In-progress download termination handling (private): |
| 2578 | |
| 2579 | bool Browser::CanCloseWithInProgressDownloads() { |
| 2580 | if (cancel_download_confirmation_state_ != NOT_PROMPTED) { |
| 2581 | // This should probably not happen. |
| 2582 | DCHECK(cancel_download_confirmation_state_ != WAITING_FOR_RESPONSE); |
| 2583 | return true; |
| 2584 | } |
| 2585 | |
| 2586 | // If there are no download in-progress, our job is done. |
| 2587 | DownloadManager* download_manager = profile_->GetDownloadManager(); |
| 2588 | if (!download_manager || download_manager->in_progress_count() == 0) |
| 2589 | return true; |
| 2590 | |
| 2591 | // Let's figure out if we are the last window for our profile. |
| 2592 | // Note that we cannot just use BrowserList::GetBrowserCount as browser |
| 2593 | // windows closing is delayed and the returned count might include windows |
| 2594 | // that are being closed. |
| 2595 | int count = 0; |
| 2596 | for (BrowserList::const_iterator iter = BrowserList::begin(); |
| 2597 | iter != BrowserList::end(); ++iter) { |
| 2598 | // Don't count this browser window or any other in the process of closing. |
| 2599 | if (*iter == this || (*iter)->is_attempting_to_close_browser_) |
| 2600 | continue; |
| 2601 | |
| 2602 | // We test the original profile, because an incognito browser window keeps |
| 2603 | // the original profile alive (and its DownloadManager). |
| 2604 | // We also need to test explicitly the profile directly so that 2 incognito |
| 2605 | // profiles count as a match. |
| 2606 | if ((*iter)->profile() == profile() || |
| 2607 | (*iter)->profile()->GetOriginalProfile() == profile()) |
| 2608 | count++; |
| 2609 | } |
| 2610 | if (count > 0) |
| 2611 | return true; |
| 2612 | |
| 2613 | cancel_download_confirmation_state_ = WAITING_FOR_RESPONSE; |
| 2614 | window_->ConfirmBrowserCloseWithPendingDownloads(); |
| 2615 | |
| 2616 | // Return false so the browser does not close. We'll close if the user |
| 2617 | // confirms in the dialog. |
| 2618 | return false; |
| 2619 | } |
| 2620 | |
| 2621 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2622 | // Browser, Assorted utility functions (private): |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2623 | |
[email protected] | 1a6b30a | 2009-08-19 03:52:11 | [diff] [blame] | 2624 | // static |
| 2625 | Browser* Browser::GetOrCreateTabbedBrowser(Profile* profile) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2626 | Browser* browser = BrowserList::FindBrowserWithType( |
[email protected] | 1a6b30a | 2009-08-19 03:52:11 | [diff] [blame] | 2627 | profile, TYPE_NORMAL); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 2628 | if (!browser) |
[email protected] | 1a6b30a | 2009-08-19 03:52:11 | [diff] [blame] | 2629 | browser = Browser::Create(profile); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2630 | return browser; |
| 2631 | } |
| 2632 | |
[email protected] | 1f7d7e9 | 2009-06-02 20:55:04 | [diff] [blame] | 2633 | void Browser::OpenURLAtIndex(TabContents* source, |
| 2634 | const GURL& url, |
| 2635 | const GURL& referrer, |
| 2636 | WindowOpenDisposition disposition, |
| 2637 | PageTransition::Type transition, |
| 2638 | int index, |
| 2639 | bool force_index) { |
| 2640 | // TODO(beng): Move all this code into a separate helper that has unit tests. |
| 2641 | |
| 2642 | // No code for these yet |
| 2643 | DCHECK((disposition != NEW_POPUP) && (disposition != SAVE_TO_DISK)); |
| 2644 | |
| 2645 | TabContents* current_tab = source ? source : GetSelectedTabContents(); |
| 2646 | bool source_tab_was_frontmost = (current_tab == GetSelectedTabContents()); |
| 2647 | TabContents* new_contents = NULL; |
| 2648 | |
| 2649 | // If the URL is part of the same web site, then load it in the same |
| 2650 | // SiteInstance (and thus the same process). This is an optimization to |
| 2651 | // reduce process overhead; it is not necessary for compatibility. (That is, |
| 2652 | // the new tab will not have script connections to the previous tab, so it |
| 2653 | // does not need to be part of the same SiteInstance or BrowsingInstance.) |
| 2654 | // Default to loading in a new SiteInstance and BrowsingInstance. |
| 2655 | // TODO(creis): should this apply to applications? |
| 2656 | SiteInstance* instance = NULL; |
| 2657 | // Don't use this logic when "--process-per-tab" is specified. |
| 2658 | if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerTab)) { |
| 2659 | if (current_tab) { |
| 2660 | const GURL& current_url = current_tab->GetURL(); |
| 2661 | if (SiteInstance::IsSameWebSite(current_url, url)) |
| 2662 | instance = current_tab->GetSiteInstance(); |
| 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | // If this is not a normal window (such as a popup or an application), we can |
| 2667 | // only have one tab so a new tab always goes into a tabbed browser window. |
| 2668 | if (disposition != NEW_WINDOW && type_ != TYPE_NORMAL) { |
| 2669 | // If the disposition is OFF_THE_RECORD we don't want to create a new |
| 2670 | // browser that will itself create another OTR browser. This will result in |
| 2671 | // a browser leak (and crash below because no tab is created or selected). |
| 2672 | if (disposition == OFF_THE_RECORD) { |
| 2673 | OpenURLOffTheRecord(profile_, url); |
| 2674 | return; |
| 2675 | } |
| 2676 | |
[email protected] | 1a6b30a | 2009-08-19 03:52:11 | [diff] [blame] | 2677 | Browser* b = GetOrCreateTabbedBrowser(profile_); |
[email protected] | 1f7d7e9 | 2009-06-02 20:55:04 | [diff] [blame] | 2678 | DCHECK(b); |
| 2679 | |
| 2680 | // If we have just created a new browser window, make sure we select the |
| 2681 | // tab. |
| 2682 | if (b->tab_count() == 0 && disposition == NEW_BACKGROUND_TAB) |
| 2683 | disposition = NEW_FOREGROUND_TAB; |
| 2684 | |
| 2685 | b->OpenURL(url, referrer, disposition, transition); |
| 2686 | b->window()->Show(); |
| 2687 | return; |
| 2688 | } |
| 2689 | |
| 2690 | if (profile_->IsOffTheRecord() && disposition == OFF_THE_RECORD) |
| 2691 | disposition = NEW_FOREGROUND_TAB; |
| 2692 | |
| 2693 | if (disposition == SINGLETON_TAB) { |
| 2694 | ShowSingleDOMUITab(url); |
| 2695 | return; |
| 2696 | } else if (disposition == NEW_WINDOW) { |
| 2697 | Browser* browser = Browser::Create(profile_); |
| 2698 | new_contents = browser->AddTabWithURL(url, referrer, transition, true, |
| 2699 | index, force_index, instance); |
| 2700 | browser->window()->Show(); |
| 2701 | } else if ((disposition == CURRENT_TAB) && current_tab) { |
| 2702 | tabstrip_model_.TabNavigating(current_tab, transition); |
| 2703 | |
| 2704 | current_tab->controller().LoadURL(url, referrer, transition); |
| 2705 | new_contents = current_tab; |
| 2706 | if (GetStatusBubble()) |
| 2707 | GetStatusBubble()->Hide(); |
| 2708 | |
[email protected] | b4a8b24 | 2009-08-16 15:20:44 | [diff] [blame] | 2709 | // Update the location bar. This is synchronous. We specifically don't |
| 2710 | // update the load state since the load hasn't started yet and updating it |
| 2711 | // will put it out of sync with the actual state like whether we're |
| 2712 | // displaying a favicon, which controls the throbber. If we updated it here, |
| 2713 | // the throbber will show the default favicon for a split second when |
| 2714 | // navigating away from the new tab page. |
[email protected] | 1f7d7e9 | 2009-06-02 20:55:04 | [diff] [blame] | 2715 | ScheduleUIUpdate(current_tab, TabContents::INVALIDATE_URL); |
| 2716 | } else if (disposition == OFF_THE_RECORD) { |
| 2717 | OpenURLOffTheRecord(profile_, url); |
| 2718 | return; |
| 2719 | } else if (disposition != SUPPRESS_OPEN) { |
| 2720 | new_contents = AddTabWithURL(url, referrer, transition, |
| 2721 | disposition != NEW_BACKGROUND_TAB, index, force_index, instance); |
| 2722 | } |
| 2723 | |
[email protected] | b4a8b24 | 2009-08-16 15:20:44 | [diff] [blame] | 2724 | if (disposition != NEW_BACKGROUND_TAB && source_tab_was_frontmost && |
| 2725 | new_contents) { |
[email protected] | 1f7d7e9 | 2009-06-02 20:55:04 | [diff] [blame] | 2726 | // Give the focus to the newly navigated tab, if the source tab was |
| 2727 | // front-most. |
| 2728 | new_contents->Focus(); |
| 2729 | } |
| 2730 | } |
| 2731 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2732 | void Browser::BuildPopupWindow(TabContents* source, |
| 2733 | TabContents* new_contents, |
| 2734 | const gfx::Rect& initial_pos) { |
[email protected] | 9f5b80a | 2009-04-08 01:26:07 | [diff] [blame] | 2735 | BuildPopupWindowHelper(source, new_contents, initial_pos, |
| 2736 | (type_ & TYPE_APP) ? TYPE_APP_POPUP : TYPE_POPUP, |
| 2737 | profile_, false); |
| 2738 | } |
| 2739 | |
| 2740 | void Browser::BuildPopupWindowHelper(TabContents* source, |
| 2741 | TabContents* new_contents, |
| 2742 | const gfx::Rect& initial_pos, |
| 2743 | Browser::Type browser_type, |
| 2744 | Profile* profile, |
| 2745 | bool start_restored) { |
| 2746 | Browser* browser = new Browser(browser_type, profile); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 2747 | browser->set_override_bounds(initial_pos); |
[email protected] | 9f5b80a | 2009-04-08 01:26:07 | [diff] [blame] | 2748 | |
| 2749 | if (start_restored) |
| 2750 | browser->set_maximized_state(MAXIMIZED_STATE_UNMAXIMIZED); |
| 2751 | |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 2752 | browser->CreateBrowserWindow(); |
[email protected] | 6b2ee92 | 2009-04-30 20:40:53 | [diff] [blame] | 2753 | browser->tabstrip_model()->AppendTabContents(new_contents, true); |
[email protected] | 2f69fb5 | 2008-12-12 18:50:39 | [diff] [blame] | 2754 | browser->window()->Show(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2755 | } |
| 2756 | |
[email protected] | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 2757 | GURL Browser::GetHomePage() const { |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 2758 | if (profile_->GetPrefs()->GetBoolean(prefs::kHomePageIsNewTabPage)) |
[email protected] | b689fce7 | 2009-03-17 22:45:34 | [diff] [blame] | 2759 | return GURL(chrome::kChromeUINewTabURL); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 2760 | GURL home_page = GURL(URLFixerUpper::FixupURL( |
[email protected] | a239c3f | 2009-02-17 22:13:19 | [diff] [blame] | 2761 | WideToUTF8(profile_->GetPrefs()->GetString(prefs::kHomePage)), |
| 2762 | std::string())); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 2763 | if (!home_page.is_valid()) |
[email protected] | b689fce7 | 2009-03-17 22:45:34 | [diff] [blame] | 2764 | return GURL(chrome::kChromeUINewTabURL); |
[email protected] | 15952e46 | 2008-11-14 00:29:05 | [diff] [blame] | 2765 | return home_page; |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2766 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2767 | |
[email protected] | 4f3dc37 | 2009-02-24 00:10:29 | [diff] [blame] | 2768 | void Browser::FindInPage(bool find_next, bool forward_direction) { |
[email protected] | 4801ecc | 2009-04-05 04:52:58 | [diff] [blame] | 2769 | ShowFindBar(); |
[email protected] | 4f3dc37 | 2009-02-24 00:10:29 | [diff] [blame] | 2770 | if (find_next) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 2771 | GetSelectedTabContents()->StartFinding(string16(), |
[email protected] | e491f1c | 2009-05-22 20:28:12 | [diff] [blame] | 2772 | forward_direction, |
| 2773 | false); // Not case sensitive. |
[email protected] | 4f3dc37 | 2009-02-24 00:10:29 | [diff] [blame] | 2774 | } |
[email protected] | fbd7759 | 2008-11-12 20:50:27 | [diff] [blame] | 2775 | } |
[email protected] | cb17f7f | 2009-02-06 18:14:48 | [diff] [blame] | 2776 | |
[email protected] | 36b6dcb | 2008-11-12 01:19:57 | [diff] [blame] | 2777 | void Browser::CloseFrame() { |
| 2778 | window_->Close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | // static |
| 2782 | std::wstring Browser::ComputeApplicationNameFromURL(const GURL& url) { |
| 2783 | std::string t; |
| 2784 | t.append(url.host()); |
| 2785 | t.append("_"); |
| 2786 | t.append(url.path()); |
| 2787 | return UTF8ToWide(t); |
| 2788 | } |
| 2789 | |
| 2790 | // static |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 2791 | void Browser::RegisterAppPrefs(const std::wstring& app_name) { |
| 2792 | // A set of apps that we've already started. |
| 2793 | static std::set<std::wstring>* g_app_names = NULL; |
| 2794 | |
| 2795 | if (!g_app_names) |
| 2796 | g_app_names = new std::set<std::wstring>; |
| 2797 | |
| 2798 | // Only register once for each app name. |
| 2799 | if (g_app_names->find(app_name) != g_app_names->end()) |
| 2800 | return; |
| 2801 | g_app_names->insert(app_name); |
| 2802 | |
| 2803 | // We need to register the window position pref. |
| 2804 | std::wstring window_pref(prefs::kBrowserWindowPlacement); |
| 2805 | window_pref.append(L"_"); |
| 2806 | window_pref.append(app_name); |
| 2807 | PrefService* prefs = g_browser_process->local_state(); |
| 2808 | DCHECK(prefs); |
| 2809 | |
| 2810 | prefs->RegisterDictionaryPref(window_pref.c_str()); |
| 2811 | } |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame] | 2812 | |
| 2813 | /////////////////////////////////////////////////////////////////////////////// |
| 2814 | // BrowserToolbarModel (private): |
| 2815 | |
| 2816 | NavigationController* Browser::BrowserToolbarModel::GetNavigationController() { |
| 2817 | // This |current_tab| can be NULL during the initialization of the |
| 2818 | // toolbar during window creation (i.e. before any tabs have been added |
| 2819 | // to the window). |
| 2820 | TabContents* current_tab = browser_->GetSelectedTabContents(); |
| 2821 | return current_tab ? ¤t_tab->controller() : NULL; |
| 2822 | } |