| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/extensions/extension_omnibox_api.h" | 5 #include "chrome/browser/extensions/extension_omnibox_api.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/extension_message_service.h" | 10 #include "chrome/browser/extensions/extension_message_service.h" |
| 11 #include "chrome/browser/profile.h" | 11 #include "chrome/browser/profile.h" |
| 12 #include "chrome/common/notification_service.h" | 12 #include "chrome/common/notification_service.h" |
| 13 | 13 |
| 14 namespace events { | 14 namespace events { |
| 15 const char kOnInputChanged[] = "experimental.omnibox.onInputChanged/"; | 15 const char kOnInputStarted[] = "experimental.omnibox.onInputStarted"; |
| 16 const char kOnInputEntered[] = "experimental.omnibox.onInputEntered/"; | 16 const char kOnInputChanged[] = "experimental.omnibox.onInputChanged"; |
| 17 const char kOnInputEntered[] = "experimental.omnibox.onInputEntered"; |
| 18 const char kOnInputCancelled[] = "experimental.omnibox.onInputCancelled"; |
| 17 }; // namespace events | 19 }; // namespace events |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 const char kDescriptionStylesOrderError[] = | 22 const char kDescriptionStylesOrderError[] = |
| 21 "Suggestion descriptionStyles must be in increasing non-overlapping order."; | 23 "Suggestion descriptionStyles must be in increasing non-overlapping order."; |
| 22 const char kDescriptionStylesLengthError[] = | 24 const char kDescriptionStylesLengthError[] = |
| 23 "Suggestion descriptionStyles contains an offset longer than the" | 25 "Suggestion descriptionStyles contains an offset longer than the" |
| 24 " description text"; | 26 " description text"; |
| 25 | 27 |
| 26 const wchar_t kSuggestionContent[] = L"content"; | 28 const wchar_t kSuggestionContent[] = L"content"; |
| 27 const wchar_t kSuggestionDescription[] = L"description"; | 29 const wchar_t kSuggestionDescription[] = L"description"; |
| 28 const wchar_t kSuggestionDescriptionStyles[] = L"descriptionStyles"; | 30 const wchar_t kSuggestionDescriptionStyles[] = L"descriptionStyles"; |
| 29 const wchar_t kDescriptionStylesType[] = L"type"; | 31 const wchar_t kDescriptionStylesType[] = L"type"; |
| 30 const wchar_t kDescriptionStylesOffset[] = L"offset"; | 32 const wchar_t kDescriptionStylesOffset[] = L"offset"; |
| 31 }; // namespace | 33 }; // namespace |
| 32 | 34 |
| 33 // static | 35 // static |
| 36 void ExtensionOmniboxEventRouter::OnInputStarted( |
| 37 Profile* profile, const std::string& extension_id) { |
| 38 profile->GetExtensionMessageService()->DispatchEventToExtension( |
| 39 extension_id, events::kOnInputStarted, "[]", profile->IsOffTheRecord(), |
| 40 GURL()); |
| 41 } |
| 42 |
| 43 // static |
| 34 bool ExtensionOmniboxEventRouter::OnInputChanged( | 44 bool ExtensionOmniboxEventRouter::OnInputChanged( |
| 35 Profile* profile, const std::string& extension_id, | 45 Profile* profile, const std::string& extension_id, |
| 36 const std::string& input, int suggest_id) { | 46 const std::string& input, int suggest_id) { |
| 37 std::string event_name = events::kOnInputChanged + extension_id; | 47 std::string event_name = ExtensionMessageService::GetPerExtensionEventName( |
| 48 events::kOnInputChanged, extension_id); |
| 38 if (!profile->GetExtensionMessageService()->HasEventListener(event_name)) | 49 if (!profile->GetExtensionMessageService()->HasEventListener(event_name)) |
| 39 return false; | 50 return false; |
| 40 | 51 |
| 41 ListValue args; | 52 ListValue args; |
| 42 args.Set(0, Value::CreateStringValue(input)); | 53 args.Set(0, Value::CreateStringValue(input)); |
| 43 args.Set(1, Value::CreateIntegerValue(suggest_id)); | 54 args.Set(1, Value::CreateIntegerValue(suggest_id)); |
| 44 std::string json_args; | 55 std::string json_args; |
| 45 base::JSONWriter::Write(&args, false, &json_args); | 56 base::JSONWriter::Write(&args, false, &json_args); |
| 46 | 57 |
| 47 profile->GetExtensionMessageService()->DispatchEventToRenderers( | 58 profile->GetExtensionMessageService()->DispatchEventToExtension( |
| 48 event_name, json_args, profile->IsOffTheRecord(), GURL()); | 59 extension_id, events::kOnInputChanged, json_args, |
| 60 profile->IsOffTheRecord(), GURL()); |
| 49 return true; | 61 return true; |
| 50 } | 62 } |
| 51 | 63 |
| 52 // static | 64 // static |
| 53 void ExtensionOmniboxEventRouter::OnInputEntered( | 65 void ExtensionOmniboxEventRouter::OnInputEntered( |
| 54 Profile* profile, const std::string& extension_id, | 66 Profile* profile, const std::string& extension_id, |
| 55 const std::string& input) { | 67 const std::string& input) { |
| 56 std::string event_name = events::kOnInputEntered + extension_id; | 68 std::string event_name = events::kOnInputEntered + extension_id; |
| 57 | 69 |
| 58 ListValue args; | 70 ListValue args; |
| 59 args.Set(0, Value::CreateStringValue(input)); | 71 args.Set(0, Value::CreateStringValue(input)); |
| 60 std::string json_args; | 72 std::string json_args; |
| 61 base::JSONWriter::Write(&args, false, &json_args); | 73 base::JSONWriter::Write(&args, false, &json_args); |
| 62 | 74 |
| 63 profile->GetExtensionMessageService()->DispatchEventToRenderers( | 75 profile->GetExtensionMessageService()->DispatchEventToExtension( |
| 64 event_name, json_args, profile->IsOffTheRecord(), GURL()); | 76 extension_id, events::kOnInputEntered, json_args, |
| 77 profile->IsOffTheRecord(), GURL()); |
| 78 |
| 79 NotificationService::current()->Notify( |
| 80 NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED, |
| 81 Source<Profile>(profile), NotificationService::NoDetails()); |
| 82 } |
| 83 |
| 84 // static |
| 85 void ExtensionOmniboxEventRouter::OnInputCancelled( |
| 86 Profile* profile, const std::string& extension_id) { |
| 87 profile->GetExtensionMessageService()->DispatchEventToExtension( |
| 88 extension_id, events::kOnInputCancelled, "[]", profile->IsOffTheRecord(), |
| 89 GURL()); |
| 65 } | 90 } |
| 66 | 91 |
| 67 bool OmniboxSendSuggestionsFunction::RunImpl() { | 92 bool OmniboxSendSuggestionsFunction::RunImpl() { |
| 68 ExtensionOmniboxSuggestions suggestions; | 93 ExtensionOmniboxSuggestions suggestions; |
| 69 ListValue* suggestions_value; | 94 ListValue* suggestions_value; |
| 70 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &suggestions.request_id)); | 95 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &suggestions.request_id)); |
| 71 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &suggestions_value)); | 96 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &suggestions_value)); |
| 72 | 97 |
| 73 suggestions.suggestions.resize(suggestions_value->GetSize()); | 98 suggestions.suggestions.resize(suggestions_value->GetSize()); |
| 74 for (size_t i = 0; i < suggestions_value->GetSize(); ++i) { | 99 for (size_t i = 0; i < suggestions_value->GetSize(); ++i) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 } | 154 } |
| 130 } | 155 } |
| 131 | 156 |
| 132 NotificationService::current()->Notify( | 157 NotificationService::current()->Notify( |
| 133 NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY, | 158 NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY, |
| 134 Source<Profile>(profile_), | 159 Source<Profile>(profile_), |
| 135 Details<ExtensionOmniboxSuggestions>(&suggestions)); | 160 Details<ExtensionOmniboxSuggestions>(&suggestions)); |
| 136 | 161 |
| 137 return true; | 162 return true; |
| 138 } | 163 } |
| OLD | NEW |