Home | History | Annotate | Download | only in autocomplete
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_AUTOCOMPLETE_CONTACT_PROVIDER_CHROMEOS_H_
      6 #define CHROME_BROWSER_AUTOCOMPLETE_CONTACT_PROVIDER_CHROMEOS_H_
      7 
      8 #include <utility>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "chrome/browser/autocomplete/autocomplete_match.h"
     15 #include "chrome/browser/autocomplete/autocomplete_provider.h"
     16 #include "chrome/browser/chromeos/contacts/contact_manager_observer.h"
     17 
     18 class AutocompleteInput;
     19 
     20 namespace contacts {
     21 class ContactManagerInterface;
     22 }
     23 
     24 // AutocompleteProvider implementation that searches through the contacts
     25 // provided by contacts::ContactManager.
     26 class ContactProvider : public AutocompleteProvider,
     27                         public contacts::ContactManagerObserver {
     28  public:
     29   // Key within AutocompleteMatch::additional_info where the corresponding
     30   // contact's ID is stored.
     31   static const char kMatchContactIdKey[];
     32 
     33   ContactProvider(
     34       AutocompleteProviderListener* listener,
     35       Profile* profile,
     36       base::WeakPtr<contacts::ContactManagerInterface> contact_manager);
     37 
     38   // AutocompleteProvider overrides:
     39   virtual void Start(const AutocompleteInput& input,
     40                      bool minimal_changes) OVERRIDE;
     41 
     42   // contacts::ContactManagerObserver overrides:
     43   virtual void OnContactsUpdated(Profile* profile) OVERRIDE;
     44 
     45  private:
     46   struct ContactData;
     47   typedef std::vector<ContactData> ContactDataVector;
     48 
     49   virtual ~ContactProvider();
     50 
     51   // Returns true if |a|'s affinity is greater than |b|'s affinity.
     52   static bool CompareAffinity(const ContactData& a, const ContactData& b);
     53 
     54   // Updates |contacts_| to match the contacts currently reported by
     55   // ContactManager.
     56   void RefreshContacts();
     57 
     58   // Adds an AutocompleteMatch object for |contact| to |matches_| if |contact|
     59   // is matched by |input|.  |input_words| is |input.text()| split on word
     60   // boundaries.
     61   void AddContactIfMatched(const AutocompleteInput& input,
     62                            const std::vector<string16>& input_words,
     63                            const ContactData& contact);
     64 
     65   // Returns an AutocompleteMatch object corresponding to the passed-in data.
     66   AutocompleteMatch CreateAutocompleteMatch(const AutocompleteInput& input,
     67                                             const ContactData& contact);
     68 
     69   base::WeakPtr<contacts::ContactManagerInterface> contact_manager_;
     70 
     71   // Contacts through which we search, ordered by descending affinity.
     72   ContactDataVector contacts_;
     73 
     74   DISALLOW_COPY_AND_ASSIGN(ContactProvider);
     75 };
     76 
     77 #endif  // CHROME_BROWSER_AUTOCOMPLETE_CONTACT_PROVIDER_CHROMEOS_H_
     78