summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_tabs_module.cc
blob: 67524494e7d6aad5ae40acd44f52f15d04603fd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

#include "base/string_util.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/extensions/extension.h"
#include "chrome/browser/extensions/extension_error_utils.h"
#include "chrome/browser/extensions/extension_function_dispatcher.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/renderer_host/render_view_host_delegate.h"
#include "chrome/browser/tab_contents/navigation_entry.h"

// TODO(port): Port these files.
#if defined(OS_WIN) || defined(OS_MACOSX)
#include "chrome/browser/window_sizer.h"
#else
#include "chrome/common/temp_scaffolding_stubs.h"
#endif

namespace {
// Keys.
const wchar_t* kIdKey = L"id";
const wchar_t* kIndexKey = L"index";
const wchar_t* kWindowIdKey = L"windowId";
const wchar_t* kUrlKey = L"url";
const wchar_t* kTitleKey = L"title";
const wchar_t* kSelectedKey = L"selected";
const wchar_t* kFocusedKey = L"focused";
const wchar_t* kFavIconUrlKey = L"favIconUrl";
const wchar_t* kLeftKey = L"left";
const wchar_t* kTopKey = L"top";
const wchar_t* kWidthKey = L"width";
const wchar_t* kHeightKey = L"height";
const wchar_t* kTabsKey = L"tabs";

// Error messages.
const char* kWindowNotFoundError = "No window with id: *.";
const char* kTabNotFoundError = "No tab with id: *.";
const char* kInvalidUrlError = "Invalid url: \"*\".";
}

// Forward declare static helper functions defined below.
static DictionaryValue* CreateWindowValue(Browser* browser, bool populate_tabs);
static ListValue* CreateTabList(Browser* browser);

// |error_message| can optionally be passed in a will be set with an appropriate
// message if the window cannot be found by id.
static Browser* GetBrowserInProfileWithId(Profile* profile,
                                          const int window_id,
                                          std::string* error_message);

// |error_message| can optionally be passed in a will be set with an appropriate
// message if the tab cannot be found by id.
static bool GetTabById(int tab_id, Profile* profile, Browser** browser,
                       TabStripModel** tab_strip,
                       TabContents** contents,
                       int* tab_index, std::string* error_message);

// Construct an absolute path from a relative path.
static GURL AbsolutePath(Profile* profile, std::string extension_id,
                         std::string relative_url);

// ExtensionTabUtil
int ExtensionTabUtil::GetWindowId(const Browser* browser) {
  return browser->session_id().id();
}

int ExtensionTabUtil::GetTabId(const TabContents* tab_contents) {
  return tab_contents->controller().session_id().id();
}

int ExtensionTabUtil::GetWindowIdOfTab(const TabContents* tab_contents) {
  return tab_contents->controller().window_id().id();
}

DictionaryValue* ExtensionTabUtil::CreateTabValue(
    const TabContents* contents) {
  // Find the tab strip and index of this guy.
  for (BrowserList::const_iterator it = BrowserList::begin();
      it != BrowserList::end(); ++it) {
    TabStripModel* tab_strip = (*it)->tabstrip_model();
    int tab_index = tab_strip->GetIndexOfTabContents(contents);
    if (tab_index != -1) {
      return ExtensionTabUtil::CreateTabValue(contents, tab_strip, tab_index);
    }
  }

  // Couldn't find it.  This can happen if the tab is being dragged.
  return ExtensionTabUtil::CreateTabValue(contents, NULL, -1);
}

DictionaryValue* ExtensionTabUtil::CreateTabValue(
    const TabContents* contents, TabStripModel* tab_strip, int tab_index) {
  DictionaryValue* result = new DictionaryValue();
  result->SetInteger(kIdKey, ExtensionTabUtil::GetTabId(contents));
  result->SetInteger(kIndexKey, tab_index);
  result->SetInteger(kWindowIdKey,
                     ExtensionTabUtil::GetWindowIdOfTab(contents));
  result->SetString(kUrlKey, contents->GetURL().spec());
  result->SetString(kTitleKey, UTF16ToWide(contents->GetTitle()));
  result->SetBoolean(kSelectedKey,
                     tab_strip && tab_index == tab_strip->selected_index());

  NavigationEntry* entry = contents->controller().GetActiveEntry();
  if (entry) {
    if (entry->favicon().is_valid())
      result->SetString(kFavIconUrlKey, entry->favicon().url().spec());
  }

  return result;
}

bool ExtensionTabUtil::GetTabById(int tab_id, Profile* profile,
                                  Browser** browser,
                                  TabStripModel** tab_strip,
                                  TabContents** contents,
                                  int* tab_index) {
  Browser* target_browser;
  TabStripModel* target_tab_strip;
  TabContents* target_contents;
  for (BrowserList::const_iterator iter = BrowserList::begin();
       iter != BrowserList::end(); ++iter) {
    target_browser = *iter;
    if (target_browser->profile() == profile) {
      target_tab_strip = target_browser->tabstrip_model();
      for (int i = 0; i < target_tab_strip->count(); ++i) {
        target_contents = target_tab_strip->GetTabContentsAt(i);
        if (target_contents->controller().session_id().id() == tab_id) {
          if (browser)
            *browser = target_browser;
          if (tab_strip)
            *tab_strip = target_tab_strip;
          if (contents)
            *contents = target_contents;
          if (tab_index)
            *tab_index = i;
          return true;
        }
      }
    }
  }
  return false;
}

// Windows ---------------------------------------------------------------------

bool GetWindowFunction::RunImpl() {
  int window_id;
  EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id));

  Browser* browser = GetBrowserInProfileWithId(profile(), window_id, &error_);
  if (!browser)
    return false;

  result_.reset(CreateWindowValue(browser, false));
  return true;
}

bool GetCurrentWindowFunction::RunImpl() {
  Browser* browser = dispatcher_->GetBrowser();
  result_.reset(CreateWindowValue(browser, false));
  return true;
}

bool GetLastFocusedWindowFunction::RunImpl() {
  Browser* browser = BrowserList::GetLastActiveWithProfile(profile());
  result_.reset(CreateWindowValue(browser, false));
  return true;
}

bool GetAllWindowsFunction::RunImpl() {
  bool populate_tabs = false;
  if (!args_->IsType(Value::TYPE_NULL)) {
    EXTENSION_FUNCTION_VALIDATE(args_->GetAsBoolean(&populate_tabs));
  }

  result_.reset(new ListValue());
  for (BrowserList::const_iterator browser = BrowserList::begin();
    browser != BrowserList::end(); ++browser) {
      // Only examine browsers in the current profile.
      if ((*browser)->profile() == profile()) {
        static_cast<ListValue*>(result_.get())->
          Append(CreateWindowValue(*browser, populate_tabs));
      }
  }

  return true;
}

bool CreateWindowFunction::RunImpl() {
  scoped_ptr<GURL> url(new GURL());

  // Look for optional url.
  if (!args_->IsType(Value::TYPE_NULL)) {
    EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
    const DictionaryValue *args = static_cast<const DictionaryValue*>(args_);
    std::string url_input;
    if (args->HasKey(kUrlKey)) {
      EXTENSION_FUNCTION_VALIDATE(args->GetString(kUrlKey, &url_input));
      url.reset(new GURL(url_input));
      if (!url->is_valid()) {
        error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidUrlError,
            url_input);
        return false;
      }
    }
  }

  // Try to position the new browser relative its originating browser window.
  gfx::Rect empty_bounds;
  gfx::Rect bounds;
  bool maximized;
  // The call offsets the bounds by kWindowTilePixels (defined in WindowSizer to
  // be 10).
  WindowSizer::GetBrowserWindowBounds(std::wstring(), empty_bounds,
                                      dispatcher_->GetBrowser(), &bounds,
                                      &maximized);

  // Any part of the bounds can optionally be set by the caller.
  if (args_->IsType(Value::TYPE_DICTIONARY)) {
    const DictionaryValue *args = static_cast<const DictionaryValue*>(args_);
    int bounds_val;
    if (args->HasKey(kLeftKey)) {
      EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kLeftKey, &bounds_val));
      bounds.set_x(bounds_val);
    }

    if (args->HasKey(kTopKey)) {
      EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kTopKey, &bounds_val));
      bounds.set_y(bounds_val);
    }

    if (args->HasKey(kWidthKey)) {
      EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kWidthKey, &bounds_val));
      bounds.set_width(bounds_val);
    }

    if (args->HasKey(kHeightKey)) {
      EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kHeightKey, &bounds_val));
      bounds.set_height(bounds_val);
    }
  }

  Browser* new_window = Browser::Create(dispatcher_->profile());
  new_window->AddTabWithURL(*(url.get()), GURL(), PageTransition::LINK, true,
                            -1, false, NULL);

  new_window->window()->SetBounds(bounds);
  new_window->window()->Show();

  // TODO(rafaelw): support |focused|, |zIndex|

  result_.reset(CreateWindowValue(new_window, false));

  return true;
}

bool UpdateWindowFunction::RunImpl() {
  EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST));
  const ListValue* args = static_cast<const ListValue*>(args_);
  int window_id;
  EXTENSION_FUNCTION_VALIDATE(args->GetInteger(0, &window_id));
  DictionaryValue* update_props;
  EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &update_props));

  Browser* browser = GetBrowserInProfileWithId(profile(), window_id, &error_);
  if (!browser)
    return false;

  gfx::Rect bounds = browser->window()->GetNormalBounds();
  // Any part of the bounds can optionally be set by the caller.
  int bounds_val;
  if (update_props->HasKey(kLeftKey)) {
    EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kLeftKey,
        &bounds_val));
    bounds.set_x(bounds_val);
  }

  if (update_props->HasKey(kTopKey)) {
    EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kTopKey,
        &bounds_val));
    bounds.set_y(bounds_val);
  }

  if (update_props->HasKey(kWidthKey)) {
    EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kWidthKey,
        &bounds_val));
    bounds.set_width(bounds_val);
  }

  if (update_props->HasKey(kHeightKey)) {
    EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kHeightKey,
        &bounds_val));
    bounds.set_height(bounds_val);
  }

  // TODO(rafaelw): This call to SetBounds() ends up resulting in the target
  // window being activated (pushed to the front). On win32, this appears to be
  // the result of HWND event handling.
  browser->window()->SetBounds(bounds);
  // TODO(rafaelw): Support |focused|.
  result_.reset(CreateWindowValue(browser, false));

  return true;
}

bool RemoveWindowFunction::RunImpl() {
  int window_id;
  EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id));

  Browser* browser = GetBrowserInProfileWithId(profile(), window_id, &error_);
  if (!browser)
    return false;

  browser->CloseWindow();

  return true;
}

// Tabs ------------------------------------------------------------------------

bool GetSelectedTabFunction::RunImpl() {
  Browser* browser;
  // windowId defaults to "current" window.
  int window_id = -1;

  if (!args_->IsType(Value::TYPE_NULL)) {
    EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id));
    browser = GetBrowserInProfileWithId(profile(), window_id, &error_);
    if (!browser)
      return false;
  } else {
    browser = dispatcher_->GetBrowser();
  }

  TabStripModel* tab_strip = browser->tabstrip_model();
  result_.reset(ExtensionTabUtil::CreateTabValue(
      tab_strip->GetSelectedTabContents(),
      tab_strip,
      tab_strip->selected_index()));
  return true;
}

bool GetAllTabsInWindowFunction::RunImpl() {
  Browser* browser;
  // windowId defaults to "current" window.
  int window_id = -1;
  if (!args_->IsType(Value::TYPE_NULL)) {
    EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id));
    browser = GetBrowserInProfileWithId(profile(), window_id, &error_);
    if (!browser)
      return false;
  } else {
    browser = dispatcher_->GetBrowser();
  }

  result_.reset(CreateTabList(browser));

  return true;
}

bool CreateTabFunction::RunImpl() {
  EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
  const DictionaryValue* args = static_cast<const DictionaryValue*>(args_);

  Browser *browser;
  // windowId defaults to "current" window.
  int window_id = -1;
  if (args->HasKey(kWindowIdKey)) {
    EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kWindowIdKey, &window_id));
    browser = GetBrowserInProfileWithId(profile(), window_id, &error_);
    if (!browser)
      return false;
  } else {
      browser = dispatcher_->GetBrowser();
  }

  TabStripModel* tab_strip = browser->tabstrip_model();

  // TODO(rafaelw): handle setting remaining tab properties:
  // -title
  // -favIconUrl

  std::string url_string;
  scoped_ptr<GURL> url(new GURL());
  if (args->HasKey(kUrlKey)) {
    EXTENSION_FUNCTION_VALIDATE(args->GetString(kUrlKey, &url_string));
    url.reset(new GURL(url_string));
    if (!url->is_valid()) {
      // The path as passed in is not valid. Try converting to absolute path.
      *url = AbsolutePath(profile(), extension_id(), url_string);
      if (!url->is_valid()) {
        error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidUrlError,
                                                         url_string);
        return false;
      }
    }
  }

  // Default to foreground for the new tab. The presence of 'selected' property
  // will override this default.
  bool selected = true;
  if (args->HasKey(kSelectedKey))
    EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(kSelectedKey, &selected));

  // If index is specified, honor the value, but keep it bound to
  // 0 <= index <= tab_strip->count()
  int index = -1;
  if (args->HasKey(kIndexKey))
    EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kIndexKey, &index));

  if (index < 0) {
    // Default insert behavior.
    index = -1;
  }
  if (index > tab_strip->count()) {
    index = tab_strip->count();
  }

  TabContents* contents = browser->AddTabWithURL(*(url.get()), GURL(),
      PageTransition::LINK, selected, index, true, NULL);
  index = tab_strip->GetIndexOfTabContents(contents);

  // Return data about the newly created tab.
  if (has_callback())
    result_.reset(ExtensionTabUtil::CreateTabValue(contents, tab_strip, index));

  return true;
}

bool GetTabFunction::RunImpl() {
  int tab_id;
  EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id));

  TabStripModel* tab_strip = NULL;
  TabContents* contents = NULL;
  int tab_index = -1;
  if (!GetTabById(tab_id, profile(), NULL, &tab_strip, &contents, &tab_index,
      &error_))
    return false;

  result_.reset(ExtensionTabUtil::CreateTabValue(contents, tab_strip,
      tab_index));
  return true;
}

bool UpdateTabFunction::RunImpl() {
  int tab_id;
  EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST));
  const ListValue* args = static_cast<const ListValue*>(args_);
  EXTENSION_FUNCTION_VALIDATE(args->GetInteger(0, &tab_id));
  DictionaryValue* update_props;
  EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &update_props));

  TabStripModel* tab_strip = NULL;
  TabContents* contents = NULL;
  int tab_index = -1;
  if (!GetTabById(tab_id, profile(), NULL, &tab_strip, &contents, &tab_index,
      &error_))
    return false;

  NavigationController& controller = contents->controller();

  // TODO(rafaelw): handle setting remaining tab properties:
  // -title
  // -favIconUrl

  // Navigate the tab to a new location if the url different.
  std::string url;
  if (update_props->HasKey(kUrlKey)) {
    EXTENSION_FUNCTION_VALIDATE(update_props->GetString(kUrlKey, &url));
    GURL new_gurl(url);

    if (!new_gurl.is_valid()) {
      // The path as passed in is not valid. Try converting to absolute path.
      new_gurl = AbsolutePath(profile(), extension_id(), url);
      if (!new_gurl.is_valid()) {
        error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidUrlError, url);
        return false;
      }
    }

    controller.LoadURL(new_gurl, GURL(), PageTransition::LINK);
  }

  bool selected = false;
  // TODO(rafaelw): Setting |selected| from js doesn't make much sense.
  // Move tab selection management up to window.
  if (update_props->HasKey(kSelectedKey)) {
    EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean(kSelectedKey,
                                                         &selected));
    if (selected && tab_strip->selected_index() != tab_index) {
      tab_strip->SelectTabContentsAt(tab_index, false);
    }
  }

  return true;
}

bool MoveTabFunction::RunImpl() {
  int tab_id;
  EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST));
  const ListValue* args = static_cast<const ListValue*>(args_);
  EXTENSION_FUNCTION_VALIDATE(args->GetInteger(0, &tab_id));
  DictionaryValue* update_props;
  EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &update_props));

  int new_index;
  EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kIndexKey, &new_index));
  EXTENSION_FUNCTION_VALIDATE(new_index >= 0);

  Browser* source_browser = NULL;
  TabStripModel* source_tab_strip = NULL;
  int tab_index = -1;
  if (!GetTabById(tab_id, profile(), &source_browser, &source_tab_strip, NULL,
      &tab_index, &error_))
    return false;

  if (update_props->HasKey(kWindowIdKey)) {
    Browser* target_browser;
    int window_id;
    EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(kWindowIdKey,
        &window_id));
    target_browser = GetBrowserInProfileWithId(profile(), window_id,
        &error_);
    if (!target_browser)
      return false;

    // If windowId is different from the current window, move between windows.
    if (ExtensionTabUtil::GetWindowId(target_browser) !=
        ExtensionTabUtil::GetWindowId(source_browser)) {
      TabStripModel* target_tab_strip = target_browser->tabstrip_model();
      TabContents *contents = source_tab_strip->DetachTabContentsAt(tab_index);
      if (!contents) {
        error_ = ExtensionErrorUtils::FormatErrorMessage(
            kTabNotFoundError, IntToString(tab_id));
        return false;
      }

      // Clamp move location to the last position.
      // This is ">" because it can append to a new index position.
      if (new_index > target_tab_strip->count())
        new_index = target_tab_strip->count();

      target_tab_strip->InsertTabContentsAt(new_index, contents,
          false, true);

      return true;
    }
  }

  // Perform a simple within-window move.
  // Clamp move location to the last position.
  // This is ">=" because the move must be to an existing location.
  if (new_index >= source_tab_strip->count())
    new_index = source_tab_strip->count() - 1;

  if (new_index != tab_index)
    source_tab_strip->MoveTabContentsAt(tab_index, new_index, false);

  return true;
}


bool RemoveTabFunction::RunImpl() {
  int tab_id;
  EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id));

  Browser* browser = NULL;
  TabContents* contents = NULL;
  if (!GetTabById(tab_id, profile(), &browser, NULL, &contents, NULL, &error_))
    return false;

  browser->CloseTabContents(contents);
  return true;
}

// static helpers

// if |populate| is true, each window gets a list property |tabs| which contains
// fully populated tab objects.
static DictionaryValue* CreateWindowValue(Browser* browser,
                                          bool populate_tabs) {
  DictionaryValue* result = new DictionaryValue();
  result->SetInteger(kIdKey, ExtensionTabUtil::GetWindowId(browser));
  result->SetBoolean(kFocusedKey, browser->window()->IsActive());
  gfx::Rect bounds = browser->window()->GetNormalBounds();

  // TODO(rafaelw): zIndex ?
  result->SetInteger(kLeftKey, bounds.x());
  result->SetInteger(kTopKey, bounds.y());
  result->SetInteger(kWidthKey, bounds.width());
  result->SetInteger(kHeightKey, bounds.height());

  if (populate_tabs) {
    result->Set(kTabsKey, CreateTabList(browser));
  }

  return result;
}

static ListValue* CreateTabList(Browser* browser) {
  ListValue* tab_list = new ListValue();
  TabStripModel* tab_strip = browser->tabstrip_model();
  for (int i = 0; i < tab_strip->count(); ++i) {
    tab_list->Append(ExtensionTabUtil::CreateTabValue(
        tab_strip->GetTabContentsAt(i), tab_strip, i));
  }

  return tab_list;
}

static Browser* GetBrowserInProfileWithId(Profile* profile,
                                          const int window_id,
                                          std::string* error_message) {
  for (BrowserList::const_iterator browser = BrowserList::begin();
      browser != BrowserList::end(); ++browser) {
    if ((*browser)->profile() == profile &&
        ExtensionTabUtil::GetWindowId(*browser) == window_id)
      return *browser;
  }

  if (error_message)
    *error_message= ExtensionErrorUtils::FormatErrorMessage(
        kWindowNotFoundError, IntToString(window_id));

  return NULL;
}

static GURL AbsolutePath(Profile* profile, std::string extension_id,
                         std::string relative_url) {
  ExtensionsService* service = profile->GetExtensionsService();
  Extension* extension = service->GetExtensionByID(extension_id);
  return Extension::GetResourceURL(extension->url(), relative_url);
}

static bool GetTabById(int tab_id, Profile* profile, Browser** browser,
                       TabStripModel** tab_strip,
                       TabContents** contents,
                       int* tab_index,
                       std::string* error_message) {
  if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip,
                                   contents, tab_index))
    return true;

  if (error_message)
    *error_message = ExtensionErrorUtils::FormatErrorMessage(
        kTabNotFoundError, IntToString(tab_id));

  return false;
}