OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/search/search_tab_helper.h" | 5 #include "chrome/browser/ui/search/search_tab_helper.h" |
6 | 6 |
7 #include "chrome/browser/google/google_util.h" | 7 #include "chrome/browser/google/google_util.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" | 9 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" |
9 #include "chrome/browser/ui/search/search_model.h" | |
10 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 10 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
12 #include "content/public/browser/navigation_controller.h" | 12 #include "content/public/browser/navigation_controller.h" |
13 #include "content/public/browser/navigation_details.h" | 13 #include "content/public/browser/navigation_details.h" |
14 #include "content/public/browser/navigation_entry.h" | 14 #include "content/public/browser/navigation_entry.h" |
15 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
16 #include "content/public/browser/notification_types.h" | 16 #include "content/public/browser/notification_types.h" |
17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 18 #include "ipc/ipc_message.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 bool IsNTP(const GURL& url) { | 22 bool IsNTP(const GURL& url) { |
22 return url.SchemeIs(chrome::kChromeUIScheme) && | 23 return url.SchemeIs(chrome::kChromeUIScheme) && |
23 url.host() == chrome::kChromeUINewTabHost; | 24 url.host() == chrome::kChromeUINewTabHost; |
24 } | 25 } |
25 | 26 |
26 } // namespace | 27 } // namespace |
27 | 28 |
28 namespace chrome { | 29 namespace chrome { |
29 namespace search { | 30 namespace search { |
30 | 31 |
31 SearchTabHelper::SearchTabHelper( | 32 SearchTabHelper::SearchTabHelper( |
32 TabContents* contents, | 33 TabContents* contents, |
33 bool is_search_enabled) | 34 bool is_search_enabled) |
34 : WebContentsObserver(contents->web_contents()), | 35 : WebContentsObserver(contents->web_contents()), |
35 is_search_enabled_(is_search_enabled), | 36 is_search_enabled_(is_search_enabled), |
36 model_(new SearchModel(contents)) { | 37 model_(contents) { |
37 if (!is_search_enabled) | 38 if (!is_search_enabled) |
38 return; | 39 return; |
39 | 40 |
40 registrar_.Add( | 41 registrar_.Add( |
41 this, | 42 this, |
42 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 43 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
43 content::Source<content::NavigationController>( | 44 content::Source<content::NavigationController>( |
44 &contents->web_contents()->GetController())); | 45 &contents->web_contents()->GetController())); |
45 } | 46 } |
46 | 47 |
47 SearchTabHelper::~SearchTabHelper() { | 48 SearchTabHelper::~SearchTabHelper() { |
48 } | 49 } |
49 | 50 |
| 51 content::WebContents* SearchTabHelper::GetNTPWebContents() { |
| 52 if (!ntp_web_contents_.get()) { |
| 53 ntp_web_contents_.reset(content::WebContents::Create( |
| 54 model_.tab_contents()->profile(), |
| 55 model_.tab_contents()->web_contents()->GetSiteInstance(), |
| 56 MSG_ROUTING_NONE, |
| 57 NULL, |
| 58 NULL)); |
| 59 ntp_web_contents_->GetController().LoadURL( |
| 60 GURL(chrome::kChromeUINewTabURL), |
| 61 content::Referrer(), |
| 62 content::PAGE_TRANSITION_START_PAGE, |
| 63 std::string()); |
| 64 } |
| 65 return ntp_web_contents_.get(); |
| 66 } |
| 67 |
50 void SearchTabHelper::OmniboxEditModelChanged(OmniboxEditModel* edit_model) { | 68 void SearchTabHelper::OmniboxEditModelChanged(OmniboxEditModel* edit_model) { |
51 if (!is_search_enabled_) | 69 if (!is_search_enabled_) |
52 return; | 70 return; |
53 | 71 |
54 if (model_->mode().is_ntp()) { | 72 if (model_.mode().is_ntp()) { |
55 if (edit_model->user_input_in_progress()) | 73 if (edit_model->user_input_in_progress()) |
56 model_->SetMode(Mode(Mode::MODE_SEARCH, true)); | 74 model_.SetMode(Mode(Mode::MODE_SEARCH, true)); |
57 return; | 75 return; |
58 } | 76 } |
59 | 77 |
60 Mode::Type mode = Mode::MODE_DEFAULT; | 78 Mode::Type mode = Mode::MODE_DEFAULT; |
61 GURL url(web_contents()->GetURL()); | 79 GURL url(web_contents()->GetURL()); |
62 // TODO(kuan): revisit this condition when zero suggest becomes available. | 80 // TODO(kuan): revisit this condition when zero suggest becomes available. |
63 if (google_util::IsInstantExtendedAPIGoogleSearchUrl(url.spec()) || | 81 if (google_util::IsInstantExtendedAPIGoogleSearchUrl(url.spec()) || |
64 (edit_model->has_focus() && edit_model->user_input_in_progress())) { | 82 (edit_model->has_focus() && edit_model->user_input_in_progress())) { |
65 mode = Mode::MODE_SEARCH; | 83 mode = Mode::MODE_SEARCH; |
66 } | 84 } |
67 model_->SetMode(Mode(mode, true)); | 85 model_.SetMode(Mode(mode, true)); |
68 } | 86 } |
69 | 87 |
70 void SearchTabHelper::NavigateToPendingEntry( | 88 void SearchTabHelper::NavigateToPendingEntry( |
71 const GURL& url, | 89 const GURL& url, |
72 content::NavigationController::ReloadType reload_type) { | 90 content::NavigationController::ReloadType reload_type) { |
73 if (!is_search_enabled_) | 91 if (!is_search_enabled_) |
74 return; | 92 return; |
75 | 93 |
76 UpdateModel(url); | 94 UpdateModel(url); |
| 95 FlushNTP(url); |
77 } | 96 } |
78 | 97 |
79 void SearchTabHelper::Observe( | 98 void SearchTabHelper::Observe( |
80 int type, | 99 int type, |
81 const content::NotificationSource& source, | 100 const content::NotificationSource& source, |
82 const content::NotificationDetails& details) { | 101 const content::NotificationDetails& details) { |
83 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | 102 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); |
84 content::LoadCommittedDetails* committed_details = | 103 content::LoadCommittedDetails* committed_details = |
85 content::Details<content::LoadCommittedDetails>(details).ptr(); | 104 content::Details<content::LoadCommittedDetails>(details).ptr(); |
86 UpdateModel(committed_details->entry->GetURL()); | 105 UpdateModel(committed_details->entry->GetURL()); |
| 106 FlushNTP(committed_details->entry->GetURL()); |
87 } | 107 } |
88 | 108 |
89 void SearchTabHelper::UpdateModel(const GURL& url) { | 109 void SearchTabHelper::UpdateModel(const GURL& url) { |
90 Mode::Type type = Mode::MODE_DEFAULT; | 110 Mode::Type type = Mode::MODE_DEFAULT; |
91 if (IsNTP(url)) | 111 if (IsNTP(url)) |
92 type = Mode::MODE_NTP; | 112 type = Mode::MODE_NTP; |
93 else if (google_util::IsInstantExtendedAPIGoogleSearchUrl(url.spec())) | 113 else if (google_util::IsInstantExtendedAPIGoogleSearchUrl(url.spec())) |
94 type = Mode::MODE_SEARCH; | 114 type = Mode::MODE_SEARCH; |
95 model_->SetMode(Mode(type, true)); | 115 model_.SetMode(Mode(type, true)); |
| 116 } |
| 117 |
| 118 void SearchTabHelper::FlushNTP(const GURL& url) { |
| 119 if (!IsNTP(url) && |
| 120 !google_util::IsInstantExtendedAPIGoogleSearchUrl(url.spec())) { |
| 121 ntp_web_contents_.reset(); |
| 122 } |
96 } | 123 } |
97 | 124 |
98 } // namespace search | 125 } // namespace search |
99 } // namespace chrome | 126 } // namespace chrome |
OLD | NEW |