Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: chrome/browser/extensions/extension_page_actions_module.cc

Issue 327007: Revert 29861 since this fail on the interactive... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/extension_page_actions_module.h" 5 #include "chrome/browser/extensions/extension_page_actions_module.h"
6 6
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "chrome/browser/browser.h" 8 #include "chrome/browser/browser.h"
9 #include "chrome/browser/browser_list.h" 9 #include "chrome/browser/browser_list.h"
10 #include "chrome/browser/profile.h" 10 #include "chrome/browser/profile.h"
11 #include "chrome/browser/extensions/extension_page_actions_module_constants.h" 11 #include "chrome/browser/extensions/extension_page_actions_module_constants.h"
12 #include "chrome/browser/extensions/extension_tabs_module.h" 12 #include "chrome/browser/extensions/extension_tabs_module.h"
13 #include "chrome/browser/extensions/extensions_service.h" 13 #include "chrome/browser/extensions/extensions_service.h"
14 #include "chrome/browser/tab_contents/navigation_entry.h" 14 #include "chrome/browser/tab_contents/navigation_entry.h"
15 #include "chrome/browser/tab_contents/tab_contents.h" 15 #include "chrome/browser/tab_contents/tab_contents.h"
16 #include "chrome/common/extensions/extension.h" 16 #include "chrome/common/extensions/extension.h"
17 #include "chrome/common/extensions/extension_error_utils.h" 17 #include "chrome/common/extensions/extension_error_utils.h"
18 #include "chrome/common/render_messages.h" 18 #include "chrome/common/render_messages.h"
19 19
20 namespace keys = extension_page_actions_module_constants; 20 namespace keys = extension_page_actions_module_constants;
21 21
22 namespace { 22 namespace {
23 // Errors. 23 // Errors.
24 const char kNoExtensionError[] = "No extension with id: *.";
24 const char kNoTabError[] = "No tab with id: *."; 25 const char kNoTabError[] = "No tab with id: *.";
25 const char kNoPageActionError[] = 26 const char kNoPageActionError[] =
26 "This extension has no page action specified."; 27 "This extension has no page action specified.";
27 const char kUrlNotActiveError[] = "This url is no longer active: *."; 28 const char kUrlNotActiveError[] = "This url is no longer active: *.";
28 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds."; 29 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds.";
29 const char kNoIconSpecified[] = "Page action has no icons to show.";
30 } 30 }
31 31
32 // TODO(EXTENSIONS_DEPRECATED): obsolete API. 32 // TODO(EXTENSIONS_DEPRECATED): obsolete API.
33 bool PageActionFunction::SetPageActionEnabled(bool enable) { 33 bool PageActionFunction::SetPageActionEnabled(bool enable) {
34 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); 34 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST));
35 const ListValue* args = static_cast<const ListValue*>(args_); 35 const ListValue* args = static_cast<const ListValue*>(args_);
36 36
37 std::string page_action_id; 37 std::string page_action_id;
38 EXTENSION_FUNCTION_VALIDATE(args->GetString(0, &page_action_id)); 38 EXTENSION_FUNCTION_VALIDATE(args->GetString(0, &page_action_id));
39 DictionaryValue* action; 39 DictionaryValue* action;
40 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &action)); 40 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &action));
41 41
42 int tab_id; 42 int tab_id;
43 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); 43 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id));
44 std::string url; 44 std::string url;
45 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); 45 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url));
46 46
47 std::string title; 47 std::string title;
48 int icon_id = 0; 48 int icon_id = 0;
49 if (enable) { 49 if (enable) {
50 // Both of those are optional. 50 // Both of those are optional.
51 if (action->HasKey(keys::kTitleKey)) 51 if (action->HasKey(keys::kTitleKey))
52 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title)); 52 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title));
53 if (action->HasKey(keys::kIconIdKey)) { 53 if (action->HasKey(keys::kIconIdKey)) {
54 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kIconIdKey, 54 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kIconIdKey,
55 &icon_id)); 55 &icon_id));
56 } 56 }
57 } 57 }
58 58
59 const ExtensionAction* page_action =
60 dispatcher()->GetExtension()->page_action();
61 if (!page_action) {
62 error_ = kNoPageActionError;
63 return false;
64 }
65
66 if (icon_id < 0 ||
67 static_cast<size_t>(icon_id) >= page_action->icon_paths().size()) {
68 error_ = (icon_id == 0) ? kNoIconSpecified : kIconIndexOutOfBounds;
69 return false;
70 }
71
72 // Find the TabContents that contains this tab id. 59 // Find the TabContents that contains this tab id.
73 TabContents* contents = NULL; 60 TabContents* contents = NULL;
74 ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents, NULL); 61 ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents, NULL);
75 if (!contents) { 62 if (!contents) {
76 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoTabError, 63 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoTabError,
77 IntToString(tab_id)); 64 IntToString(tab_id));
78 return false; 65 return false;
79 } 66 }
80 67
81 // Make sure the URL hasn't changed. 68 // Make sure the URL hasn't changed.
82 NavigationEntry* entry = contents->controller().GetActiveEntry(); 69 NavigationEntry* entry = contents->controller().GetActiveEntry();
83 if (!entry || url != entry->url().spec()) { 70 if (!entry || url != entry->url().spec()) {
84 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url); 71 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url);
85 return false; 72 return false;
86 } 73 }
87 74
75 // Find our extension.
76 Extension* extension = NULL;
77 ExtensionsService* service = profile()->GetExtensionsService();
78 extension = service->GetExtensionById(extension_id());
79 if (!extension) {
80 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoExtensionError,
81 extension_id());
82 return false;
83 }
84
85 const ExtensionAction* page_action = extension->page_action();
86 if (!page_action) {
87 error_ = kNoPageActionError;
88 return false;
89 }
90
88 // Set visibility and broadcast notifications that the UI should be updated. 91 // Set visibility and broadcast notifications that the UI should be updated.
89 contents->SetPageActionEnabled(page_action, enable, title, icon_id); 92 contents->SetPageActionEnabled(page_action, enable, title, icon_id);
90 contents->PageActionStateChanged(); 93 contents->PageActionStateChanged();
91 94
92 return true; 95 return true;
93 } 96 }
94 97
95 bool PageActionFunction::InitCommon(int tab_id) { 98 bool PageActionFunction::InitCommon(int tab_id) {
96 page_action_ = dispatcher()->GetExtension()->page_action(); 99 page_action_ = dispatcher()->GetExtension()->page_action();
97 if (!page_action_) { 100 if (!page_action_) {
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 if (!InitCommon(tab_id)) 252 if (!InitCommon(tab_id))
250 return false; 253 return false;
251 254
252 std::string text; 255 std::string text;
253 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"text", &text)); 256 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"text", &text));
254 257
255 state_->set_badge_text(text); 258 state_->set_badge_text(text);
256 contents_->PageActionStateChanged(); 259 contents_->PageActionStateChanged();
257 return true; 260 return true;
258 } 261 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_browsertests_misc.cc ('k') | chrome/browser/gtk/location_bar_view_gtk.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698