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