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