|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 #include "chrome/browser/extensions/extension_offscreen_tabs_module.h" | |
6 | |
7 #include <algorithm> | |
8 #include <vector> | |
9 | |
10 #include "base/base64.h" | |
11 #include "base/json/json_writer.h" | |
12 #include "base/memory/ref_counted_memory.h" | |
13 #include "base/stl_util.h" | |
14 #include "base/string_number_conversions.h" | |
15 #include "base/string_util.h" | |
16 #include "base/values.h" | |
17 #include "chrome/browser/extensions/extension_event_router.h" | |
18 #include "chrome/browser/extensions/extension_function_dispatcher.h" | |
19 #include "chrome/browser/extensions/extension_host.h" | |
20 #include "chrome/browser/extensions/extension_message_service.h" | |
21 #include "chrome/browser/extensions/extension_offscreen_tabs_module_constants.h" | |
22 #include "chrome/browser/extensions/extension_service.h" | |
23 #include "chrome/browser/extensions/extension_tabs_module.h" | |
24 #include "chrome/browser/net/url_fixer_upper.h" | |
25 #include "chrome/browser/profiles/profile.h" | |
26 #include "chrome/browser/ui/browser.h" | |
27 #include "chrome/browser/ui/browser_navigator.h" | |
28 #include "chrome/browser/ui/browser_window.h" | |
29 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
30 #include "chrome/browser/ui/window_sizer.h" | |
31 #include "chrome/common/chrome_notification_types.h" | |
32 #include "chrome/common/extensions/extension.h" | |
33 #include "chrome/common/extensions/extension_error_utils.h" | |
34 #include "chrome/common/extensions/extension_messages.h" | |
35 #include "chrome/common/pref_names.h" | |
36 #include "chrome/common/url_constants.h" | |
37 #include "content/browser/renderer_host/backing_store.h" | |
38 #include "content/browser/renderer_host/render_view_host.h" | |
39 #include "content/browser/renderer_host/render_view_host_delegate.h" | |
40 #include "content/browser/tab_contents/navigation_entry.h" | |
41 #include "content/browser/tab_contents/tab_contents.h" | |
42 #include "content/browser/tab_contents/tab_contents_view.h" | |
43 #include "content/common/content_client.h" | |
44 #include "content/common/notification_service.h" | |
45 #include "skia/ext/image_operations.h" | |
46 #include "skia/ext/platform_canvas.h" | |
47 #include "third_party/skia/include/core/SkBitmap.h" | |
48 #include "ui/gfx/codec/jpeg_codec.h" | |
49 #include "ui/gfx/codec/png_codec.h" | |
50 | |
51 using WebKit::WebInputEvent; | |
52 | |
53 namespace keys = extension_offscreen_tabs_module_constants; | |
54 | |
55 namespace { | |
56 | |
57 class Tab; | |
58 | |
59 // Offscreen Tab --------------------------------------------------------------- | |
60 | |
61 // This class is responsible for the creation and destruction of offscreen tabs, | |
62 // as well as dispatching an onUpdated event. | |
63 class OffscreenTab : public NotificationObserver { | |
sky
2011/09/16 16:29:21
You have way too many things named tab in this fil
alexbost
2011/09/16 20:18:14
I completely agree :) I was trying to keep names s
| |
64 public: | |
65 OffscreenTab(); | |
66 virtual ~OffscreenTab(); | |
67 void Init(const GURL& url, | |
68 const int width, | |
69 const int height, | |
70 Profile* profile); | |
71 | |
72 TabContents* contents() { return tab_.get()->tab_contents(); } | |
73 Tab* parent_tab() { return parent_tab_; } | |
74 DictionaryValue* CreateValue(); // The caller owns the returned value. | |
sky
2011/09/16 16:29:21
Document what this does too. Write comments assumi
alexbost
2011/09/16 20:18:14
Done.
| |
75 | |
76 void SetURL(const GURL& url); | |
77 void SetSize(int width, int height); | |
78 void SetParentTab(Tab* tab); | |
79 | |
80 private: | |
81 virtual void Observe(int type, | |
82 const NotificationSource& source, | |
83 const NotificationDetails& details) OVERRIDE; | |
84 | |
85 NotificationRegistrar registrar_; | |
86 | |
87 scoped_ptr<TabContentsWrapper> tab_; // TabContentsWrapper associated with | |
88 // this offscreen tab. | |
89 Tab* parent_tab_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(OffscreenTab); | |
92 }; | |
93 | |
94 typedef std::vector<OffscreenTab*> TabVector; | |
95 typedef TabVector::iterator TabIterator; | |
96 | |
97 // Tab ------------------------------------------------------------------------- | |
98 | |
99 // Holds info about a tab that has spawned at least one offscreen tab. | |
100 // Each Tab keeps track of its child offscreen tabs. The Tab is also responsible | |
101 // for killing its children when it navigates away or gets closed. | |
102 class Tab : public NotificationObserver { | |
103 public: | |
104 Tab(); | |
105 virtual ~Tab(); | |
106 void Init(TabContents* tab_contents, const std::string& extension_id); | |
107 | |
108 TabContents* contents() { return tab_->tab_contents(); } | |
109 const TabVector& offscreen_tabs() { return offscreen_tabs_; } | |
110 const std::string& extension_id() const { return extension_id_; } | |
111 | |
112 // Tab takes ownership of OffscreenTab. | |
113 void AddOffscreenTab(OffscreenTab *tab); | |
114 // Caller assumes ownership of OffscreenTab. | |
115 void RemoveOffscreenTab(OffscreenTab *tab); | |
116 | |
117 private: | |
118 virtual void Observe(int type, | |
119 const NotificationSource& source, | |
120 const NotificationDetails& details) OVERRIDE; | |
121 | |
122 NotificationRegistrar registrar_; | |
123 | |
124 TabContentsWrapper* tab_; // TabContentsWrapper associated with this tab. | |
125 TabVector offscreen_tabs_; // Offscreen tabs spawned by this tab. | |
126 std::string extension_id_; // Id of the extension running in this tab. | |
127 | |
128 DISALLOW_COPY_AND_ASSIGN(Tab); | |
129 }; | |
130 | |
131 // Map ------------------------------------------------------------------------- | |
132 | |
133 // This map keeps track of all tabs that are happy parents of offscreen tabs. | |
134 class Map { | |
135 public: | |
136 Map(); | |
137 ~Map(); | |
138 | |
139 // Gets an offscreen tab by ID. | |
140 bool GetOffscreenTab(const int tab_id, | |
141 ExtensionFunctionDispatcher* dispatcher, | |
142 Profile* profile, | |
143 OffscreenTab** offscreen_tab, | |
144 std::string* error_message); | |
145 // Gets a parent tab by ID. | |
146 bool GetTab(const int tab_id, | |
147 Tab** tab, | |
148 std::string* error_message); | |
149 // Creates a mapping between a parent tab and an offscreen tab. | |
150 bool AddOffscreenTab(OffscreenTab* offscreen_tab, | |
151 ExtensionFunctionDispatcher* dispatcher, | |
152 Profile* profile, | |
153 const std::string& ext_id, | |
154 std::string* error_message); | |
155 // Removes the mapping between a parent tab and an offscreen tab. | |
156 // May cause the Tab object associated with the parent to be deleted. | |
157 bool RemoveOffscreenTab(const int tab_id, | |
158 ExtensionFunctionDispatcher* dispatcher, | |
159 Profile* profile, | |
160 std::string* error_message); | |
161 // Removes a parent tab from the map along with its child offscreen tabs. | |
162 bool RemoveTab(const int tab_id, std::string* error_message); | |
sky
2011/09/16 16:29:21
Document ownership.
alexbost
2011/09/16 20:18:14
Done.
| |
163 | |
164 private: | |
165 typedef base::hash_map<int, Tab*> TabMap; | |
166 TabMap map; | |
167 | |
168 DISALLOW_COPY_AND_ASSIGN(Map); | |
169 }; | |
170 | |
171 // Variables ------------------------------------------------------------------- | |
172 | |
173 Map* map = NULL; | |
174 | |
175 // We are assuming that offscreen tabs will not be created by background pages | |
176 // with the exception of the API test background page. We keep track of the | |
177 // offscreen tabs associated with the test API background page via this variable | |
178 // These tab contents are created just for convenience and do not do anything. | |
179 // TODO(alexbost): Think about handling multiple background pages each spawning | |
180 // offscreen tabs. Would background pages want to spawn offscreen tabs? | |
181 TabContents* background_page_tab_contents = NULL; | |
182 | |
183 // Util ------------------------------------------------------------------------ | |
184 | |
185 // Gets the map of parent tabs to offscreen tabs. | |
186 Map* GetMap() { | |
187 if (map == NULL) | |
188 map = new Map(); | |
189 | |
190 return map; | |
191 } | |
192 | |
193 // Gets the TabContents associated with the test API background page. | |
194 TabContents* GetBackgroundPageTabContents(Profile* profile) { | |
195 if (background_page_tab_contents == NULL) { | |
196 background_page_tab_contents = | |
197 new TabContents(profile, NULL, MSG_ROUTING_NONE, NULL, NULL); | |
198 new TabContentsWrapper(background_page_tab_contents); | |
199 } | |
200 | |
201 return background_page_tab_contents; | |
202 } | |
203 | |
204 // Gets the contents of the tab that instantiated the extension API call. | |
205 // In the case of background pages we use tab contents created by us. | |
206 bool GetCurrentTab(ExtensionFunctionDispatcher* dispatcher, | |
sky
2011/09/16 16:29:21
Name GetCurrentTabContents to match GetBackgroundP
alexbost
2011/09/16 20:18:14
Done.
| |
207 Profile* profile, | |
208 TabContents** tab_contents, | |
209 std::string* error_message) { | |
210 *tab_contents = dispatcher->delegate()->GetAssociatedTabContents(); | |
211 | |
212 // Background page (no associated tab contents). | |
213 if (!*tab_contents) | |
214 *tab_contents = GetBackgroundPageTabContents(profile); | |
sky
2011/09/16 16:29:21
Are we sure this is only going to get hit for test
alexbost
2011/09/16 20:18:14
It does get hit because the test runs in a backgro
sky
2011/09/16 20:23:28
What prevents a non-test background page from doin
alexbost
2011/09/16 21:46:53
Nothing right now. How can I distinguish between t
sky
2011/09/16 21:51:45
I don't know enough about background extensions an
| |
215 | |
216 if (*tab_contents) | |
217 return true; | |
218 | |
219 *error_message = keys::kCurrentTabNotFound; | |
220 return false; | |
221 } | |
222 | |
223 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
224 // Takes |url_string| and returns a GURL which is either valid and absolute | |
225 // or invalid. If |url_string| is not directly interpretable as a valid (it is | |
226 // likely a relative URL) an attempt is made to resolve it. |extension| is | |
227 // provided so it can be resolved relative to its extension base | |
228 // (chrome-extension://<id>/). Using the source frame url would be more correct, | |
229 // but because the api shipped with urls resolved relative to their extension | |
230 // base, we decided it wasn't worth breaking existing extensions to fix. | |
231 GURL ResolvePossiblyRelativeURL(const std::string& url_string, | |
232 const Extension* extension) { | |
233 GURL url = GURL(url_string); | |
234 if (!url.is_valid()) | |
235 url = extension->GetResourceURL(url_string); | |
236 | |
237 return url; | |
238 } | |
239 | |
240 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
241 bool IsCrashURL(const GURL& url) { | |
242 // Check a fixed-up URL, to normalize the scheme and parse hosts correctly. | |
243 GURL fixed_url = | |
244 URLFixerUpper::FixupURL(url.possibly_invalid_spec(), std::string()); | |
245 return (fixed_url.SchemeIs(chrome::kChromeUIScheme) && | |
246 (fixed_url.host() == chrome::kChromeUIBrowserCrashHost || | |
247 fixed_url.host() == chrome::kChromeUICrashHost)); | |
248 } | |
249 | |
250 // Offscreen Tab --------------------------------------------------------------- | |
251 | |
252 OffscreenTab::OffscreenTab() {} | |
sky
2011/09/16 16:29:21
member initialize parent_tab_.
alexbost
2011/09/16 20:18:14
Done.
| |
253 OffscreenTab::~OffscreenTab() {} | |
254 | |
255 void OffscreenTab::Init(const GURL& url, | |
256 const int width, | |
257 const int height, | |
258 Profile* profile) { | |
259 // Create the offscreen tab. | |
260 TabContents* tab_contents = new TabContents( | |
261 profile, NULL, MSG_ROUTING_NONE, NULL, NULL); | |
262 tab_.reset(new TabContentsWrapper(tab_contents)); | |
263 | |
264 SetSize(width, height); // Setting the size starts the renderer. | |
265 SetURL(url); | |
266 | |
267 // Register for tab notifications. | |
268 registrar_.Add(this, | |
269 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
270 Source<NavigationController>(&contents()->controller())); | |
271 } | |
272 | |
273 DictionaryValue* OffscreenTab::CreateValue() { | |
274 DictionaryValue* result = new DictionaryValue(); | |
275 | |
276 result->SetInteger(keys::kIdKey, ExtensionTabUtil::GetTabId(contents())); | |
277 result->SetString(keys::kUrlKey, contents()->GetURL().spec()); | |
278 result->SetInteger(keys::kWidthKey, | |
279 contents()->view()->GetContainerSize().width()); | |
280 result->SetInteger(keys::kHeightKey, | |
281 contents()->view()->GetContainerSize().height()); | |
282 | |
283 return result; | |
284 } | |
285 | |
286 void OffscreenTab::SetURL(const GURL& url) { | |
287 contents()->controller().LoadURL( | |
288 url, GURL(), PageTransition::LINK, std::string()); | |
289 } | |
290 | |
291 void OffscreenTab::SetSize(int width, int height) { | |
292 contents()->view()->SizeContents(gfx::Size(width, height)); | |
293 } | |
294 | |
295 void OffscreenTab::SetParentTab(Tab* tab) { | |
sky
2011/09/16 16:29:21
Change this to set_parent_tab and inline it (like
alexbost
2011/09/16 20:18:14
Done.
| |
296 parent_tab_ = tab; | |
297 } | |
298 | |
299 void OffscreenTab::Observe(int type, | |
300 const NotificationSource& source, | |
301 const NotificationDetails& details) { | |
302 CHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | |
303 | |
304 DictionaryValue* changed_properties = new DictionaryValue(); | |
305 changed_properties->SetString(keys::kUrlKey, contents()->GetURL().spec()); | |
306 | |
307 ListValue args; | |
308 args.Append( | |
309 Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents()))); | |
310 args.Append(changed_properties); | |
311 args.Append(CreateValue()); | |
312 std::string json_args; | |
313 base::JSONWriter::Write(&args, false, &json_args); | |
314 | |
315 ListValue event_args; | |
316 event_args.Set(0, Value::CreateStringValue(keys::kEventOnUpdated)); | |
317 event_args.Set(1, Value::CreateStringValue(json_args)); | |
318 | |
319 // Dispatch an onUpdated event. | |
320 // The primary use case for broadcasting the event is | |
321 // when the offscreen tab is generated by a test API background page. | |
322 if (parent_tab_->contents() == background_page_tab_contents) { | |
323 Profile* profile = TabContentsWrapper::GetCurrentWrapperForContents( | |
324 parent_tab_->contents())->profile(); | |
325 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | |
326 keys::kEventOnUpdated, json_args, profile, GURL()); | |
327 } else { | |
328 // Send a routed event directly to the parent tab. | |
329 parent_tab_->contents()->render_view_host()->process()->Send( | |
sky
2011/09/16 16:29:21
I'm not sure what happens if the tab crashes. The
alexbost
2011/09/16 20:18:14
Done.
| |
330 new ExtensionMsg_MessageInvoke( | |
331 parent_tab_->contents()->render_view_host()->routing_id(), | |
332 parent_tab_->extension_id(), | |
333 keys::kDispatchEvent, | |
334 event_args, | |
335 GURL())); | |
336 } | |
337 } | |
338 | |
339 // Tab ------------------------------------------------------------------------- | |
340 | |
341 Tab::Tab() { | |
342 tab_ = NULL; | |
sky
2011/09/16 16:29:21
Use member initializer list.
alexbost
2011/09/16 20:18:14
Done.
| |
343 } | |
344 | |
345 Tab::~Tab() { | |
346 // Kill child offscreen tabs. | |
347 STLDeleteElements(&offscreen_tabs_); | |
348 | |
349 bool removed = GetMap()->RemoveTab( | |
350 ExtensionTabUtil::GetTabId(contents()), new std::string()); | |
351 CHECK(removed); | |
352 } | |
353 | |
354 void Tab::Init(TabContents* tab_contents, const std::string& extension_id) { | |
355 CHECK(tab_contents); | |
356 | |
357 tab_ = TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); | |
358 CHECK(tab_); | |
359 | |
360 extension_id_ = extension_id; | |
361 | |
362 // Register for tab notifications. | |
363 registrar_.Add(this, | |
364 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
365 Source<NavigationController>(&contents()->controller())); | |
366 | |
367 registrar_.Add(this, | |
368 content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | |
369 Source<TabContents>(contents())); | |
370 } | |
371 | |
372 void Tab::AddOffscreenTab(OffscreenTab *tab) { | |
373 offscreen_tabs_.push_back(tab); | |
374 } | |
375 | |
376 void Tab::RemoveOffscreenTab(OffscreenTab *tab) { | |
377 TabIterator it_tab = std::find( | |
378 offscreen_tabs_.begin(), offscreen_tabs_.end(), tab); | |
379 offscreen_tabs_.erase(it_tab); | |
380 } | |
381 | |
382 void Tab::Observe(int type, | |
383 const NotificationSource& source, | |
384 const NotificationDetails& details) { | |
385 CHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED || | |
386 type == content::NOTIFICATION_TAB_CONTENTS_DESTROYED); | |
387 | |
388 delete this; | |
389 } | |
390 | |
391 // Map ------------------------------------------------------------------------- | |
392 | |
393 Map::Map() {} | |
394 Map::~Map() {} | |
395 | |
396 bool Map::GetOffscreenTab(const int tab_id, | |
397 ExtensionFunctionDispatcher* dispatcher, | |
398 Profile* profile, | |
399 OffscreenTab** offscreen_tab, | |
400 std::string* error_message) { | |
401 // Ensure that the current tab is the parent of the offscreen tab. | |
402 TabContents* tab_contents = NULL; | |
403 if (!GetCurrentTab(dispatcher, profile, &tab_contents, error_message)) | |
404 return false; | |
405 | |
406 Tab* tab = NULL; | |
407 if (!GetTab(ExtensionTabUtil::GetTabId(tab_contents), &tab, error_message)) | |
408 return false; | |
409 TabVector offscreen_tabs = tab->offscreen_tabs(); | |
410 | |
411 for (TabIterator it = offscreen_tabs.begin(); | |
412 it != offscreen_tabs.end(); ++it) { | |
413 if (ExtensionTabUtil::GetTabId((*it)->contents()) == tab_id) { | |
414 *offscreen_tab = *it; | |
415 return true; | |
416 } | |
417 } | |
418 | |
419 *error_message = ExtensionErrorUtils::FormatErrorMessage( | |
420 keys::kOffscreenTabNotFoundError, base::IntToString(tab_id)); | |
421 return false; | |
422 } | |
423 | |
424 bool Map::GetTab(const int tab_id, Tab** tab, std::string* error_message) { | |
425 TabMap::iterator it = map.find(tab_id); | |
426 | |
427 if (it == map.end()) { | |
428 *error_message = ExtensionErrorUtils::FormatErrorMessage( | |
429 keys::kTabNotFoundError, base::IntToString(tab_id)); | |
430 return false; | |
431 } | |
432 | |
433 *tab = it->second; | |
434 | |
435 return true; | |
436 } | |
437 | |
438 bool Map::AddOffscreenTab(OffscreenTab* offscreen_tab, | |
439 ExtensionFunctionDispatcher* dispatcher, | |
440 Profile* profile, | |
441 const std::string& ext_id, | |
442 std::string* error_message) { | |
443 // Get parent tab. | |
444 TabContents* tab_contents = NULL; | |
445 if (!GetCurrentTab(dispatcher, profile, &tab_contents, error_message)) | |
446 return false; | |
447 int tab_id = ExtensionTabUtil::GetTabId(tab_contents); | |
448 | |
449 Tab* tab = NULL; | |
450 if (!GetTab(tab_id, &tab, error_message)) { | |
451 tab = map[tab_id] = new Tab(); | |
452 tab->Init(tab_contents, ext_id); | |
453 } | |
454 | |
455 // Add child to parent. | |
456 tab->AddOffscreenTab(offscreen_tab); | |
457 // Add parent to child. | |
458 offscreen_tab->SetParentTab(tab); | |
sky
2011/09/16 16:29:21
Shouldn't the parent be passed to Init? Otherwise
alexbost
2011/09/16 20:18:14
Done.
| |
459 | |
460 return true; | |
461 } | |
462 | |
463 bool Map::RemoveOffscreenTab(const int tab_id, | |
464 ExtensionFunctionDispatcher* dispatcher, | |
465 Profile* profile, | |
466 std::string* error_message) { | |
467 OffscreenTab* offscreen_tab = NULL; | |
468 if (!GetOffscreenTab(tab_id, dispatcher, profile, | |
469 &offscreen_tab, error_message)) | |
470 return false; | |
471 | |
472 Tab* tab = offscreen_tab->parent_tab(); | |
473 | |
474 tab->RemoveOffscreenTab(offscreen_tab); | |
475 | |
476 // If this was the last offscreen tab for the parent tab, remove the parent. | |
477 if (tab->offscreen_tabs().empty()) | |
478 delete tab; // Causes tab to be removed from the map! | |
479 | |
480 return true; | |
sky
2011/09/16 16:29:21
Who takes ownership of offscreen_tab?
alexbost
2011/09/16 20:18:14
Done.
| |
481 } | |
482 | |
483 bool Map::RemoveTab(const int tab_id, std::string* error_message) { | |
484 if (map.find(tab_id) == map.end()) { | |
485 *error_message = ExtensionErrorUtils::FormatErrorMessage( | |
486 keys::kTabNotFoundError, base::IntToString(tab_id)); | |
487 return false; | |
488 } | |
489 | |
490 map.erase(tab_id); | |
491 | |
492 return true; | |
493 } | |
494 | |
495 } // namespace | |
496 | |
497 // API functions --------------------------------------------------------------- | |
498 | |
499 // create ---------------------------------------------------------------------- | |
500 | |
501 CreateOffscreenTabFunction::CreateOffscreenTabFunction() {} | |
502 CreateOffscreenTabFunction::~CreateOffscreenTabFunction() {} | |
503 | |
504 bool CreateOffscreenTabFunction::RunImpl() { | |
505 DictionaryValue* create_props; | |
506 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &create_props)); | |
507 | |
508 std::string url_string; | |
509 GURL url; | |
510 EXTENSION_FUNCTION_VALIDATE( | |
511 create_props->GetString(keys::kUrlKey, &url_string)); | |
512 url = ResolvePossiblyRelativeURL(url_string, GetExtension()); | |
513 if (!url.is_valid()) { | |
514 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
515 keys::kInvalidUrlError, url_string); | |
516 return false; | |
517 } | |
518 if (IsCrashURL(url)) { | |
519 error_ = keys::kNoCrashBrowserError; | |
520 return false; | |
521 } | |
522 | |
523 gfx::Rect window_bounds; | |
524 bool maximized; | |
525 if (!create_props->HasKey(keys::kWidthKey) || | |
526 !create_props->HasKey(keys::kHeightKey)) { | |
527 Browser* browser = GetCurrentBrowser(); | |
528 if (!browser) { | |
529 error_ = keys::kNoCurrentWindowError; | |
530 return false; | |
531 } | |
532 | |
533 WindowSizer::GetBrowserWindowBounds(std::string(), gfx::Rect(), | |
534 browser, &window_bounds, | |
535 &maximized); | |
536 } | |
537 | |
538 int width = window_bounds.width(); | |
539 if (create_props->HasKey(keys::kWidthKey)) | |
540 EXTENSION_FUNCTION_VALIDATE( | |
541 create_props->GetInteger(keys::kWidthKey, &width)); | |
542 | |
543 int height = window_bounds.height(); | |
544 if (create_props->HasKey(keys::kHeightKey)) | |
545 EXTENSION_FUNCTION_VALIDATE( | |
546 create_props->GetInteger(keys::kHeightKey, &height)); | |
547 | |
548 OffscreenTab* tab = new OffscreenTab(); | |
549 tab->Init(url, width, height, profile_); | |
550 | |
551 // Add the offscreen tab to the map so we don't lose track of it. | |
552 if (!GetMap()->AddOffscreenTab(tab, dispatcher(), profile_, | |
553 extension_id(), &error_)) { | |
554 delete tab; // Prevent leaking of offscreen tab. | |
555 return false; | |
556 } | |
557 | |
558 if (has_callback()) { | |
559 result_.reset(tab->CreateValue()); | |
560 SendResponse(true); | |
561 } | |
562 | |
563 return true; | |
564 } | |
565 | |
566 // get ------------------------------------------------------------------------- | |
567 | |
568 GetOffscreenTabFunction::GetOffscreenTabFunction() {} | |
569 GetOffscreenTabFunction::~GetOffscreenTabFunction() {} | |
570 | |
571 bool GetOffscreenTabFunction::RunImpl() { | |
572 int tab_id; | |
573 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | |
574 | |
575 OffscreenTab* tab = NULL; | |
576 if (!GetMap()-> | |
577 GetOffscreenTab(tab_id, dispatcher(), profile_, &tab, &error_)) { | |
578 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
579 keys::kOffscreenTabNotFoundError, base::IntToString(tab_id)); | |
580 return false; | |
581 } | |
582 | |
583 result_.reset(tab->CreateValue()); | |
584 SendResponse(true); | |
585 | |
586 return true; | |
587 } | |
588 | |
589 // getAll ---------------------------------------------------------------------- | |
590 | |
591 GetAllOffscreenTabFunction::GetAllOffscreenTabFunction() {} | |
592 GetAllOffscreenTabFunction::~GetAllOffscreenTabFunction() {} | |
593 | |
594 bool GetAllOffscreenTabFunction::RunImpl() { | |
595 TabContents* tab_contents = NULL; | |
596 if (!GetCurrentTab(dispatcher(), profile_, &tab_contents, &error_)) | |
597 return false; | |
598 | |
599 Tab* tab = NULL; | |
600 if (!GetMap()-> | |
601 GetTab(ExtensionTabUtil::GetTabId(tab_contents), &tab, &error_)) | |
602 return false; | |
603 | |
604 TabVector offscreen_tabs = tab->offscreen_tabs(); | |
605 | |
606 ListValue* tab_list = new ListValue(); | |
607 for (TabIterator it_tab = offscreen_tabs.begin(); | |
608 it_tab != offscreen_tabs.end(); ++it_tab) | |
609 tab_list->Append((*it_tab)->CreateValue()); | |
610 | |
611 result_.reset(tab_list); | |
612 SendResponse(true); | |
613 | |
614 return true; | |
615 } | |
616 | |
617 // remove ---------------------------------------------------------------------- | |
618 | |
619 RemoveOffscreenTabFunction::RemoveOffscreenTabFunction() {} | |
620 RemoveOffscreenTabFunction::~RemoveOffscreenTabFunction() {} | |
621 | |
622 bool RemoveOffscreenTabFunction::RunImpl() { | |
623 int tab_id; | |
624 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | |
625 | |
626 OffscreenTab* tab = NULL; | |
627 if (!GetMap()->GetOffscreenTab(tab_id, dispatcher(), profile_, &tab, &error_)) | |
628 return false; | |
629 | |
630 if (!GetMap()->RemoveOffscreenTab(tab_id, dispatcher(), profile_, &error_)) | |
631 return false; | |
632 | |
633 delete tab; | |
634 | |
635 return true; | |
636 } | |
637 | |
638 // sendKeyboardEvent ----------------------------------------------------------- | |
639 | |
640 SendKeyboardEventOffscreenTabFunction:: | |
641 SendKeyboardEventOffscreenTabFunction() {} | |
642 SendKeyboardEventOffscreenTabFunction:: | |
643 ~SendKeyboardEventOffscreenTabFunction() {} | |
644 | |
645 bool SendKeyboardEventOffscreenTabFunction::RunImpl() { | |
646 int tab_id; | |
647 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | |
648 | |
649 OffscreenTab* tab = NULL; | |
650 if (!GetMap()->GetOffscreenTab(tab_id, dispatcher(), profile_, &tab, &error_)) | |
651 return false; | |
652 | |
653 // JavaScript KeyboardEvent. | |
654 DictionaryValue* js_keyboard_event = NULL; | |
655 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &js_keyboard_event)); | |
656 | |
657 NativeWebKeyboardEvent keyboard_event; | |
658 | |
659 std::string type; | |
660 if (js_keyboard_event->HasKey(keys::kKeyboardEventTypeKey)) { | |
661 EXTENSION_FUNCTION_VALIDATE( | |
662 js_keyboard_event->GetString(keys::kKeyboardEventTypeKey, &type)); | |
663 } else { | |
664 error_ = keys::kInvalidKeyboardEventObjectError; | |
665 return false; | |
666 } | |
667 | |
668 if (type.compare(keys::kKeyboardEventTypeValueKeypress) == 0) { | |
669 keyboard_event.type = WebInputEvent::Char; | |
670 } else if (type.compare(keys::kKeyboardEventTypeValueKeydown) == 0) { | |
671 keyboard_event.type = WebInputEvent::KeyDown; | |
672 } else if (type.compare(keys::kKeyboardEventTypeValueKeyup) == 0) { | |
673 keyboard_event.type = WebInputEvent::KeyUp; | |
674 } else { | |
675 error_ = keys::kInvalidKeyboardEventObjectError; | |
676 return false; | |
677 } | |
678 | |
679 int key_code; | |
680 if (js_keyboard_event->HasKey(keys::kKeyboardEventKeyCodeKey)) { | |
681 EXTENSION_FUNCTION_VALIDATE(js_keyboard_event-> | |
682 GetInteger(keys::kKeyboardEventKeyCodeKey, &key_code)); | |
683 } else { | |
684 error_ = keys::kInvalidKeyboardEventObjectError; | |
685 return false; | |
686 } | |
687 | |
688 keyboard_event.nativeKeyCode = key_code; | |
689 keyboard_event.windowsKeyCode = key_code; | |
690 keyboard_event.setKeyIdentifierFromWindowsKeyCode(); | |
691 | |
692 // Keypress = type character | |
693 if (type.compare(keys::kKeyboardEventTypeValueKeypress) == 0) { | |
694 int char_code; | |
695 if (js_keyboard_event->HasKey(keys::kKeyboardEventCharCodeKey)) { | |
696 EXTENSION_FUNCTION_VALIDATE(js_keyboard_event-> | |
697 GetInteger(keys::kKeyboardEventCharCodeKey, &char_code)); | |
698 keyboard_event.text[0] = char_code; | |
699 keyboard_event.unmodifiedText[0] = char_code; | |
700 } else { | |
701 error_ = keys::kInvalidKeyboardEventObjectError; | |
702 return false; | |
703 } | |
704 } | |
705 | |
706 bool alt_key; | |
707 if (js_keyboard_event->HasKey(keys::kKeyboardEventAltKeyKey)) | |
708 EXTENSION_FUNCTION_VALIDATE(js_keyboard_event-> | |
709 GetBoolean(keys::kKeyboardEventAltKeyKey, &alt_key)); | |
710 if (alt_key) | |
711 keyboard_event.modifiers |= WebInputEvent::AltKey; | |
712 | |
713 bool ctrl_key; | |
714 if (js_keyboard_event->HasKey(keys::kKeyboardEventCtrlKeyKey)) | |
715 EXTENSION_FUNCTION_VALIDATE(js_keyboard_event-> | |
716 GetBoolean(keys::kKeyboardEventCtrlKeyKey, &ctrl_key)); | |
717 if (ctrl_key) | |
718 keyboard_event.modifiers |= WebInputEvent::ControlKey; | |
719 | |
720 bool meta_key = false; | |
721 if (js_keyboard_event->HasKey(keys::kMouseEventMetaKeyKey)) | |
722 EXTENSION_FUNCTION_VALIDATE(js_keyboard_event-> | |
723 GetBoolean(keys::kMouseEventMetaKeyKey, &meta_key)); | |
724 if (meta_key) | |
725 keyboard_event.modifiers |= WebInputEvent::MetaKey; | |
726 | |
727 bool shift_key; | |
728 if (js_keyboard_event->HasKey(keys::kKeyboardEventShiftKeyKey)) | |
729 EXTENSION_FUNCTION_VALIDATE(js_keyboard_event-> | |
730 GetBoolean(keys::kKeyboardEventShiftKeyKey, &shift_key)); | |
731 if (shift_key) | |
732 keyboard_event.modifiers |= WebInputEvent::ShiftKey; | |
733 | |
734 // Forward the event. | |
735 tab->contents()->render_view_host()-> | |
736 ForwardKeyboardEvent(keyboard_event); | |
737 | |
738 if (has_callback()) { | |
739 result_.reset(tab->CreateValue()); | |
740 SendResponse(true); | |
741 } | |
742 | |
743 return true; | |
744 } | |
745 | |
746 // sendMouseEvent -------------------------------------------------------------- | |
747 | |
748 SendMouseEventOffscreenTabFunction::SendMouseEventOffscreenTabFunction() {} | |
749 SendMouseEventOffscreenTabFunction::~SendMouseEventOffscreenTabFunction() {} | |
750 | |
751 bool SendMouseEventOffscreenTabFunction::RunImpl() { | |
752 int tab_id; | |
753 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | |
754 | |
755 OffscreenTab* tab = NULL; | |
756 if (!GetMap()->GetOffscreenTab(tab_id, dispatcher(), profile_, &tab, &error_)) | |
757 return false; | |
758 | |
759 // JavaScript MouseEvent. | |
760 DictionaryValue* js_mouse_event = NULL; | |
761 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &js_mouse_event)); | |
762 | |
763 std::string type; | |
764 if (js_mouse_event->HasKey(keys::kMouseEventTypeKey)) { | |
765 EXTENSION_FUNCTION_VALIDATE( | |
766 js_mouse_event->GetString(keys::kMouseEventTypeKey, &type)); | |
767 } else { | |
768 error_ = keys::kInvalidMouseEventObjectError; | |
769 return false; | |
770 } | |
771 | |
772 if (type.compare(keys::kMouseEventTypeValueMousewheel) == 0) { | |
773 WebKit::WebMouseWheelEvent wheel_event; | |
774 | |
775 wheel_event.type = WebInputEvent::MouseWheel; | |
776 | |
777 if (js_mouse_event->HasKey(keys::kMouseEventWheelDeltaXKey) && | |
778 js_mouse_event->HasKey(keys::kMouseEventWheelDeltaYKey)) { | |
779 int delta_x, delta_y; | |
780 EXTENSION_FUNCTION_VALIDATE(js_mouse_event-> | |
781 GetInteger(keys::kMouseEventWheelDeltaXKey, &delta_x)); | |
782 EXTENSION_FUNCTION_VALIDATE(js_mouse_event-> | |
783 GetInteger(keys::kMouseEventWheelDeltaYKey, &delta_y)); | |
784 wheel_event.deltaX = delta_x; | |
785 wheel_event.deltaY = delta_y; | |
786 } else { | |
787 error_ = keys::kInvalidMouseEventObjectError; | |
788 return false; | |
789 } | |
790 | |
791 // Forward the event. | |
792 tab->contents()->render_view_host()->ForwardWheelEvent(wheel_event); | |
793 } else { | |
794 WebKit::WebMouseEvent mouse_event; | |
795 | |
796 if (type.compare(keys::kMouseEventTypeValueMousedown) == 0 || | |
797 type.compare(keys::kMouseEventTypeValueClick) == 0) { | |
798 mouse_event.type = WebKit::WebInputEvent::MouseDown; | |
799 } else if (type.compare(keys::kMouseEventTypeValueMouseup) == 0) { | |
800 mouse_event.type = WebKit::WebInputEvent::MouseUp; | |
801 } else if (type.compare(keys::kMouseEventTypeValueMousemove) == 0) { | |
802 mouse_event.type = WebKit::WebInputEvent::MouseMove; | |
803 } else { | |
804 error_ = keys::kInvalidMouseEventObjectError; | |
805 return false; | |
806 } | |
807 | |
808 int button; | |
809 if (js_mouse_event->HasKey(keys::kMouseEventButtonKey)) { | |
810 EXTENSION_FUNCTION_VALIDATE( | |
811 js_mouse_event->GetInteger(keys::kMouseEventButtonKey, &button)); | |
812 } else { | |
813 error_ = keys::kInvalidMouseEventObjectError; | |
814 return false; | |
815 } | |
816 | |
817 if (button == keys::kMouseEventButtonValueLeft) { | |
818 mouse_event.button = WebKit::WebMouseEvent::ButtonLeft; | |
819 } else if (button == keys::kMouseEventButtonValueMiddle) { | |
820 mouse_event.button = WebKit::WebMouseEvent::ButtonMiddle; | |
821 } else if (button == keys::kMouseEventButtonValueRight) { | |
822 mouse_event.button = WebKit::WebMouseEvent::ButtonRight; | |
823 } else { | |
824 error_ = keys::kInvalidMouseEventObjectError; | |
825 return false; | |
826 } | |
827 | |
828 if (HasOptionalArgument(2) && HasOptionalArgument(3)) { | |
829 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &mouse_event.x)); | |
830 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &mouse_event.y)); | |
831 } else { | |
832 error_ = keys::kNoMouseCoordinatesError; | |
833 return false; | |
834 } | |
835 | |
836 bool alt_key = false; | |
837 if (js_mouse_event->HasKey(keys::kMouseEventAltKeyKey)) | |
838 EXTENSION_FUNCTION_VALIDATE(js_mouse_event-> | |
839 GetBoolean(keys::kMouseEventAltKeyKey, &alt_key)); | |
840 if (alt_key) | |
841 mouse_event.modifiers |= WebInputEvent::AltKey; | |
842 | |
843 bool ctrl_key = false; | |
844 if (js_mouse_event->HasKey(keys::kMouseEventCtrlKeyKey)) | |
845 EXTENSION_FUNCTION_VALIDATE(js_mouse_event-> | |
846 GetBoolean(keys::kMouseEventCtrlKeyKey, &ctrl_key)); | |
847 if (ctrl_key) | |
848 mouse_event.modifiers |= WebInputEvent::ControlKey; | |
849 | |
850 bool meta_key = false; | |
851 if (js_mouse_event->HasKey(keys::kMouseEventMetaKeyKey)) | |
852 EXTENSION_FUNCTION_VALIDATE(js_mouse_event-> | |
853 GetBoolean(keys::kMouseEventMetaKeyKey, &meta_key)); | |
854 if (meta_key) | |
855 mouse_event.modifiers |= WebInputEvent::MetaKey; | |
856 | |
857 bool shift_key = false; | |
858 if (js_mouse_event->HasKey(keys::kMouseEventShiftKeyKey)) | |
859 EXTENSION_FUNCTION_VALIDATE(js_mouse_event-> | |
860 GetBoolean(keys::kMouseEventShiftKeyKey, &shift_key)); | |
861 if (shift_key) | |
862 mouse_event.modifiers |= WebInputEvent::ShiftKey; | |
863 | |
864 mouse_event.clickCount = 1; | |
865 | |
866 // Forward the event. | |
867 tab->contents()->render_view_host()->ForwardMouseEvent(mouse_event); | |
868 | |
869 // If the event is a click, | |
870 // fire a mouseup event in addition to the mousedown. | |
871 if (type.compare(keys::kMouseEventTypeValueClick) == 0) { | |
872 mouse_event.type = WebKit::WebInputEvent::MouseUp; | |
873 tab->contents()->render_view_host()->ForwardMouseEvent(mouse_event); | |
874 } | |
875 } | |
876 | |
877 if (has_callback()) { | |
878 result_.reset(tab->CreateValue()); | |
879 SendResponse(true); | |
880 } | |
881 | |
882 return true; | |
883 } | |
884 | |
885 // toDataUrl ------------------------------------------------------------------- | |
886 | |
887 ToDataUrlOffscreenTabFunction::ToDataUrlOffscreenTabFunction() {} | |
888 ToDataUrlOffscreenTabFunction::~ToDataUrlOffscreenTabFunction() {} | |
889 | |
890 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
891 // TODO(alexbost): We want to optimize this function in order to get more image | |
892 // updates on the browser side. One improvement would be to implement another | |
893 // hash map in order to get offscreen tabs in O(1). | |
894 bool ToDataUrlOffscreenTabFunction::RunImpl() { | |
895 int tab_id; | |
896 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | |
897 | |
898 OffscreenTab* tab = NULL; | |
899 if (!GetMap()->GetOffscreenTab(tab_id, dispatcher(), profile_, &tab, &error_)) | |
900 return false; | |
901 | |
902 image_format_ = FORMAT_JPEG; // Default format is JPEG. | |
903 image_quality_ = kDefaultQuality; // Default quality setting. | |
904 | |
905 if (HasOptionalArgument(1)) { | |
906 DictionaryValue* options; | |
907 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); | |
908 | |
909 if (options->HasKey(keys::kFormatKey)) { | |
910 std::string format; | |
911 EXTENSION_FUNCTION_VALIDATE( | |
912 options->GetString(keys::kFormatKey, &format)); | |
913 | |
914 if (format == keys::kFormatValueJpeg) { | |
915 image_format_ = FORMAT_JPEG; | |
916 } else if (format == keys::kFormatValuePng) { | |
917 image_format_ = FORMAT_PNG; | |
918 } else { | |
919 // Schema validation should make this unreachable. | |
920 EXTENSION_FUNCTION_VALIDATE(0); | |
921 } | |
922 } | |
923 | |
924 if (options->HasKey(keys::kQualityKey)) { | |
925 EXTENSION_FUNCTION_VALIDATE( | |
926 options->GetInteger(keys::kQualityKey, &image_quality_)); | |
927 } | |
928 } | |
929 | |
930 // captureVisibleTab() can return an image containing sensitive information | |
931 // that the browser would otherwise protect. Ensure the extension has | |
932 // permission to do this. | |
933 if (!GetExtension()-> | |
934 CanCaptureVisiblePage(tab->contents()->GetURL(), &error_)) | |
935 return false; | |
936 | |
937 // The backing store approach works on Linux but not on Mac. | |
938 // TODO(alexbost): Test on Windows | |
939 #if !defined(OS_MACOSX) | |
940 RenderViewHost* render_view_host = tab->contents()->render_view_host(); | |
941 | |
942 // If a backing store is cached for the tab we want to capture, | |
943 // and it can be copied into a bitmap, then use it to generate the image. | |
944 BackingStore* backing_store = render_view_host->GetBackingStore(false); | |
945 if (backing_store && CaptureSnapshotFromBackingStore(backing_store)) | |
946 return true; | |
947 #endif | |
948 | |
949 // Ask the renderer for a snapshot of the tab. | |
950 TabContentsWrapper* tab_wrapper = | |
951 TabContentsWrapper::GetCurrentWrapperForContents(tab->contents()); | |
952 tab_wrapper->CaptureSnapshot(); | |
953 registrar_.Add(this, | |
954 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, | |
955 Source<TabContentsWrapper>(tab_wrapper)); | |
956 | |
957 AddRef(); // Balanced in ToDataUrlOffscreenTabFunction::Observe(). | |
958 | |
959 return true; | |
960 } | |
961 | |
962 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
963 // Build the image of a tab's contents out of a backing store. | |
964 // This may fail if we can not copy a backing store into a bitmap. | |
965 // For example, some uncommon X11 visual modes are not supported by | |
966 // CopyFromBackingStore(). | |
967 bool ToDataUrlOffscreenTabFunction::CaptureSnapshotFromBackingStore( | |
968 BackingStore* backing_store) { | |
969 | |
970 skia::PlatformCanvas temp_canvas; | |
971 if (!backing_store->CopyFromBackingStore(gfx::Rect(backing_store->size()), | |
972 &temp_canvas)) { | |
973 return false; | |
974 } | |
975 VLOG(1) << "captureVisibleTab() got image from backing store."; | |
976 | |
977 SendResultFromBitmap( | |
978 skia::GetTopDevice(temp_canvas)->accessBitmap(false)); | |
979 return true; | |
980 } | |
981 | |
982 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
983 void ToDataUrlOffscreenTabFunction::Observe(int type, | |
984 const NotificationSource& source, | |
985 const NotificationDetails& details) { | |
986 CHECK(type == chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN); | |
987 | |
988 const SkBitmap *screen_capture = Details<const SkBitmap>(details).ptr(); | |
989 const bool error = screen_capture->empty(); | |
990 | |
991 if (error) { | |
992 error_ = keys::kInternalVisibleTabCaptureError; | |
993 SendResponse(false); | |
994 } else { | |
995 VLOG(1) << "Got image from renderer."; | |
996 SendResultFromBitmap(*screen_capture); | |
997 } | |
998 | |
999 Release(); // Balanced in ToDataUrlOffscreenTabFunction::RunImpl(). | |
1000 } | |
1001 | |
1002 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
1003 // Turn a bitmap of the screen into an image, set that image as the result, | |
1004 // and call SendResponse(). | |
1005 void ToDataUrlOffscreenTabFunction::SendResultFromBitmap( | |
1006 const SkBitmap& screen_capture) { | |
1007 std::vector<unsigned char> data; | |
1008 SkAutoLockPixels screen_capture_lock(screen_capture); | |
1009 bool encoded = false; | |
1010 std::string mime_type; | |
1011 switch (image_format_) { | |
1012 case FORMAT_JPEG: | |
1013 encoded = gfx::JPEGCodec::Encode( | |
1014 reinterpret_cast<unsigned char*>(screen_capture.getAddr32(0, 0)), | |
1015 gfx::JPEGCodec::FORMAT_SkBitmap, | |
1016 screen_capture.width(), | |
1017 screen_capture.height(), | |
1018 static_cast<int>(screen_capture.rowBytes()), | |
1019 image_quality_, | |
1020 &data); | |
1021 mime_type = keys::kMimeTypeJpeg; | |
1022 break; | |
1023 case FORMAT_PNG: | |
1024 encoded = gfx::PNGCodec::EncodeBGRASkBitmap( | |
1025 screen_capture, | |
1026 true, // Discard transparency. | |
1027 &data); | |
1028 mime_type = keys::kMimeTypePng; | |
1029 break; | |
1030 default: | |
1031 NOTREACHED() << "Invalid image format."; | |
1032 } | |
1033 | |
1034 if (!encoded) { | |
1035 error_ = keys::kInternalVisibleTabCaptureError; | |
1036 SendResponse(false); | |
1037 return; | |
1038 } | |
1039 | |
1040 std::string base64_result; | |
1041 base::StringPiece stream_as_string( | |
1042 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); | |
1043 | |
1044 base::Base64Encode(stream_as_string, &base64_result); | |
1045 base64_result.insert(0, base::StringPrintf("data:%s;base64,", | |
1046 mime_type.c_str())); | |
1047 result_.reset(new StringValue(base64_result)); | |
1048 SendResponse(true); | |
1049 } | |
1050 | |
1051 // update ---------------------------------------------------------------------- | |
1052 | |
1053 UpdateOffscreenTabFunction::UpdateOffscreenTabFunction() {} | |
1054 UpdateOffscreenTabFunction::~UpdateOffscreenTabFunction() {} | |
1055 | |
1056 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
1057 bool UpdateOffscreenTabFunction::RunImpl() { | |
1058 int tab_id; | |
1059 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | |
1060 | |
1061 OffscreenTab* tab = NULL; | |
1062 if (!GetMap()->GetOffscreenTab(tab_id, dispatcher(), profile_, &tab, &error_)) | |
1063 return false; | |
1064 | |
1065 DictionaryValue* update_props; | |
1066 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); | |
1067 | |
1068 // Url | |
1069 if (update_props->HasKey(keys::kUrlKey)) { | |
1070 std::string url_string; | |
1071 GURL url; | |
1072 EXTENSION_FUNCTION_VALIDATE( | |
1073 update_props->GetString(keys::kUrlKey, &url_string)); | |
1074 url = ResolvePossiblyRelativeURL(url_string, GetExtension()); | |
1075 if (!url.is_valid()) { | |
1076 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
1077 keys::kInvalidUrlError, url_string); | |
1078 return false; | |
1079 } | |
1080 if (IsCrashURL(url)) { | |
1081 error_ = keys::kNoCrashBrowserError; | |
1082 return false; | |
1083 } | |
1084 | |
1085 // JavaScript URLs can do the same kinds of things as cross-origin XHR, so | |
1086 // we need to check host permissions before allowing them. | |
1087 if (url.SchemeIs(chrome::kJavaScriptScheme)) { | |
1088 if (!GetExtension()->CanExecuteScriptOnPage( | |
1089 tab->contents()->GetURL(), NULL, &error_)) { | |
1090 return false; | |
1091 } | |
1092 | |
1093 ExtensionMsg_ExecuteCode_Params params; | |
1094 params.request_id = request_id(); | |
1095 params.extension_id = extension_id(); | |
1096 params.is_javascript = true; | |
1097 params.code = url.path(); | |
1098 params.all_frames = false; | |
1099 params.in_main_world = true; | |
1100 | |
1101 RenderViewHost* render_view_host = | |
1102 tab->contents()->render_view_host(); | |
1103 render_view_host->Send( | |
1104 new ExtensionMsg_ExecuteCode(render_view_host->routing_id(), | |
1105 params)); | |
1106 | |
1107 Observe(tab->contents()); | |
1108 AddRef(); // balanced in Observe() | |
1109 | |
1110 return true; | |
1111 } | |
1112 | |
1113 tab->SetURL(url); | |
1114 | |
1115 // The URL of a tab contents never actually changes to a JavaScript URL, so | |
1116 // this check only makes sense in other cases. | |
1117 if (!url.SchemeIs(chrome::kJavaScriptScheme)) | |
1118 CHECK_EQ(url.spec(), tab->contents()->GetURL().spec()); | |
1119 } | |
1120 | |
1121 // Width and height | |
1122 if (update_props->HasKey(keys::kWidthKey) || | |
1123 update_props->HasKey(keys::kHeightKey)) { | |
1124 int width; | |
1125 if (update_props->HasKey(keys::kWidthKey)) | |
1126 EXTENSION_FUNCTION_VALIDATE( | |
1127 update_props->GetInteger(keys::kWidthKey, &width)); | |
1128 else | |
1129 tab->contents()->view()->GetContainerSize().width(); | |
1130 | |
1131 int height; | |
1132 if (update_props->HasKey(keys::kHeightKey)) | |
1133 EXTENSION_FUNCTION_VALIDATE( | |
1134 update_props->GetInteger(keys::kHeightKey, &height)); | |
1135 else | |
1136 tab->contents()->view()->GetContainerSize().height(); | |
1137 | |
1138 tab->SetSize(width, height); | |
1139 } | |
1140 | |
1141 // Callback | |
1142 if (has_callback()) { | |
1143 result_.reset(tab->CreateValue()); | |
1144 SendResponse(true); | |
1145 } | |
1146 | |
1147 return true; | |
1148 } | |
1149 | |
1150 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
1151 bool UpdateOffscreenTabFunction::OnMessageReceived( | |
1152 const IPC::Message& message) { | |
1153 if (message.type() != ExtensionHostMsg_ExecuteCodeFinished::ID) | |
1154 return false; | |
1155 | |
1156 int message_request_id; | |
1157 void* iter = NULL; | |
1158 if (!message.ReadInt(&iter, &message_request_id)) { | |
1159 NOTREACHED() << "malformed extension message"; | |
1160 return true; | |
1161 } | |
1162 | |
1163 if (message_request_id != request_id()) | |
1164 return false; | |
1165 | |
1166 IPC_BEGIN_MESSAGE_MAP(UpdateOffscreenTabFunction, message) | |
1167 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ExecuteCodeFinished, | |
1168 OnExecuteCodeFinished) | |
1169 IPC_END_MESSAGE_MAP() | |
1170 return true; | |
1171 } | |
1172 | |
1173 // TODO(alexbost): Needs refactoring. Similar method in extension_tabs_module. | |
1174 void UpdateOffscreenTabFunction:: | |
1175 OnExecuteCodeFinished(int request_id, | |
1176 bool success, | |
1177 const std::string& error) { | |
1178 if (!error.empty()) { | |
1179 CHECK(!success); | |
1180 error_ = error; | |
1181 } | |
1182 | |
1183 SendResponse(success); | |
1184 | |
1185 Observe(NULL); | |
1186 Release(); // balanced in Execute() | |
1187 } | |
1188 | |
OLD | NEW |