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