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" |
19 #include "chrome/browser/ui/search/instant_search_prerenderer.h" | 20 #include "chrome/browser/ui/search/instant_search_prerenderer.h" |
20 #include "chrome/browser/ui/search/search_model.h" | 21 #include "chrome/browser/ui/search/search_model.h" |
21 #include "chrome/browser/ui/search/search_tab_helper.h" | 22 #include "chrome/browser/ui/search/search_tab_helper.h" |
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
23 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" | 24 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
24 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
25 #include "content/public/browser/render_process_host.h" | 26 #include "content/public/browser/render_process_host.h" |
26 #include "content/public/browser/user_metrics.h" | 27 #include "content/public/browser/user_metrics.h" |
27 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
28 #include "content/public/browser/web_contents_view.h" | 29 #include "content/public/browser/web_contents_view.h" |
29 | 30 |
30 using content::UserMetricsAction; | 31 using content::UserMetricsAction; |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) { | 35 InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) { |
35 DCHECK(profile); | 36 DCHECK(profile); |
36 InstantService* instant_service = | 37 InstantService* instant_service = |
37 InstantServiceFactory::GetForProfile(profile); | 38 InstantServiceFactory::GetForProfile(profile); |
38 return instant_service ? instant_service->instant_search_prerenderer() : NULL; | 39 return instant_service ? instant_service->instant_search_prerenderer() : NULL; |
39 } | 40 } |
40 | 41 |
41 } // namespace | 42 } // namespace |
42 | 43 |
43 //////////////////////////////////////////////////////////////////////////////// | 44 //////////////////////////////////////////////////////////////////////////////// |
44 // BrowserInstantController, public: | 45 // BrowserInstantController, public: |
45 | 46 |
46 BrowserInstantController::BrowserInstantController(Browser* browser) | 47 BrowserInstantController::BrowserInstantController(Browser* browser) |
47 : browser_(browser), | 48 : browser_(browser), |
48 instant_(this) { | 49 instant_(this), |
| 50 instant_unload_handler_(browser) { |
49 browser_->search_model()->AddObserver(this); | 51 browser_->search_model()->AddObserver(this); |
50 | 52 |
51 InstantService* instant_service = | 53 InstantService* instant_service = |
52 InstantServiceFactory::GetForProfile(profile()); | 54 InstantServiceFactory::GetForProfile(profile()); |
| 55 instant_service->OnBrowserInstantControllerCreated(); |
53 instant_service->AddObserver(this); | 56 instant_service->AddObserver(this); |
54 } | 57 } |
55 | 58 |
56 BrowserInstantController::~BrowserInstantController() { | 59 BrowserInstantController::~BrowserInstantController() { |
57 browser_->search_model()->RemoveObserver(this); | 60 browser_->search_model()->RemoveObserver(this); |
58 | 61 |
59 InstantService* instant_service = | 62 InstantService* instant_service = |
60 InstantServiceFactory::GetForProfile(profile()); | 63 InstantServiceFactory::GetForProfile(profile()); |
61 instant_service->RemoveObserver(this); | 64 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; |
62 } | 121 } |
63 | 122 |
64 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition, | 123 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition, |
65 const GURL& url) { | 124 const GURL& url) { |
66 // Unsupported dispositions. | 125 // Unsupported dispositions. |
67 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW || | 126 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW || |
68 disposition == NEW_FOREGROUND_TAB) | 127 disposition == NEW_FOREGROUND_TAB) |
69 return false; | 128 return false; |
70 | 129 |
71 // The omnibox currently doesn't use other dispositions, so we don't attempt | 130 // The omnibox currently doesn't use other dispositions, so we don't attempt |
(...skipping 18 matching lines...) Expand all Loading... |
90 return false; | 149 return false; |
91 } | 150 } |
92 | 151 |
93 return instant_.SubmitQuery(search_terms); | 152 return instant_.SubmitQuery(search_terms); |
94 } | 153 } |
95 | 154 |
96 Profile* BrowserInstantController::profile() const { | 155 Profile* BrowserInstantController::profile() const { |
97 return browser_->profile(); | 156 return browser_->profile(); |
98 } | 157 } |
99 | 158 |
| 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 |
100 content::WebContents* BrowserInstantController::GetActiveWebContents() const { | 169 content::WebContents* BrowserInstantController::GetActiveWebContents() const { |
101 return browser_->tab_strip_model()->GetActiveWebContents(); | 170 return browser_->tab_strip_model()->GetActiveWebContents(); |
102 } | 171 } |
103 | 172 |
104 void BrowserInstantController::ActiveTabChanged() { | 173 void BrowserInstantController::ActiveTabChanged() { |
105 instant_.ActiveTabChanged(); | 174 instant_.ActiveTabChanged(); |
106 } | 175 } |
107 | 176 |
108 void BrowserInstantController::TabDeactivated(content::WebContents* contents) { | 177 void BrowserInstantController::TabDeactivated(content::WebContents* contents) { |
109 instant_.TabDeactivated(contents); | 178 instant_.TabDeactivated(contents); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 content::RenderProcessHost* rph = contents->GetRenderProcessHost(); | 239 content::RenderProcessHost* rph = contents->GetRenderProcessHost(); |
171 instant_service->SendSearchURLsToRenderer(rph); | 240 instant_service->SendSearchURLsToRenderer(rph); |
172 | 241 |
173 // Reload the contents to ensure that it gets assigned to a non-priviledged | 242 // Reload the contents to ensure that it gets assigned to a non-priviledged |
174 // renderer. | 243 // renderer. |
175 if (!instant_service->IsInstantProcess(rph->GetID())) | 244 if (!instant_service->IsInstantProcess(rph->GetID())) |
176 continue; | 245 continue; |
177 contents->GetController().Reload(false); | 246 contents->GetController().Reload(false); |
178 } | 247 } |
179 } | 248 } |
OLD | NEW |