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_engines/search_engine_tab_helper.h" | 5 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h" |
6 | 6 |
7 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/search_engines/template_url.h" | 8 #include "chrome/browser/search_engines/template_url.h" |
9 #include "chrome/browser/search_engines/template_url_fetcher.h" | 9 #include "chrome/browser/search_engines/template_url_fetcher.h" |
10 #include "chrome/browser/search_engines/template_url_service.h" | 10 #include "chrome/browser/search_engines/template_url_service.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 using content::WebContents; | 22 using content::WebContents; |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 // Returns true if the entry's transition type is FORM_SUBMIT. | 26 // Returns true if the entry's transition type is FORM_SUBMIT. |
27 bool IsFormSubmit(const NavigationEntry* entry) { | 27 bool IsFormSubmit(const NavigationEntry* entry) { |
28 return (content::PageTransitionStripQualifier(entry->GetTransitionType()) == | 28 return (content::PageTransitionStripQualifier(entry->GetTransitionType()) == |
29 content::PAGE_TRANSITION_FORM_SUBMIT); | 29 content::PAGE_TRANSITION_FORM_SUBMIT); |
30 } | 30 } |
31 | 31 |
32 string16 GenerateKeywordFromNavigationEntry(const NavigationEntry* entry, | 32 string16 GenerateKeywordFromNavigationEntry(const NavigationEntry* entry) { |
33 bool is_autodetected_provider) { | |
34 // Don't autogenerate keywords for pages that are the result of form | 33 // Don't autogenerate keywords for pages that are the result of form |
35 // submissions. | 34 // submissions. |
36 if (IsFormSubmit(entry)) | 35 if (IsFormSubmit(entry)) |
37 return string16(); | 36 return string16(); |
38 | 37 |
39 // We want to use the user typed URL if available since that represents what | 38 // We want to use the user typed URL if available since that represents what |
40 // the user typed to get here, and fall back on the regular URL if not. | 39 // the user typed to get here, and fall back on the regular URL if not. |
41 GURL url = entry->GetUserTypedURL(); | 40 GURL url = entry->GetUserTypedURL(); |
42 if (!url.is_valid()) { | 41 if (!url.is_valid()) { |
43 url = entry->GetURL(); | 42 url = entry->GetURL(); |
44 if (!url.is_valid()) | 43 if (!url.is_valid()) |
45 return string16(); | 44 return string16(); |
46 } | 45 } |
47 | 46 |
48 return TemplateURLService::GenerateKeyword(url, is_autodetected_provider); | 47 return TemplateURLService::GenerateKeyword(url, true); |
49 } | 48 } |
50 | 49 |
51 } // namespace | 50 } // namespace |
52 | 51 |
53 SearchEngineTabHelper::SearchEngineTabHelper(WebContents* web_contents) | 52 SearchEngineTabHelper::SearchEngineTabHelper(WebContents* web_contents) |
54 : content::WebContentsObserver(web_contents) { | 53 : content::WebContentsObserver(web_contents) { |
55 DCHECK(web_contents); | 54 DCHECK(web_contents); |
56 } | 55 } |
57 | 56 |
58 SearchEngineTabHelper::~SearchEngineTabHelper() { | 57 SearchEngineTabHelper::~SearchEngineTabHelper() { |
(...skipping 20 matching lines...) Expand all Loading... |
79 const GURL& doc_url, | 78 const GURL& doc_url, |
80 const search_provider::OSDDType& msg_provider_type) { | 79 const search_provider::OSDDType& msg_provider_type) { |
81 // Checks to see if we should generate a keyword based on the OSDD, and if | 80 // Checks to see if we should generate a keyword based on the OSDD, and if |
82 // necessary uses TemplateURLFetcher to download the OSDD and create a | 81 // necessary uses TemplateURLFetcher to download the OSDD and create a |
83 // keyword. | 82 // keyword. |
84 | 83 |
85 // Make sure page_id is the current page and other basic checks. | 84 // Make sure page_id is the current page and other basic checks. |
86 DCHECK(doc_url.is_valid()); | 85 DCHECK(doc_url.is_valid()); |
87 Profile* profile = | 86 Profile* profile = |
88 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 87 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
89 if (!web_contents()->IsActiveEntry(page_id)) | 88 if (!web_contents()->IsActiveEntry(page_id) || |
90 return; | 89 !profile->GetTemplateURLFetcher() || profile->IsOffTheRecord()) |
91 if (!profile->GetTemplateURLFetcher()) | |
92 return; | |
93 if (profile->IsOffTheRecord()) | |
94 return; | 90 return; |
95 | 91 |
96 TemplateURLFetcher::ProviderType provider_type; | 92 TemplateURLFetcher::ProviderType provider_type = |
97 switch (msg_provider_type) { | 93 (msg_provider_type == search_provider::AUTODETECTED_PROVIDER) ? |
98 case search_provider::AUTODETECTED_PROVIDER: | 94 TemplateURLFetcher::AUTODETECTED_PROVIDER : |
99 provider_type = TemplateURLFetcher::AUTODETECTED_PROVIDER; | 95 TemplateURLFetcher::EXPLICIT_PROVIDER; |
100 break; | |
101 | |
102 case search_provider::EXPLICIT_PROVIDER: | |
103 provider_type = TemplateURLFetcher::EXPLICIT_PROVIDER; | |
104 break; | |
105 | |
106 default: | |
107 NOTREACHED(); | |
108 return; | |
109 } | |
110 | 96 |
111 // If the current page is a form submit, find the last page that was not a | 97 // If the current page is a form submit, find the last page that was not a |
112 // form submit and use its url to generate the keyword from. | 98 // form submit and use its url to generate the keyword from. |
113 const NavigationController& controller = web_contents()->GetController(); | 99 const NavigationController& controller = web_contents()->GetController(); |
114 const NavigationEntry* entry = controller.GetLastCommittedEntry(); | 100 const NavigationEntry* entry = controller.GetLastCommittedEntry(); |
115 for (int index = controller.GetLastCommittedEntryIndex(); | 101 for (int index = controller.GetLastCommittedEntryIndex(); |
116 (index > 0) && IsFormSubmit(entry); | 102 (index > 0) && IsFormSubmit(entry); |
117 entry = controller.GetEntryAtIndex(index)) | 103 entry = controller.GetEntryAtIndex(index)) |
118 --index; | 104 --index; |
119 if (IsFormSubmit(entry)) | 105 if (IsFormSubmit(entry)) |
120 return; | 106 return; |
121 | 107 |
122 string16 keyword = GenerateKeywordFromNavigationEntry(entry, | 108 // Autogenerate a keyword for the autodetected case; in the other cases we'll |
123 provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER); | 109 // generate a keyword later after fetching the OSDD. |
| 110 string16 keyword; |
| 111 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { |
| 112 keyword = GenerateKeywordFromNavigationEntry(entry); |
| 113 if (keyword.empty()) |
| 114 return; |
| 115 } |
124 | 116 |
125 // Download the OpenSearch description document. If this is successful, a | 117 // Download the OpenSearch description document. If this is successful, a |
126 // new keyword will be created when done. | 118 // new keyword will be created when done. |
127 profile->GetTemplateURLFetcher()->ScheduleDownload(keyword, doc_url, | 119 profile->GetTemplateURLFetcher()->ScheduleDownload(keyword, doc_url, |
128 entry->GetFavicon().url, web_contents(), | 120 entry->GetFavicon().url, web_contents(), |
129 new TemplateURLFetcherUICallbacks(this, web_contents()), provider_type); | 121 new TemplateURLFetcherUICallbacks(this, web_contents()), provider_type); |
130 } | 122 } |
131 | 123 |
132 void SearchEngineTabHelper::GenerateKeywordIfNecessary( | 124 void SearchEngineTabHelper::GenerateKeywordIfNecessary( |
133 const content::FrameNavigateParams& params) { | 125 const content::FrameNavigateParams& params) { |
134 if (!params.searchable_form_url.is_valid()) | 126 if (!params.searchable_form_url.is_valid()) |
135 return; | 127 return; |
136 | 128 |
137 Profile* profile = | 129 Profile* profile = |
138 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 130 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
139 if (profile->IsOffTheRecord()) | 131 if (profile->IsOffTheRecord()) |
140 return; | 132 return; |
141 | 133 |
142 const NavigationController& controller = web_contents()->GetController(); | 134 const NavigationController& controller = web_contents()->GetController(); |
143 int last_index = controller.GetLastCommittedEntryIndex(); | 135 int last_index = controller.GetLastCommittedEntryIndex(); |
144 // When there was no previous page, the last index will be 0. This is | 136 // When there was no previous page, the last index will be 0. This is |
145 // normally due to a form submit that opened in a new tab. | 137 // normally due to a form submit that opened in a new tab. |
146 // TODO(brettw) bug 916126: we should support keywords when form submits | 138 // TODO(brettw) bug 916126: we should support keywords when form submits |
147 // happen in new tabs. | 139 // happen in new tabs. |
148 if (last_index <= 0) | 140 if (last_index <= 0) |
149 return; | 141 return; |
| 142 |
150 string16 keyword(GenerateKeywordFromNavigationEntry( | 143 string16 keyword(GenerateKeywordFromNavigationEntry( |
151 controller.GetEntryAtIndex(last_index - 1), true)); | 144 controller.GetEntryAtIndex(last_index - 1))); |
152 if (keyword.empty()) | 145 if (keyword.empty()) |
153 return; | 146 return; |
154 | 147 |
155 TemplateURLService* url_service = | 148 TemplateURLService* url_service = |
156 TemplateURLServiceFactory::GetForProfile(profile); | 149 TemplateURLServiceFactory::GetForProfile(profile); |
157 if (!url_service) | 150 if (!url_service) |
158 return; | 151 return; |
159 | 152 |
160 if (!url_service->loaded()) { | 153 if (!url_service->loaded()) { |
161 url_service->Load(); | 154 url_service->Load(); |
(...skipping 26 matching lines...) Expand all Loading... |
188 // the favicon url wasn't obtained before the load started. This assumes the | 181 // the favicon url wasn't obtained before the load started. This assumes the |
189 // latter. | 182 // latter. |
190 // TODO(sky): Need a way to set the favicon that doesn't involve generating | 183 // TODO(sky): Need a way to set the favicon that doesn't involve generating |
191 // its url. | 184 // its url. |
192 const GURL& favicon_url = current_favicon.is_valid() ? | 185 const GURL& favicon_url = current_favicon.is_valid() ? |
193 current_favicon : TemplateURL::GenerateFaviconURL(params.referrer.url); | 186 current_favicon : TemplateURL::GenerateFaviconURL(params.referrer.url); |
194 new_url->SetURL(url.spec(), 0, 0); | 187 new_url->SetURL(url.spec(), 0, 0); |
195 new_url->SetFaviconURL(favicon_url); | 188 new_url->SetFaviconURL(favicon_url); |
196 url_service->Add(new_url); | 189 url_service->Add(new_url); |
197 } | 190 } |
OLD | NEW |