summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_omnibox_api.cc
blob: 2f3f8f1061c9244c379bcd4059136f560711c164 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extension_omnibox_api.h"

#include "base/json/json_writer.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_message_service.h"
#include "chrome/browser/profile.h"
#include "chrome/common/notification_service.h"

namespace events {
const char kOnInputStarted[] = "experimental.omnibox.onInputStarted";
const char kOnInputChanged[] = "experimental.omnibox.onInputChanged";
const char kOnInputEntered[] = "experimental.omnibox.onInputEntered";
const char kOnInputCancelled[] = "experimental.omnibox.onInputCancelled";
};  // namespace events

namespace {
const char kDescriptionStylesOrderError[] =
    "Suggestion descriptionStyles must be in increasing non-overlapping order.";
const char kDescriptionStylesLengthError[] =
    "Suggestion descriptionStyles contains an offset longer than the"
    " description text";

const wchar_t kSuggestionContent[] = L"content";
const wchar_t kSuggestionDescription[] = L"description";
const wchar_t kSuggestionDescriptionStyles[] = L"descriptionStyles";
const wchar_t kDescriptionStylesType[] = L"type";
const wchar_t kDescriptionStylesOffset[] = L"offset";
};  // namespace

// static
void ExtensionOmniboxEventRouter::OnInputStarted(
    Profile* profile, const std::string& extension_id) {
  profile->GetExtensionMessageService()->DispatchEventToExtension(
      extension_id, events::kOnInputStarted, "[]", profile->IsOffTheRecord(),
      GURL());
}

// static
bool ExtensionOmniboxEventRouter::OnInputChanged(
    Profile* profile, const std::string& extension_id,
    const std::string& input, int suggest_id) {
  std::string event_name = ExtensionMessageService::GetPerExtensionEventName(
      events::kOnInputChanged, extension_id);
  if (!profile->GetExtensionMessageService()->HasEventListener(event_name))
    return false;

  ListValue args;
  args.Set(0, Value::CreateStringValue(input));
  args.Set(1, Value::CreateIntegerValue(suggest_id));
  std::string json_args;
  base::JSONWriter::Write(&args, false, &json_args);

  profile->GetExtensionMessageService()->DispatchEventToExtension(
      extension_id, events::kOnInputChanged, json_args,
      profile->IsOffTheRecord(), GURL());
  return true;
}

// static
void ExtensionOmniboxEventRouter::OnInputEntered(
    Profile* profile, const std::string& extension_id,
    const std::string& input) {
  std::string event_name = events::kOnInputEntered + extension_id;

  ListValue args;
  args.Set(0, Value::CreateStringValue(input));
  std::string json_args;
  base::JSONWriter::Write(&args, false, &json_args);

  profile->GetExtensionMessageService()->DispatchEventToExtension(
      extension_id, events::kOnInputEntered, json_args,
      profile->IsOffTheRecord(), GURL());

  NotificationService::current()->Notify(
      NotificationType::EXTENSION_OMNIBOX_INPUT_ENTERED,
      Source<Profile>(profile), NotificationService::NoDetails());
}

// static
void ExtensionOmniboxEventRouter::OnInputCancelled(
    Profile* profile, const std::string& extension_id) {
  profile->GetExtensionMessageService()->DispatchEventToExtension(
      extension_id, events::kOnInputCancelled, "[]", profile->IsOffTheRecord(),
      GURL());
}

bool OmniboxSendSuggestionsFunction::RunImpl() {
  ExtensionOmniboxSuggestions suggestions;
  ListValue* suggestions_value;
  EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &suggestions.request_id));
  EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &suggestions_value));

  suggestions.suggestions.resize(suggestions_value->GetSize());
  for (size_t i = 0; i < suggestions_value->GetSize(); ++i) {
    ExtensionOmniboxSuggestion& suggestion = suggestions.suggestions[i];
    DictionaryValue* suggestion_value;
    EXTENSION_FUNCTION_VALIDATE(suggestions_value->GetDictionary(
        i, &suggestion_value));
    EXTENSION_FUNCTION_VALIDATE(suggestion_value->GetStringAsUTF16(
        kSuggestionContent, &suggestion.content));
    EXTENSION_FUNCTION_VALIDATE(suggestion_value->GetStringAsUTF16(
        kSuggestionDescription, &suggestion.description));

    if (suggestion_value->HasKey(kSuggestionDescriptionStyles)) {
      ListValue* styles;
      EXTENSION_FUNCTION_VALIDATE(
          suggestion_value->GetList(kSuggestionDescriptionStyles, &styles));

      suggestion.description_styles.clear();

      int last_offset = -1;
      for (size_t j = 0; j < styles->GetSize(); ++j) {
        DictionaryValue* style;
        std::string type;
        int offset;
        EXTENSION_FUNCTION_VALIDATE(styles->GetDictionary(j, &style));
        EXTENSION_FUNCTION_VALIDATE(
            style->GetString(kDescriptionStylesType, &type));
        EXTENSION_FUNCTION_VALIDATE(
            style->GetInteger(kDescriptionStylesOffset, &offset));

        int type_class =
            (type == "none") ? ACMatchClassification::NONE :
            (type == "match") ? ACMatchClassification::MATCH :
            (type == "dim") ? ACMatchClassification::DIM : -1;
        EXTENSION_FUNCTION_VALIDATE(type_class != -1);

        if (offset <= last_offset) {
          error_ = kDescriptionStylesOrderError;
          return false;
        }
        if (static_cast<size_t>(offset) >= suggestion.description.length()) {
          error_ = kDescriptionStylesLengthError;
          return false;
        }

        suggestion.description_styles.push_back(
            ACMatchClassification(offset, type_class));
        last_offset = offset;
      }
    }

    // Ensure the styles cover the whole range of text.
    if (suggestion.description_styles.empty() ||
        suggestion.description_styles[0].offset != 0) {
      suggestion.description_styles.insert(
          suggestion.description_styles.begin(),
          ACMatchClassification(0, ACMatchClassification::NONE));
    }
  }

  NotificationService::current()->Notify(
      NotificationType::EXTENSION_OMNIBOX_SUGGESTIONS_READY,
      Source<Profile>(profile_),
      Details<ExtensionOmniboxSuggestions>(&suggestions));

  return true;
}