• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EXTENSION_APP_PROVIDER_H_
6 #define CHROME_BROWSER_AUTOCOMPLETE_EXTENSION_APP_PROVIDER_H_
7 
8 #include <vector>
9 
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "chrome/browser/autocomplete/autocomplete_input.h"
14 #include "chrome/browser/autocomplete/autocomplete_match.h"
15 #include "chrome/browser/autocomplete/autocomplete_provider.h"
16 #include "components/metrics/proto/omnibox_input_type.pb.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "ui/base/window_open_disposition.h"
20 
21 // This provider is responsible for keeping track of which Extension Apps are
22 // installed and their URLs.  An instance of it gets created and managed by
23 // AutocompleteController.
24 class ExtensionAppProvider : public AutocompleteProvider,
25                              public content::NotificationObserver {
26  public:
27   ExtensionAppProvider(AutocompleteProviderListener* listener,
28                        Profile* profile);
29 
30   // AutocompleteProvider:
31   virtual void Start(const AutocompleteInput& input,
32                      bool minimal_changes) OVERRIDE;
33 
34   // Launch an Extension App from |match| details provided by the Omnibox. If
35   // the application wants to launch as a window or panel, |disposition| is
36   // ignored; otherwise it's used to determine in which tab we'll launch the
37   // application.
38   static void LaunchAppFromOmnibox(const AutocompleteMatch& match,
39                                    Profile* profile,
40                                    WindowOpenDisposition disposition);
41 
42  private:
43   friend class ExtensionAppProviderTest;
44   FRIEND_TEST_ALL_PREFIXES(ExtensionAppProviderTest, CreateMatchSanitize);
45 
46   // ExtensionApp stores the minimal metadata that we need to match against
47   // eligible apps.
48   struct ExtensionApp {
49     // App's name.
50     base::string16 name;
51     // App's launch URL (for platform apps, which don't have a launch URL, this
52     // just points to the app's origin).
53     base::string16 launch_url;
54     // If false, then the launch_url will not be considered for matching,
55     // not shown next to the match, and not displayed as the editable text if
56     // the user selects the match with the arrow keys.
57     bool should_match_against_launch_url;
58   };
59   typedef std::vector<ExtensionApp> ExtensionApps;
60 
61   virtual ~ExtensionAppProvider();
62 
63   void AddExtensionAppForTesting(const ExtensionApp& extension_app);
64 
65   // Construct a match for the specified parameters.
66   AutocompleteMatch CreateAutocompleteMatch(const AutocompleteInput& input,
67                                             const ExtensionApp& app,
68                                             size_t name_match_index,
69                                             size_t url_match_index);
70 
71   // Fetch the current app list and cache it locally.
72   void RefreshAppList();
73 
74   // Calculate the relevance of the match.
75   int CalculateRelevance(metrics::OmniboxInputType::Type type,
76                          int input_length,
77                          int target_length,
78                          const GURL& url);
79 
80   // content::NotificationObserver implementation:
81   virtual void Observe(int type,
82                        const content::NotificationSource& source,
83                        const content::NotificationDetails& details) OVERRIDE;
84 
85   content::NotificationRegistrar registrar_;
86 
87   // Our cache of ExtensionApp objects (name + url) representing the extension
88   // apps we know/care about.
89   ExtensionApps extension_apps_;
90 
91   DISALLOW_COPY_AND_ASSIGN(ExtensionAppProvider);
92 };
93 
94 #endif  // CHROME_BROWSER_AUTOCOMPLETE_EXTENSION_APP_PROVIDER_H_
95