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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
// 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_sidebar_api.h"
#include "base/json/json_writer.h"
#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/string16.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_event_router.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/extensions/extension_tabs_module.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/sidebar/sidebar_container.h"
#include "chrome/browser/sidebar/sidebar_manager.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_error_utils.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"
#include "ipc/ipc_message_utils.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace {
// Errors.
const char kNoTabError[] = "No tab with id: *.";
const char kInvalidUrlError[] = "Invalid url: \"*\".";
const char kNoCurrentWindowError[] = "No current browser window was found";
const char kNoDefaultTabError[] = "No default tab was found";
const char kInvalidExpandContextError[] =
"Sidebar can be expanded only in response to an explicit user gesture";
// Keys.
const char kBadgeTextKey[] = "text";
const char kImageDataKey[] = "imageData";
const char kStateKey[] = "state";
const char kTabIdKey[] = "tabId";
const char kTitleKey[] = "title";
const char kUrlKey[] = "url";
// Events.
const char kOnStateChanged[] = "experimental.sidebar.onStateChanged";
} // namespace
namespace extension_sidebar_constants {
// Sidebar states.
const char kActiveState[] = "active";
const char kHiddenState[] = "hidden";
const char kShownState[] = "shown";
}
static GURL ResolvePossiblyRelativeURL(const std::string& url_string,
const Extension* extension) {
GURL url = GURL(url_string);
if (!url.is_valid())
url = extension->GetResourceURL(url_string);
return url;
}
static bool CanUseHost(const Extension* extension,
const GURL& url,
std::string* error) {
if (extension->HasHostPermission(url))
return true;
if (error) {
*error = ExtensionErrorUtils::FormatErrorMessage(
extension_manifest_errors::kCannotAccessPage, url.spec());
}
return false;
}
// static
void ExtensionSidebarEventRouter::OnStateChanged(
Profile* profile, TabContents* tab, const std::string& content_id,
const std::string& state) {
int tab_id = ExtensionTabUtil::GetTabId(tab);
DictionaryValue* details = new DictionaryValue;
details->Set(kTabIdKey, Value::CreateIntegerValue(tab_id));
details->Set(kStateKey, Value::CreateStringValue(state));
ListValue args;
args.Set(0, details);
std::string json_args;
base::JSONWriter::Write(&args, false, &json_args);
const std::string& extension_id(content_id);
profile->GetExtensionEventRouter()->DispatchEventToExtension(
extension_id, kOnStateChanged, json_args, profile, GURL());
}
// List is considered empty if it is actually empty or contains just one value,
// either 'null' or 'undefined'.
static bool IsArgumentListEmpty(const ListValue* arguments) {
if (arguments->empty())
return true;
if (arguments->GetSize() == 1) {
Value* first_value = 0;
if (!arguments->Get(0, &first_value))
return true;
if (first_value->GetType() == Value::TYPE_NULL)
return true;
}
return false;
}
bool SidebarFunction::RunImpl() {
if (!args_.get())
return false;
DictionaryValue* details = NULL;
DictionaryValue default_details;
if (IsArgumentListEmpty(args_.get())) {
details = &default_details;
} else {
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
}
int tab_id;
TabContents* tab_contents = NULL;
if (details->HasKey(kTabIdKey)) {
EXTENSION_FUNCTION_VALIDATE(details->GetInteger(kTabIdKey, &tab_id));
if (!ExtensionTabUtil::GetTabById(tab_id, profile(), include_incognito(),
NULL, NULL, &tab_contents, NULL)) {
error_ = ExtensionErrorUtils::FormatErrorMessage(
kNoTabError, base::IntToString(tab_id));
return false;
}
} else {
Browser* browser = GetCurrentBrowser();
if (!browser) {
error_ = kNoCurrentWindowError;
return false;
}
if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) {
error_ = kNoDefaultTabError;
return false;
}
}
if (!tab_contents)
return false;
std::string content_id(GetExtension()->id());
return RunImpl(tab_contents, content_id, *details);
}
bool CollapseSidebarFunction::RunImpl(TabContents* tab,
const std::string& content_id,
const DictionaryValue& details) {
SidebarManager::GetInstance()->CollapseSidebar(tab, content_id);
return true;
}
bool ExpandSidebarFunction::RunImpl(TabContents* tab,
const std::string& content_id,
const DictionaryValue& details) {
// TODO(alekseys): enable this check back when WebKit's user gesture flag
// reporting for extension calls is fixed.
// if (!user_gesture()) {
// error_ = kInvalidExpandContextError;
// return false;
// }
SidebarManager::GetInstance()->ExpandSidebar(tab, content_id);
return true;
}
bool GetStateSidebarFunction::RunImpl(TabContents* tab,
const std::string& content_id,
const DictionaryValue& details) {
SidebarManager* manager = SidebarManager::GetInstance();
const char* result = extension_sidebar_constants::kHiddenState;
if (manager->GetSidebarTabContents(tab, content_id)) {
bool is_active = false;
// Sidebar is considered active only if tab is selected, sidebar UI
// is expanded and this extension's content is displayed on it.
SidebarContainer* active_sidebar =
manager->GetActiveSidebarContainerFor(tab);
// Check if sidebar UI is expanded and this extension's content
// is displayed on it.
if (active_sidebar && active_sidebar->content_id() == content_id) {
if (!details.HasKey(kTabIdKey)) {
is_active = NULL != GetCurrentBrowser();
} else {
int tab_id;
EXTENSION_FUNCTION_VALIDATE(details.GetInteger(kTabIdKey, &tab_id));
// Check if this tab is selected.
Browser* browser = GetCurrentBrowser();
TabContents* contents = NULL;
int default_tab_id = -1;
if (browser &&
ExtensionTabUtil::GetDefaultTab(browser, &contents,
&default_tab_id)) {
is_active = default_tab_id == tab_id;
}
}
}
result = is_active ? extension_sidebar_constants::kActiveState :
extension_sidebar_constants::kShownState;
}
result_.reset(Value::CreateStringValue(result));
return true;
}
bool HideSidebarFunction::RunImpl(TabContents* tab,
const std::string& content_id,
const DictionaryValue& details) {
SidebarManager::GetInstance()->HideSidebar(tab, content_id);
return true;
}
bool NavigateSidebarFunction::RunImpl(TabContents* tab,
const std::string& content_id,
const DictionaryValue& details) {
std::string url_string;
EXTENSION_FUNCTION_VALIDATE(details.GetString(kUrlKey, &url_string));
GURL url = ResolvePossiblyRelativeURL(url_string, GetExtension());
if (!url.is_valid()) {
error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidUrlError,
url_string);
return false;
}
if (!url.SchemeIs(chrome::kExtensionScheme) &&
!CanUseHost(GetExtension(), url, &error_)) {
return false;
}
// Disallow requests outside of the requesting extension view's extension.
if (url.SchemeIs(chrome::kExtensionScheme)) {
std::string extension_id(url.host());
if (extension_id != GetExtension()->id())
return false;
}
SidebarManager::GetInstance()->NavigateSidebar(tab, content_id, GURL(url));
return true;
}
bool SetBadgeTextSidebarFunction::RunImpl(TabContents* tab,
const std::string& content_id,
const DictionaryValue& details) {
string16 badge_text;
EXTENSION_FUNCTION_VALIDATE(details.GetString(kBadgeTextKey, &badge_text));
SidebarManager::GetInstance()->SetSidebarBadgeText(
tab, content_id, badge_text);
return true;
}
bool SetIconSidebarFunction::RunImpl(TabContents* tab,
const std::string& content_id,
const DictionaryValue& details) {
BinaryValue* binary;
EXTENSION_FUNCTION_VALIDATE(details.GetBinary(kImageDataKey, &binary));
IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize());
void* iter = NULL;
scoped_ptr<SkBitmap> bitmap(new SkBitmap);
EXTENSION_FUNCTION_VALIDATE(
IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get()));
SidebarManager::GetInstance()->SetSidebarIcon(tab, content_id, *bitmap);
return true;
}
bool SetTitleSidebarFunction::RunImpl(TabContents* tab,
const std::string& content_id,
const DictionaryValue& details) {
string16 title;
EXTENSION_FUNCTION_VALIDATE(details.GetString(kTitleKey, &title));
SidebarManager::GetInstance()->SetSidebarTitle(tab, content_id, title);
return true;
}
bool ShowSidebarFunction::RunImpl(TabContents* tab,
const std::string& content_id,
const DictionaryValue& details) {
SidebarManager::GetInstance()->ShowSidebar(tab, content_id);
return true;
}
|