OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_extensions_delegate_impl.h" | 5 #include "chrome/browser/autocomplete/keyword_extensions_delegate_impl.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h" | 8 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/extensions/extension_util.h" | 10 #include "chrome/browser/extensions/extension_util.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "content/public/browser/notification_details.h" | 12 #include "content/public/browser/notification_details.h" |
13 #include "content/public/browser/notification_source.h" | 13 #include "content/public/browser/notification_source.h" |
14 #include "extensions/browser/extension_system.h" | 14 #include "extensions/browser/extension_system.h" |
15 #include "extensions/browser/notification_types.h" | 15 #include "extensions/browser/notification_types.h" |
16 | 16 |
17 namespace omnibox_api = extensions::api::omnibox; | 17 namespace omnibox_api = extensions::api::omnibox; |
18 | 18 |
19 int KeywordExtensionsDelegateImpl::global_input_uid_ = 0; | 19 int KeywordExtensionsDelegateImpl::global_input_uid_ = 0; |
20 | 20 |
21 KeywordExtensionsDelegateImpl::KeywordExtensionsDelegateImpl( | 21 KeywordExtensionsDelegateImpl::KeywordExtensionsDelegateImpl( |
| 22 Profile* profile, |
22 KeywordProvider* provider) | 23 KeywordProvider* provider) |
23 : KeywordExtensionsDelegate(provider), | 24 : KeywordExtensionsDelegate(provider), |
| 25 profile_(profile), |
24 provider_(provider) { | 26 provider_(provider) { |
25 DCHECK(provider_); | 27 DCHECK(provider_); |
26 | 28 |
27 current_input_id_ = 0; | 29 current_input_id_ = 0; |
28 // Extension suggestions always come from the original profile, since that's | 30 // Extension suggestions always come from the original profile, since that's |
29 // where extensions run. We use the input ID to distinguish whether the | 31 // where extensions run. We use the input ID to distinguish whether the |
30 // suggestions are meant for us. | 32 // suggestions are meant for us. |
31 registrar_.Add(this, | 33 registrar_.Add(this, |
32 extensions::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY, | 34 extensions::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY, |
33 content::Source<Profile>(profile()->GetOriginalProfile())); | 35 content::Source<Profile>(profile_->GetOriginalProfile())); |
34 registrar_.Add( | 36 registrar_.Add( |
35 this, | 37 this, |
36 extensions::NOTIFICATION_EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED, | 38 extensions::NOTIFICATION_EXTENSION_OMNIBOX_DEFAULT_SUGGESTION_CHANGED, |
37 content::Source<Profile>(profile()->GetOriginalProfile())); | 39 content::Source<Profile>(profile_->GetOriginalProfile())); |
38 registrar_.Add(this, | 40 registrar_.Add(this, |
39 extensions::NOTIFICATION_EXTENSION_OMNIBOX_INPUT_ENTERED, | 41 extensions::NOTIFICATION_EXTENSION_OMNIBOX_INPUT_ENTERED, |
40 content::Source<Profile>(profile())); | 42 content::Source<Profile>(profile_)); |
41 } | 43 } |
42 | 44 |
43 KeywordExtensionsDelegateImpl::~KeywordExtensionsDelegateImpl() { | 45 KeywordExtensionsDelegateImpl::~KeywordExtensionsDelegateImpl() { |
44 } | 46 } |
45 | 47 |
46 void KeywordExtensionsDelegateImpl::IncrementInputId() { | 48 void KeywordExtensionsDelegateImpl::IncrementInputId() { |
47 current_input_id_ = ++global_input_uid_; | 49 current_input_id_ = ++global_input_uid_; |
48 } | 50 } |
49 | 51 |
50 bool KeywordExtensionsDelegateImpl::IsEnabledExtension( | 52 bool KeywordExtensionsDelegateImpl::IsEnabledExtension( |
51 Profile* profile, | |
52 const std::string& extension_id) { | 53 const std::string& extension_id) { |
53 ExtensionService* extension_service = | 54 ExtensionService* extension_service = |
54 extensions::ExtensionSystem::Get(profile)->extension_service(); | 55 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
55 const extensions::Extension* extension = | 56 const extensions::Extension* extension = |
56 extension_service->GetExtensionById(extension_id, false); | 57 extension_service->GetExtensionById(extension_id, false); |
57 return extension && | 58 return extension && |
58 (!profile->IsOffTheRecord() || | 59 (!profile_->IsOffTheRecord() || |
59 !extensions::util::IsIncognitoEnabled(extension_id, profile)); | 60 !extensions::util::IsIncognitoEnabled(extension_id, profile_)); |
60 } | 61 } |
61 | 62 |
62 bool KeywordExtensionsDelegateImpl::Start( | 63 bool KeywordExtensionsDelegateImpl::Start( |
63 const AutocompleteInput& input, | 64 const AutocompleteInput& input, |
64 bool minimal_changes, | 65 bool minimal_changes, |
65 const TemplateURL* template_url, | 66 const TemplateURL* template_url, |
66 const base::string16& remaining_input) { | 67 const base::string16& remaining_input) { |
67 DCHECK(template_url); | 68 DCHECK(template_url); |
68 | 69 |
69 if (input.want_asynchronous_matches()) { | 70 if (input.want_asynchronous_matches()) { |
70 std::string extension_id = template_url->GetExtensionId(); | 71 std::string extension_id = template_url->GetExtensionId(); |
71 if (extension_id != current_keyword_extension_id_) | 72 if (extension_id != current_keyword_extension_id_) |
72 MaybeEndExtensionKeywordMode(); | 73 MaybeEndExtensionKeywordMode(); |
73 if (current_keyword_extension_id_.empty()) | 74 if (current_keyword_extension_id_.empty()) |
74 EnterExtensionKeywordMode(extension_id); | 75 EnterExtensionKeywordMode(extension_id); |
75 } | 76 } |
76 | 77 |
77 extensions::ApplyDefaultSuggestionForExtensionKeyword( | 78 extensions::ApplyDefaultSuggestionForExtensionKeyword( |
78 profile(), template_url, remaining_input, &matches()->front()); | 79 profile_, template_url, remaining_input, &matches()->front()); |
79 | 80 |
80 if (minimal_changes) { | 81 if (minimal_changes) { |
81 // If the input hasn't significantly changed, we can just use the | 82 // If the input hasn't significantly changed, we can just use the |
82 // suggestions from last time. We need to readjust the relevance to | 83 // suggestions from last time. We need to readjust the relevance to |
83 // ensure it is less than the main match's relevance. | 84 // ensure it is less than the main match's relevance. |
84 for (size_t i = 0; i < extension_suggest_matches_.size(); ++i) { | 85 for (size_t i = 0; i < extension_suggest_matches_.size(); ++i) { |
85 matches()->push_back(extension_suggest_matches_[i]); | 86 matches()->push_back(extension_suggest_matches_[i]); |
86 matches()->back().relevance = matches()->front().relevance - (i + 1); | 87 matches()->back().relevance = matches()->front().relevance - (i + 1); |
87 } | 88 } |
88 } else if (input.want_asynchronous_matches()) { | 89 } else if (input.want_asynchronous_matches()) { |
89 extension_suggest_last_input_ = input; | 90 extension_suggest_last_input_ = input; |
90 extension_suggest_matches_.clear(); | 91 extension_suggest_matches_.clear(); |
91 | 92 |
92 // We only have to wait for suggest results if there are actually | 93 // We only have to wait for suggest results if there are actually |
93 // extensions listening for input changes. | 94 // extensions listening for input changes. |
94 if (extensions::ExtensionOmniboxEventRouter::OnInputChanged( | 95 if (extensions::ExtensionOmniboxEventRouter::OnInputChanged( |
95 profile(), template_url->GetExtensionId(), | 96 profile_, template_url->GetExtensionId(), |
96 base::UTF16ToUTF8(remaining_input), current_input_id_)) | 97 base::UTF16ToUTF8(remaining_input), current_input_id_)) |
97 set_done(false); | 98 set_done(false); |
98 } | 99 } |
99 return input.want_asynchronous_matches(); | 100 return input.want_asynchronous_matches(); |
100 } | 101 } |
101 | 102 |
102 void KeywordExtensionsDelegateImpl::EnterExtensionKeywordMode( | 103 void KeywordExtensionsDelegateImpl::EnterExtensionKeywordMode( |
103 const std::string& extension_id) { | 104 const std::string& extension_id) { |
104 DCHECK(current_keyword_extension_id_.empty()); | 105 DCHECK(current_keyword_extension_id_.empty()); |
105 current_keyword_extension_id_ = extension_id; | 106 current_keyword_extension_id_ = extension_id; |
106 | 107 |
107 extensions::ExtensionOmniboxEventRouter::OnInputStarted( | 108 extensions::ExtensionOmniboxEventRouter::OnInputStarted( |
108 profile(), current_keyword_extension_id_); | 109 profile_, current_keyword_extension_id_); |
109 } | 110 } |
110 | 111 |
111 void KeywordExtensionsDelegateImpl::MaybeEndExtensionKeywordMode() { | 112 void KeywordExtensionsDelegateImpl::MaybeEndExtensionKeywordMode() { |
112 if (!current_keyword_extension_id_.empty()) { | 113 if (!current_keyword_extension_id_.empty()) { |
113 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( | 114 extensions::ExtensionOmniboxEventRouter::OnInputCancelled( |
114 profile(), current_keyword_extension_id_); | 115 profile_, current_keyword_extension_id_); |
115 current_keyword_extension_id_.clear(); | 116 current_keyword_extension_id_.clear(); |
116 } | 117 } |
117 } | 118 } |
118 | 119 |
119 void KeywordExtensionsDelegateImpl::Observe( | 120 void KeywordExtensionsDelegateImpl::Observe( |
120 int type, | 121 int type, |
121 const content::NotificationSource& source, | 122 const content::NotificationSource& source, |
122 const content::NotificationDetails& details) { | 123 const content::NotificationDetails& details) { |
123 TemplateURLService* model = provider_->GetTemplateURLService(); | 124 TemplateURLService* model = provider_->GetTemplateURLService(); |
124 const AutocompleteInput& input = extension_suggest_last_input_; | 125 const AutocompleteInput& input = extension_suggest_last_input_; |
(...skipping 13 matching lines...) Expand all Loading... |
138 // session. | 139 // session. |
139 base::string16 keyword, remaining_input; | 140 base::string16 keyword, remaining_input; |
140 if (matches()->empty() || current_keyword_extension_id_.empty() || | 141 if (matches()->empty() || current_keyword_extension_id_.empty() || |
141 !KeywordProvider::ExtractKeywordFromInput( | 142 !KeywordProvider::ExtractKeywordFromInput( |
142 input, &keyword, &remaining_input)) | 143 input, &keyword, &remaining_input)) |
143 return; | 144 return; |
144 | 145 |
145 const TemplateURL* template_url( | 146 const TemplateURL* template_url( |
146 model->GetTemplateURLForKeyword(keyword)); | 147 model->GetTemplateURLForKeyword(keyword)); |
147 extensions::ApplyDefaultSuggestionForExtensionKeyword( | 148 extensions::ApplyDefaultSuggestionForExtensionKeyword( |
148 profile(), template_url, remaining_input, &matches()->front()); | 149 profile_, template_url, remaining_input, &matches()->front()); |
149 OnProviderUpdate(true); | 150 OnProviderUpdate(true); |
150 return; | 151 return; |
151 } | 152 } |
152 | 153 |
153 case extensions::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY: { | 154 case extensions::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY: { |
154 const omnibox_api::SendSuggestions::Params& suggestions = | 155 const omnibox_api::SendSuggestions::Params& suggestions = |
155 *content::Details< | 156 *content::Details< |
156 omnibox_api::SendSuggestions::Params>(details).ptr(); | 157 omnibox_api::SendSuggestions::Params>(details).ptr(); |
157 if (suggestions.request_id != current_input_id_) | 158 if (suggestions.request_id != current_input_id_) |
158 return; // This is an old result. Just ignore. | 159 return; // This is an old result. Just ignore. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 | 204 |
204 default: | 205 default: |
205 NOTREACHED(); | 206 NOTREACHED(); |
206 return; | 207 return; |
207 } | 208 } |
208 } | 209 } |
209 | 210 |
210 void KeywordExtensionsDelegateImpl::OnProviderUpdate(bool updated_matches) { | 211 void KeywordExtensionsDelegateImpl::OnProviderUpdate(bool updated_matches) { |
211 provider_->listener_->OnProviderUpdate(updated_matches); | 212 provider_->listener_->OnProviderUpdate(updated_matches); |
212 } | 213 } |
OLD | NEW |