summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authorpinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-17 17:15:49 +0000
committerpinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-17 17:15:49 +0000
commitd18be496b6e7160ef944c88f765ba977eb12ca03 (patch)
tree2547d50f1ffc6bd1a2fd0d9a4d884cb106501b2c /chrome/browser
parenta09603c94f6f4a152d9ac8cd4ad9d9a36ade8c30 (diff)
downloadchromium_src-d18be496b6e7160ef944c88f765ba977eb12ca03.zip
chromium_src-d18be496b6e7160ef944c88f765ba977eb12ca03.tar.gz
chromium_src-d18be496b6e7160ef944c88f765ba977eb12ca03.tar.bz2
Cleaned up the SDEF. Added execute javascript command. Updated tests. Fixed a bug in tabapplescript. Added more examples.
patch from (v.a.shreyas@gmail.com) BUG=none TEST=run tests included in CL. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56375 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/cocoa/applescript/examples/advanced_tab_manipulation.applescript24
-rw-r--r--chrome/browser/cocoa/applescript/examples/copy_html.applescript16
-rw-r--r--chrome/browser/cocoa/applescript/examples/delete_bookmarks.applescript13
-rw-r--r--chrome/browser/cocoa/applescript/examples/execute_javascript.applescript10
-rw-r--r--chrome/browser/cocoa/applescript/examples/open_tabs_from_bookmark_folder.applescript12
-rw-r--r--chrome/browser/cocoa/applescript/examples/tab_manipulation.applescript45
-rw-r--r--chrome/browser/cocoa/applescript/scripting.sdef32
-rw-r--r--chrome/browser/cocoa/applescript/tab_applescript.h3
-rw-r--r--chrome/browser/cocoa/applescript/tab_applescript.mm22
-rw-r--r--chrome/browser/cocoa/applescript/window_applescript_test.mm39
10 files changed, 191 insertions, 25 deletions
diff --git a/chrome/browser/cocoa/applescript/examples/advanced_tab_manipulation.applescript b/chrome/browser/cocoa/applescript/examples/advanced_tab_manipulation.applescript
new file mode 100644
index 0000000..d45bd86
--- /dev/null
+++ b/chrome/browser/cocoa/applescript/examples/advanced_tab_manipulation.applescript
@@ -0,0 +1,24 @@
+-- Copyright (c) 2010 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.
+
+tell application "Chromium"
+ tell tab 1 of window 1
+ print -- Prints the tab, prompts the user for location.
+ end tell
+
+ tell tab 1 of window 1
+ save in "/Users/Foo/Documents/Google" as "only html"
+ -- Saves the contents of the tab without the accompanying resources.
+
+ save in "/Users/Foo/Documents/Google" as "complete html"
+ -- Saves the contents of the tab with the accompanying resources.
+
+ -- Note: both the |in| and |as| part are optional, without it user is
+ -- prompted for one.
+ end tell
+
+ tell tab 1 of window 1
+ view source -- View the HTML of the tab in a new tab.
+ end tell
+end tell
diff --git a/chrome/browser/cocoa/applescript/examples/copy_html.applescript b/chrome/browser/cocoa/applescript/examples/copy_html.applescript
new file mode 100644
index 0000000..fd7b4e0
--- /dev/null
+++ b/chrome/browser/cocoa/applescript/examples/copy_html.applescript
@@ -0,0 +1,16 @@
+-- Copyright (c) 2010 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.
+
+-- This script copies the HTML of a tab to a TextEdit document.
+tell application "Chromium"
+ tell tab 1 of window 1 to view source
+ repeat while (loading of tab 2 of window 1)
+ end repeat
+ tell tab 2 of window 1 to select all
+ tell tab 2 of window 1 to copy selection
+end tell
+
+tell application "TextEdit"
+ set text of document 1 to the clipboard
+end tell
diff --git a/chrome/browser/cocoa/applescript/examples/delete_bookmarks.applescript b/chrome/browser/cocoa/applescript/examples/delete_bookmarks.applescript
new file mode 100644
index 0000000..341b100
--- /dev/null
+++ b/chrome/browser/cocoa/applescript/examples/delete_bookmarks.applescript
@@ -0,0 +1,13 @@
+-- Copyright (c) 2010 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.
+
+-- This script deletes all the items within a bookmark folder.
+tell application "Chromium"
+ set var to bookmark folder "New" of bookmarks bar
+ -- Change the folder to whichever you want.
+ tell var
+ delete every bookmark item
+ delete every bookmark folder
+ end tell
+end tell
diff --git a/chrome/browser/cocoa/applescript/examples/execute_javascript.applescript b/chrome/browser/cocoa/applescript/examples/execute_javascript.applescript
new file mode 100644
index 0000000..33a34b4
--- /dev/null
+++ b/chrome/browser/cocoa/applescript/examples/execute_javascript.applescript
@@ -0,0 +1,10 @@
+-- Copyright (c) 2010 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.
+
+-- This script execute a string of javascript code.
+tell application "Chromium"
+ tell tab 1 of window 1
+ execute javascript "alert('Hello World')"
+ end tell
+end tell
diff --git a/chrome/browser/cocoa/applescript/examples/open_tabs_from_bookmark_folder.applescript b/chrome/browser/cocoa/applescript/examples/open_tabs_from_bookmark_folder.applescript
new file mode 100644
index 0000000..af36fc1
--- /dev/null
+++ b/chrome/browser/cocoa/applescript/examples/open_tabs_from_bookmark_folder.applescript
@@ -0,0 +1,12 @@
+-- Copyright (c) 2010 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.
+
+tell application "Chromium"
+ set var to bookmark folder "New" of bookmarks bar
+ -- Change the folder to whichever you want.
+ repeat with i in (bookmark items of var)
+ set u to URL of i
+ tell window 1 to make new tab with properties {u}
+ end repeat
+end tell
diff --git a/chrome/browser/cocoa/applescript/examples/tab_manipulation.applescript b/chrome/browser/cocoa/applescript/examples/tab_manipulation.applescript
new file mode 100644
index 0000000..9038c17
--- /dev/null
+++ b/chrome/browser/cocoa/applescript/examples/tab_manipulation.applescript
@@ -0,0 +1,45 @@
+-- Copyright (c) 2010 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.
+
+-- Contains some common tab manipulation commands.
+tell application "Chromium"
+ tell window 1 to make new tab with properties {URL:"http://google.com"}
+ -- create a new tab and navigate to a particular URL.
+
+ set var to active tab index of window 1
+ set active tab index of window 1 to (var - 1) -- Select the previous tab.
+
+ set var to active tab index of window 1
+ set active tab index of window 1 to (var + 1) -- Select the next tab.
+
+ get title of tab 1 of window 1 -- Get the URL that the user can see.
+
+ get loading of tab 1 of window 1 -- Check if a tab is loading.
+
+ -- Common edit/manipulation commands.
+ tell tab 1 of window 1
+ undo
+
+ redo
+
+ cut selection -- Cut a piece of text and place it on the system clipboard.
+
+ copy selection -- Copy a piece of text and place it on the system clipboard.
+
+ paste selection -- Paste a text from the system clipboard.
+
+ select all
+ end tell
+
+ -- Common navigation commands.
+ tell tab 1 of window 1
+ go back
+
+ go forward
+
+ reload
+
+ stop
+ end tell
+end tell
diff --git a/chrome/browser/cocoa/applescript/scripting.sdef b/chrome/browser/cocoa/applescript/scripting.sdef
index a65ed4e..86c00c6 100644
--- a/chrome/browser/cocoa/applescript/scripting.sdef
+++ b/chrome/browser/cocoa/applescript/scripting.sdef
@@ -233,6 +233,9 @@
<responds-to command="save">
<cocoa method="handlesSaveScriptCommand:"/>
</responds-to>
+ <responds-to command="execute">
+ <cocoa method="handlesExecuteJavascriptScriptCommand:"/>
+ </responds-to>
</class>
<class name="bookmark folder" code="CrBF" description="A bookmarks folder that contains other bookmarks folder and bookmark items.">
<cocoa class="BookmarkFolderAppleScript"/>
@@ -256,37 +259,44 @@
<property name="URL" code="URL " description="The URL of the bookmark." type="text"/>
</class>
<command name="reload" code="CrSuRlod" description="Reload a tab.">
- <direct-parameter type="specifier"/>
+ <direct-parameter description="The tab to execute the command in." type="specifier"/>
</command>
<command name="go back" code="CrSuBack" description="Go Back (If Possible).">
- <direct-parameter type="specifier"/>
+ <direct-parameter description="The tab to execute the command in." type="specifier"/>
</command>
<command name="go forward" code="CrSuFwd " description="Go Forward (If Possible).">
- <direct-parameter type="specifier"/>
+ <direct-parameter description="The tab to execute the command in." type="specifier"/>
</command>
<command name="select all" code="CrSuSlAl" description="Select all.">
- <direct-parameter type="specifier"/>
+ <direct-parameter description="The tab to execute the command in." type="specifier"/>
</command>
<command name="cut selection" code="CrSuCut " description="Cut selected text (If Possible).">
- <direct-parameter type="specifier"/>
+ <direct-parameter description="The tab to execute the command in." type="specifier"/>
</command>
<command name="copy selection" code="CrSuCop " description="Copy text.">
- <direct-parameter type="specifier"/>
+ <direct-parameter description="The tab to execute the command in." type="specifier"/>
</command>
<command name="paste selection" code="CrSuPast" description="Paste text (If Possible).">
- <direct-parameter type="specifier"/>
+ <direct-parameter description="The tab to execute the command in." type="specifier"/>
</command>
<command name="undo" code="CrSuUndo" description="Undo the last change.">
- <direct-parameter type="specifier"/>
+ <direct-parameter description="The tab to execute the command in." type="specifier"/>
</command>
<command name="redo" code="CrSuRedo" description="Redo the last change.">
- <direct-parameter type="specifier"/>
+ <direct-parameter description="The tab to execute the command in." type="specifier"/>
</command>
<command name="stop" code="CrSustop" description="Stop the current tab from loading.">
- <direct-parameter type="specifier"/>
+ <direct-parameter description="The tab to execute the command in." type="specifier"/>
</command>
<command name="view source" code="CrSuVSrc" description="View the HTML source of the tab.">
- <direct-parameter type="specifier"/>
+ <direct-parameter description="The tab to execute the command in." type="specifier"/>
+ </command>
+ <command name="execute" code="CrSuExJa" description="Execute a piece of javascript.">
+ <direct-parameter description="The tab to execute the command in." type="specifier"/>
+ <parameter name="javascript" code="JvSc" description="The javascript code to execute." type="text">
+ <cocoa key="javascript"/>
+ </parameter>
+ <result type="any"/>
</command>
</suite>
</dictionary> \ No newline at end of file
diff --git a/chrome/browser/cocoa/applescript/tab_applescript.h b/chrome/browser/cocoa/applescript/tab_applescript.h
index b4e6269..28ba1fe 100644
--- a/chrome/browser/cocoa/applescript/tab_applescript.h
+++ b/chrome/browser/cocoa/applescript/tab_applescript.h
@@ -71,6 +71,9 @@ class TabContents;
// Displays the HTML of the tab in a new tab.
- (void)handlesViewSourceScriptCommand:(NSScriptCommand*)command;
+// Executes a piece of javascript in the tab.
+- (id)handlesExecuteJavascriptScriptCommand:(NSScriptCommand*)command;
+
@end
#endif// CHROME_BROWSER_COCOA_APPLESCRIPT_TAB_APPLESCRIPT_H_
diff --git a/chrome/browser/cocoa/applescript/tab_applescript.mm b/chrome/browser/cocoa/applescript/tab_applescript.mm
index 73b4f8b..ee4af12 100644
--- a/chrome/browser/cocoa/applescript/tab_applescript.mm
+++ b/chrome/browser/cocoa/applescript/tab_applescript.mm
@@ -29,6 +29,13 @@
- (id)init {
if ((self = [super init])) {
+ SessionID session;
+ SessionID::id_type futureSessionIDOfTab = session.id() + 1;
+ // Holds the SessionID that the new tab is going to get.
+ scoped_nsobject<NSNumber> numID(
+ [[NSNumber alloc]
+ initWithInt:futureSessionIDOfTab]);
+ [self setUniqueID:numID];
[self setTempURL:@""];
}
return self;
@@ -271,4 +278,19 @@
}
}
+- (id)handlesExecuteJavascriptScriptCommand:(NSScriptCommand*)command {
+ RenderViewHost* view = tabContents_->render_view_host();
+ if (!view) {
+ NOTREACHED();
+ return nil;
+ }
+
+ std::wstring script = base::SysNSStringToWide(
+ [[command evaluatedArguments] objectForKey:@"javascript"]);
+ view->ExecuteJavascriptInWebFrame(L"", script);
+
+ // TODO(Shreyas): Figure out a way to get the response back.
+ return nil;
+}
+
@end
diff --git a/chrome/browser/cocoa/applescript/window_applescript_test.mm b/chrome/browser/cocoa/applescript/window_applescript_test.mm
index 809afb3..6d0a9d3 100644
--- a/chrome/browser/cocoa/applescript/window_applescript_test.mm
+++ b/chrome/browser/cocoa/applescript/window_applescript_test.mm
@@ -76,33 +76,44 @@ IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, Tabs) {
// Insert a new tab.
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertTab) {
// Emulate what applescript would do when creating a new tab.
+ // Emulates a script like |set var to make new tab with
+ // properties URL:"http://google.com"}|.
scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]);
+ scoped_nsobject<NSNumber> var([[aTab.get() uniqueID] copy]);
[aTab.get() setURL:@"http://google.com"];
scoped_nsobject<WindowAppleScript> aWindow(
[[WindowAppleScript alloc] initWithBrowser:browser()]);
[aWindow.get() insertInTabs:aTab.get()];
- EXPECT_EQ([aTab.get() container], aWindow.get());
- EXPECT_NSEQ(AppleScript::kTabsProperty,
- [aTab.get() containerProperty]);
- TabAppleScript* tab2 = [[aWindow.get() tabs] objectAtIndex:1];
+
+ // Represents the tab after it is inserted.
+ TabAppleScript* tab = [[aWindow.get() tabs] objectAtIndex:1];
EXPECT_EQ(GURL("http://google.com"),
- GURL(base::SysNSStringToUTF8([tab2 URL])));
+ GURL(base::SysNSStringToUTF8([tab URL])));
+ EXPECT_EQ([tab container], aWindow.get());
+ EXPECT_NSEQ(AppleScript::kTabsProperty,
+ [tab containerProperty]);
+ EXPECT_NSEQ(var.get(), [tab uniqueID]);
}
// Insert a new tab at a particular position
IN_PROC_BROWSER_TEST_F(WindowAppleScriptTest, InsertTabAtPosition) {
- scoped_nsobject<TabAppleScript> tab1([[TabAppleScript alloc] init]);
- scoped_nsobject<TabAppleScript> tab2([[TabAppleScript alloc] init]);
+ // Emulate what applescript would do when creating a new tab.
+ // Emulates a script like |set var to make new tab with
+ // properties URL:"http://google.com"} at before tab 1|.
+ scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]);
+ scoped_nsobject<NSNumber> var([[aTab.get() uniqueID] copy]);
+ [aTab.get() setURL:@"http://google.com"];
scoped_nsobject<WindowAppleScript> aWindow(
[[WindowAppleScript alloc] initWithBrowser:browser()]);
- [aWindow.get() insertInTabs:tab1.get()];
- [aWindow.get() insertInTabs:tab2.get()];
+ [aWindow.get() insertInTabs:aTab.get() atIndex:0];
- scoped_nsobject<TabAppleScript> aTab([[TabAppleScript alloc] init]);
- [aWindow.get() insertInTabs:aTab.get() atIndex:1];
- TabAppleScript* tab = [[aWindow.get() tabs] objectAtIndex:1];
- EXPECT_NSEQ([aTab.get() uniqueID],
- [tab uniqueID]);
+ // Represents the tab after it is inserted.
+ TabAppleScript* tab = [[aWindow.get() tabs] objectAtIndex:0];
+ EXPECT_EQ(GURL("http://google.com"),
+ GURL(base::SysNSStringToUTF8([tab URL])));
+ EXPECT_EQ([tab container], aWindow.get());
+ EXPECT_NSEQ(AppleScript::kTabsProperty, [tab containerProperty]);
+ EXPECT_NSEQ(var.get(), [tab uniqueID]);
}
// Inserting and deleting tabs.