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( | |
sky
2012/07/28 02:27:53
Shouldn't we destroy this at some point? Otherwise
dhollowa
2012/07/31 17:03:46
Yes, agreed. I've added logic to delete the WC wh
| |
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); |
77 } | 95 } |
78 | 96 |
79 void SearchTabHelper::Observe( | 97 void SearchTabHelper::Observe( |
80 int type, | 98 int type, |
81 const content::NotificationSource& source, | 99 const content::NotificationSource& source, |
82 const content::NotificationDetails& details) { | 100 const content::NotificationDetails& details) { |
83 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | 101 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); |
84 content::LoadCommittedDetails* committed_details = | 102 content::LoadCommittedDetails* committed_details = |
85 content::Details<content::LoadCommittedDetails>(details).ptr(); | 103 content::Details<content::LoadCommittedDetails>(details).ptr(); |
86 UpdateModel(committed_details->entry->GetURL()); | 104 UpdateModel(committed_details->entry->GetURL()); |
87 } | 105 } |
88 | 106 |
89 void SearchTabHelper::UpdateModel(const GURL& url) { | 107 void SearchTabHelper::UpdateModel(const GURL& url) { |
90 Mode::Type type = Mode::MODE_DEFAULT; | 108 Mode::Type type = Mode::MODE_DEFAULT; |
91 if (IsNTP(url)) | 109 if (IsNTP(url)) |
92 type = Mode::MODE_NTP; | 110 type = Mode::MODE_NTP; |
93 else if (google_util::IsInstantExtendedAPIGoogleSearchUrl(url.spec())) | 111 else if (google_util::IsInstantExtendedAPIGoogleSearchUrl(url.spec())) |
94 type = Mode::MODE_SEARCH; | 112 type = Mode::MODE_SEARCH; |
95 model_->SetMode(Mode(type, true)); | 113 model_.SetMode(Mode(type, true)); |
96 } | 114 } |
97 | 115 |
98 } // namespace search | 116 } // namespace search |
99 } // namespace chrome | 117 } // namespace chrome |
OLD | NEW |