OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/autocomplete/keyword_provider.h" | 5 #include "chrome/browser/autocomplete/keyword_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/string16.h" | 10 #include "base/string16.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/autocomplete/autocomplete_match.h" | 12 #include "chrome/browser/autocomplete/autocomplete_match.h" |
13 #include "chrome/browser/extensions/extension_omnibox_api.h" | 13 #include "chrome/browser/extensions/extension_omnibox_api.h" |
14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/search_engines/template_url.h" | 16 #include "chrome/browser/search_engines/template_url.h" |
17 #include "chrome/browser/search_engines/template_url_service.h" | 17 #include "chrome/browser/search_engines/template_url_service.h" |
18 #include "chrome/browser/search_engines/template_url_service_factory.h" | 18 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 19 #include "chrome/common/chrome_notification_types.h" |
19 #include "content/common/notification_details.h" | 20 #include "content/common/notification_details.h" |
20 #include "content/common/notification_source.h" | 21 #include "content/common/notification_source.h" |
21 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
22 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
23 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
24 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
25 | 26 |
26 // Helper functor for Start(), for ending keyword mode unless explicitly told | 27 // Helper functor for Start(), for ending keyword mode unless explicitly told |
27 // otherwise. | 28 // otherwise. |
28 class KeywordProvider::ScopedEndExtensionKeywordMode { | 29 class KeywordProvider::ScopedEndExtensionKeywordMode { |
(...skipping 27 matching lines...) Expand all Loading... |
56 return remaining_input; | 57 return remaining_input; |
57 } | 58 } |
58 | 59 |
59 KeywordProvider::KeywordProvider(ACProviderListener* listener, Profile* profile) | 60 KeywordProvider::KeywordProvider(ACProviderListener* listener, Profile* profile) |
60 : AutocompleteProvider(listener, profile, "Keyword"), | 61 : AutocompleteProvider(listener, profile, "Keyword"), |
61 model_(NULL), | 62 model_(NULL), |
62 current_input_id_(0) { | 63 current_input_id_(0) { |
63 // Extension suggestions always come from the original profile, since that's | 64 // Extension suggestions always come from the original profile, since that's |
64 // where extensions run. We use the input ID to distinguish whether the | 65 // where extensions run. We use the input ID to distinguish whether the |
65 // suggestions are meant for us. | 66 // suggestions are meant for us. |
66 registrar_.Add(this, NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY, | 67 registrar_.Add(this, |
| 68 chrome::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY, |
67 Source<Profile>(profile->GetOriginalProfile())); | 69 Source<Profile>(profile->GetOriginalProfile())); |
68 registrar_.Add(this, | 70 registrar_.Add( |
69 NotificationType::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED, | 71 this, chrome::NOTIFICATION_EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED, |
70 Source<Profile>(profile->GetOriginalProfile())); | 72 Source<Profile>(profile->GetOriginalProfile())); |
71 registrar_.Add(this, NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED, | 73 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_OMNIBOX_INPUT_ENTERED, |
72 Source<Profile>(profile)); | 74 Source<Profile>(profile)); |
73 } | 75 } |
74 | 76 |
75 KeywordProvider::KeywordProvider(ACProviderListener* listener, | 77 KeywordProvider::KeywordProvider(ACProviderListener* listener, |
76 TemplateURLService* model) | 78 TemplateURLService* model) |
77 : AutocompleteProvider(listener, NULL, "Keyword"), | 79 : AutocompleteProvider(listener, NULL, "Keyword"), |
78 model_(model), | 80 model_(model), |
79 current_input_id_(0) { | 81 current_input_id_(0) { |
80 } | 82 } |
81 | 83 |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 keyword_desc.find(ASCIIToUTF16("%s")), | 446 keyword_desc.find(ASCIIToUTF16("%s")), |
445 prefix_length, | 447 prefix_length, |
446 result.description.length(), | 448 result.description.length(), |
447 ACMatchClassification::DIM, | 449 ACMatchClassification::DIM, |
448 &result.description_class); | 450 &result.description_class); |
449 } | 451 } |
450 | 452 |
451 return result; | 453 return result; |
452 } | 454 } |
453 | 455 |
454 void KeywordProvider::Observe(NotificationType type, | 456 void KeywordProvider::Observe(int type, |
455 const NotificationSource& source, | 457 const NotificationSource& source, |
456 const NotificationDetails& details) { | 458 const NotificationDetails& details) { |
457 TemplateURLService* model = | 459 TemplateURLService* model = |
458 profile_ ? TemplateURLServiceFactory::GetForProfile(profile_) : model_; | 460 profile_ ? TemplateURLServiceFactory::GetForProfile(profile_) : model_; |
459 const AutocompleteInput& input = extension_suggest_last_input_; | 461 const AutocompleteInput& input = extension_suggest_last_input_; |
460 | 462 |
461 switch (type.value) { | 463 switch (type) { |
462 case NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED: | 464 case chrome::NOTIFICATION_EXTENSION_OMNIBOX_INPUT_ENTERED: |
463 // Input has been accepted, so we're done with this input session. Ensure | 465 // Input has been accepted, so we're done with this input session. Ensure |
464 // we don't send the OnInputCancelled event, or handle any more stray | 466 // we don't send the OnInputCancelled event, or handle any more stray |
465 // suggestions_ready events. | 467 // suggestions_ready events. |
466 current_keyword_extension_id_.clear(); | 468 current_keyword_extension_id_.clear(); |
467 current_input_id_ = 0; | 469 current_input_id_ = 0; |
468 return; | 470 return; |
469 | 471 |
470 case NotificationType::EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED: { | 472 case chrome::NOTIFICATION_EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED: { |
471 // It's possible to change the default suggestion while not in an editing | 473 // It's possible to change the default suggestion while not in an editing |
472 // session. | 474 // session. |
473 string16 keyword, remaining_input; | 475 string16 keyword, remaining_input; |
474 if (matches_.empty() || current_keyword_extension_id_.empty() || | 476 if (matches_.empty() || current_keyword_extension_id_.empty() || |
475 !ExtractKeywordFromInput(input, &keyword, &remaining_input)) | 477 !ExtractKeywordFromInput(input, &keyword, &remaining_input)) |
476 return; | 478 return; |
477 | 479 |
478 const TemplateURL* template_url( | 480 const TemplateURL* template_url( |
479 model->GetTemplateURLForKeyword(keyword)); | 481 model->GetTemplateURLForKeyword(keyword)); |
480 ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url, | 482 ApplyDefaultSuggestionForExtensionKeyword(profile_, template_url, |
481 remaining_input, | 483 remaining_input, |
482 &matches_[0]); | 484 &matches_[0]); |
483 listener_->OnProviderUpdate(true); | 485 listener_->OnProviderUpdate(true); |
484 return; | 486 return; |
485 } | 487 } |
486 | 488 |
487 case NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY: { | 489 case chrome::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY: { |
488 const ExtensionOmniboxSuggestions& suggestions = | 490 const ExtensionOmniboxSuggestions& suggestions = |
489 *Details<ExtensionOmniboxSuggestions>(details).ptr(); | 491 *Details<ExtensionOmniboxSuggestions>(details).ptr(); |
490 if (suggestions.request_id != current_input_id_) | 492 if (suggestions.request_id != current_input_id_) |
491 return; // This is an old result. Just ignore. | 493 return; // This is an old result. Just ignore. |
492 | 494 |
493 string16 keyword, remaining_input; | 495 string16 keyword, remaining_input; |
494 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) { | 496 if (!ExtractKeywordFromInput(input, &keyword, &remaining_input)) { |
495 NOTREACHED(); | 497 NOTREACHED(); |
496 return; | 498 return; |
497 } | 499 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 } | 544 } |
543 | 545 |
544 void KeywordProvider::MaybeEndExtensionKeywordMode() { | 546 void KeywordProvider::MaybeEndExtensionKeywordMode() { |
545 if (!current_keyword_extension_id_.empty()) { | 547 if (!current_keyword_extension_id_.empty()) { |
546 ExtensionOmniboxEventRouter::OnInputCancelled( | 548 ExtensionOmniboxEventRouter::OnInputCancelled( |
547 profile_, current_keyword_extension_id_); | 549 profile_, current_keyword_extension_id_); |
548 | 550 |
549 current_keyword_extension_id_.clear(); | 551 current_keyword_extension_id_.clear(); |
550 } | 552 } |
551 } | 553 } |
OLD | NEW |