| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/browser_instant_controller.h" | 5 #include "chrome/browser/ui/browser_instant_controller.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_web_ui.h" | 9 #include "chrome/browser/extensions/extension_web_ui.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/search/instant_service.h" | 11 #include "chrome/browser/search/instant_service.h" |
| 12 #include "chrome/browser/search/instant_service_factory.h" | 12 #include "chrome/browser/search/instant_service_factory.h" |
| 13 #include "chrome/browser/search/search.h" | 13 #include "chrome/browser/search/search.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/browser_window.h" | 15 #include "chrome/browser/ui/browser_window.h" |
| 16 #include "chrome/browser/ui/omnibox/location_bar.h" | 16 #include "chrome/browser/ui/omnibox/location_bar.h" |
| 17 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" | 17 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
| 18 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 18 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 19 #include "chrome/browser/ui/search/instant_ntp.h" | |
| 20 #include "chrome/browser/ui/search/instant_search_prerenderer.h" | 19 #include "chrome/browser/ui/search/instant_search_prerenderer.h" |
| 21 #include "chrome/browser/ui/search/search_model.h" | 20 #include "chrome/browser/ui/search/search_model.h" |
| 22 #include "chrome/browser/ui/search/search_tab_helper.h" | 21 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 24 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" | 23 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
| 25 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
| 26 #include "content/public/browser/render_process_host.h" | 25 #include "content/public/browser/render_process_host.h" |
| 27 #include "content/public/browser/user_metrics.h" | 26 #include "content/public/browser/user_metrics.h" |
| 28 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 29 #include "content/public/browser/web_contents_view.h" | 28 #include "content/public/browser/web_contents_view.h" |
| 30 | 29 |
| 31 using content::UserMetricsAction; | 30 using content::UserMetricsAction; |
| 32 | 31 |
| 33 namespace { | 32 namespace { |
| 34 | 33 |
| 35 InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) { | 34 InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) { |
| 36 DCHECK(profile); | 35 DCHECK(profile); |
| 37 InstantService* instant_service = | 36 InstantService* instant_service = |
| 38 InstantServiceFactory::GetForProfile(profile); | 37 InstantServiceFactory::GetForProfile(profile); |
| 39 return instant_service ? instant_service->instant_search_prerenderer() : NULL; | 38 return instant_service ? instant_service->instant_search_prerenderer() : NULL; |
| 40 } | 39 } |
| 41 | 40 |
| 42 } // namespace | 41 } // namespace |
| 43 | 42 |
| 44 //////////////////////////////////////////////////////////////////////////////// | 43 //////////////////////////////////////////////////////////////////////////////// |
| 45 // BrowserInstantController, public: | 44 // BrowserInstantController, public: |
| 46 | 45 |
| 47 BrowserInstantController::BrowserInstantController(Browser* browser) | 46 BrowserInstantController::BrowserInstantController(Browser* browser) |
| 48 : browser_(browser), | 47 : browser_(browser), |
| 49 instant_(this), | 48 instant_(this) { |
| 50 instant_unload_handler_(browser) { | |
| 51 browser_->search_model()->AddObserver(this); | 49 browser_->search_model()->AddObserver(this); |
| 52 | 50 |
| 53 InstantService* instant_service = | 51 InstantService* instant_service = |
| 54 InstantServiceFactory::GetForProfile(profile()); | 52 InstantServiceFactory::GetForProfile(profile()); |
| 55 instant_service->OnBrowserInstantControllerCreated(); | |
| 56 instant_service->AddObserver(this); | 53 instant_service->AddObserver(this); |
| 57 } | 54 } |
| 58 | 55 |
| 59 BrowserInstantController::~BrowserInstantController() { | 56 BrowserInstantController::~BrowserInstantController() { |
| 60 browser_->search_model()->RemoveObserver(this); | 57 browser_->search_model()->RemoveObserver(this); |
| 61 | 58 |
| 62 InstantService* instant_service = | 59 InstantService* instant_service = |
| 63 InstantServiceFactory::GetForProfile(profile()); | 60 InstantServiceFactory::GetForProfile(profile()); |
| 64 instant_service->RemoveObserver(this); | 61 instant_service->RemoveObserver(this); |
| 65 instant_service->OnBrowserInstantControllerDestroyed(); | |
| 66 } | |
| 67 | |
| 68 bool BrowserInstantController::MaybeSwapInInstantNTPContents( | |
| 69 const GURL& url, | |
| 70 content::WebContents* source_contents, | |
| 71 content::WebContents** target_contents) { | |
| 72 if (url != GURL(chrome::kChromeUINewTabURL)) | |
| 73 return false; | |
| 74 | |
| 75 GURL extension_url(url); | |
| 76 if (ExtensionWebUI::HandleChromeURLOverride(&extension_url, profile())) { | |
| 77 // If there is an extension overriding the NTP do not use the Instant NTP. | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 InstantService* instant_service = | |
| 82 InstantServiceFactory::GetForProfile(profile()); | |
| 83 scoped_ptr<content::WebContents> instant_ntp = | |
| 84 instant_service->ReleaseNTPContents(); | |
| 85 if (!instant_ntp) | |
| 86 return false; | |
| 87 | |
| 88 *target_contents = instant_ntp.get(); | |
| 89 if (source_contents) { | |
| 90 // If the Instant NTP hasn't yet committed an entry, we can't call | |
| 91 // CopyStateFromAndPrune. Instead, load the Local NTP URL directly in the | |
| 92 // source contents. | |
| 93 // TODO(sreeram): Always using the local URL is wrong in the case of the | |
| 94 // first tab in a window where we might want to use the remote URL. Fix. | |
| 95 if (!instant_ntp->GetController().CanPruneAllButLastCommitted()) { | |
| 96 source_contents->GetController().LoadURL(chrome::GetLocalInstantURL( | |
| 97 profile()), content::Referrer(), content::PAGE_TRANSITION_GENERATED, | |
| 98 std::string()); | |
| 99 *target_contents = source_contents; | |
| 100 } else { | |
| 101 instant_ntp->GetController().CopyStateFromAndPrune( | |
| 102 &source_contents->GetController(), false); | |
| 103 ReplaceWebContentsAt( | |
| 104 browser_->tab_strip_model()->GetIndexOfWebContents(source_contents), | |
| 105 instant_ntp.Pass()); | |
| 106 } | |
| 107 } else { | |
| 108 // If the Instant NTP hasn't yet committed an entry, we can't call | |
| 109 // PruneAllButLastCommitted. In that case, there shouldn't be any entries | |
| 110 // to prune anyway. | |
| 111 if (instant_ntp->GetController().CanPruneAllButLastCommitted()) | |
| 112 instant_ntp->GetController().PruneAllButLastCommitted(); | |
| 113 else | |
| 114 CHECK(!instant_ntp->GetController().GetLastCommittedEntry()); | |
| 115 | |
| 116 // If |source_contents| is NULL, then the caller is responsible for | |
| 117 // inserting instant_ntp into the tabstrip and will take ownership. | |
| 118 ignore_result(instant_ntp.release()); | |
| 119 } | |
| 120 return true; | |
| 121 } | 62 } |
| 122 | 63 |
| 123 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition, | 64 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition, |
| 124 const GURL& url) { | 65 const GURL& url) { |
| 125 // Unsupported dispositions. | 66 // Unsupported dispositions. |
| 126 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW || | 67 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW || |
| 127 disposition == NEW_FOREGROUND_TAB) | 68 disposition == NEW_FOREGROUND_TAB) |
| 128 return false; | 69 return false; |
| 129 | 70 |
| 130 // The omnibox currently doesn't use other dispositions, so we don't attempt | 71 // The omnibox currently doesn't use other dispositions, so we don't attempt |
| (...skipping 18 matching lines...) Expand all Loading... |
| 149 return false; | 90 return false; |
| 150 } | 91 } |
| 151 | 92 |
| 152 return instant_.SubmitQuery(search_terms); | 93 return instant_.SubmitQuery(search_terms); |
| 153 } | 94 } |
| 154 | 95 |
| 155 Profile* BrowserInstantController::profile() const { | 96 Profile* BrowserInstantController::profile() const { |
| 156 return browser_->profile(); | 97 return browser_->profile(); |
| 157 } | 98 } |
| 158 | 99 |
| 159 void BrowserInstantController::ReplaceWebContentsAt( | |
| 160 int index, | |
| 161 scoped_ptr<content::WebContents> new_contents) { | |
| 162 DCHECK_NE(TabStripModel::kNoTab, index); | |
| 163 scoped_ptr<content::WebContents> old_contents(browser_->tab_strip_model()-> | |
| 164 ReplaceWebContentsAt(index, new_contents.release())); | |
| 165 instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents.Pass(), | |
| 166 index); | |
| 167 } | |
| 168 | |
| 169 content::WebContents* BrowserInstantController::GetActiveWebContents() const { | 100 content::WebContents* BrowserInstantController::GetActiveWebContents() const { |
| 170 return browser_->tab_strip_model()->GetActiveWebContents(); | 101 return browser_->tab_strip_model()->GetActiveWebContents(); |
| 171 } | 102 } |
| 172 | 103 |
| 173 void BrowserInstantController::ActiveTabChanged() { | 104 void BrowserInstantController::ActiveTabChanged() { |
| 174 instant_.ActiveTabChanged(); | 105 instant_.ActiveTabChanged(); |
| 175 } | 106 } |
| 176 | 107 |
| 177 void BrowserInstantController::TabDeactivated(content::WebContents* contents) { | 108 void BrowserInstantController::TabDeactivated(content::WebContents* contents) { |
| 178 instant_.TabDeactivated(contents); | 109 instant_.TabDeactivated(contents); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 content::RenderProcessHost* rph = contents->GetRenderProcessHost(); | 170 content::RenderProcessHost* rph = contents->GetRenderProcessHost(); |
| 240 instant_service->SendSearchURLsToRenderer(rph); | 171 instant_service->SendSearchURLsToRenderer(rph); |
| 241 | 172 |
| 242 // Reload the contents to ensure that it gets assigned to a non-priviledged | 173 // Reload the contents to ensure that it gets assigned to a non-priviledged |
| 243 // renderer. | 174 // renderer. |
| 244 if (!instant_service->IsInstantProcess(rph->GetID())) | 175 if (!instant_service->IsInstantProcess(rph->GetID())) |
| 245 continue; | 176 continue; |
| 246 contents->GetController().Reload(false); | 177 contents->GetController().Reload(false); |
| 247 } | 178 } |
| 248 } | 179 } |
| OLD | NEW |